Find Files for Rename


Three years ago, I wrote Used More Bash Utilities for batch renaming files. One of my Facebook friends has pointed out that it fails for file names containing whitespace.


Recently, I would like to remove the extension name .sh of all shell scripts in ~/bin, so that which {script} will work without .sh.


A for loop over $(ls) is a basic solution, but find is more correct since it’s possible that a file name contains a space character.

find ~/bin -maxdepth 1 -name "*.sh" -type f -print0 |\
while read -d $'\0' file; do
  mv $file ${file%.*}

A list of matching files is piped to the while loop which iterates through each item delimited by the null character '\0'. The shell expansion ${file%.*} trim off the file extension.


No comment

Your email address will not be published. Required fields are marked *.