Find Files for Rename

Background

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.

Problem

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

Method

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%.*}
done

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.

Linux 

No comment

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