9

I got a bunch of files with the filename messed up. All the file names have the same beginning which appear to be windows file directories. The problem is there are 700+ files and I really don't want go through and manually rename all of them. These are examples of the file names (Note: None of these have file directories):

G:some\really\long\file\path\then\the\name1.jpg
G:some\really\long\file\path\then\the\name2.png
G:some\really\long\file\path\then\the\filename.txt
G:some\really\long\file\path\then\the\file_name.mov
...

The important take away is that each file has G:some\really\long\file\path\then\the\ before the file name that I would like.

So I would want the above files to look like:

name1.jpg
name2.png
filename.txt
file_name.mov

I've tried the rename command and was not successful. I am still new with Linux and really am not sure on how to go about it or really what to google. Any help would be appreciated.

18

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 replace old with new
  • .* 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
  • Do I just need the first line, or do I need to do that for every file? – kingcobra1986 Aug 24 '17 at 5:25
  • you just need the first line @kingcobra1986 the others are just the output. * matches all files but let me change that to G* - done - so now it matches all files starting with G in the current directory and will work on them all at once. If they are in multiple directories we can deal with that too in various ways – Zanna Aug 24 '17 at 5:27
  • That worked. Can you explain what exactly happens in that line? – kingcobra1986 Aug 24 '17 at 5:46
  • 1
    @kingcobra1986 sorry, I added notes. Hope it's clear, if not, please feel free to ask for more! – Zanna Aug 24 '17 at 5:52
  • Ha, I like the idea to use the .*\\ since of the greedy .* – Ziazis Aug 24 '17 at 6:57
10

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’
6

If you're looking for GUI solutions, there are many bulk renaming applications. I would suggest Thunar, which is the default file manager for Xfce. It comes with a handy bulk renaming tool. You can install Thunar by running the following command in Terminal:

sudo apt-get install thunar

You'll probably be able to install from Ubuntu Software too.

How to use the utility

  1. Once you've installed Thunar, launch the Bulk Rename utility.
  2. Click on the + icon and add the files you want to rename.
  3. Click on the drop-down box just below the list of selected files and select Search & Replace.
  4. In the Search For: box enter G:some\really\long\file\path\then\the\ (the part you want to get rid of), leave the Replace With: box empty. You should see the preview of the changes in the New Name column.
  5. To apply the changes click on the Rename Files button.

Besides Search & Replace this utility supports the following actions:

  • Insert Date / Time
  • Insert / Overwrite
  • Numbering
  • Remove Characters
  • Uppercase / Lowercase
  • isn't there one for nautilus (I think there is!)? Seems rather overkill to suggest installing a new file browser for this purpose. – Zanna Aug 24 '17 at 5:26
  • @Zanna There is one in v3.22, but I think still not available in Ubuntu repos. – pomsky Aug 24 '17 at 5:28
  • 2
    Thunar is quite handy and seems to coexist well with other file browsers, so I think it may be worthwhile to install it for this purpose. But can you expand this answer with an explanation of how to do bulk renaming in Thunar? – Eliah Kagan Aug 24 '17 at 12:57
  • 1
    @EliahKagan and it's lightweight too – pomsky Aug 24 '17 at 14:01
  • Nicely done, now I can upvote (I've got a Xubuntu I can test it in too :) ) – Zanna Aug 24 '17 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.