Let’s examine the folder structure:
Search for files with space(s) in the filename(s)
To do a search within the current folder for all files with a space in the filename do:
find . -maxdepth 1 -type f -name "* *" | while read file; do ls -la "$file"; done |more
-rw-r--r-- 1 ugu5ma staff 8 Dec 29 14:38 ./file 2.txt
-rw-r--r-- 1 ugu5ma staff 13 Dec 29 14:38 ./file 3.txt
-rw-r--r-- 1 ugu5ma staff 6 Dec 29 14:38 ./file 1.txt
the “-maxdepth 1” parameter limits the search within the current folder.
“-maxdepth 2” would search within the current and the first subfolder(s)
To do a recursive search for all files with a space in the filename do:
find . -type f -name "* *" | while read file; do ls -la "$file"; done |more
-rw-r--r-- 1 ugu5ma staff 8 Dec 29 14:38 ./file 2.txt
-rw-r--r-- 1 ugu5ma staff 13 Dec 29 14:38 ./file 3.txt
-rw-r--r-- 1 ugu5ma staff 6 Dec 29 14:38 ./file 1.txt
-rw-r--r-- 1 ugu5ma staff 8 Dec 29 14:38 ./1/subfolder/file 2.txt
-rw-r--r-- 1 ugu5ma staff 13 Dec 29 14:38 ./1/subfolder/file 3.txt
-rw-r--r-- 1 ugu5ma staff 6 Dec 29 14:38 ./1/subfolder/file 1.txt
-rw-r--r-- 1 ugu5ma staff 8 Dec 29 14:38 ./1/file 2.txt
-rw-r--r-- 1 ugu5ma staff 13 Dec 29 14:38 ./1/file 3.txt
-rw-r--r-- 1 ugu5ma staff 6 Dec 29 14:38 ./1/file 1.txt
-rw-r--r-- 1 ugu5ma staff 8 Dec 29 14:38 ./3/file 2.txt
-rw-r--r-- 1 ugu5ma staff 13 Dec 29 14:38 ./3/file 3.txt
-rw-r--r-- 1 ugu5ma staff 6 Dec 29 14:38 ./3/file 1.txt
-rw-r--r-- 1 ugu5ma staff 8 Dec 29 14:38 ./2/file 2.txt
-rw-r--r-- 1 ugu5ma staff 13 Dec 29 14:38 ./2/file 3.txt
-rw-r--r-- 1 ugu5ma staff 6 Dec 29 14:38 ./2/file 1.txt
Rename files with space(s) in filename(s)
To replace the space with an underscore for files within the current folder execute:
find . -maxdepth 1 -type f -name "* *" | while read file; do mv "$file" ${file// /_}; done
➜ ordner ls -l
total 24
drwxr-xr-x 6 ugu5ma staff 192 Dec 29 14:51 1
drwxr-xr-x 5 ugu5ma staff 160 Dec 29 14:39 2
drwxr-xr-x 5 ugu5ma staff 160 Dec 29 14:39 3
-rw-r--r-- 1 ugu5ma staff 6 Dec 29 14:38 file_1.txt
-rw-r--r-- 1 ugu5ma staff 8 Dec 29 14:38 file_2.txt
-rw-r--r-- 1 ugu5ma staff 13 Dec 29 14:38 file_3.txt
the files in the subfolders are still untouched:
ordner find . -type f -name "* *" | while read file; do ls -la "$file"; done |more
-rw-r--r-- 1 ugu5ma staff 8 Dec 29 14:38 ./1/subfolder/file 2.txt
-rw-r--r-- 1 ugu5ma staff 13 Dec 29 14:38 ./1/subfolder/file 3.txt
-rw-r--r-- 1 ugu5ma staff 6 Dec 29 14:38 ./1/subfolder/file 1.txt
-rw-r--r-- 1 ugu5ma staff 8 Dec 29 14:38 ./1/file 2.txt
-rw-r--r-- 1 ugu5ma staff 13 Dec 29 14:38 ./1/file 3.txt
-rw-r--r-- 1 ugu5ma staff 6 Dec 29 14:38 ./1/file 1.txt
-rw-r--r-- 1 ugu5ma staff 8 Dec 29 14:38 ./3/file 2.txt
-rw-r--r-- 1 ugu5ma staff 13 Dec 29 14:38 ./3/file 3.txt
-rw-r--r-- 1 ugu5ma staff 6 Dec 29 14:38 ./3/file 1.txt
-rw-r--r-- 1 ugu5ma staff 8 Dec 29 14:38 ./2/file 2.txt
-rw-r--r-- 1 ugu5ma staff 13 Dec 29 14:38 ./2/file 3.txt
-rw-r--r-- 1 ugu5ma staff 6 Dec 29 14:38 ./2/file 1.txt
To replace the space with an underscore for files within the current folder and subfolders execute:
find . -type f -name "* *" | while read file; do mv "$file" ${file// /_}; done
Danger zone: delete files in current and subfolder with space(s) in filename(s)
find . -type f -name "* *" | while read file; do rm "$file"; done
You must be logged in to post a comment.