@文件管理及文本处理常用命令

1. head命令


#显示文件的头部信息,默认显示文件的前十行内容 


选项:

		-n		#取消默认输出
        
        -c		#显示文件的最前面的字符  

[root@qls ~]# head passwd 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin



[root@qls ~]# head  -n 2  passwd 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

===

[root@qls ~]# head  -2  passwd 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin


[root@qls ~]# head  -20  passwd 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin


[root@qls ~]# head  -c5  passwd 
root:[root@qls ~]# 


2. tail命令


#显示文件的尾部信息,默认显示文件最后10行 

	选项:
	
			-n		#取消默认输出
            
            -c		#显示最后几个字符 
            
            -f		#实时显示文件的尾部变化信息  
            
            -F		#实时更新文件的内容,当文件不存在,不会退出,会一直尝试读取这个文件,直到文件存在 

[root@qls ~]# tail  passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin


[root@qls ~]# tail  -n 1  passwd
postfix:x:89:89::/var/spool/postfix:/sbin/nologin

===

[root@qls ~]# tail  -1  passwd
postfix:x:89:89::/var/spool/postfix:/sbin/nologin


[root@qls ~]# tail  -c5  passwd 			#显示最后5个字符,只能看见4个,另外一个是换行符 
ogin
[root@qls ~]# 


[root@qls ~]# tail -f  passwd 
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin



hello

#另外一个窗口进行测试  
[root@qls ~]# echo 'hello'  >> passwd



[root@qls ~]# tail  -f  test.log
tail: cannot open ‘test.log’ for reading: No such file or directory
tail: no files remaining
[root@qls ~]# tail  -F  test.log
tail: cannot open ‘test.log’ for reading: No such file or directory



tail: ‘test.log’ has appeared;  following end of new file
test


3. tailf命令

#实时更新文件的内容  


tailf    ====    tail   -f

当文件内容没有发生变化时,tailf不会去读取磁盘中的信息,减少磁盘的读写 ,tail  -f  当文件内容没有发生变化时,会一直向磁盘进行读取


选项:  

	-n			#取消默认输出 
	
	

4. locate命令

#根据本地的数据库进行查找文件,不会查找到最新的文件  

[root@qls ~]# yum  install  -y  mlocate

选项:

		-i		#不区分大小写  
		
		-r		#简单使用正则表达式   $  结尾  

/var/lib/mlocate/mlocate.db

[root@qls ~]# updatedb			#更新数据库  

#把系统中所有文件名称包含passwd的文件全部查找出来 

[root@qls ~]# locate  passwd
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
/etc/security/opasswd
/root/passwd
/usr/bin/gpasswd
/usr/bin/grub2-mkpasswd-pbkdf2
/usr/bin/passwd
/usr/lib/firewalld/services/kpasswd.xml


[root@qls ~]# locate    hostnamectl
/usr/bin/hostnamectl
/usr/share/bash-completion/completions/hostnamectl
/usr/share/man/man1/hostnamectl.1.gz
/usr/share/zsh/site-functions/_hostnamectl

[root@qls ~]# touch  hostnamectl

[root@qls ~]# updatedb	

[root@qls ~]# locate    hostnamectl
/root/hostnamectl
/usr/bin/hostnamectl
/usr/share/bash-completion/completions/hostnamectl
/usr/share/man/man1/hostnamectl.1.gz
/usr/share/zsh/site-functions/_hostnamectl

[root@qls ~]# updatedb 
[root@qls ~]# locate    hostnamectl
/root/hostnamectl
/usr/bin/hostnamectl
/usr/share/bash-completion/completions/hostnamectl
/usr/share/man/man1/hostnamectl.1.gz
/usr/share/zsh/site-functions/_hostnamectl
[root@qls ~]# locate  -i  hostnamectl
/opt/HOSTNAMEctl
/root/hostnamectl
/usr/bin/hostnamectl
/usr/share/bash-completion/completions/hostnamectl
/usr/share/man/man1/hostnamectl.1.gz
/usr/share/zsh/site-functions/_hostnamectl


[root@qls ~]# locate  -r  hostname$
/etc/hostname
/etc/selinux/targeted/active/modules/100/hostname
/usr/bin/hostname
/usr/bin/nmtui-hostname
/usr/lib64/gettext/hostname

5. which命令

#查找命令的绝对路径  

[root@qls ~]# which  ping
/usr/bin/ping

#查找命令的绝对路径时,是通过PATH环境变量中的路径进行查找  
[root@qls ~]# echo  $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

#不使用which的别名进行查找命令的绝对路径 

[root@qls ~]# \which   cp
/usr/bin/cp


[root@qls ~]# rpm -qf  `which  ping`
iputils-20160308-10.el7.x86_64

[root@qls ~]# rpm  -qf  /usr/bin/ping
iputils-20160308-10.el7.x86_64

