Tai Lung: *gasps* The Wuxi finger hold!
Po: Oh, you know this hold.
Tai Lung: You’re bluffing. You’re bluffing! Shifu didn’t teach you that!
Po: Nope. I figured it out. Skidoosh. — [Kung Fu Panda]

courtesy — http://blogs.quovantis.com/the-art-of-learning-informally/

Bulk renaming files using end pattern (%)

If you want to rename all files with extension *.jpg to *.png.

for example below is the list of files in current directory

$ ls -ltrtotal 0
-rw-r--r-- 1 rchint1 Domain Users 0 Jul 18 20:02 test1.jpg
-rw-r--r-- 1 rchint1 Domain Users 0 Jul 18 20:02 test2.jpg
-rw-r--r-- 1 rchint1 Domain Users 0 Jul 18 20:02 test3.jpg
-rw-r--r-- 1 rchint1 Domain Users 0 Jul 18 20:02 test4.jpg
-rw-r--r-- 1 rchint1 Domain Users 0 Jul 18 20:02 test5.jpg
-rw-r--r-- 1 rchint1 Domain Users 0 Jul 18 20:02 test6.jpg
-rw-r--r-- 1 rchint1 Domain Users 0 Jul 18 20:02 test7.jpg
-rw-r--r-- 1 rchint1 Domain Users 0 Jul 18 20:02 test8.jpg
-rw-r--r-- 1 rchint1 Domain Users 0 Jul 18 20:02 test9.jpg
-rw-r--r-- 1 rchint1 Domain Users 0 Jul 18 20:02 test10.jpg

${var%pattern} : will trim pattern from var from rear end. Now using this technique we can iterate through the list of files and rename files like below.

$ for FILE in $(find . -name "*.jpg"); do mv $FILE ${FILE%.jpg}.png; done$ ls -ltr
total 0
-rw-r--r-- 1 rchint1 Domain Users 0 Jul 18 20:02 test1.png
-rw-r--r-- 1 rchint1 Domain Users 0 Jul 18 20:02 test2.png
-rw-r--r-- 1 rchint1 Domain Users 0 Jul 18 20:02 test3.png
-rw-r--r-- 1 rchint1 Domain Users 0 Jul 18 20:02 test4.png
-rw-r--r-- 1 rchint1 Domain Users 0 Jul 18 20:02 test5.png
-rw-r--r-- 1 rchint1 Domain Users 0 Jul 18 20:02 test6.png
-rw-r--r-- 1 rchint1 Domain Users 0 Jul 18 20:02 test7.png
-rw-r--r-- 1 rchint1 Domain Users 0 Jul 18 20:02 test8.png
-rw-r--r-- 1 rchint1 Domain Users 0 Jul 18 20:02 test9.png
-rw-r--r-- 1 rchint1 Domain Users 0 Jul 18 20:02 test10.png

Bulk renaming files using start pattern (#)

If you want to rename all files starting with test to abc.

${var#pattern} : will trim pattern from var from front. Now using this technique we can iterate through the list of files and rename files like below.

$ for FILE in $(find . -name "*.png"); do mv $FILE abc${FILE#./test}; done$ ls -ltr
total 0
-rw-r--r-- 1 rchint1 Domain Users 0 Jul 18 20:02 abc1.png
-rw-r--r-- 1 rchint1 Domain Users 0 Jul 18 20:02 abc2.png
-rw-r--r-- 1 rchint1 Domain Users 0 Jul 18 20:02 abc3.png
-rw-r--r-- 1 rchint1 Domain Users 0 Jul 18 20:02 abc4.png
-rw-r--r-- 1 rchint1 Domain Users 0 Jul 18 20:02 abc5.png
-rw-r--r-- 1 rchint1 Domain Users 0 Jul 18 20:02 abc6.png
-rw-r--r-- 1 rchint1 Domain Users 0 Jul 18 20:02 abc7.png
-rw-r--r-- 1 rchint1 Domain Users 0 Jul 18 20:02 abc8.png
-rw-r--r-- 1 rchint1 Domain Users 0 Jul 18 20:02 abc9.png
-rw-r--r-- 1 rchint1 Domain Users 0 Jul 18 20:02 abc10.png

Skidoosh

Software Engineer, Learner, Developer, Prokopton

Software Engineer, Learner, Developer, Prokopton