linux 常用命令


linux 常用命令

 

******************************

查看磁盘空间:df -h

查看剩余内存:free -h

 

查看时间:date

查看日期:cal

 

******************************

ls:列出目录内容

 

常用选项:

-a:列出所有文件,包含隐藏文件

-d:显示指定目录的详细信息,不显示目录的内容

-l:以长格式显示文件

-h:文件大小格式化显示

-t:按时间排序,默认按字母升序排列

-r:以相反的结果显示

 

长格式说明:

-rw-rw-r--:

开头的"-"表示普通文件,如果是"-d"表示目录,“-l”表示软连接;

后面的表示文件的所有者、同组用户、其他用户的读、写、可执行权限

 

2:表示文件硬连接数目

root:表示文件的所有者为root

root:表示文件所属用户组的名称

0:表示文件的大小

11月 2 09:39:表示文件创建或者修改时间

file1:表示文件名称

 

******************************

查看文件类型:file file_name

查看文件内容:less file_name

查看文件内容:cat file_name

 

创建目录:mkdir -p directory

删除文件、目录:rm -rf file_name/directory

 

创建硬连接:ln file link_name(较少使用)

创建软连接:ln -s file link_name

 

******************************

cp:复制文件、目录

 

常用选项:

-r:递归复制目录内容

 

示例:

cp file1 file2:复制文件file1,文件命名为file2

cp file1 file2 dir1:将文件file1、file2复制到目录dir1中,dir1要预先存在

