|
|
Archiving
Directories and Files
Linux Tips by Burleson Consulting |
There are several reasons you may want to create an
archive of a file or directory. Here are some of the most common ones:
* Archive and compress unused directories to conserve
disk space
* Create an archive of a directory and files before an
upgrade allowing you to restore the original contents if there is a problem
* Archive a directory before making any major changes
* Create an archive to move several files and folders to
another system as one
* Create an archive as a means of software distribution
One of the most useful utilities for archiving a set of
files and directories is tar. The name tar is short for Tape ARchiver because
tar was originally written to write data to a tape backup device.
The following is a basic example of archiving a
directory into a tar archive file and also extracting the archive into its
original structure.
$ tar -cvf examples.tar examples
examples/
examples/declaration.txt
examples/gettysburg.txt
examples/preamble.txt
$ rm ?r examples
$ ls
examples.tar
$ tar ?xvf examples.tar
examples/
examples/declaration.txt
examples/gettysburg.txt
examples/preamble.txt
$ ls
examples examples.tar
In this example we have demonstrated the two most common
uses of tar. The first tar command combines the ?c (create) option to create a
new archive, the ?v (verbose) option to list the files and directories it's
archiving and the ?f option to write to a file rather than to tape. Remember
that tar was originally written for use with tape drives and it still defaults
to tape if you do not use the ?f option.
The two arguments for this tar command are the
destination file (example.tar in our example here) and the files which should be
added to that file. This can be confusing since most other Linux commands take
a source argument before the destination. tar takes them in the order of
destination then source so you can list multiple source files to be archived
into a single file. Also not that we have to specify the file extension (.tar)
if we want our new archive to have an extension. You can name a tar archive
(typically called a tarfile or tarball) anything you want, but the .tar
extension is a widely accepted convention.
In the second tar command the ?v and ?f options have the
same result and the ?x (extract) option tells tar that we want to extract the
contents of a tar file rather than create one. We then give tar the name of the
archive to be extracted and it goes to work restoring our files.
A Warning about Relative and Absolute Paths in tar
As with other commands tar can be used with either
relative or absolute paths. When specifying the tarfile to be created or
extracted there is little difference between these two methods; however, if you
use an absolute path when listing files to be archived you might get a surprise
when you extract them!
If an absolute path is used to tell tar which files to
archive, like with the command below the archive will record the absolute path
and restore those files to that path, no matter where tar is run from or where
the tarfile is.
$ tar -cf examples.tar /home/tclark/examples
If an absolute path is not specified on archiving the
files will be extracted into the working directory or the appropriate subfolder
of the working directory.
Combining Files
On occasion, the need to combine or concatenate two or
more files may arise. The cat (short for concatenate) command is a nice simple
way to get this done. The cat command can take several file names as arguments
and will output them all as one file in the order they are listed.
By default cat will output the concatenation of the
files to the screen but it is easy to redirect the output to a file with the >
redirection operator.
In this example we use cat to concatenate file1.dat,
file2.dat and file3.dat into the file combined.dat.
$ cat file1.dat file2.dat file3.dat > combined.dat
In the next section we'll see how the cat command can be
useful for more than just putting files together.
Displaying file contents
Oddly enough, the cat command is also used to display
the contents of a file or to redirect the contents of a file to another command
for additional processing, which is presented in further detail in the chapter
on shell scripting.
The following is a demonstration of how we can use the
cat command to display the contents of preamble.txt.
$ cat preamble.txt
U.S. Constitution: Preamble
We the People of the United States, in Order to form a
more perfect Union,
establish Justice, insure domestic Tranquility, provide
for the common defense,
promote the general Welfare, and secure the Blessings of
Liberty to ourselves
and our Posterity, do ordain and establish this
Constitution for the United
States of America.
The more command or the less command can be used to
display larger files which will not all fit on the screen at once. The more
command is antiquated and has been largely abandoned in favor of the less
command. The less command actually has more options than more. In other words,
less honors all of the more options plus additional options only available with
less, such as the ability to navigate to a particular line of a file or to a
particular percentage of a file. To exit the less display, the q subcommand can
be used.
So since less is better than more we'll use that. The
following is an example of using less to display a file called preamble.txt.
The output has been truncated to save trees and the ellipsis (...) is not part
of the less output.
$ less declaration.txt
The Declaration of Independence of the Thirteen Colonies
In CONGRESS, July 4, 1776
The unanimous Declaration of the thirteen united States
of America,
When in the Course of human events, it becomes necessary
for one people to dissolve the political bands which have connected them with another, and to
assume among the powers of the earth, the separate and equal station to which the Laws of Nature
and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they
should declare the causes which impel them to the separation.
We hold these truths to be self-evident, that all men
are created equal, that they are endowed by ...
Great Britain [George III] is a history of repeated
injuries and usurpations, all having in direct object the establishment of an absolute Tyranny
over these States. To prove this, let Facts be submitted to a candid world.
declaration.txt (END)
In less you can typically use the arrow keys to move up
and down one line at a time or use the b and space bar to move up and down
(respectively) one page at a time. Remember, when you're done viewing a file
with less all you need to do is type q to quit.
Displaying Beginning Lines of a File
Sometimes a user might have a large file for which they
only need to display the first few lines. For instance, perhaps the user would
like to see the error code on a dump file and the code and error messages appear
within the first fifteen lines of the dump file. The following example
demonstrates how to display the first fifteen lines of a file using the head
command. The head command takes a number as an option and uses it as the number
of lines to be displayed. The default is 10.
$ head -15 declaration.txt
The Declaration of Independence of the Thirteen Colonies
In CONGRESS, July 4, 1776
The unanimous Declaration of the thirteen united States
of America,
When in the Course of human events, it becomes necessary
for one people to dissolve the political bands which have connected them with another, and to
assume among the powers of the earth, the separate and equal station to which the Laws of Nature
and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they
should declare the causes which impel them to the separation.
We hold these truths to be self-evident, that all men
are created equal, that they are endowed by their Creator with certain unalienable Rights, that
among these are Life, Liberty and the pursuit of Happiness. --That to secure these rights, Governments
are instituted among Men, deriving their just powers from the consent of the governed, --That
whenever any Form of Government becomes
In this example and often in use it may seem like head
is displaying mroe lines than you asked for. That typically is because the
lines are too long for the display so a single line may be continued on the next
line.
Displaying Ending Lines of a File
The need might arise to see only the last lines of a
file. A good example of this might be an error log file where the user would
like to see the last few messages written to the log. The tail command can be
used to display the last lines of a file, while passing the number of lines to
be displayed. The following example requests the last eight lines in the file
called declaration.txt.
$ tail -8 declaration.txt
they are accustomed. But when a long train of abuses and
usurpations, pursuing invariably the same Object evinces a design to reduce them under
absolute Despotism, it is their right, it is their duty, to throw off such Government, and to provide
new Guards for their future security.
?Such has been the patient sufferance of these Colonies;
and such is now the necessity which constrains them to alter their former Systems of
Government. The history of the present King of Great Britain [George III] is a history of repeated
injuries and usurpations, all having in direct object the establishment of an absolute Tyranny
over these States. To prove this, let Facts be submitted to a candid world.
Again it appears we are getting more than eight lines,
but this is just the result of long lines wrapping onto two lines.
Display Active Writes to a File
Sometimes you need to go one step further and watch as
lines are being written to a file. Perhaps, for example, an application is
compressing and copying files to an alternate location, writing messages to a
log file called message.log as it processes each file. A curious user might
want to observe the progress of the application. In this case, the tail command
with the ?f (follow) option can be used to read the messages as they are written
to a file. The following example assumes that the current working directory is
the same directory where the log file resides.
$ tail -f message.log
* A clever Linux user can also use the less command to
display the beginning lines of a file, the ending lines of a file, or to follow
active writes to a file like tail ?f does. See the man entry for the less
command to see how this is done.
These commands are useful for viewing a file in a human
readable format. Occasionally you may need to view a file in hex format as
shown in the next section.
This is an excerpt from "Easy
Linux Commands" by Linux guru Jon Emmons. You can purchase it for only
$19.95 (30%-off) at
this link.