您的位置:首页 > 运维架构 > Linux

Linux 基础命令知识3

2015-07-08 18:09 656 查看



提取快照和备份数据都是日常工作,这节了解一下Shell基本的归档命令知识


tar归档

tar用于将多个文件和文件夹保存为单个文件,同时保留所有的文件属性,如所有者、权限等。

#tar 归档语法 tar -cf output.tar [SOURCES]

tar -cf output.tar file1 file2 file

# 命令参数-c 代表 “create file” -f 表示“指定文件名(specify filename)” -f后面必须紧跟文件名

[/code]


tar向归档文件中添加文件

#-r  append file to tar file

tar -rvf output.tar appent.file

#-v verbosly list files processed 打印处理的文件名称

#用下面的方法显示出归档文件的内容 -t list the contents of archive

tar -tf output.tar

hello.gz

shell_curl.sh


tar -tvvf test.tar

-rwxr-xr-x root/root       181 2013-09-03 18:16 hello.gz

-rwxr-xr-x root/root        71 2013-09-04 18:14 shell_curl.sh

[/code]


tar从归档文件中提取文件或文件夹

选项 -x 表示提取extratale file from the archive

选项 -C 表示指定提取的目标地址文件 change the directory DIR

#当使用 -x时-f指定stdin用于提取内容 -c时-f表示指定stdout作为归档文件

tar -xvf test.tar test

tar -cf - file1 file2  | tar -xvf - -C ~/directory

[/code]


tar拼接两个归档文件

#使用-A选项 append tar file to archive

tar -Avf test.tar test2.tar

tar -tf test.tar

hello.gz

shell_curl.sh

shell_for.sh

[/code]


tar通过检查时间戳来更新归档的内容

#-u only update file newer to the archive

tar -uvvf archive.tar filea

[/code]


从归档文件中删除文件

tar -f archive.tar --delete file1 file2

tar --delte --file archive.tar file1 file2

[/code]


压缩归档文件

tar 命名只能用来对文件进行归档,它并不具备压缩功能,所以多数用户在使用归档文件时都会对文件采用某种形式的压缩,归档文件通常被压缩为一下格式之一:.tar.gz ,.tar.bz2, .tar.lzma,.tar.lzo

#可以通过 -z 来指定使用gzip格式 -j指定bunzip格式

#--exculude 排除所有的.txt文件

tar -czvvf arch.tar.gz * --exculude "*.txt"


tar -czvvf test.tar.gz shell_*

-rwxrwxrwx root/root       114 2013-09-26 11:05 shell_26.sh

-rwxr-xr-x root/root       147 2013-09-05 17:29 shell_case.sh

-rwxr-xr-x you/you          71 2013-09-04 18:14 shell_curl.sh

-rwxr-xr-x root/root       199 2013-09-05 12:22 shell_findMaxFile.sh

-rwxr-xr-x root/root       344 2013-09-04 17:40 shell_for.sh

-rwxr-xr-x root/root       362 2013-09-05 17:23 shell_getInet.sh

[/code]


gunzip 或 gzip压缩


gunzip 和 zip 压缩文件

gzip filename

ls

filename.gz

#gzip会删除原文件生成一个压缩文件filename.gz


#gzip查看压缩文件的属性信息

gzip -l hello.gz

compressed        uncompressed  ratio uncompressed_name

181                 211         25.6% hello


#从stdin 中读取并写入到stdout中:

cat file | gzip -c > file.gz

[/code]


压缩归档文件

方法1

#-z 选项指定使用gzip格式进行压缩

tar -czvvf archive.tar.gz [files]

#-a 指定从文件扩展名自动判断压缩格式

tar -cavvf archive.tar.[files]

#ps tar 命令解压-x选项用于解压,tar -zxvf archive.tar.gz 或者 tar -xavf archive.tar.gz

[/code]

方法2

#创建一个归档文件

tar -cvvf archive.tar [files]

#压缩归档文件

gzip archive.tar


#PS: gzip -9 test.img 指定压缩率 1最下 9最大 压缩率越大,压缩速度越慢

[/code]


解压缩

gunzip filename.gz


#zcat 无需解压文件,直接读取内容到stdout

zcat fileame.gz

[/code]

[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: