首页 > 系统相关 >Linux tar Command – How to Compress Files in Linux

Linux tar Command – How to Compress Files in Linux

时间:2023-01-03 12:55:44浏览次数:63  
标签:Files tar gz command file Linux directory archive

https://www.freecodecamp.org/news/how-to-compress-files-in-linux-with-tar-command

How to Compress and Extract Files Using the tar Command on Linux

UPDATED OCT 28, 2022, 11:15 AM EST | 4 MIN READ

Run "tar -czvf (archive name).tar.gz (pathtofile)” in the Terminal to compress a file or folder. To extract an archive to the current folder, run the command “tar -xzvf (archive file)".

The tar command on Linux is often used to create .tar.gz or .tgz archive files, also called “tarballs.” This command has a large number of options, but you just need to remember a few letters to quickly create archives with tar. The tar command can extract the resulting archives, too.

The GNU tar command included with Linux distributions has integrated compression. It can create a .tar archive and then compress it with gzip or bzip2 compression in a single command. That’s why the resulting file is a .tar.gz file or .tar.bz2 file.

Compress an Entire Directory or a Single File

Use the following command to compress an entire directory or a single file on Linux. It’ll also compress every other directory inside a directory you specify–in other words, it works recursively.

tar -czvf name-of-archive.tar.gz /path/to/directory-or-file

Here’s what those switches actually mean:

  • -c: Create an archive.
  • -z: Compress the archive with gzip.
  • -v: Display progress in the terminal while creating the archive, also known as “verbose” mode. The v is always optional in these commands, but it’s helpful.
  • -f: Allows you to specify the filename of the archive.
 

Let’s say you have a directory named “stuff” in the current directory and you want to save it to a file named archive.tar.gz. You’d run the following command:

tar -czvf archive.tar.gz stuff

Or, let’s say there’s a directory at /usr/local/something on the current system and you want to compress it to a file named archive.tar.gz. You’d run the following command:

tar -czvf archive.tar.gz /usr/local/something

Run "tar -czvf" on the desired folder to bundle and compress all of the contents.

Compress Multiple Directories or Files at Once

RELATED: How to Manage Files from the Linux Terminal: 11 Commands You Need to Know

While tar is frequently used to compress a single directory, you could also use it to compress multiple directories, multiple individual files, or both. Just provide a list of files or directories instead of a single one. For example, let’s say you want to compress the /home/ubuntu/Downloads directory, the /usr/local/stuff directory, and the /home/ubuntu/Documents/notes.txt file. You’d just run the following command:

tar -czvf archive.tar.gz /home/ubuntu/Downloads /usr/local/stuff /home/ubuntu/Documents/notes.txt

Just list as many directories or files as you want to back up.

Ubuntu Terminal compressing multiple directories at once with the "tar" command.

Exclude Directories and Files

In some cases, you may wish to compress an entire directory, but not include certain files and directories. You can do so by appending an --exclude switch for each directory or file you want to exclude.

For example, let’s say you want to compress /home/ubuntu, but you don’t want to compress the /home/ubuntu/Downloads and /home/ubuntu/.cache directories. Here’s how you’d do it:  
tar -czvf archive.tar.gz /home/ubuntu --exclude=/home/ubuntu/Downloads --exclude=/home/ubuntu/.cache

The --exclude switch is very powerful. It doesn’t take names of directories and files–it actually accepts patterns. There’s a lot more you can do with it. For example, you could archive an entire directory and exclude all .mp4 files with the following command:

tar -czvf archive.tar.gz /home/ubuntu --exclude=*.mp4

The "tar" command can be tuned to selectively ignore some files.

Use bzip2 Compression Instead

While gzip compression is most frequently used to create .tar.gz or .tgz files, tar also supports bzip2 compression. This allows you to create bzip2-compressed files, often named .tar.bz2, .tar.bz, or .tbz files. To do so, just replace the -z for gzip in the commands here with a -j for bzip2.

 

Gzip is faster, but it generally compresses a bit less, so you get a somewhat larger file. Bzip2 is slower, but it compresses a bit more, so you get a somewhat smaller file. Gzip is also more common, with some stripped-down Linux systems including gzip support by default, but not bzip2 support. In general, though, gzip and bzip2 are practically the same thing, and both will work similarly.

