I have some files which was written as : test_026542_time0663550.5.jpeg and I would like to rename as test_0663550.jpeg in order to classify easily by order number because I have some errors with the current names.
Asked
Active
Viewed 81 times
0
muru
- 193,181
- 53
- 473
- 722
user3601754
- 173
- 1
- 5
-
3Flat directory or recursively? Any risk on dupes after renaming? – Jacob Vlijm Nov 07 '15 at 15:54
-
2And are they all in the form: `test_X_timeY.Z.jpeg`? – muru Nov 07 '15 at 16:12
-
What parts of the filename always change ? Which parts never change ? – Sergiy Kolodyazhnyy Nov 07 '15 at 19:42
-
Answer posted, please review – Sergiy Kolodyazhnyy Nov 07 '15 at 20:48
2 Answers
0
Use the rename function. It's basic(prototype form) is $ rename s/"SEARCH"/"REPLACE"/g *. This replaces the file "SEARCH" with "REPLACE". The /g means global, so if you had a "SEARCH SEARCH.jpg", it would be renamed "REPLACE REPLACE.jpg". The * is the extension type like .mp3 or .pdf. So, your example would be:
$ rename s/'test_026542_time0663550.5.jpeg'/'test_0663550_.jpeg'/g *
This should help.
Tony Lancer
- 1,003
- 9
- 23
-
Thanks for your help ;) but i m trying to automate it because i have a thousand of files... – user3601754 Nov 07 '15 at 15:52
-
-
0
My battery is running out , so I'll make this short
Find+awk oneliner
find . -type f -printf "%f\n" | awk -F "_"
'{ORIG=$0;gsub(/\_[[:digit:]].*\_time/,"_");gsub(/\.[[:digit:]].*\.jpeg/,".jpeg"); NEW=$0;cmd="mv "ORIG"
"NEW;system(cmd)}'
Demo
testdir2:$ ls
test_13442_time1446925878.1.jpeg test_27796_time1446925887.4.jpeg
test_25495_time1446925884.3.jpeg test_29681_time1446925881.2.jpeg
testdir2:find . -type f -printf "%f\n" | awk -F "_"
'{ORIG=$0;gsub(/\_[[:digit:]].*\_time/,"_");gsub(/\.[[:digit:]].*\.jpeg/,".jpeg"); NEW=$0;cmd="mv "ORIG"
"NEW;system(cmd)}'
testdir2:$ ls
test_1446925878.jpeg test_1446925881.jpeg test_1446925884.jpeg test_1446925887.jpeg
Sergiy Kolodyazhnyy
- 103,293
- 19
- 273
- 492