UNIX: A Shortcut to Create a Backup Copy of a File

Posted July 19, 2005 by barretj in UNIX

The following Tech-Recipes tutorial describes how to copy and rename a file using a shorter method than the normal cp file file.bak.

If you have a file named foobar and you want to make a backup copy named foobar.bak, you can use the following steps:

cp foobar foobar.bak

If the file has a long name and you have a similarly named file, tab-expansion will not work.

Here is what you can do instead:

cp foobar{,.bak}

The shell will automagically expand this to cp foobar foobar.bak.

Note: I tested this in bash and zsh. Other shells may or may not work

The Conversation

Follow the reactions below and share your own thoughts.

Comments are closed.