Example is shown below : ~]# tar -tvf myarchive.tbz2 | more tbz2 fileĬontent of tar file with the extensions. Example 11) List the content of tar.bz2 or. Lrwxrwxrwx root/root 0 01:23 etc/mtab -> /proc/self/mounts Example is shown below : ~]# tar -tvf myarchive.tgz | more tgz fileĬontents of tar file with the extensions tar.gz or. ~]# tar -zcpvf myarchive.tgz /etc/ /opt/ -exclude=*.html Example 10) List the content of tar.gz or. Let’s assume we want to exclude the file type of html while creating the compressed tar file. Using “– exclude” option in tar command we can exclude the particular file or file type while creating tar file. ~]# tar -jcpvf myarchive.tbz2 /etc/ /opt/ Example 9) Exclude particular files or type while creating tar file This can be achieved by using the option ( -j) in the tar command.Extensions of such tar files will be either tar.bz2 or. Let’s assume that we want to create compressed (bzip2) tar file of /etc and /opt folder.
#Linux untar command archive
~]# tar -zcpvf myarchive.tgz /etc/ /opt/ Example 8) Create and compress archive file (bzip2) Extensions of such tar files will be either tar.gz or. This can be achieved using ‘ -z‘ option in tar command. Let’s assume that we want to create a tar file of /etc and /opt folder and also want to compress it using gzip tool. tmp]# Example 7) Create and Compress archive file (gzip) 1 root root 953 Aug 24 01:33 /tmp/root/anaconda-ks.cfg
# tar –xvf tmp]# tar -xvf /root/myarchive.tar root/anaconda-ks.cfg -C /tmp/ 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 :