Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a folder contains one or more tar.gz files. I want to add current date before each files when the script run.

For example:

Two file name(those files will be created each day, but the name will remain the same):

file1.tar.gz 
file2.tar.gz

In first day(2011-10-07), those two files will be renamed to:

2011-10-07_file1.tar.gz
2011-10-07_file2.tar.gz

In next day(2011-10-08), they are changed to:

2011-10-08_file1.tar.gz
2011-10-08_file2.tar.gz

Finally, the folder contains the following files:

2011-10-07_file1.tar.gz
2011-10-07_file2.tar.gz
2011-10-08_file1.tar.gz
2011-10-08_file2.tar.gz

How to achieve this using one line? I tried to use "rename" command, but I can only add the date after the file, not before.

My code

cdate=`date +"%Y-%m-%d"`; rename .gz .gz.$cdate *.gz
share|improve this question

3 Answers 3

up vote 6 down vote accepted

Clearly, the key is to avoid renaming files that already have a date prefix.

cdate=$(date +"%Y-%m-%d")
shopt -s extglob
for file in !([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]*.gz); do
  mv "$file" "${cdate}_$file"
done
share|improve this answer
    
Thank you. I modified a little because I have some other files in same folder and it works perfect now. cdate=$(date +"%Y-%m-%d") shopt -s extglob for file in [a-z 0-9]*.gz ; do mv "$file" "${cdate}_$file" done –  garconcn Oct 7 '11 at 18:50
1  
I'd recommend using for file in file*.gz instead of that "messy" regex. –  frogstarr78 Oct 7 '11 at 19:27

Your example code might get you things like file1.2011-10-07.2011-10-08.tar.gz and I doubt that's what you want.

In addition, rename is not a standard command so it really depends on what yours does.

Since you say there are only ever a fixed number of files with fixed names, you could probably use a simple script like:

cdate=$(date +%Y-%m-%d)
for file in file1.tar.gz file2.tar.gz; do
    mv "$file" "$cdate"_"$file"
done
share|improve this answer
    
Thank you. The file name are fixed, but later, there maybe more files in the same folder. –  garconcn Oct 7 '11 at 18:53

Assuming the files will never contain underscores in their names, a simple bash 4+ solution would be to use extglob:

shopt -s extglob
today=$(date +%F)
for f in ./!(*_*).tar.gz; do mv "$f" "$today"_"$f"; done

A more robust version:

shopt -s extglob
today=$(date +%F)
for f in ./!(????-??-??_*).tar.gz; do mv "$f" "$today"_"$f"; done
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.