Archiving Directories and Files with tar

Still hanging in there the tar command remains the de facto standard for archiving files and directories in UNIX and UNIX-like operating systems. Here are some tar basics from Easy Linux Commands

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
$ rm –r examples
$ ls
$ tar –xvf examples.tar
$ 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 (examples.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.

Easy Linux CommandsFor more tips like this check out my book Easy Linux Commands, only $19.95 from Rampant TechPress.

Buy it now!

linux, unix, system administration, sysadmin, tar, tape archive

Preventing Accidental File Deletion in Linux and UNIX

A little trick that some Linux users like to implement to prevent accidental file deletion is via the use of an alias. Aliases are similar to variables and can either be set in a session or by placing the alias command in the .profile or .bash_profile file with a text editor.

By adding this alias the user will be prompted to confirm each file before it is deleted; otherwise Linux, unlike Microsoft Windows, will delete whatever files match the filename criteria without warning!

$ alias rm='rm -i'
$ touch touch1.fil touch2.fil touch3.fil
$ rm touch*
rm: remove regular empty file `touch1.fil'? y
rm: remove regular empty file `touch2.fil'? y
rm: remove regular empty file `touch3.fil'? y

Easy Linux CommandsFor more tips like this check out my book Easy Linux Commands, only $19.95 from Rampant TechPress.

Buy it now!

linux, unix, system administration, sysadmin, bash

Jumping on the Linux Bandwagon

In an interesting chain reaction Oracle and Microsoft have both recently committed to supporting Linux distributions.

Recently Oracle announced they will offer RedHat Linux support at very competitive rates. Beyond offering some healthy competition for RedHat support Oracle’s commitment also makes Oracle on Linux a single vendor solution for software support.

Microsoft was not far behind in announcing a partnership with Novel for Suse Linux sales support.

Only time will tell what this will do to the Unix/Unix-like OS market but it’s sure to shake things up in the short term.

linux, unix, microsoft, redhat, system administration, oracle, database administration, dba, sysadmin

Customize your bash prompt

If you’re like me you always intend to customize your UNIX or Linux prompt but never seem to find the time to look up the options and make the change. Well, to give you a jump start here’s an excerpt from my book Easy Linux Commands.

In order to eliminate the need to frequently issue the pwd command to determine the current working directory, many Linux users choose to display the working directory within the Linux command prompt. Some Linux administrators even provide this service for their users when they create their Linux accounts. If a user’s command prompt does not contain the working directory, the command prompt can be modified by changing the prompt string 1 (PS1) shell variable as demonstrated here:

$ PS1="[\u@\h \w]\\$ "
[tclark@appsvr /home/tclark]$

This example of setting the PS1 variable also adds the username (\u) and hostname (\h) to the prompt. This can be very useful if you frequently connect to different hosts and as different users.

In order to avoid having to modify the prompt at each login, the following line of code can be placed within the appropriate configuration file, such as .bash_profile for the bash shell, within the home directory. To do this you can use a text editor like vi to add the following line to your .bash_profile file:

export PS1="[\u@\h \w]\\$ "

Note: Files that begin with a period will not appear when you list the contents of a directory. To see these hidden files use the command ls –a

There are even more options which you can put into your PS1 prompt. While it’s nice to keep your prompt fairly short you may find some of the other options useful. The following table contains a list of values that can be displayed within the PS1 and/or PS2 prompt strings:


Displayed Value


History number of current command


Command number of current command


Current date


Host name




Shell name


Current time


User name


Current working directory


Current working directory (full path)

As a bonus, here are a few of my favorite options for the PS1 prompt:

export PS1='\u@\h$ '

export PS1='\w$ '

export PS1='\d$ '
Mon Oct 30$

You may notice that all these have the dollar sign ($) in them which is typical of bash prompts. There is also a space after the dollar sign so you can easily tell where your prompt ends and your commands begin.

Easy Linux CommandsFor more tips like this check out my book Easy Linux Commands, only $19.95 from Rampant TechPress.

Buy it now!

linux, unix, system administration, sysadmin, bash, shell, unix shell, shell prompt

Make directories and subdirectories simultaneously

Here’s a review of the mkdir command and the often overlooked -p option from Easy Linux Commands by Terry Clark and I.

Creating New Directories

The mkdir (make directory) command is used to create a new directory. The directory to be created can be referenced via an absolute path or via a relative path starting with the current working directory.

Make a directory using an absolute path:

$ ls
$ mkdir /home/tclark/new_dir1
$ ls
examples new_dir1

Make a directory using a relative path:

$ mkdir new_dir2
$ ls
examples new_dir1 new_dir2

The –p (parent) option allows creation of a complete directory branch when parent directories do not yet exist. This will actually create both the parent directory and subdirectory specified. In our example that means the new_dir3 and sub_dir3 are both created with a single command.

Make a new directory with a subdirectory using the –p option:

$ mkdir -p new_dir3/sub_dir3
$ ls
examples new_dir1 new_dir2 new_dir3
$ ls new_dir3

Of course you can also list more than one directory to create with a single mkdir command.

Easy Linux CommandsFor more tips like this check out my book Easy Linux Commands, only $19.95 from Rampant TechPress.

Buy it now!

unix, linux, system administration, sysadmin