开发者

How do I rename many files at once by applying conversion logic, such as changing the case?

开发者 https://www.devze.com 2023-02-09 20:03 出处:网络
What\'s a good tool to do this?I\'m using Windows and have Cygw开发者_如何学Cin, so if there\'s a unix tool or easy scripting solution, that would work for me.You haven\'t really provided very much in

What's a good tool to do this? I'm using Windows and have Cygw开发者_如何学Cin, so if there's a unix tool or easy scripting solution, that would work for me.


You haven't really provided very much information regarding the names of the files.

Assuming you want to take a slew of files and just convert them from upper case to lower case, you could do something like.

rename.sh


#!/bin/bash

# this script takes one argument, a filename.  If the file doesn't exist, we die.
# to accomodate files with spaces in the name, we'll grab $* as the filename.

filename="$*"

lowername=$( echo "$filename" | tr [A-Z] [a-z] )

if [ ! -f $filename ]; then
echo "File: $filename: file not found!"
exit 1  
fi

printf "%-70s" "Renaming $filename to $lowername: "
st=$( mv "$filename" "$lowername" 2>&1 )
if (( $? == 0 )); then
printf "%-8s\n" "[ ok ]" 
else
printf "%-8s\n" "[ err ]"
fi

You could then use this script with a directory tree of files, or a subset of files, by using the find command.

find /some/directory/with/files -type f -name \*JPG -exec bash rename.sh {} \;

You can now modify the 'tr' args, and the 'find' args to customise the renaming rules for which files get renamed, and how they are renamed.

0

精彩评论

暂无评论...
验证码 换一张
取 消