How to rename a group of files with what looks like a Windows file path in their names
I just removed everything up to the last backslash with rename
$ rename -n 's/.*\\//' G*
rename(G:some\really\long\file\path\then\the\file_name.mov, file_name.mov)
rename(G:some\really\long\file\path\then\the\filename.txt, filename.txt)
rename(G:some\really\long\file\path\then\the\name1.jpg, name1.jpg)
rename(G:some\really\long\file\path\then\the\name2.png, name2.png)
Remove -n
after testing to actually rename the files.
Notes
-
-n
don't do anything, just print what will be changed -
s/old/new
replaceold
withnew
-
.*
any number of any characters -
\\
The first backslash is to escape the second one. - Since regex are greedy this expression
.*\\
eats all the preceding backslashes too. - Since the last two delimiters
//
are empty everything matched in the search part is deleted
You can do this in pure bash
using shell parameter expansion.
${file##*\\}
(cut-up-to-last-prefix) strips everything from the start of the filename until last \
seen.
for file in *; do
mv -v "$file" "${file##*\\}";
done
The rename results are:
‘G:some\\really\\long\\file\\path\\then\\the\\file_name.mov’ -> ‘file_name.mov’
‘G:some\\really\\long\\file\\path\\then\\the\\filename.txt’ -> ‘filename.txt’
‘G:some\\really\\long\\file\\path\\then\\the\\name1.jpg’ -> ‘name1.jpg’
‘G:some\\really\\long\\file\\path\\then\\the\\name2.png’ -> ‘name2.png’