6. whereis命令

#查找系统二进制程序、man帮助文件、源代码文件     查找不到自己创建的文件  

选项:

	-b		#只查找二进制程序文件
	
	-m		#查找man帮助文件 
	
	-s		#查找源代码文件  

[root@qls ~]# whereis   ping
ping: /usr/bin/ping /usr/share/man/man8/ping.8.gz


[root@qls ~]# whereis -b  ping
ping: /usr/bin/ping
[root@qls ~]# whereis -m  ping
ping: /usr/share/man/man8/ping.8.gz

7. type命令

#显示命令的类型  

选项:

	-a		#显示内置命令的绝对路径  
	
	-p		#只显示命令的绝对路径

help命令可以显示系统中所有的内置命令 


[root@qls ~]# type  ping
ping is /usr/bin/ping

[root@qls ~]# type  cd
cd is a shell builtin

[root@qls ~]# type  -a  cd
cd is a shell builtin
cd is /usr/bin/cd


[root@qls ~]# type  -p  ping
/usr/bin/ping


[root@qls ~]# type  -ap  cd
/usr/bin/cd


8. find命令

#查找和搜索文件  

选项:

	-type		#根据文件类型进行查找
	
		f		#普通文件
		
		d		#目录
		
		l		#软连接文件 
		
		s		#socket文件  套接字文件  
		
		p		#管道文件 


	-name		#根据名称进行查找  
	
	
	-iname		#查找的时候忽略大小写  
	
	
语法:
						  选项,条件            干什么  
                          
                          
	命令      地区        类型     名称       动作     默认动作就是打印 
	
#在/etc目录下进行查找      查找类型为普通文件    名称为  hostname   精确查找  
[root@qls ~]# find  /etc  -type  f   -name  "hostname"  
/etc/hostname


[root@qls ~]# touch  /opt/hostname{,ctl}
[root@qls ~]# ll /opt/
total 0
-rw-r--r--. 1 root root 0 Jul 10 10:32 hostname
-rw-r--r--. 1 root root 0 Jul 10 10:32 hostnamectl
[root@qls ~]# touch  /opt/test_hostname
[root@qls ~]# touch  /opt/test_hostname.txt
[root@qls ~]# ll /opt/
total 0
-rw-r--r--. 1 root root 0 Jul 10 10:32 hostname
-rw-r--r--. 1 root root 0 Jul 10 10:32 hostnamectl
-rw-r--r--. 1 root root 0 Jul 10 10:32 test_hostname
-rw-r--r--. 1 root root 0 Jul 10 10:33 test_hostname.txt
[root@qls ~]# find  /opt/  -type  f  -name "hostname"
/opt/hostname


#查找以hostname开头的文件

[root@qls ~]# find  /opt/    -name  "hostname*"
/opt/hostname
/opt/hostnamectl

#查找以hostname为结尾的文件  

[root@qls ~]# find   /opt/  -name  "*hostname"
/opt/hostname
/opt/test_hostname

#查找文件名称包含hostname的文件 

[root@qls ~]# find  /opt/  -name "*hostname*"
/opt/hostname
/opt/hostnamectl
/opt/test_hostname
/opt/test_hostname.txt

#查找所有文件,包括隐藏文件  

[root@qls ~]# touch   /opt/.hostname.log
[root@qls ~]# find  /opt/  -name "*hostname*"
/opt/hostname
/opt/hostnamectl
/opt/test_hostname
/opt/test_hostname.txt
/opt/.hostname.log


[root@qls ~]# find  /opt/  -type d -iname "*hostname*"
/opt/HOSTNAMECTL


9. rz命令


#将本地的文件上传到Linux操作系统     

#不能上传目录,需要将目录打成一个压缩包进行上传     只能上传 4G以下的文件  

[root@qls ~]# yum  install  lrzsz   -y

选项:

	-E		#当上传的文件已经存在时,系统会进行重命名   会在原来的文件名称后面加上.数字  从0开始 
	
	
#执行命令,会跳出一个Windows界面的窗口,选择你要上传的文件  

[root@qls ~]# rz

[root@qls ~]# ll
total 16
-rw-r--r--. 1 root root 13140 Jul  7 12:23 day03.md


[root@qls ~]# rz  -E

[root@qls ~]# ll
total 32
-rw-r--r--. 1 root root 13140 Jul  7 12:23 day03.md
-rw-r--r--. 1 root root 13140 Jul  7 12:23 day03.md.0

10. sz命令

#将Linux系统的文件下载到本地 

[root@qls ~]# sz  /etc/hosts



11. wget命令


[root@qls ~]# yum  install  -y  wget 

#联网下载软件包  


选项:  

	-O		#指定下载的路径和名称  
	
	-q		#静默输出  
	
	--limit-rate=10k		#限制下载的速率     k  ===KB    m  == MB
	
	

