How to append “.backup” to the name of each file in your current directory?
3 Answers
If you have files with special characters and/or sub directories you should use:
find . -maxdepth 1 -type f -exec mv {} {}.backup \;
This can do the trick
for FILE in $(find . -type f) ; do mv $FILE ${FILE}.backup ; done
-
2This would append to all files below the current directory not just to the ones in the current directory.Joseph R.– Joseph R.2014-02-09 10:55:27 +00:00Commented Feb 9, 2014 at 10:55
-
2Your answer only works if there are no spaces or newlines in the filenames. It also backs up all files in subdirectories of the current directory.Timo– Timo2014-02-09 10:56:03 +00:00Commented Feb 9, 2014 at 10:56
-
@Timo, space and newlines are not the only ones. tabs and all the wildcard characters (*, ?, [) are also a problem (except in
zsh).Stéphane Chazelas– Stéphane Chazelas2014-02-09 16:03:50 +00:00Commented Feb 9, 2014 at 16:03
With a POSIX shell:
for file in *;do
[ -f "$file" ] && mv -- "$file" "$file.backup"
done
With perl's rename:
rename -- '-f && s/\Z/.backup/' *
-
*doesn't include file names that start with a dot.jfs– jfs2014-02-09 11:23:35 +00:00Commented Feb 9, 2014 at 11:23