Skip to main content
tar

Restoring Tar Archives

By March 10, 2018September 12th, 2022No Comments

Restoring Tar ArchivesIn the previous tutorial, we looked at how we can list tar archives on your Linux host. Having looked at creating and listing these files we now move onto Restoring Tar Archives.

Restoring Tar Archives

No matter if we are using tar archives for backups or just an easy way to distribute collections of files, we will need, at some time, to restore them. This is where we can make use of the –expand or -x option.

The first point to understand is that, by default, files are restored to a relative path to the directory from which tar is executed. If we list our archive again I will be able to explain the process a little more. Just in case you don’t have the archive to hand we will create it again and then list it:

$ tar --create --file=my.tar /etc/services /etc/hosts          
 tar: Removing leading `/' from member names
 $ tar --list --file=~my.tar
 etc/services
 etc/hosts

If we restore these files they will be created as  etc/services and etc/hosts relative to our working directory. If this was our home directory then the etc directory will be created as ~/etc and it will contain the two files services and hosts.

If our working directory is /home/ubuntu/ and we add the path etc/hosts the restored file becomes /home/ubuntu/etc/hosts

We can test this by running the following command from our home directory:

$ cd
 $ tar --extract --file=my.tar
 $ ls ~/etc
 hosts  services

We begin by ensuring we are in the home directory. The directory we extract the archive from does not have to the directory in which the archive is located. In this case it is. The cd command used without arguments will take you to your home directory.

Next we extract the archive with –extract, we could also use the short for of -x. Additionally, we could use the –verbose options as discussed when we were looking at listing tar archives. The use of a single –verbose would list the files names being restored and –verbose –verbose would list the file details being restored. Again, -v or -vv could also work in the same way making use of the shorter options.

The important point to note is that, when restoring tar archives,  these files are restored to their path relative to where tar is executed from. We were in our home directory so they were created below etc in our home directory.

Where would we need to execute tar from to restore to the original location?

Well where do you think? The root directory. We will also need to run this with sudo so we have the correct permissions to create files on the /etc/ .

To backup files we need to be able to read them and have read and execute to the directory in which they are located. To restore files we need to the write permission to the directory in which they are restored to.

The following example show how to restore these files to the original location:

$ cd /                                    
 $ pwd                                     
 /
 $ sudo tar --extract --file=/home/ubuntu/my.tar

Our first move, in this case, is to change to the root directory. We verify the location with the command pwd.

Using sudo to preface the tar command, we extract our archive. My user account is called ubuntu so we extract the file pointing to the location of the archive within /home/ubuntu. If your account is different then use the path to your home folder and tar file.

The files are now restored relative to the root directory and, as such, will restore to the original /etc folder.

If our working directory is / we add the path etc/hosts the restored file becomes /etc/hosts.

If we only needed to extract a single file from the archive we could make use of the following command where only the host file is extracted:

$ sudo tar --extract -vvv --file /home/ubuntu/my.tar etc/hosts
Similarly to creating archives, we can add more files to the list. If the list become extensive then we can create the a text file with a list of files. This can be used with the --files-from option. In our case, the archive only contains the two files but we can specify them as a files list. Firstly, as a simple list:

$ sudo tar --extract --file /home/ubuntu/my.tar etc/hosts etc/hosts

Now we will create a text file and use this as an argument in the tar command:

$ cd /
 $ echo etc/hosts >> ~/partialrestore.txt
 $ echo etc/services >> ~/partialrestore.txt
 $ cat ~/partialrestore.txt
 etc/hosts
 etc/services
 $ sudo tar --extract --file /home/ubuntu/my.tar \
    --files-from=/home/ubuntu/partialrestore.txt

We can see that we are able to use tar as a flexible solution to archive and restore files. This said I am sure that you have a question relating to forcing a restore to the original location. We will look at this next.

Next up we look at compressing archives but now the video now follows