Ubuntu Unleashed 2019 Edition: Covering 18.04, 18.10, 19.04

(singke) #1

Copying Files Using tar


One choice for copying files into another location is to use the tar
command; you just create a tar file that is piped to tar to be uncompressed
in the new location. To accomplish this, first change to the source directory.
Then the entire command resembles this:


Click here to view code image
matthew@seymour:~$ tar -cvf files | (cd target_directory ; tar -xpf)


In this command, files is the filenames you want to include; you can use * to
include the entire current directory.


When you change to the source directory and execute tar, you use the cvf
arguments to do the following:


c—Creates   an  archive.
v—Specifies verbose—that is, lists the files processed so you can see that
it is working.
f—Specifies the filename of the archive. (In this case, it is -.)

The following tar command options can be useful for creating file copies for
backup purposes:


l—Stay  in  the local   file    system  (so that    you do  not include remote
volumes).
atime-preserve—Do not change access times on files, even though
you are accessing them now (to preserve the old access information for
archival purposes).

The contents of the tar file (held for you temporarily in the buffer, which is
named -) are then piped to the second expression, which extracts the files to
the target directory. In shell programming (refer to Chapter 14, “Automating
Tasks and Shell Scripting”), enclosing an expression in parentheses causes it
to operate in a subshell and be executed first.


After you change to the target directory, you use the following options with
tar:


x—Extracts  files   from    a   tar archive.
p—Preserves permissions.
f—Specifies the filename, which in this case is -, the temporary buffer
that holds the files archived with tar.
Free download pdf