cp dir1/* dir2:将dir1中的文件复制到dir2中,dir2要预先存在

cp -r dir1 dir2:将dir1复制为dir2,dir2的内容与dir1相同,如果dir2不存在可创建

 

******************************

mv:文件、目录移动或者重命名

 

示例:

mv file1 file2:将文件file1重命名为file2

mv file1 dir1:将文件file1移动到目录dir1下

mv dir1 dir2:如果目录dir2存在,则将目录dir1移动到目录dir2下,如果目录dir2不存在,则将dir1重命名为dir2

 

******************************

type:查看命令类型

 

命令类型:可执行文件、shell内置命令、shell函数、别名

 

示例:

      

 

******************************

which:查看命令的位置,不包含内置命令

 

示例

      

 

******************************

重定向

 

文件描述符

标准输入:0

标准输出:1

标准错误输出:2

说明:默认情况下,标准输出、标准错误都输出到控制台,不存储到文件

 

 

重定向操作符

> :标准输出重定向

>> :标准输出追加重定向

 

2> :标准错误重定向(2与>之间不能有空格)

2>> :标准错误追加重定向

2>&1:标准错误重定向到标准输出(所有符号、数字之间均不能有空格)

 

&> :标准输出、标准错误输出重定向到同一个文件

&>> :标准输出、标准错误输出追加重定向到同一个文件

 

> /dev/null:标准输出不显示在屏幕、同时不保存

2> /dev/null:标准错误输出不显示在屏幕、同时不保存

&> /dev/null:标准输出、标准错误输出同时不显示在屏幕、也不保存

 

示例 1:标准输出重定向到文件

[root@centos test]# echo "hello world"            #echo命令默认输出到控制台
hello world

[root@centos test]# echo "hello world" > file     #echo输出重定向到文件file
[root@centos test]# cat file
hello world

[root@centos test]# echo "hello world2" > file    #echo输出重定向到文件file(清空file,再写入)
[root@centos test]# cat file                      
hello world2

[root@centos test]# echo "hello world3" >> file   #echo输出追加重定向到文件file(直接追加写入)
[root@centos test]# echo "hello world4" >> file
[root@centos test]# cat file                      
hello world2
hello world3
hello world4

 

示例 2:标准错误重定向到文件

[root@centos test]# ls
file
[root@centos test]# ls file2                 #标准错误默认输出到控制台
ls: 无法访问file2: 没有那个文件或目录


[root@centos test]# ls file2 2> error        #标准错误重定向到文件error
[root@centos test]# cat error
ls: 无法访问file2: 没有那个文件或目录


[root@centos test]# ls file3 2> error        #标准错误重定向到文件error(先清空文件,再写入)
[root@centos test]# cat error
ls: 无法访问file3: 没有那个文件或目录


[root@centos test]# ls file3 2>> error       #标准错误追加重定向到文件(直接追加写入)
[root@centos test]# ls file3 2>> error
[root@centos test]# cat error
ls: 无法访问file3: 没有那个文件或目录
ls: 无法访问file3: 没有那个文件或目录
ls: 无法访问file3: 没有那个文件或目录

 

示例 3:标准输出、标准错误重定向到文件(2>&1)

[root@centos test]# ls
file
[root@centos test]# ls file2 > output 2>&1         #标准错误、标准输出重定向到文件output
[root@centos test]# cat output
ls: 无法访问file2: 没有那个文件或目录


[root@centos test]# ls file3 > output 2>&1         #标准错误、标准输出重定向到文件output(先清空文件output,再写入)
[root@centos test]# cat output
ls: 无法访问file3: 没有那个文件或目录


[root@centos test]# ls file3 >> output 2>&1        #标准错误、标准输出追加重定向到文件(直接追加写入)
[root@centos test]# ls file3 >> output 2>&1
[root@centos test]# cat output
ls: 无法访问file3: 没有那个文件或目录
ls: 无法访问file3: 没有那个文件或目录
ls: 无法访问file3: 没有那个文件或目录

说明:2>&1之间不能有空格

 

示例 3:标准错误、标准输出重定向到文件(&>、&>>)

[root@centos test]# ls
file
[root@centos test]# ls file2                   #标准错误默认输出到控制台
ls: 无法访问file2: 没有那个文件或目录

[root@centos test]# ls file2 &> output         #标准错误、标准输出重定向到文件output
[root@centos test]# cat output
ls: 无法访问file2: 没有那个文件或目录


[root@centos test]# ls file3 &> output         #标准错误、标准输出重定向到文件output(先清空文件,再写入)
[root@centos test]# cat output
ls: 无法访问file3: 没有那个文件或目录

[root@centos test]# ls file3 &>> output        #标准错误、标准输出追加重定向到文件output(直接追加写入)
[root@centos test]# ls file3 &>> output
[root@centos test]# cat output
ls: 无法访问file3: 没有那个文件或目录
ls: 无法访问file3: 没有那个文件或目录
ls: 无法访问file3: 没有那个文件或目录

 

 

******************************

| :管道操作

 

sort:排序,命令格式为:sort [options] file_name

uniq:对相邻的输出去重,命令格式为:uniq [options] file_name

wc:打印行数、字数、字节数,命令格式为:wc [options] file_name

示例:

      

grep:搜索文件中的行,命令格式为:grep [options] regex file_name

其中:regex为正则表达式

 

head:显示文件的头几行,命令格式为:head [options] file_name

tail:显示文件的尾几行,命令格式为:tail [options] file_name

示例:

head -n 5 file:查看file的头5行

tail -n 5 file:查看文件的尾5行

tail -n 5 -f file:实时查看文件的尾5行

 

tee:将输入流同时输出到文件以及标准输出

示例:

      

 

******************************

拓展

 

通配符

* :匹配任意多个字符

? :匹配单个字符

[characters]:匹配字符集中的某个字符

[!characters]:不匹配字符集中的任意字符

[[:class:]]:匹配任意一个属于指定字符集的字符

 

常用的[[:class:]]:

[:alnum:]:数字、小写、大写字符集

[:alpha:]:小写、大写字符集

[:digit:]:0-9字符集

[:lower:]:小写字符集

[:upper:]:大写字符集

 

路径名拓展:

示例:

g*:以g开头的文件

* :匹配任意文件

p*.txt:匹配p开头,.txt结尾的字符串

 

花括号拓展:{}

示例

 

波浪线拓展:~

~:当前用户目录

~用户名称:指定用户目录,波浪线与用户名称之间不能有空格

示例

     

 

算数拓展:$(( 。。。。 ))

示例

      

 

参数拓展:$Parameter_name

示例

      

 

命令替换:$(command),将一个命令的输出作为另一个命令的输入

示例

      

 

******************************

引号

 

双引号:除" % ",“ \ ”,“ ' ”外,其余特殊符号都将被当成普通字符

单引号:所有字符都将被当成普通字符

示例

      

 

******************************

转义字符:\,使用转义字符后,特殊字符变为普通字符,

示例

\\:表示 “\”

      

 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值