(Below tips are copied from LINUX BY EXAMPLE.com)
Often over time, we will want to reorganize a group of files by renaming them.
To rename *.txt to *.bak
(e.g. to rename ham.txt to ham.bak)
for f in *.txt; do mv "$f" "${f%.txt}.bak"; done
To remove ‘new-’ from new-*
(e.g. to rename new-ham.txt to ham.txt)
for f in new-*; do mv "$f" "${f#new-}"; done
${variable%pattern} vs ${variable#pattern}
The funny-looking symbol, ${f%.txt} is a useful match-and-remove string operator:
If the pattern ‘.txt’ matches the end of variable $f, it will remove the matching part (that’s ‘.txt‘) and return the rest. Try this:
f=new-ham.txt # define $f as 'new-ham.txt'
echo ${f%.txt} # display 'new-ham'
What about ${f#new-}? It’s almost the same, but it matches the pattern at the beginning of the variable.
echo ${f#new-} # display 'ham.txt'
Advertisement
March 27, 2007 at 10:40 am |
Nice tip, especially for those, who don’t have knowledge of bash programming.
Thanks for sharing it.
December 1, 2008 at 7:24 pm |
Thank you! this just came in handy for me.
February 5, 2010 at 10:07 pm |
Thanks for your sharing this. It was exactly what I was looking for.
December 8, 2010 at 5:00 pm |
Thanks quick and concise exactly what I needed!
November 1, 2011 at 8:29 pm |
laptop…
[...]Rename multiple files in Linux « My Notebook[...]…
November 7, 2011 at 1:36 am |
notebooks…
[...]Rename multiple files in Linux « My Notebook[...]…
December 6, 2011 at 1:43 pm |
jak kupić…
[...]Rename multiple files in Linux « My Notebook[...]…