Removing Blank Spaces from a Group of File Names
This set of commands iterate over each file in the current directory
and will replace any blank spaces in a filename with an underscore
(_
).
First, we’ll create a set of sample files:
$ for i in {0..9}; do touch "file-${i} (number-${i}).txt"; done
$ ls
file-0\ (number-0).txt file-4\ (number-4).txt file-8\ (number-8).txt
file-1\ (number-1).txt file-5\ (number-5).txt file-9\ (number-9).txt
file-2\ (number-2).txt file-6\ (number-6).txt
file-3\ (number-3).txt file-7\ (number-7).txt
Now, we’ll execute the set of commands:
$ for f in *\ *; do mv "$f" "${f// /_}"; done
And now you can see that the blanks have been replaced.
$ ls
file-0_(number-0).txt file-4_(number-4).txt file-8_(number-8).txt
file-1_(number-1).txt file-5_(number-5).txt file-9_(number-9).txt
file-2_(number-2).txt file-6_(number-6).txt
file-3_(number-3).txt file-7_(number-7).txt
Remove Parentheses from File Names
This script will rename Windows backup files by removing the ’
(date)’ from the filename. It will remove the parentheses and everything
between them from the file name, so that a file with the name
file-1 (2023-09-10).txt
will be renamed to
file-1.txt
. Note that since the data between the
parentheses will be removed, the resulting file name must be unique for
this script to prevent data from being overwritten. Other than that it
should work for any fileset that meets the beforementioned
constraints.
First, let’s create some sample files:
$ for i in {0..9}; do touch "file-${i} (number-${i}).txt"; done
$ ls
file-0\ (number-0).txt file-4\ (number-4).txt file-8\ (number-8).txt
file-1\ (number-1).txt file-5\ (number-5).txt file-9\ (number-9).txt
file-2\ (number-2).txt file-6\ (number-6).txt
file-3\ (number-3).txt file-7\ (number-7).txt
Now, we’ll execute the script on the sample files:
$ for f in *; do n=$(echo $f | sed "s/[ (][^)]*[)]//g");mv "${f}" "${n}"; done
If we look at the directory listing now we see that the file names are updated:
$ ls
file-0.txt file-2.txt file-4.txt file-6.txt file-8.txt
file-1.txt file-3.txt file-5.txt file-7.txt file-9.txt
Rename Files That Start With a Special Character
Suppose you find that you have a file with a special character and you want to delete it:
$ ls
-badfile.txt PrintHood reg57.txt
Favorites Recent scripts
$ rm -badfile.txt
rm: invalid option -- 'b'
Try 'rm ./-badfile.txt' to remove the file '-badfile.txt'.
Try 'rm --help' for more information.
$ ls *.txt
ls: invalid option -- 'e'
Try 'ls --help' for more information.
First, find the inode
of the file by using
ls -i
on the command line:
$ ls -i
54804119 -badfile.txt 56634824 PrintHood
56634825 Recent 56634807 Favorites
54804251 reg57.txt 56634833 scripts
The “-i” flag will display the file’s inode:
54804119
-badfile.txt
The inode for the “bad” file is 54804119. Once the inode is identified, use the find command to rename the file:
$ find . -inum 54804119 -exec mv {} NewName \;
$ ls NewName
NewName
Now you can delete it.
$ rm NewName
Tags: cli, rename, find, inode, motd
Bulk Change of File Extensions in Directory
If you have a directory of files where you want to change all of the file extensions you may find this code snippet useful to rename them:
for i in *.tmp
do
mv ${i} ${i%.*}.txt
done