example file: filename.ext1.ext2.ext3 how to remove 开发者_运维问答only ext3 here in unix bash shell under sun solaris? in all directories:
find . -name "*.ext3" | mv '{}' ?
You can pipe the output of the find
command into a while read
loop. Even file names with spaces will be read correctly in this manner. Do not use a for file in $(find ...)
construct. It's very inefficient (the find must complete before the for loop starts, it will have problems with files with spaces in the name, and it can overload the command line buffer which will happen without the slightest indication.
You can also use the $[var%name}
construct The %
means small right filter. It filters the right side of an environment variable with the matching glob. A single %
means match the smallest possible amount. A double %%
means match the largest amount. The #
and ##
are for the left side of the string.
Also be careful to use quotation marks around your file names incase of spaces in the name.
find . -type f -name "*.ext3" | while read file
do
mv "$file" "${file%.ext3}"
done
A bit safer might be to use find . -name "*.exe" -print0
which will separate file names with nulls instead of returns. This is useful incase a is in the file name. Then, you have to set your IFS
variable to handle nulls and not spaces, tabs, and NL.
It would be:
for i in `find . -type f -name "*.ext3"` ; do mv $i $(echo $i | sed -e '/\.ext3$//') ; done
In the case the files have spaces, you can do this slightly different:
find . -type f -name "*.ext3" | while read file ; do mv "$file" "$(echo $file | sed -e '/\.ext3$//')" ; done
精彩评论