[Linux Shell学习系列三]常用Shell(Bash)命令

您所在的位置:网站首页 显示所有文件怎么操作 [Linux Shell学习系列三]常用Shell(Bash)命令

[Linux Shell学习系列三]常用Shell(Bash)命令

2024-07-17 14:48| 来源: 网络整理| 查看: 265

D6

3.1 查看文件和目录

1. ls命令

$ ls #列出当前目录下所有文件和目录,但不显示详细信息 a b c.txt d.txt e.txt $ ls -1 #每行显示一条记录 a b c.txt d.txt e.txt $ ls -l #以长列表格式显示文件和目录,包括文件类型、大小、修改日期和时间、权限等信息 total 8 drwxrwxr-x. 3 user1 user1 4096 May 11 15:01 a drwxrwxr-x. 3 user1 user1 4096 May 11 15:01 b -rw-rw-r--. 1 user1 user1 0 May 11 15:50 c.txt -rw-rw-r--. 1 user1 user1 0 May 11 15:52 d.txt -rw-rw-r--. 1 user1 user1 0 May 11 15:50 e.txt #第一个字符:文件类型,--普通文件,d-目录,s-套接字文件,l-链接文件 #字段1:文件权限,9个字符,前三个-所有者,中间三个-用户组,后面三个-其他用户的读、写、执行权限 #字段2:链接数 #字段3:所有者,此处为user1 #字段4:用户组,user1 #字段5:文件大小,默认字节 #字段6:文件最近一次修改时间 #字段7:文件名 $ls -lh #将文件大小显示为适合阅读的格式 total 8.0K drwxrwxr-x. 3 user1 user1 4.0K May 11 15:01 a drwxrwxr-x. 3 user1 user1 4.0K May 11 15:01 b -rw-rw-r--. 1 user1 user1 0 May 11 15:50 c.txt -rw-rw-r--. 1 user1 user1 0 May 11 15:52 d.txt -rw-rw-r--. 1 user1 user1 0 May 11 15:50 e.txt $ ls -F #使用不同的特殊字符归类不同的文件类型 a/ b/ c.txt d.txt e.txt #/——表示目录 #无特殊字符——表示普通文件 #@——表示链接文件 #*——表示可执行文件 $ ls -ld a #以长列表格式列出某个目录的信息 drwxrwxr-x. 3 user1 user1 4096 May 11 15:01 a $ ls -R #递归地列出子目录的内容 .: a b c.txt d.txt e.txt ./a: test ./a/test: a.txt file1.txt file2.txt ./b: test ./b/test: b.txt file10.txt file3.txt file4.txt $ ls -ltr #以长列表格式按文件或目录的修改时间倒序地列出文件和目录 total 8 drwxrwxr-x. 3 user1 user1 4096 May 11 15:01 b drwxrwxr-x. 3 user1 user1 4096 May 11 15:01 a -rw-rw-r--. 1 user1 user1 0 May 11 15:50 c.txt -rw-rw-r--. 1 user1 user1 0 May 11 15:50 e.txt -rw-rw-r--. 1 user1 user1 0 May 11 15:52 d.txt $ ls -ls #以长列表格式按文件大小顺序列出目录和文件 total 8 4 drwxrwxr-x. 3 user1 user1 4096 May 11 15:01 a 4 drwxrwxr-x. 3 user1 user1 4096 May 11 15:01 b 0 -rw-rw-r--. 1 user1 user1 0 May 11 15:50 c.txt 0 -rw-rw-r--. 1 user1 user1 0 May 11 15:52 d.txt 0 -rw-rw-r--. 1 user1 user1 0 May 11 15:50 e.txt $ ls -a #列出包括隐藏文件或目录在内的所有文件和目录,包括.(当前目录)和..(父目录) . .. a b c.txt d.txt e.txt $ ls -A #列出包括隐藏文件或目录在内的所有文件和目录,不包括.(当前目录)和..(父目录) a b c.txt d.txt e.txt $ ls -i #显示文件或目录的inode编号,可能在系统维护操作时需要(如find命令中用inode编号移除文件名中有特殊字符的文件) 265182 a 917505 b 265183 c.txt 265184 d.txt 265185 e.txt $ ls -n #类似于-l,但用uid和gid代替显示所有者和用户组 total 8 drwxrwxr-x. 3 1001 1001 4096 May 11 15:01 a drwxrwxr-x. 3 1001 1001 4096 May 11 15:01 b -rw-rw-r--. 1 1001 1001 0 May 11 15:50 c.txt -rw-rw-r--. 1 1001 1001 0 May 11 15:52 d.txt -rw-rw-r--. 1 1001 1001 0 May 11 15:50 e.txt

 

2. cat命令

可以查看文件的内容、连接文件、创建一个或多个文件和重定向输出到终端或文件。

格式:cat [OPTION] [FILE]...

