打包、压缩、文件查找、通配符

打包、压缩与解压缩

gzip压缩文件特点:
1.后缀名为gz
2.可以通过gzip -9 file1.tar 命令调节压缩比
3.压缩文件后,原来的文件消失。
4.解压缩:gzip -d file1.tar.gz
切记不是背出来的 而是通过man命令查出来的。
tar打包命令
-f, --file=ARCHIVE
              use archive file or device ARCHIVE
-r, --append
              append files to the end of an archive

1.tar -cf archive.tar foo bar 打包
2.tar -tvf archive.tar 详细查看
或者粗略查看
[dennis@localhost ~]$ tar -tf file.tar
file2.txt
file2.txt
file1.txt
file2.txt

3.tar -xf archive.tar 解包
或者 tar -xf archive.tar ./dir 解包之后放到固定的目录下。
4.tar -Af archive.tar file.tar 将archive.tar的内容合并到file.tar
或者:tar -f archive.tar -A file.tar
或者:tar -f archive.tar -r file1.txt 将file1.txt加入到archive.tar中
5.tar -f archive.tar --delete file.txt 将archive.tar中的file.txt删除掉

观察一个现象,复制同一个文件不可以,但打包文件下加入一个相同的文件为什么就可以呢?
[dennis@localhost ~]$ tar -f file.tar -r file1.txt
[dennis@localhost ~]$ tar -tvf file.tar
-rw-rw-r-- dennis/dennis     0 2020-05-01 20:19 file2.txt
-rw-rw-r-- dennis/dennis     0 2020-05-01 20:19 file2.txt
-rw-rw-r-- dennis/dennis    12 2020-05-04 09:39 file1.txt
-rw-rw-r-- dennis/dennis     0 2020-05-01 20:19 file2.txt
-rw-rw-r-- dennis/dennis    12 2020-05-04 09:39 file1.txt

[dennis@localhost ~]$ cp file2.txt ./
cp: ‘file2.txt’ and ‘./file2.txt’ are the same file

cat命令

concatenate连接
连接几个文件
从键盘创建文件
查看文件

1.cat /etc/passwd 
只能显示一屏的内容,一屏40行。

2.cat -n /var/log/messages
显示行号(替换nl命令:显示行号)

3.[root@localhost ~]# cat -A /etc/issue
下面是输入命令后的效果:
\S$
Kernel \r on an \m$
$
显示所有。这就包含特殊的字符 比如换行符$

4.tac /etc/issue 
从最后一行显示

5.-n与-b的区别:
[root@localhost ~]# cat -n a.txt
     1	白日依山尽
     2	黄河入海流
     3	
     4	更上一层楼
[root@localhost ~]# cat -b a.txt
     1	白日依山尽
     2	黄河入海流

     3	更上一层楼

6.这种格式被称作为nonprinting
[root@localhost ~]# cat -v a.txt
M-gM-^YM-=M-fM-^WM-%M-dM->M-^]M-eM-1M-1M-eM-0M-=
M-iM-;M-^DM-fM-2M-3M-eM-^EM-%M-fM-5M-7M-fM-5M-^A

