Example is shown below # tar -xvf archive.tar -C /tmp/ 6) Extract specific file from tar archive In case you want to extract tar file to a specific folder or directory then use ‘-C’ option followed by path of a folder. 5) Extract tar archive to specific folder # tar -xvf archive.tarĪbove command will extract all the files and directories of archive.tar file in the current working directory. To extract an archive file, use ‘-x’ option in the tar command as shown below. Note: In the Compressed tar file we can’t append file or directory. Verify whether file is appended or not, run # tar -tvf archive.tar var/log/auth.log Let’s add /var/log/auth.log file to archive.tar, run # tar -rvf archive.tar /var/log/auth.log ‘-r’ option in the tar command is used to append or add file to existing archive file. In the below example, we are trying to list whether ‘syslog’ file is there in the tar file or not. Listing a specific file or directory from tar file. Use ‘-t‘ option in tar command to view the content of tar files without extracting it. In above command, ‘-c’ option specify to create a tar file, ‘ -v’ is used for verbose output and ‘-f’ option is used to specify the archive name. Tar file contains all the files and directories of /etc folder and /var/log/syslog file. Let’s create a tar file of /etc directory and ‘/root/anaconda-ks.cfg’ file, run # tar -cvf archive.tar /etc /var/log/syslogĪbove command will create a tar file with the name “archive.tar” in the current folder. Without any further delay, let’s jump into tar command examples. Note: hyphen ( – ) in the tar command while using options is optional. -z, –gzip : Compress and extract the archive through gzip.-J, –xz : Compress and extrach the archive through xz.-j, –bzip2 : Compress and extract archive through bzip2.-C, –directory=DIR: Change to DIR before performing any operations.-X, –exclude-from=file : exclude patterns listed in file.-u, –update : only append files newer than copy in archive.-r, –append : append files to the end of an archive.-d, –diff, –compare : find differences between archive and file system.-x, –extract, –get : extract files from an archive.-t, –list : list the contents of an archive.So it always pays to check the contents of big archives before extracting. Note: if you use the -P option, tar will archive absolute paths. You could extract an archive, expect its files to appear in your current working directory, and instead overwrite system files (or your own work) elsewhere by mistake. One is the ability to restore an archive in places other than its original source. There are very obvious, good reasons why tar converts paths to relative ones. Also, this does the same: tar -C / -xvf foo.tar home/foo/bar # -C is the ‘change directory’ option If you want to simulate absolute paths, do cd / first and make sure you're the superuser. So no, the way you posted isn't (necessarily) the correct way to do it. In the case of my foo.tar file, I could extract /home/foo/bar by saying: tar -xvf foo.tar home/foo/bar # Note: no leading slash If you need to extract a particular folder, have a look at what's in the tar file: tar -tvf foo.tarĪnd note the exact filename. Tar: Removing leading `/' from member names GNU tar even says so if you try to store an absolute path: tar -cf foo.tar /home/foo
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |