Saturday, May 4, 2024
 Popular · Latest · Hot · Upcoming
1
rated 0 times [  1] [ 0]  / answers: 1 / hits: 986  / 2 Years ago, fri, september 9, 2022, 10:29:01

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.


More From » scripts

 Answers
5

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.


[#2904] Friday, September 9, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
pipeag

Total Points: 489
Total Questions: 107
Total Answers: 115

Location: Iraq
Member since Fri, Jun 5, 2020
4 Years ago
pipeag questions
Sun, Jan 9, 22, 04:46, 2 Years ago
Sun, Oct 10, 21, 10:48, 3 Years ago
Sat, Jan 14, 23, 20:05, 1 Year ago
;