M-fM-^[M-4M-dM-8M-^JM-dM-8M-^@M-eM-1M-^BM-fM-%M-<
nonprinting是什么?  
they are characters that take up space or have a formatting function but do not appear on the printed page: spaces, tab characters, paragraph breaks, and the like.

less与more

more命令查看有百分数显示(本人更亲赖于这个命令)
more /var/log/messages |grep Dennis
回车:一行一行看
空格:一页一页看
b:back 一行一行向上看
ctrl+b:一页一页向上看
:f显示行数
/string 找固定的词语
使用管道筛选特定用户信息 查询的信息作为grep输入。
Linux grep 命令用于查找文件里符合条件的字符串。

less命令查看无百分数显示
适合查看比较多的文件。
up down健向上向下进行翻行
page up 与 page down健进行上下翻页
n跳转到下一个关键字处 N跳转到上一个关键字处
q离开

head 与 tail命令 查看日志文件


head默认显示头10行
[root@localhost ~]# head -3 /var/log/messages
Apr 28 21:30:01 localhost systemd: Started Session 9 of user root.
Apr 28 21:38:32 localhost systemd: Got automount request for /proc/sys/fs/binfmt_misc, triggered by 4997 (find)
Apr 28 21:38:32 localhost systemd: Mounting Arbitrary Executable File Formats File System...
[root@localhost ~]# tail -3 /var/log/messages
Apr 29 14:00:01 localhost systemd: Started Session 5 of user root.
Apr 29 14:01:01 localhost systemd: Started Session 6 of user root.
Apr 29 14:10:01 localhost systemd: Started Session 7 of user root.
查看 /etc/man_db.conf 11到20行
[root@localhost ~]# cat -n /etc/man_db.conf |head -n 20 |tail -n 10


文件查找

which查看可执行文件

which cd
用于查找固定的命令 
which -a cd
列出所有命令路径 不单单是第一个
type与history均为内置命令,在内存中,没有文件,所以用which找不到。
[root@localhost ~]# type type
type is a shell builtin
所以查不到,但为什么这里可以查到cd这个内置命令?
内置命令不一定没有文件,像cd,外部命令一定有文件。需要的时候加载进来。
以下命令是查看cp这个命令的位置及显示别名信息,这更加印证了为什么直接用cp带提示的原因了。
[root@localhost ~]# which cp
alias cp='cp -i'
	/usr/bin/cp

whereis

比which要广一些:

查看二进制格式文件(-b) 说明文件下的文件(-m)源文件(-s)及其它特殊文件(-u)

[root@localhost ~]# whereis -l
bin: /usr/bin
bin: /usr/sbin
bin: /usr/lib
bin: /usr/lib64
bin: /etc
bin: /usr/etc
bin: /usr/games
bin: /usr/local/bin
bin: /usr/local/sbin
bin: /usr/local/etc
bin: /usr/local/lib
bin: /usr/local/games
bin: /usr/include
bin: /usr/local
bin: /usr/libexec
bin: /usr/share
man: /usr/share/man/man0p
man: /usr/share/man/man1
man: /usr/share/man/man1p
man: /usr/share/man/man1x
man: /usr/share/man/man2
查看可以查找的范围。
whereis -b ifconfig

locate

搜索要快一些,从locatedb中搜索。但是每天只更新一次,所以查不到的时候使用updatedb进行更新。

使用updatedb命令之后查找到了我刚创建的a.txt
[root@localhost ~]# updatedb
[root@localhost ~]# locate a.txt
/root/a.txt
查看数据库情况:
[root@localhost ~]# locate -S
Database /var/lib/mlocate/mlocate.db:
	9,400 directories
	130,149 files
	6,240,046 bytes in file names
	2,911,672 bytes used to store database
查找最近用的六个文件:
[root@localhost ~]# locate -l 6 passwd
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
/etc/security/opasswd
/usr/bin/gpasswd
/usr/bin/grub2-mkpasswd-pbkdf2

查找 固定目录下的以a开头的文件
[root@localhost ~]# locate /root/a
/root/a.txt
/root/anaconda-ks.cfg
不能使用./相对路径
locate /a 具有什么含义?为什么通过这个命令查找出了不以a开头的文件?
locate a
为我所用:
此命令在查找以某个字母开头的文件和准确查找某个文件上比较出色。而且速度很快,但切记不能使用相对路径查找,效果不好。比如:locate ./a 但可以使用locate Desktop/a.txt这样的相对命令进行查找,这里不解。

find

使用find a.txt
只能查找到当前目录的文件,如果a.txt在下级目录,它是查不到的。所以默认是当前目录
find . a.txt
在当前目录及当前目录的子目录查找a.txt


加入其它属性
mtime
ctime
atime
以上三组是带time关键字,说明的是几天前。
mtime:modified,文件内容的修改。包括创建。
ctime:changed,文件状态被修改,比如权限。
atime:accessed 文件被访问。

1单选(1分)
如何在/tmp目录中查找过去24小时产生的,大小大于5k的普通文件并以不带提示的方式删除。
A.
find /tmp \(–type f -a -ctime -1 -a -size +5k \) -exec rm {} \;
B.
find /tmp \(–type f -a -mtime -1 -a -size +5k \) -exec rm {} \;
C.
find /tmp \(–type f -a -mtime -1 -a -size +5k \) -exec rm -f {} \;
D.
find /tmp \(–type f -a -ctime -1 -a -size -5k \) -exec rm -r {} \;

这道题比较综合,过去24h为一天,所以用time,且创建的这个文件,所以用m。而查找的文件类型为普通文件,所以选项-type为f,中间用-a逻辑符连接,最后运行rm命令,-f代表不带提示进行删除。never prompt。

[root@localhost ~]# find /home -mtime -2

[root@localhost ~]# find . -perm 755
[root@localhost ~]# find / -type b
[root@localhost ~]# find . -type f -size 5k
[root@localhost ~]# find . -name 'a.txt'
./Desktop/a.txt
./a.txt
配合动作表达式:
find . -name '*.tmp' -exec rm{} \;

通配符

四种通配符:

  • ? [] {}
*  任意长度的字符
? 任意单个字符  如??sh
[] 中括号通配符内要求的是按照一定顺序且连续的字母或数字
[dennis@localhost ~]$ cp /tmp/file/file[1-3].txt .
中括号通配符的第二种形式:
[dennis@localhost ~]$ ls file[1,2,3].txt
大括号通配符内为字符串
[dennis@localhost ~]$ ls {file1,file2}.txt
file1.txt  file2.txt
!
[dennis@localhost ~]$ ls file[!1-2].txt
file3.txt



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值