views:

284

answers:

5

Every week I put up a bunch of .jpg screen captures on my project's wiki to show the difference between this week's status and last weeks.

The files are called things like ProgrammerAtasks_current.jpg and ProgrammerBtasks_lastweek.jpg so there is a pair of *_current.jpg and *_lastweek.jpg for each status.

Each week I delete the old *_lastweek.jpg and then rename the old *current to *lastweek.

I can use rm *_lastweek* to do the first part of the task, but how do I automate renaming *_current.jpg to *_lastweek.jpg?

+2  A: 

You should give the rename command a try. I believe it'll do exactly what you need.

rename current.jpg lastweek.jpg *.jpg
Derek Park
+1  A: 

This will rename all *_current.jpg files in the current dir to *_lastweek.jpg

for f in *_current.jpg; do mv "$f" "${f%_current.jpg}_lastweek.jpg"; done
Kevin Ballard
+2  A: 

The rename command is really your best choice as Derek Park has stated, but just to round out this list lets have a sed/xargs solution:

ls | sed 's/\(.*\)_current.jpg/\1/' | xargs -I{} mv {}_current.jpg {}_lastweek.jpg

Or, you could use find/sed/xargs for a more robust approach (it will even chase down files in subfolders):

find /your/directory/ -name '*_current.jpg' | sed 's/\(.*\)_current.jpg/\1/' | xargs -I{} mv {}_current.jpg {}_lastweek.jpg

akdom
find -print0 | xargs --null solutions have the nice benefit that they really can handle huge numbers of files, unlike wildcard-based solutions, which sometimes max out around 4000 files.
Gregg Lind
A: 

I usually do this along the lines of the following (bash shell), this is similar to what the other answers do:

for file in *_current.jpg;
do
  mv $file `echo $file | sed -e 's/current/lastweek/g'`;
done;

Of course, it could be combined into a one-liner by removing the newlines, but I figured it was better to display it this way for clarity.

Another option would be to put the '_' character in front of the 'current' and 'lastweek' strings, just in case, but the Markdown wasn't displaying it properly, so I took them out.

kaybenleroll
A: 

There's also a nice tool called mmv... which incidentally also does copying. appending and symlinking...

Anders Eurenius