'rename files with changing pattern
i want to rename different files in bash with pattern and found this option:
rename 's/.2007/(2007)/g' *.*
with this pattern I can rename every file with ".2007" in name to "(2007)"
--> this is exactly what i want to do.
Next step:
i want to automate this, because i have files with 1995 - 2017. It is a possibility to do:
rename 's/.2007/(2007)/g' *.*
rename 's/.2008/(2008)/g' *.*
rename 's/.2009/(2009)/g' *.*
etc.
but actually, is there another solution?
my files are named like (they are not the same length...):
FILENAME.ANOTHERFILENAME.2007.jpg
FILENAME.2007.jpg
FILENAME.ANOTHERFILENAME.SOMETIMESONEMORE.2007.jpg
Solution 1:[1]
With Perl‘s rename:
rename -n 's/.([1-2][0-9]{3})/($1)/' *.*
This renames all files with 1000 to 2999. If everything looks fine remove -n
.
Solution 2:[2]
For the use-cases where it's not so much about automation but rather about batch processing of a set of files, I find renameutils
and its qmv
("quick move") very useful: it enables you to edit the target filenames in a text editor which may be easier/faster than designing regex's for some.
https://www.nongnu.org/renameutils/ (it's in *buntu repos)
But for applications that need to run w/o human intervention, rename
is certainly more suitable.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Cyrus |
Solution 2 | edison23 |