$ cat c.txt #查看文件内存 a b c d 1 2 3 4 2 4 6 8 4 8 12 16 $ cat d.txt #查看文件内容 This is d.txt. $ cat c.txt d.txt #查看多个文件的内容 a b c d 1 2 3 4 2 4 6 8 4 8 12 16 This is d.txt. $ cat -n c.txt #显示文件内容的行号 1 a b c d 2 1 2 3 4 3 2 4 6 8 4 4 8 12 16 $ cat -b c.txt #显示文件内容的行号,与-n不同,只标识非空白行的行号 1 a b c d 2 1 2 3 4 3 2 4 6 8 4 4 8 12 16 $ cat -e c.txt #在每一行的结尾显示$字符 a b c d$ 1 2 3 4$ 2 4 6 8$ 4 8 12 16$ $ cat > f.txt #接收标准输入的内容并在标准输出中显示,此处重定向标准输出到新的文件 abcdef #此处输入abcdef并按Ctrl+D组合键退出,内容会写到文件中 $ cat f.txt #查看生成的文件内容 abcdef $ cat d.txt >> f.txt #利用重定向,将文件内容追加到文件中,用>则覆盖 $ cat f.txt abcdef This is d.txt.

tac命令是将cat命令倒过来,用法:

$ tac c.txt #tac命令以行倒序的形式显示文件内容 4 8 12 16 2 4 6 8 1 2 3 4 a b c d

 

D7

3. more和less命令

1)more命令是一个用于一次翻阅一整屏文件的过滤器。

$ more /etc/inittab #用more命令查看一个文件,会自动清空屏幕并显示文件的开始部分 # inittab is no longer used when using systemd. # # ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. # # Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target # # systemd uses 'targets' instead of runlevels. By default, there are two main targets: # # multi-user.target: analogous to runlevel 3 # graphical.target: analogous to runlevel 5 # # To view current default target, run: # systemctl get-default # # To set a default target, run: # systemctl set-default TARGET.target #此时按空格键,more会将文件下移一个你当前终端窗口的高度,显示下一页的内容。 $ more -5 /etc/inittab #使用-num(整数)选项,可以指定一次显示的行数 # inittab is no longer used when using systemd. # # ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. # # Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target ---More--(37%) $ cat /etc/inittab | more #通过管道将cat命令显示的内容输出到more,一次输出但慢慢查看

2) less命令

less命令在查看前不需要加载整个文件,因此更快速;与more命令相比,支持向前和向后翻页。

$ less /etc/fstab #用less命令查看一个文件

a)用less命令打开一个文件后,可以使用搜索功能:

向前搜索:

/——输入/后面是要搜索的关键字,然后键入回车,显示内容的第一行将自动跳转到关键字第一次出现的位置,并高亮显示所有搜索到的关键字;

n——输入n,显示内容的第一行将向前跳转到下一个匹配;

N——输入N,显示内容的第一行将向回跳转到前一个匹配;

 向后搜索:

?——与/相反,向回搜索关键字;

n——向回搜索下一个匹配;

N——向前搜索下一个匹配。

b)浏览较大的文件时,可以使用如下屏幕导航命令:

Ctrl+F——向前翻一个窗口的内容

Ctrl+B——向回翻一个窗口的内容

Ctrl+D——向前翻半个窗口的内容

Ctrl+U——向回翻半个窗口的内容

G——跳转到文件末尾

g——跳转到文件开头

q or ZZ——退出less

c)可以使用less打开多个文件

$ less file1 #打开file1 #此时输入:":e",显示 Examine: #此时输入:"file2",可打开file2,末尾显示: file2 (file2 of 2) (End)

当使用less打开了多个文件时,可使用如下关键字切换文件:

:n——跳转到下一个文件;

:p——跳转到前一个文件。

d)less可以在文件的特定位置做一个标记,并跳转到标记位置:

m——后跟任意小写字母,使用这个字母标记当前位置;

'(单引号)——后跟任意小写字母,返回到这个小写字母标记的位置。

#输入m,可看到: mark: #输入"'",可看到: goto mark:

e)在less中输入F显示新写入的内容(注:我尝试该命令有下面的显示,但等待很久都没有更新内容)

#输入F,显示: Waiting for data...(interrupt to abort)

 

4. head命令

head命令用于打印指定输入的开头部分内容。默认打印每个指定输入的前10行。

$ head -n 2 c.txt #使用[-n 正整数N],显示前N行 a b c d 1 2 3 4 $ head -2 c.txt #直接使用[-正整数N],显示前N行 a b c d 1 2 3 4 $ head -n -1 c.txt #使用[-n -正整数N],显示除倒数N行外的所有行 a b c d 1 2 3 4 2 4 6 8 $ head -c 10 c.txt #使用[-c 正整数N],显示前N个字节的数据 a b c d 1

 

5. tail命令

tail命令与head命令相反,打印指定输入的结尾部分的内容。默认打印最后10行。

