1

I just switched to using Ubuntu full time (coming from Windows 10), and I’ve read that it’s not a good idea to have spaces in file names on Linux. So I found a script online (see below) to replace spaces in file names with underscores, and I was planning on using it to change the names of some of my files that I transfer over from Windows, many of which currently have spaces.

I created a test folder with some files in it just to make sure the script does what I want it to do. However, rather than running the script to change the names inside my test folder, I accidentally ran it on my whole home directory, which also changed some of the names inside my .config folder and other directories I didn't want renamed. Here is the script I used:

!/bin/bash
find -name "* *" -print0 | sort -rz | \
  while read -d $'\0' f; do mv -v "$f" "$(dirname "$f")/$(basename "${f// /_}")"; done

and here is some of the sample output:

renamed './.config/google-chrome/Default/Managed Extension Settings' -> './.config/google-chrome/Default/Managed_Extension_Settings'
renamed './.config/google-chrome/Default/Secure Preferences' -> './.config/google-chrome/Default/Secure_Preferences'
renamed './vmware/windows_10_x64/Windows 10.vmxf' -> './vmware/windows_10_x64/Windows_10.vmxf'

I’m fairly new to Linux (I’m sure you can tell), and I was wondering if someone more experienced could help me restore the filenames to what they were before I ran the script – just undo all the changes the script made. Thanks.

4
  • 1
    I have absolutely no help for you. None. In your shoes, I'd re-install the OS. There is nothing to help you at this point - but there is a lesson to be learned. Before you type in random things you saw online, make sure you know what they're going to do. Also, backups are important. They're very important when you're using an entirely new operating system. That said, good on you for stepping up and admitting what you did. It gets easier. Don't let this one mishap stop you from learning more new things. Each and every single one of us has done something similar - or worse. All of us.
    – KGIII
    Aug 4, 2020 at 0:25
  • 2
    just create another user for yourself, if you only ran it on your home directory
    – WU-TANG
    Aug 4, 2020 at 1:23
  • 3
    if you still have ALL of the "sample output", i guess you could go back through it line by line and mv all the destinations back to the sources... I would save all of that sample output in a file so you dont lose it, and then work from it. I would almost hate to advise another script to do it... but it would seem possible
    – WU-TANG
    Aug 4, 2020 at 1:31
  • @WU-TANG I did save all the output to a file because I thought there might be a way to mv the files back with a script like you said. Do you have any suggestions for how I could make a script like this? There are a LOT of file to rename, so I don't too badly want to do it manually.
    – BenM
    Aug 4, 2020 at 2:09

2 Answers 2

1

If you still have the output from the script, you can manually rename the files that were inadvertently changed. For future reference, Linux can handle spaces in filenames just fine.

0

This comes with no warranty...
This should be ran on the command line the home directory.
The sample output file should be in the home directory too, to make things easy.
Carefully cut and paste the whole thing and execute.

grep "renamed" sampleoutputfilename.txt | sed s/renamed// | awk '{FS="->";print "unrenamed" $2 " moving back to " $1;system("mv " $2 " " $1)}'

keep in mind i did this based on a sample output file that i never saw.. So i made my own that looked like this.

 renamed './.config/google-chrome/Default/Managed Extension Settings'
-> './.config/google-chrome/Default/Managed_Extension_Settings'
 renamed './.config/google-chrome/Default/Secure Preferences' -> './.config/google-chrome/Default/Secure_Preferences'
 renamed './vmware/windows_10_x64/Windows 10.vmxf' -> './vmware/windows_10_x64/Windows_10.vmxf'
 renamed './this1.txt' -> './that1.txt'
 renamed './Videos/this 2.txt' -> './Videos/that2.txt'
 renamed './this 3.txt' -> './that3.txt'
 renamed './.config/geany/this 4.txt' -> './.config/geany/that4.txt'
 renamed './this5.txt' -> './that5.txt'
 renamed './this 6.txt' -> './that6.txt'

....So that format is what I assumed.
You may want to test it first by cutting and pasting all of the this/that sample lines into a testsampleoutput.txt file... and then creating all of the "that" files for testing.

`touch './that1.txt' './Videos/that2.txt' './that3.txt' './that5.txt' './that6.txt'

you may not have geany installed so you can leave that one off.

5
  • Ok, I'll give this a try after work today.
    – BenM
    Aug 4, 2020 at 13:01
  • I tried the your pipeline on a test folder, and it worked, but I added some more test directories just to be extra sure it works before really using it. However, it gave me an error for directories that originally had spaces in their names, like the first line in your sample output above. Although, after taking another look at my original output file, I realized that a lot of the files that were renamed didn't really need to be reverted back to their old names, I just ended up reverting everything I needed manually.
    – BenM
    Aug 5, 2020 at 11:41
  • Thank you to everyone for your help.
    – BenM
    Aug 5, 2020 at 11:44
  • What was the error? if your sample file had the proper order, it should not have error-ed... meaning as long as "rename ./.had spaces/had more spaces/" was listed in the file before "rename ./.had spaces/had more spaces/had more spaces.txt". I could see if you tried to move a file back to a directory path that has not been yet corrected, an error occurring. Is that just how your test data was because of your random choice of lines, or is the "sample output" also out of order? As long as parent directories are always listed before its sub-directories in the, it should work.
    – WU-TANG
    Aug 5, 2020 at 15:00
  • The folder in my test folder that was giving me the error was called "folder_with_space", and it had nothing in it. I accordingly added a line, "renamed './folder with space' -> './folder_with_space'", to the top of my test sample output file. But when I ran the command, the output was correct for everything except this folder. The error was: unrenamedwith moving back to './folder sh: 1 Syntax error: Unterminated quoted string
    – BenM
    Aug 7, 2020 at 14:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .