The Magazine
Linux Journal is the premier source for how-tos, projects, product reviews, expert advice and opinions for everything Linux.
| We're Linux, Again | Feb 09, 2010 |
| Quick Compiz Screenshots | Feb 09, 2010 |
| Welcome to the New LinuxJournal.com | Feb 09, 2010 |
| Symbian Opens Up | Feb 04, 2010 |
| The Small Picture: More OpenOffice.org Extensions | Feb 04, 2010 |
| Pass the Bug, Collect $500 | Feb 02, 2010 |
This Week's Member Giveaway
This week 5 lucky Members will receive a copy of The Official Ubuntu Server Book by Benjamin Mako Hill and Linux Journal's very own Kyle Rankin. No entry necessary. Check back here early next week to find out who the lucky Online Members are.














Comments
Trying to figure how the command works
Enjoyed the video
Problem:
I can see the removal of the parts, but not the re-arrangement.
Background:
Ok, finally figured out the following:
1) The command uses "substring removal" / "parameter substitution". Took awhile to find this out - my first search term was " ${f# " and got stuff on F# prog language. Live and Learn.
2) The " f# " is for removing the shortest prefix (ie, from the left).
3) The " f$ " is for removing the shortest prefix (ie, from the right).
Problem Restated:
1) Where in the command does it say ----> move the " aa " from the the beginning to the end of the filename?
Correction to the video
1) " f# " is for removing the shortest _not_ the longest prefix
Link that was helpful: Linux Journal Article
I really enjoy learning about the CLI:) and look forward to these videos.
Zeek
Renaming Details
First, if I said "longest" I mis-spoke: "#" and "%" remove the "shortest" match, "##" and "%%" remove the longest match.
The rename command is this:
mv -i $f 2009${f#*-2009}-${%-2009*}There are 4 parts to the final file name:
2009 # Literal ${f#*-2009} # Based on original name - # Literal ${f%-2009*} # Based on original nameGiven the name "aa-2009-01-01". The first substitution removes the shortest prefix that matches "*-2009", in this case that prefix is "aa-2009", leaving "-01-01". The second substitution removes the shortest suffix that matches "-2009*", in this case that suffix is "-2009-01-01", leaving "aa". So the final name is formed from:
2009 ==> 2009 ${f#*-2009} ==> -01-01 - ==> - ${f%-2009*} ==> aaResulting in the file name "2009-01-01-aa", the desired result.
Mitch Frazier is an Associate Editor for Linux Journal and the Web Editor for linuxjournal.com.
Thanks
I got it!
Very Clever
That was a surprisingly quick reply - Thanks
Zeek
Cheers
Ways
rename 's/foo/bar/g' *
rename .oldextension .newextension *.oldextension
mmv 'banana_*_*.asc' 'banana_#2_#1.asc'
Bulk renaming : http://is.gd/50bGB
You can also use "rename" command
The "rename" command offers as way to directly do essentially the same thing. It does a find/replace of the groups of files you specify.
Only in a few cases
Rename will work for you in some cases but not all, specifically it wouldn't work for the example in the video, unless I'm missing something...
Mitch Frazier is an Associate Editor for Linux Journal and the Web Editor for linuxjournal.com.
rename will work with
rename will work with regex...
rename s/\(..\)-\(.*\)/\$2-\$1/ *2009*... but I never considered this usage of parameter substitution. Thanks!
Post new comment