$ tail -n 2 c.txt #打印末尾2行 2 4 6 8 4 8 12 16 $ tail -2 c.txt #打印末尾2行 2 4 6 8 4 8 12 16 $ tail -f c.txt #打印文件中新写入的行,对监控日志非常有用 a b c d 1 2 3 4 2 4 6 8 4 8 12 16 #此处会一直等待新内容 $ tail -f c.txt --pid=31741 #在特定的进程结束时终结tail命令 #这里的31741进程对应执行的run.sh脚本,会写入文件10次,run.sh结束后,tail命令也会结束 $cat run.sh for i in {0..10};do sleep 10 echo $(date) >> c.txt done $ tail -f g.txt --retry #执行tail时g.txt不存在,执行run.sh创建出来并持续写入 $ cat run.sh for i in {0..10};do sleep 10 echo $(date) >> g.txt done #tail命令输出的内容 tail: warning: --retry only effective for the initial open tail: cannot open 'g.txt' for reading: No such file or directory tail: 'g.txt' has appeared; following end of new file Tue May 12 09:50:25 CST 2020 Tue May 12 09:50:35 CST 2020 ...... $ tail -f g.txt #执行时g.txt不存在且不加--retry选项 tail: cannot open 'g.txt' for reading: No such file or directory tail: no files remaining

 

6. file命令

file命令用于接收一个文件作为参数并执行某些测试,以确定正确的文件类型。

#基本方法 $ file /etc/inittab /etc/inittab: ASCII text $ file /etc/init.d/network /etc/init.d/network: Bourne-Again shell script, ASCII text executable $ file /usr/bin/file /usr/bin/file: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=1304f666b8d56dc664e73696a426431aa46ec676, stripped $ file /etc /etc: directory #使用-i选项,可以MIME类型的格式显示文件类型信息 $ file -i /etc/inittab /etc/inittab: text/plain; ascii $ file -i /etc/init.d/network /etc/init.d/network: text/x-shellscript; charset=us-ascii $ file -i /usr/bin/file /usr/bin/file: application/x-executable; charset=binary $ file -i /etc /etc: inode/directory; charset=binary #使用-N选项,输出的队列可以在文件名之后无空白填充的形式显示,对比如下 $ file * a: directory b: directory c.txt: ASCII text d.txt: ASCII text e.txt: empty f.txt: ASCII text run.sh: ASCII text $ file -N * a: directory b: directory c.txt: ASCII text d.txt: ASCII text e.txt: empty f.txt: ASCII text run.sh: ASCII text

 

7. wc命令

wc命令用于查看文件的行数、单词数和字符数等信息。

格式如:wc filename

输出格式如:X Y Z filename,X——行数,Y——单词数,Z——字节数,filename——文件名

$ wc c.txt 37 214 991 c.txt $ wc -l c.txt #使用-l,只显示行数 37 c.txt $ wc -w c.txt #使用-w,只显示单词数 214 c.txt $ wc -c c.txt #使用-c,只显示字节数 991 c.txt $ wc -L c.txt #使用-L,显示文件的最长的行的长度 28 c.txt

 

8. find命令

find命令用于根据指定的参数搜索和定位文件和目录的列表。

# find /etc -name inittab #指定目录,通过-name指定文件名查找 /etc/inittab # cd /etc/ # find . -name inittab #在当前目录下茶轴 ./inittab $ find . -iname c.txt #在当前目录下,不区分大小写的c.txt ./C.txt ./c.txt $ find . -type d -name test #在当前目录下,查找名为test的目录 ./a/test ./b/test $ find . -type f -name "*.txt" #在当前目录下,查找txt文件 $ find . -type f -perm 0775 #在当前目录下,查找文件权限是755的所有文件 ./run.sh $ find . -type f ! -perm 0775 #在当前目录下,查找文件权限不是755的所有文件 $ find . -type f -perm 775|xargs chmod 755 #在当前目录下,查找文件权限是755的所有文件,并将其权限改为755 # find /etc -type f ! -perm /a+w #查找/etc目录下所有只读文件 # find ~ -type f -perm /a+x #查找当前用户主目录下所有可执行文件 $ find ./b -type f -name "b*" -exec rm {} \; #查找b目录下所有b开头的文件并删除 $ find . -type f -empty #查找当前目录下所有空文件 $ find . -type d -empty #查找当前目录下所有空目录 $ find /tmp/ -type f -name ".*" #查找tmp目录下所有隐藏文件 # find /tmp/ -user root #查找tmp目录下所有者是root的文件和目录 # find /tmp/ -group root #查找tmp目录下用户组是root的文件和目录 $ find ~ -type f -mtime 3 #查找当前用户主目录下3天前修改的文件 $ find ~ -type f -mtime 30 #查找当前用户主目录下30天前修改的文件 $ find ~ -type f -mtime -3 #查找当前用户主目录下3天内修改的文件 $ find ~ -type f -mtime +1 -mtime -6 #查找当前用户主目录下1天前6天内修改的文件 $ find ~ -type f -cmin -60 #查找当前用户主目录下1小时内变更过的文件 $ find ~ -type f -amin -60 #查找当前用户主目录下1小时内访问过的文件 $ find . -type f -size +10c #查找当前目录下大于10个字节的文件 $ find . -type f -size +10c -size -20c #查找当前目录下大于10个字节且小于20个字节的文件

注:可以看到find与管道命令配合使用进行操作。

 

本节结束

 



【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3