For example, instead of the first example we provided for compressing the stuff directory, you’d run the following command:

tar -cjvf archive.tar.bz2 stuff

Terminal running bzip2 compression instead of gzip.

Extract an Archive

Once you have an archive, you can extract it with the tar command. The following command will extract the contents of archive.tar.gz to the current directory.

tar -xzvf archive.tar.gz

It’s the same as the archive creation command we used above, except the -x switch replaces the -c switch. This specifies you want to extract an archive instead of create one.

 

You may want to extract the contents of the archive to a specific directory. You can do so by appending the -C switch to the end of the command. For example, the following command will extract the contents of the archive.tar.gz file to the /tmp directory.

tar -xzvf archive.tar.gz -C /tmp

If the file is a bzip2-compressed file, replace the “z” in the above commands with a “j”.

Run "tar" with"-xzvf" instead of the "-czvf" argument to extract files from a tarball archive.


This is the simplest possible usage of the tar command. The command includes a large number of additional options, so we can’t possibly list them all here. For more information. run the info tar command at the shell to view the tar command’s detailed information page. Press the q key to quit the information page when you’re done. You can also read tar’s manual online.

If you’re using a graphical Linux desktop, you could also use the file-compression utility or file manager included with your desktop to create or extract .tar files. On Windows, you can extract and create .tar archives with the free 7-Zip utility.

标签:Files,tar,gz,command,file,Linux,directory,archive
From: https://www.cnblogs.com/kungfupanda/p/17021759.html

相关文章

  • NewStarCTF flip-flop 复现(CBC字节翻转攻击)
    NewStarCTFflip-flop复现(CBC字节翻转攻击)本来是一道nc题.这里复现就把拿到admin权限为目标了.题目importosfromCrypto.CipherimportAESauth_major_key=os.......
  • 基于linux下的shell中常用的控制语句
    学习目标:for语句while语句if语句case语句expect语句exitbreakcontinue退出命令的区别:exit直接退出当前脚本break仅仅退出本次循环continue退出本次循环进行下一次循......
  • 基于linux下的shell变量
    变量的定义:变量即在程序运行过程中它的值是允许改变的量,变量是用一串固定的字符来标志不固定的值的一种方法,变量是一种使用方便的占位符,用于引用计算机内存地址,该地址可以存......
  • 基于linux下的shell中的运算及应用实例
    运算方式及运算符号:运算符号意义(*标示常用)+,-加法,减法*,/,%乘法,除法,取余**幂运算++,--自增加,自减少<,<=,>,>=比较符号=,+=,-=,*=,......
  • 基于linux下的shell正则表达式(grep,sed,awk)
    正则表达式:正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符、及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一......
  • 基于linux下的samba文件共享系统
    SMB文件共享:服务端口:通常使用TCP/445进行所有连接。还使用UDP137,UDP138和TCP/139进行向后兼容。主配置文件:/etc/samba/smb.conf配置环境:准备两台虚拟机,进行配置IP,y......
  • 基于linux下的时间同步
    时间同步:在服务器端共享时间:vim/etc/chrony.conf29localstratum10开启时间共享功能并设定共享级别这个参数开启后本机不去同步别人的时间到本机22allow172.25.254.0/2......
  • 基于Linux下的定时任务
    定时任务:[root@foundation21~]#systemctlstatuscrond.service 首先查看定时服务是否开启[root@foundation21~]#crontab-uroot-e   建立定时任务,注意使用date......
  • 基于Linux下的临时文件的管理
    对临时文件的管理:[[email protected]]#cd/usr/lib/tmpfiles.d/ 切换路径[[email protected]]#vimwestos.conf  [[email protected]]......
  • 基于linux下的shell脚本练习
    shell脚本的简介:打开文本编辑器(可以使用vi/vim命令来创建文件),新建一个文件test.sh,扩展名为sh(sh代表shell),扩展名并不影响脚本执行,见名知意就好,如果你用php写shell脚本,扩......