2

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

Prady
  • 93
  • 2
  • 5

3 Answers3

4

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

find . -maxdepth 1 -type f -exec mv {} {}.backup \;
Timo
  • 6,202
  • 1
  • 26
  • 28
0

This can do the trick

for FILE in $(find . -type f) ; do mv $FILE ${FILE}.backup ; done
Boogy
  • 876
  • 6
  • 8
  • 2
    This would append to all files below the current directory not just to the ones in the current directory. – Joseph R. Feb 09 '14 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. – Timo Feb 09 '14 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 Feb 09 '14 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/' *
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
Joseph R.
  • 38,849
  • 7
  • 107
  • 143