1. Archiving and backup
-
We can use
gzip
andbzip2
to compress one or more files:ls -l /etc > foo.txt
ls -lh foo.*
gzip foo.txt
ls -lh foo.*
gunzip foo.txt
ls -lh foo.*
ls -l /etc | gzip > foo.txt.gz
gunzip -c foo.txt.gz
zcat foo.txt.gz | less
zless foo.txt.gz
bzip2 foo.txt
ls -lh foo.*
bunzip2 foo.txt.bz2
ls -lh foo.*
-
We can use
tar
to archive files.Let's create a test directory:
mkdir -p testdir/dir-{001..100}
touch testdir/dir-{001..100}/file-{A..Z}
ls testdir/
ls testdir/dir-001/
Create a tar archive of the entire directory:
tar -c -f testdir.tar testdir
tar -cf testdir.tar testdir
ls -lh
The option
-c
meanscreate
, and the option-f
is for the filename of the archive.The option
-t
is used to list the contents of the archive, and-v
is for verbose:tar -tf testdir.tar | less
tar -tvf testdir.tar | less
Now let's extract the archive in a new location:
mkdir foo
cd foo
tar -xf ../testdir.tar
ls
tree -C | less -r
cd .. ; rm -rf foo/
-
By default,
tar
removes the leading/
from absolute filenames:echo $(pwd)/testdir
tar cf testdir2.tar $(pwd)/testdir
tar tf testdir2.tar | less
mkdir foo
tar xf testdir2.tar -C foo/
tree foo -C | less -r
rm -rf foo
-
We can extract only some files from the archive (not all the files):
mkdir foo
cd foo
tar tf ../testdir.tar testdir/dir-001/file-A
tar xf ../testdir.tar testdir/dir-001/file-A
tree
tar xf ../testdir.tar testdir/dir-002/file-{A,B,C}
tree
We can also use
--wildcards
, like this:tar xf ../testdir.tar --wildcards 'testdir/dir-*/file-A'
tree -C | less -r
cd .. ; rm -rf foo
-
Sometimes it is useful to combine
tar
withfind
andgzip
:find testdir -name 'file-A'
find testdir -name 'file-A' \
-exec tar rf testdir3.tar '{}' '+'tar tf testdir3.tar | less
find testdir -name 'file-B' \
-exec tar rf testdir3.tar '{}' '+'tar tf testdir3.tar | less
The option 'r' is for appending files to an archive.
find testdir -name 'file-A' \
| tar cf - -T - \
| gzip > testdir.tgzThe first
-
makes tar to send the output to stdout instead of a file. The option-T
or--files-from
includes in the archive only the files listed in the given file. In this case we are reading the list of files from-
, which means the stdin and is the list of files coming from the commandfind
. Then we are passing the output oftar
togzip
in order to compress it.We can also use the options
z
orj
to compress the archive:find testdir -name 'file-A' \
| tar czf testdir.tgz -T -find testdir -name 'file-A' \
| tar cjf testdir.tbz -T -ls -lh
The option
j
usesbzip2
compression, instead ofbzip
. -
The
zip
program is both a compression tool and an archiver:zip -r testdir.zip testdir
ls -lh
The option
-r
is for recursion.mkdir -p foo
cd foo
unzip ../testdir.zip
tree | less
unzip -l ../testdir.zip testdir/dir-007/file-*
rm -rf testdir
unzip ../testdir.zip testdir/dir-007/file-*
tree | lesscd .. ; rm -rf foo
-
We can use
rsync
to synchronize files and directories:rsync -av testdir foo
ls foo
rsync -av testdir foo
Notice that in the second case no files are copied because
rsync
detects that there are no differences between the source and the destination.touch testdir/dir-099/file-Z
rsync -av testdir foo
With the option
--delete
we can also delete the files on the destination directory that are not present on the source directory.rm testdir/dir-099/file-Z
rsync -av testdir foo
ls foo/testdir/dir-099/file-Z
rsync -av --delete testdir foo
ls foo/testdir/dir-099/file-Z
rsync
can be used over the network as well, usually combined withssh
.