My task is to write a script that searches for all files without .old
extension within given directory and renames files to this format: filename.old
. I've tried this script:
#!/bin/bash
for i in $(grep "\.[^old]&" $1 | ls)
do
mv "$1/$i" "$1/$i.old"
done
but it gives a wrong output.
f1, f2.old, f3, f4.old
.f1.old, f2.old, f3.old, f4.old
.f1.old, f2.old.old, f3.old, f4.old.old
.
Each time when I launch script it keeps adding .old
extension, so it becomes like this: f1.old.old, f2.old.old.old, f3.old.old, f4.old.old.old
.How can this be improved?
You could use a one-liner like so:
find . -mindepth 1 ! -name '*.old' -exec mv {} {}.old \;
Example on GNU/Linux (Ubuntu 14.04 LTS):
mkdir so
cd so
touch f1 f2.old f3 f4.old
find . -mindepth 1 ! -name '*.old' -exec mv {} {}.old \;
ls
Result:
f1.old f2.old f3.old f4.old
Explanation:
find .
means find in current directory-mindepth 1
will return the files without returning the current directory .
(see https://askubuntu.com/questions/153770/how-to-have-find-not-return-the-current-directory)! -name '*.old'
will skip any files ending with .old
-exec mv
executes the mv
(move) command on the returned files denoted with {}
and adds an extension to it with {}.old
meaning whatever-filename-was-returned.oldYou can modify your script like so to get similar result:
test.sh.old
#!/bin/bash
for i in $(find . -mindepth 1 ! -name '*.old'); do
mv "$i" "$i.old"
done
Execute with bash test.sh.old
to get similar results.
You may have to try some test cases to see if the one-liner and the modified test.sh.old
file passes those test conditions. I tested it with the sample you provided and this returns the desired results.