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$ lsfile-0\ (number-0).txt file-4\ (number-4).txt file-8\ (number-8).txtfile-1\ (number-1).txt file-5\ (number-5).txt file-9\ (number-9).txtfile-2\ (number-2).txt file-6\ (number-6).txtfile-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.
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.txtFavorites Recent scripts$ rm -badfile.txtrm: invalid option -- 'b'Try 'rm ./-badfile.txt' to remove the file '-badfile.txt'.Try 'rm --help' for more information.$ ls *.txtls: invalid option -- 'e'Try 'ls --help' for more information.