1. Archiving and backup
-
We can use
gzipandbzip2to compress one or more files:ls -l /etc > foo.txtls -lh foo.*gzip foo.txtls -lh foo.*gunzip foo.txtls -lh foo.*ls -l /etc | gzip > foo.txt.gzgunzip -c foo.txt.gzzcat foo.txt.gz | lesszless foo.txt.gzbzip2 foo.txtls -lh foo.*bunzip2 foo.txt.bz2ls -lh foo.* -
We can use
tarto 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 testdirtar -cf testdir.tar testdirls -lhThe option
-cmeanscreate, and the option-fis for the filename of the archive.The option
-tis used to list the contents of the archive, and-vis for verbose:tar -tf testdir.tar | lesstar -tvf testdir.tar | lessNow let's extract the archive in a new location:
mkdir foocd footar -xf ../testdir.tarlstree -C | less -rcd .. ; rm -rf foo/ -
By default,
tarremoves the leading/from absolute filenames:echo $(pwd)/testdirtar cf testdir2.tar $(pwd)/testdirtar tf testdir2.tar | lessmkdir footar xf testdir2.tar -C foo/tree foo -C | less -rrm -rf foo -
We can extract only some files from the archive (not all the files):
mkdir foocd footar tf ../testdir.tar testdir/dir-001/file-Atar xf ../testdir.tar testdir/dir-001/file-Atreetar xf ../testdir.tar testdir/dir-002/file-{A,B,C}treeWe can also use
--wildcards, like this:tar xf ../testdir.tar --wildcards 'testdir/dir-*/file-A'tree -C | less -rcd .. ; rm -rf foo -
Sometimes it is useful to combine
tarwithfindandgzip:find testdir -name 'file-A'find testdir -name 'file-A' \
-exec tar rf testdir3.tar '{}' '+'tar tf testdir3.tar | lessfind testdir -name 'file-B' \
-exec tar rf testdir3.tar '{}' '+'tar tf testdir3.tar | lessThe 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-Tor--files-fromincludes 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 oftartogzipin order to compress it.We can also use the options
zorjto compress the archive:find testdir -name 'file-A' \
| tar czf testdir.tgz -T -find testdir -name 'file-A' \
| tar cjf testdir.tbz -T -ls -lhThe option
jusesbzip2compression, instead ofbzip. -
The
zipprogram is both a compression tool and an archiver:zip -r testdir.zip testdirls -lhThe option
-ris for recursion.mkdir -p foocd foounzip ../testdir.ziptree | lessunzip -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
rsyncto synchronize files and directories:rsync -av testdir fools foorsync -av testdir fooNotice that in the second case no files are copied because
rsyncdetects that there are no differences between the source and the destination.touch testdir/dir-099/file-Zrsync -av testdir fooWith the option
--deletewe can also delete the files on the destination directory that are not present on the source directory.rm testdir/dir-099/file-Zrsync -av testdir fools foo/testdir/dir-099/file-Zrsync -av --delete testdir fools foo/testdir/dir-099/file-Zrsynccan be used over the network as well, usually combined withssh.