Creating a bash script to change file names to lower case -
Creating a bash script to change file names to lower case -
i trying write bash script convert file names lowercase, have problem because not work 1 case.
when have file1
, file1
, , utilize on file1
replace letters file1.
#!/bin/bash testfile="" flag="1" file in * testfile=`echo file | tr '[a-z]' '[a-z]'` file2 in * if [ `echo $testfile` = `echo $file2` ] flag="0" fi done if [ $flag = "1" ] mv $file `echo $file | tr '[a-z]' '[a-z]'` fi flag="1" done
looks
testfile=`echo file | tr '[a-z]' '[a-z]'`
should
testfile=`echo "$file" | tr '[a-z]' '[a-z]'`
re-writing script prepare other minor things
#!/bin/bash testfile= flag=1 file in *; testfile=$(tr '[a-z]' '[a-z]' <<< "$file") file2 in *; if [ "$testfile" = "$file2" ]; flag=0 fi done if [ $flag -eq 1 ]; mv -- "$file" "$(tr '[a-z]' '[a-z]' <<< "$file")" fi flag=1 done
quote variables prevent word-splitting ("$file"
instead of $file
) generally preferable utilize $()
instead of tildes don't utilize string comparing don't have to use --
delimit arguments in commands take (in order prevent files -file
beingness treated options) by convention, should utilize capital variable names environment variables, though kept them in above. pipes vs here strings (<<<
) doesn't matter much here, <<<
faster , safer. though more simply, think want
#!/bin/bash file in *; testfile=$(tr '[a-z]' '[a-z]' <<< "$file") [ -e "$testfile" ] || mv -- "$file" "$testfile" done
or on modern mv
implementations (not technically posix)
#!/bin/bash file in *; mv -n -- "$file" "$(tr '[a-z]' '[a-z]' <<< "$file")" done
from man
page
-n, --no-clobber not overwrite existing file
bash
Comments
Post a Comment