smyjs172lxy

1.echo

#显示你要输出的结果,所见即所得,创建新文件,写入内容到文件中,打印变量。给输出的结果加上颜色
-n #不显示换行符
-e #支持特殊的字符

#显示特殊字符
[root@lxy ~]# echo hello world
hello world
[root@lxy ~]# echo -e "hello\nworld" #\n换行符
hello
world
[root@lxy ~]# echo -e "hello\tworld" #\t tab键
hello world

#打印变量
[root@lxy ~]# echo $PS1
\[\e[37;40m\][\[\e[32;40m\]\u\[\e[37;40m\]@\h \[\e[36;40m\]\w\[\e[0m\]]\$
[root@lxy ~]# echo $USER
root

#创建新文件,并写入内容到文件中
[root@lxy ~]# echo hello world > test.txt #   >   标准输出重定向,覆盖源文件内容
[root@lxy ~]# ll
total 4
-rw-r--r--. 1 root root 12 Dec  2 18:24 test.txt

[root@lxy ~]# cat test.txt
123
[root@lxy ~]# echo 456 >> test.txt # >>   标准输出追加重定向,将内容追加到文件的底部
[root@lxy ~]# ll
total 4
-rw-r--r--. 1 root root 8 Dec  2 18:28 test.txt
[root@lxy ~]# cat test.txt
123
456

[root@lxy ~]# echo $USER #打印变量
root
[root@lxy ~]# echo \'$USER\' #强引用,所见即所得
$USER
[root@lxy ~]# echo "$USER" #弱引用,所见即所得,会解析变量
root
[root@lxy ~]# echo {1..5} #所见即所得,支持通配符,会解析变量
1 2 3 4 5
[root@lxy ~]# echo \'{1..5}\'
{1..5}
[root@lxy ~]# echo "{1..5}"
{1..5}
[root@lxy ~]# alias name=hostname
[root@lxy ~]#
[root@lxy ~]# name
lxy
[root@lxy ~]# alias name=hostname -I #不加引号,如果定义的值出现空格,不会看做是一个整体
-bash: alias: -I: not found
[root@lxy ~]# alias name=\'hostname -I\'
[root@lxy ~]# name
10.0.0.100


单双引号及不加引号的区别:
不加引号: #所见即所得,支持通配符,会解析变量,如果定义的值出现空格,不会看做是一个整体
单引号: #强引用,所见即所得
双引号: #弱引用,所见即所得,会解析变量
反引号: #`` 会优先执行反引号里面的命令,将命令的输出结果交给外面的命令,``里面必须是命令, === $()

[root@lxy ~]# echo `hostname`
lxy
[root@lxy ~]# echo $(hostname)
lxy

echo -e "\033[30m 黑色字 \033[0m"
echo -e "\033[31m 红色字 \033[0m"
echo -e "\033[32m 绿色字 \033[0m"
echo -e "\033[33m 黄色字 \033[0m"
echo -e "\033[34m 蓝色字 \033[0m"
echo -e "\033[35m 紫色字 \033[0m"
echo -e "\033[36m 天蓝字 \033[0m"
echo -e "\033[37m 白色字 \033[0m"

[root@lxy ~]# echo -e "   好湿\n江山风景美如画;\n本想吟诗赞天下。\n奈何自己没文化;\n一句卧槽浪好大。"
  好湿
江山风景美如画;
本想吟诗赞天下。
奈何自己没文化;
一句卧槽浪好大。
echo $? #显示上一条命令输出结果,0表示成功,非0表示失败

2.cat

#查看文件内容,合并多个文件,创建新文件及编辑文件,显示行号
	-n		#给输出结果加上行号
	-A		#给输出的内容每行的结尾加上一个标识符
	
[root@lxy ~]# cat  test.txt 
123
456
[root@lxy ~]# cat -n  test.txt
     1	123
     2	456
[root@lxy ~]# cat -A test.txt
123$
456$
[root@lxy ~]# echo  \'123456 \'  >>test.txt
[root@lxy ~]# cat test.txt
123
456
123456 
[root@lxy ~]# cat -A  test.txt 
123$
456$
123456 $

#合并多个文件

[root@lxy ~]# cat hosts 
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@lxy ~]# cat test.txt 
123
456
123456 
[root@lxy ~]# cat hosts  test.txt 
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
123
456
123456 
[root@lxy ~]# cat hosts  test.txt  >test.log
[root@lxy ~]# cat test.log 
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
123
456
123456 

#创建一个文件及编辑文件
[root@lxy ~]# cat >file.txt<<EOF
> oldboy
> 123456
> olfgirl
> EOF
[root@lxy ~]# cat file.txt 
oldboy
123456
olfgirl
[root@lxy ~]# cat >>file.txt<<eof
> test
> eof
[root@lxy ~]# cat file.txt 
oldboy
123456
olfgirl
test

3.tac

#倒着显示文件内容,默认显示文件最后一行

[root@lxy ~]# tac file.txt
test
olfgirl
123456
oldboy

4.more

#分页显示文件内容,文件内容显示完成之后会自动退出

[root@lxy ~]# cp  /etc/services   ./

	-n		#n表示数字,指定一页显示多少行
	+n		#指定从第几行开始显示文件内容
	
	
	内部命令
	
		空格/f		 #向下翻页
		b			  #向上翻页
		回车键			#向下翻一行
		=			  #显示当前光标所在的行数
		/			  #搜索,不会高亮显示
			n			#向下查找,用在/中
		q			  #退出
		h			  #帮助信息	
		

5.less


#分页显示文件内容,文件内容显示完成,不会自动退出

-i #搜索的时候忽略大小写
-N #给显示出来的内容加上行号

内部命令

空格/f #向下翻页
b  #向上分页
回车键 #向下翻一行
=  #显示当前页的从第几行到多少行及总行数。
 #显示总字节大小,总共显示多少字节,及显示内容显示的百分比
 
/ #搜索,可以高亮显示

n #向下查找
N #向上查找

q #退出

6.head

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

	-n		#取消默认输出,后面跟数字,指定显示多少行	
	-c		#显示文件的前几个字符

[root@lxy ~]# head  services 
# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $
#
# Network services, Internet style
# IANA services version: last updated 2013-04-10
#
# Note that it is presently the policy of IANA to assign a single well-known
# port number for both TCP and UDP; hence, most entries here have two entries
# even if the protocol doesn\'t support UDP operations.
# Updated from RFC 1700, ``Assigned Numbers\'\' (October 1994).  Not all ports
[root@lxy ~]# head  -n 1 services 
# /etc/services:
[root@lxy ~]# head -1 services 
# /etc/services:
[root@lxy ~]# head -2 services 
# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $

[root@lxy ~]# head -c 5 services 			#显示文件的前5个字符
# /et[root@lxy ~]# 

7.tail

#显示文件尾部信息,默认显示最后十行内容

	-n		#取消默认输出,指定显示多少行
	-c		#显示文件最后几个字符	
	-f		#实时查看文件内容的更新	
	-F		#实时查看文件内容的更新,当文件不存在时,会一直尝试读取该文件

[root@lxy ~]# tail  services
3gpp-cbsp       48049/tcp               # 3GPP Cell Broadcast Service Protocol
isnetserv       48128/tcp               # Image Systems Network Services
isnetserv       48128/udp               # Image Systems Network Services
blp5            48129/tcp               # Bloomberg locator
blp5            48129/udp               # Bloomberg locator
com-bardac-dw   48556/tcp               # com-bardac-dw
com-bardac-dw   48556/udp               # com-bardac-dw
iqobject        48619/tcp               # iqobject
iqobject        48619/udp               # iqobject
matahari        49000/tcp               # Matahari Broker
[root@lxy ~]# tail  -n 1 services 
matahari        49000/tcp               # Matahari Broker
[root@lxy ~]# tail  -1   services 
matahari        49000/tcp               # Matahari Broker
[root@lxy ~]# cat -n services  | tail -1  
 11176	matahari        49000/tcp               # Matahari Broker

|		#管道,将管道前面的命令的输出结果交给管道后面的命令进行处理

[root@lxy ~]# tail -c5  services 		#显示最后5个字符,
oker				#最后有一个换行符


[root@lxy ~]# tail  -f  test.log 		#实时的查看文件的更新
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
123
456
123456 



123

[root@lxy ~]# tail  -20f  /var/log/messages		#指定从最后的20行内容开始显示
[root@lxy ~]# tail  -F  file.log		#会实时的查看文件内容的更新,文件不存在时,会一直尝试读取,直到文件存在或者手动退出

tail: cannot open ‘file.log’ for reading: No such file or directory
tail: ‘file.log’ has appeared;  following end of new file
123
^C

8.tailf

#实时查看文件的更新,默认显示最后十行,当文件没有更新时,不会读取磁盘。减少磁盘的io读写
	-n		#取消默认输出,显示指定的行数

[root@lxy ~]# tailf  /var/log/messages		
[root@lxy ~]# tailf -20   /var/log/messages	

分类:

技术点:

相关文章:

  • 2021-06-21
  • 2021-05-14
  • 2021-07-29
  • 2021-10-02
  • 2022-12-23
  • 2021-11-21
  • 2021-12-02
  • 2022-01-17
猜你喜欢
  • 2021-11-21
  • 2021-11-21
  • 2021-11-21
  • 2021-12-05
  • 2022-12-23
  • 2021-11-21
  • 2021-10-27
相关资源
相似解决方案