shell - Bash command ls ordering options -
new here (and coding). i'm writing simple bash script helps me change character character in files of current directory. here's code:
#!/bin/bash #this script changes characters in files of current directory num=$(ls -1 | wc -l | awk '{print $1}') echo "there $num files in folder." echo "changing $1 $2 in names..." echo "done! here's happened:" in $(seq 1 $num) oldname=$(ls -1 | head -$i | tail -1) newname=$(ls -1 | head -$i | tail -1 | sed -e "s/${1}/${2}/g") mv -v "$oldname" "$newname" done
so if write ./ccin.sh " " "_"
should changing spaces in names of files in current directory underscores. except doesn't, , think ls
's fault. for
loop skipping on files because ls
changes order according sorts files every time mv
modifies 1 of them. need isolate names of files in way not rely on "last modified" property. thought of file size, won't work if there 2 files have same size – e.g., i'm trying code on directory full of empty files.
is there way sort files ls
such remain in same order mv
modifies names?
you can try substitute loop one:
find . -type f | while read file oldname="$file" newname=`echo $file| sed -e "s/${1}/${2}/g"` mv -v "$oldname" "$newname" done
instead of running ls
multiple times in order file rename, run find once , iterate on results
wiki
Comments
Post a Comment