I was wondering how I can modify this for loop, so it only loops through the filenames that do not have an ".old" extension.
for filename in $(ls "$1")
do
echo $filename | grep '\.old$' > /dev/null
if [ $? != 0 ]
then mv $1/$filename $1/$filename.old
fi
done
one way to loop through is using grep:
eg
ls | grep -v ".old"
another way is using find
eg
find . -type f ! -name "*.old" -print
Do not use ls; it is unnnecessary and will break your script if any filenames contain spaces or other pathological characters.
for file in "$1"/*
do
case $file in
*.old) ;; ## do nothing
*) mv "$file" "$file.old" ;;
esac
done
You should probably also check that the new filename doesn't already exist.
Thanks for the replys I'll give those suggestions a try.
anbu23
5
for filename in $(ls "$1")
do
[[ -d "$filename" || ${filename##*.} = "old" ]] || mv $1/$filename $1/$filename.old
done
Try that script on the directory TESTING after running these commands:
(
mkdir TESTING
cd TESTING
touch "file one" "file two" "file three"
)