[root@qls ~]# wget  http://www.baidu.com
--2020-07-10 11:57:50--  http://www.baidu.com/
Resolving www.baidu.com (www.baidu.com)... 112.80.248.75, 112.80.248.76
Connecting to www.baidu.com (www.baidu.com)|112.80.248.75|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2381 (2.3K) [text/html]
Saving to: ‘index.html’

100%[===============================================================================>] 2,381       --.-K/s   in 0.008s  

2020-07-10 11:57:51 (290 KB/s) - ‘index.html’ saved [2381/2381]

[root@qls ~]# wget   http://nginx.org/download/nginx-1.18.0.tar.gz
--2020-07-10 12:00:16--  http://nginx.org/download/nginx-1.18.0.tar.gz
Resolving nginx.org (nginx.org)... 95.211.80.227, 62.210.92.35, 2001:1af8:4060:a004:21::e3
Connecting to nginx.org (nginx.org)|95.211.80.227|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1039530 (1015K) [application/octet-stream]
Saving to: ‘nginx-1.18.0.tar.gz’

100%[===============================================================================>] 1,039,530    282KB/s   in 3.6s   

2020-07-10 12:00:20 (282 KB/s) - ‘nginx-1.18.0.tar.gz’ saved [1039530/1039530]

[root@qls ~]# ll
total 1020
-rw-r--r--. 1 root root    2381 Jul 10 11:57 index.html
-rw-r--r--. 1 root root 1039530 Apr 21 22:33 nginx-1.18.0.tar.gz


[root@qls ~]# wget  -O  /opt/nginx.tar.gz  http://nginx.org/download/nginx-1.18.0.tar.gz
--2020-07-10 12:01:41--  http://nginx.org/download/nginx-1.18.0.tar.gz
Resolving nginx.org (nginx.org)... 62.210.92.35, 95.211.80.227, 2001:1af8:4060:a004:21::e3
Connecting to nginx.org (nginx.org)|62.210.92.35|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1039530 (1015K) [application/octet-stream]
Saving to: ‘/opt/nginx.tar.gz’

100%[===============================================================================>] 1,039,530    272KB/s   in 3.7s   

2020-07-10 12:01:46 (272 KB/s) - ‘/opt/nginx.tar.gz’ saved [1039530/1039530]

[root@qls ~]# ll /opt/
total 1016
-rw-r--r--. 1 root root       0 Jul 10 10:32 hostname
-rw-r--r--. 1 root root       0 Jul 10 10:32 hostnamectl
drwxr-xr-x. 2 root root       6 Jul 10 10:39 HOSTNAMECTL
-rw-r--r--. 1 root root 1039530 Apr 21 22:33 nginx.tar.gz


[root@qls ~]# wget  -q  http://nginx.org/download/nginx-1.19.1.tar.gz
[root@qls ~]# ll
total 2100
-rw-r--r--. 1 root root   41240 Mar 23 00:20 032220_1620_Zabbix1.png
-rw-r--r--. 1 root root    9810 Jul  8 00:06 download.html
-rw-r--r--. 1 root root    2381 Jul 10 11:57 index.html
-rw-r--r--. 1 root root 1039530 Apr 21 22:33 nginx-1.18.0.tar.gz
-rw-r--r--. 1 root root 1047223 Jul  7 23:59 nginx-1.19.1.tar.gz



[root@qls ~]# wget   --limit-rate=10k   http://nginx.org/download/nginx-1.18.0.tar.gz ^C
[root@qls ~]# rm -rf ./*
[root@qls ~]# wget   --limit-rate=10k   http://nginx.org/download/nginx-1.18.0.tar.gz
--2020-07-10 12:11:13--  http://nginx.org/download/nginx-1.18.0.tar.gz
Resolving nginx.org (nginx.org)... 62.210.92.35, 95.211.80.227, 2001:1af8:4060:a004:21::e3
Connecting to nginx.org (nginx.org)|62.210.92.35|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1039530 (1015K) [application/octet-stream]
Saving to: ‘nginx-1.18.0.tar.gz’

32% [========================>                                                       ] 335,872     10.0KB/s  eta 70s   



12. curl命令

#通过url规则进行文件传输工具  

#测试网站使用的命令  


选项:

		-o			#将访问的数据内容写入到指定的文件中 
        
        -s			#静默输出  
		
		

[root@qls ~]# curl  -o  ./baidu.html    www.baidu.com
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2381  100  2381    0     0    311      0  0:00:07  0:00:07 --:--:--   657
[root@qls ~]# ll
total 1356
-rw-r--r--. 1 root root    2381 Jul 10 12:15 baidu.html


[root@qls ~]# curl  -s   -o   ./nginx.tar.gz    http://nginx.org/download/nginx-1.18.0.tar.gz




  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值