15+ tar command usages with examples – Unix/Linux--referenc
|
副标题[/!--empirenews.page--]
reference :http://crybit.com/tar-command-usages-with-examples/ The ‘tar’ saves many files together into a single tape or disk archive,and can restore individual files from the archive. It is very useful in such conditions like when we want to send a lot of files via email,transfer files from one machine to another etc. Here I am explaining some common and useful switches and it usages with examples. Syntax: # tar [options] file.tar file1 file2 .. .. .. Where?file.tar?is the tar file and?file1?and?file2 .. ..?are the files to make a tar. I have created two files?file1.txt?and?file2.txt?for making examples. [root@localhost TAR]# ll total 8 -rw-r--r--. 1 root root 2770 Feb 7 22:37 file1.txt -rw-r--r--. 1 root root 887 Feb 7 22:38 file2.txt Common usages of tar command: How to create a tar file ?Syntax: # tar -cf archive.tar files .. .. Example: [root@localhost TAR]# tar -cf file.tar file1.txt file2.txt [root@localhost TAR]# ll file.tar -rw-r--r--. 1 root root 10240 Feb 7 22:42 file.tar How to list all files in an archive.tar ? # tar -tf archive.tar Example: [root@localhost TAR]# tar -tf file.tar file1.txt file2.txt How to extract all files from archive.tar ? tar -xf archive.tar Example: [root@localhost TAR]# tar -xf file.tar [root@localhost TAR]# ll total 20 -rw-r--r--. 1 root root 2770 Feb 7 22:37 file1.txt -rw-r--r--. 1 root root 887 Feb 7 22:38 file2.txt -rw-r--r--. 1 root root 10240 Feb 7 22:42 file.tar Switches with example: 1,-v,–verboseverbosely list files processed:Syntax:List all files in an archive.tar verbosely: tar -tvf archive.tar Example: [root@localhost TAR]# tar -tvf file.tar -rw-r--r-- root/root 2770 2014-02-07 22:37 file1.txt -rw-r--r-- root/root 887 2014-02-07 22:38 file2.txt 2,-c,–createcreate a new archive. 3,-t,–listlist the contents of an archive. 4,-x,–extract,–getextract files from an archive. 5,-d,–diff,–comparefind differences between archive and file system.Example: [root@localhost TAR]# tar -tf file.tar file2.txt file3.txt file1.txt [root@localhost TAR]# tar -df file.tar file1.txt file2.txt file4.txt tar: file4.txt: Not found in archive tar: Exiting with failure status due to previous errors ----Verbosely---- [root@localhost TAR]# tar -dvf file.tar file1.txt file2.txt file2.txt file1.txt [root@localhost TAR]# tar -dvf file.tar file1.txt file2.txt file6.txt file2.txt file1.txt tar: file6.txt: Not found in archive tar: Exiting with failure status due to previous errors 6,–deletedelete from the archive (not on mag tapes!)Example:Delete?file1.txt?from the archive file.tar [root@localhost TAR]# tar --delete -f file.tar file1.txt [root@localhost TAR]# tar -tf file.tar file2.txt 7,-r,–appendAppend files to the end of an archive.Example:Append?file3.txt?to?file.tar [root@localhost TAR]# tar -rf file.tar file3.txt [root@localhost TAR]# tar -tf file.tar file1.txt file2.txt file3.txt 8,-A,–catenate,–concatenateAppend tar files to an archive.Create another tar file [root@localhost TAR]# tar -cf archive.tar file1.txt file3.txt Append tar file to an archive. [root@localhost TAR]# tar -Af file.tar archive.tar [root@localhost TAR]# tar -tf file.tar file2.txt file3.txt file1.txt file1.txt file3.txt 9,–test-labeltest the archive volume label and exit. 10,-u,–updateOnly append files newer than copy in archive.Example: [root@localhost TAR]# tar -tf file.tar file1.txt file2.txt [root@localhost TAR]# tar -uf file.tar file1.txt file3.txt file2.txt [root@localhost TAR]# tar -tf file.tar file1.txt file2.txt file3.txt 11,-C,–directory=DIRChange to directory DIR. Example:Extract files to another directory: [root@localhost TAR]# tar -xvf file.tar -C /root/TAR2 file1.txt file2.txt [root@localhost TAR]# cd - /root/TAR2 [root@localhost TAR2]# ll total 28 -rw-r--r--. 1 root root 23250 Feb 7 23:11 file1.txt -rw-r--r--. 1 root root 887 Feb 7 22:38 file2.txt 12,-p,–preserve-permissionsExtract information about file permissions (default for superuser) Create archive with?compression:It is very helpful to make an archive of files which has comparatively large size. Commonly using compression methods are?“BZIP”?and?“GZIP”. Switches with examples,?compression?related. 13,-j,–bzip2filter the archive through bzip2 Example: [root@localhost TAR]# tar -jcf file.tar.bz file2.txt file1.txt [root@localhost TAR]# ll total 128 -rw-r--r--. 1 root root 23250 Feb 7 23:11 file1.txt -rw-r--r--. 1 root root 887 Feb 7 22:38 file2.txt -rw-r--r--. 1 root root 30720 Feb 7 23:30 file.tar -rw-r--r--. 1 root root 1797 Feb 7 23:42 file.tar.bz See,the tar file size is decreased to 1797 with BZIP 14,-z,–gzipfilter the archive through gzip (编辑:百科站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- filesystems – rsync在不同文件系统之间复制未修改的文件
- pthreads:以编程方式收集有关在不同状态上花费的时间的信息
- linux – 如何确定glibc(glibcxx)二进制版的版本将取决于?
- linux – 当两个驱动器都被更大的驱动器替换时,dell perc 6
- 一篇了解Linux 发行版 Manjaro,yyds!
- linux – 更改默认网关,多个NIC
- linux – 如何获取youtube视频的URL以进行下载?
- 在我的程序中使用OpenBSD的malloc,realloc和free
- linux – PostgreSQL高性能安装程序
- linux – 修补和编译Ext4作为内核模块
- NMAP - A Stealth Port Scanner--reference
- linux – 什么比这更快?通过nfs-mount或通过scp
- linux – 为什么在配置文件中接受认证文件而不在
- linux – 定期运行Chef / Puppet的优缺点是什么?
- Linux Shell脚本Ldd命令原理及使用方法介绍
- SELinux安全上下文的修改和操作chcon和restoreco
- 详解Unix和Linux操作系统中Cron的用法总结
- linux – Secondary IP(eth0:0)就像主服务器IP一
- python – ImportError:没有名为’pysqlite2’的
- 命令行 – cURL命令失败

