2

How to append “.backup” to the name of each file in your current directory?

0

3 Answers 3

4

If you have files with special characters and/or sub directories you should use:

find . -maxdepth 1 -type f -exec mv {} {}.backup \;
1

This can do the trick

for FILE in $(find . -type f) ; do mv $FILE ${FILE}.backup ; done
3
  • 2
    This would append to all files below the current directory not just to the ones in the current directory. Commented Feb 9, 2014 at 10:55
  • 2
    Your 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. Commented 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). Commented Feb 9, 2014 at 16:03
0

With a POSIX shell:

for file in *;do
  [ -f "$file" ] && mv -- "$file" "$file.backup"
done

With perl's rename:

rename -- '-f && s/\Z/.backup/' *
1
  • * doesn't include file names that start with a dot. Commented Feb 9, 2014 at 11:23

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.