

Let’s assume you want to extract only anaconda-ks.cfg file from the tar file under /tmp folder. Example is shown below ~]# tar -xvf myarchive.tar -C /tmp/ Example 6) Extract particular file or directory from tar file In case you want to extract tar file to a particular folder or directory then use ‘ -C‘ option followed by path of a folder. Example 5) Extract tar archive file to a particular folder ~]# tar -xvf myarchive.tarĪbove command will extract all the files and directories of myarchive tar file in the current working directory. Let’s extract the content of above created tar file. ‘ -x‘ option is used to extract the files and directories from the tar archive file. Example 4) Extract files and directories from archive file Note: In the Compressed tar file we can’t append file or directory. Let’s add /etc/fstab file in ‘ data.tar‘ ~]# tar -rvf data.tar /etc/fstab ‘ -r‘ option in the tar command is used to append or add file to existing tar file. ~]# Example 3) Append or add files to end of archive

rw- root/root 953 01:33 root/anaconda-ks.cfg ~]# tar -tvf myarchive.tar root/anaconda-ks.cfg In the below example i am trying to view whether ‘anaconda-ks.cfg’ file is there in the tar file or not. Listing a specific file or directory from tar file. Using ‘ –t‘ option in tar command we can view the content of tar files without extracting it. ~]# Example 2) List the content of archive file In the tar command ‘ -c‘ option specify to create a tar file, ‘ -v’ is used for verbose output and ‘ -f’ option is used to specify the archive file name. Tar file contains all the files and directories of /etc folder and anaconda-ks.cfg file. ~]# tar -cvf myarchive.tar /etc /root/anaconda-ks.cfgĪbove command will create a tar file with the name “ myarchive” in the current folder. Let’s create a tar file of /etc directory and ‘/root/ anaconda-ks.cfg’ file.

Note : hyphen ( – ) in the tar command options are optional. Some of the commonly used options in tar command are listed below :
