I have a directory containing thousands of files with names
I want to change their names to
so take the prefix and put it as a postfix, can this be done in only one command
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.Sign up to join this community
This is possible with the
First check what would be done (by suppliying
-n). If it looks good, drop the
-n and run again:
rename -n 's/t_(.+)\.png$/$1_t.png/' *.png # check only rename 's/t_(.+)\.png$/$1_t.png/' *.png # actually rename the files
If the prefix is separated by an underscore (
_), you can do the following:
rename -n 's/^([^_]*)_(.*)\.(.*)$/$2_$1.$3/' file(s)
It will work with any prefix and any extension.
-n to perform the rename if you're happy with the result.
^- Match the beginning of the file name
([^_]*)- Match any character that is not an underscore
[^_]*and capture it as
_- Match the first underscore
(.*)\.(.*)- Match anything
.*before and after the last
.and capture it as
.must be escaped because it is a special character in Regex -->
$- Match the end of the line
$2_$1.$3- "Filename_Prefix.Extension" from the search pattern captures.