[Wolves] copy of several files from source folder to target folder with timestamp appended to them

David Goodwin david at codepoets.co.uk
Thu Nov 21 16:55:08 UTC 2013


On 21 Nov 2013, at 16:30, Suntish Narain <suntish at googlemail.com> wrote:

> Hi 
> 
> I have a situation where I have several files, say file1.txt, file2.txt, file3.txt and file4.txt (but the number of files can change!) that I want to move from the source folder to a target folder. The only difference is that I want the files in the target folder to have todays' date appended to it, so would be file1_20131121.txt, file2_20131121.txt.., file4_20131121.txt
> 
> I thought 
> cp /home/user/Documents/source/file* /home/user/Documents/target/file*_$(date +%Y%m%d).txt would work but no.. it says directory does not exist..
> 
> but 
> mv /home/user/Documents/source/file1.txt /home/user/Documents/target/file1_$(date +%Y%m%d).txt works


I think you have to do it something like :

for file in /path/to/source/file*
do
	shortFileName=$(basename $file .txt)
	mv $file /path/to/somewhere/else/$shortFileName_$(date +%F).txt
done

Note, this won't work if you have any spaces in your file names (it'll fail miserably), but you indicate that you're only interested in file1.txt, file2.txt etc which is ok.


David.





More information about the Wolves mailing list