Quickly Backing Up Files in Bash

· by Victor Mendonça · Read in about 1 min · (123 words) ·

Here’s a quick and simple way to backup files in Bash by using Bash’s built-in brace expansion {,}.

Let’s first create a file:

$ ls -l > listing.txt

Now let’s create a backup:

$ cp listing.txt{,.bak}

And the result is a new file with the .bak extension:

$ ls -l listing*
Permissions Size User   Group Date Modified Name
.rw-r--r--  2.5k victor users 15 Oct 14:21  listing.txt
.rw-r--r--  2.5k victor users 15 Oct 14:21  listing.txt.bak

How about getting fancy and adding a date?

cp listing.txt{,.$(date +%Y%m%d_%H%M)}

And the result:

$ ls -l listing.tx*
Permissions Size User   Group Date Modified Name
.rw-r--r--  2.5k victor users 15 Oct 14:21  listing.txt
.rw-r--r--  2.5k victor users 15 Oct 14:21  listing.txt.bak
.rw-r--r--  2.5k victor users 15 Oct 14:23  listing.txt.20181015_1423
code with