LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-14-2015, 12:16 PM   #1
arrals.vl
Member
 
Registered: May 2012
Posts: 47

Rep: Reputation: Disabled
rename files after copy is finished


Hello,

I have some files which i need to copy in a schedule to another directory.
I need to add an extension, for example ".tmp" to the end of the files during the copy process.
Then, after copy has finished i need to remove the ".tmp" and leave the files with their original file-name.

Please would you suggest which is the best approach to do this?

Thank you!
 
Old 06-14-2015, 12:24 PM   #2
Keruskerfuerst
Senior Member
 
Registered: Oct 2005
Location: Horgau, Germany
Distribution: Manjaro KDE, Win 10
Posts: 2,197

Rep: Reputation: 164Reputation: 164
Which programming language?
Try to write structure chart first!
 
Old 06-14-2015, 12:37 PM   #3
arrals.vl
Member
 
Registered: May 2012
Posts: 47

Original Poster
Rep: Reputation: Disabled
Hi,

I would like bash script.
I have written this super simple but it does it only for one file, not for several files.
Files have the pattern filename: 20150615_150000_log.txt

Code:
#!/bin/bash

date
time=$(date +%Y%m%d_%H)

for i in $time*.txt
do
t="${i%*}".tmp

cp -vf $directory2*.txt $t
cp -vf $t ./directory2
mv ./directory2/$t ./directory2/$i
done

date
 
Old 06-14-2015, 12:46 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,799

Rep: Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065
Excluding typos, the code doesn't really make sense

1. If you want more than one file, why set time variable to a singular value?

2. What is the purpose of ... ${i*} ... this says to remove nothing from the end??

3. Assuming this is one of the typos where there should be a slash after 'directory2', there is still no variable set with this name
Code:
cp -vf $directory2*.txt $t
There are other issues but I think you need to restate your problem and then attempt to do the process manually, after all, a script is just a way to automate the manual process.
 
Old 06-14-2015, 04:32 PM   #5
arrals.vl
Member
 
Registered: May 2012
Posts: 47

Original Poster
Rep: Reputation: Disabled
Hello,

Thanks for replying.

1. The filename are like this:
20150614_150001_log1.txt
20150614_153045_log2.txt
I want to copy files in that specific hour, in this case all the files 20150614_15*.

2. The purpose of ${i*} is to add tmp extension in the end of each file:
20150614_150001_log1.txt.tmp
20150614_153045_log2.txt.tmp

3. The directory2 is the directory where i should copy these files.
After copy has finished, i will remove *tmp extensions and leave the files with their original filenames (without tmp)

Thank you for your help!
 
Old 06-15-2015, 01:40 AM   #6
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
The edited one:

Code:
#!/bin/bash

date
time=$(date +Y%m%d_%H)

for i in $time*.txt
do
t="${i}".tmp

cp -vf "${i}" ./directory2/"${t}"
mv ./directory2/$t ./directory2/$i
done

date
 
Old 06-15-2015, 04:14 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,799

Rep: Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065
So the extra detail has helped and veerain's example show's how to implement, however I still am not sure I follow the reason to copy the file into the directory with a changed name to then simply rename it back to the original name ... could just be me of course
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
copy file and rename n times in succession coryjsanders Linux - Newbie 2 12-08-2012 01:06 AM
bash script to copy rename files dunstable Linux - Newbie 4 10-08-2012 11:53 AM
copy to a new directory and rename w/date stamp diamond_D Linux - Newbie 3 10-25-2010 05:49 PM
Guide me for copy and rename commands Ye Myint Linux - Newbie 7 11-14-2008 08:27 AM
nautilus crashes whenever I finished download files woranl Fedora 0 09-04-2004 02:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration