Linux中常用命令汇总一

Shell 可以看作是一个命令解释器,为我们提供了交互式的文本控制台界面。我们可以 通过终端控制台来输入命令,由 shell 进行解释并最终交给内核执行。 本篇文章就将分类介绍 常用的基本 shell 命令。

1、帮助命令

1.1、man获得帮助信息

  • 基本语法

man [命令或配置文件]  (功能描述:获得帮助信息)
  • 显示说明

信息

功能

NAME

命令的名称和单行描述

SYNOPSIS

怎样使用命令

DESCRIPTION

命令功能的深入讨论

EXAMPLES

怎样使用命令的例子

SEE ALSO

相关主题(通常是手册页)

  • 实例

查看ls命令的帮助信息: man ls

$ clear
LS(1)                                                   General Commands Manual                                                   LS(1)

NAME
     ls – list directory contents

SYNOPSIS
     ls [-@ABCFGHILOPRSTUWabcdefghiklmnopqrstuvwxy1%,] [--color=when] [-D format] [file ...]

DESCRIPTION
     For each operand that names a file of a type other than directory, ls displays its name as well as any requested, associated
     information.  For each operand that names a file of type directory, ls displays the names of files contained within that
     directory, as well as any requested, associated information.

     If no operands are given, the contents of the current directory are displayed.  If more than one operand is given, non-directory
     operands are displayed first; directory and non-directory operands are sorted separately and in lexicographical order.

     The following options are available:

     -@      Display extended attribute keys and sizes in long (-l) output.

     -A      Include directory entries whose names begin with a dot (‘.’) except for . and ...  Automatically set for the super-user

1.2、help获得shell内置命令的帮助信息

一部分基础功能的系统命令是直接内嵌在 shell 中的,系统加载启动之后会随着 shell 一起加载,常驻系统内存中。这部分命令被称为“内置(built-in)命令”;相应的其它命令被称为“外部命令”。

  • 基本语法

help 命令(功能描述:获得 shell 内置命令的帮助信息)
  • 实例

查看 cd 命令的帮助信息:help cd

[root@liuchao ~]# help cd
cd: cd [-L|[-P [-e]]] [dir]
    Change the shell working directory.

    Change the current directory to DIR.  The default DIR is the value of the
    HOME shell variable.

    The variable CDPATH defines the search path for the directory containing
    DIR.  Alternative directory names in CDPATH are separated by a colon (:).
    A null directory name is the same as the current directory.  If DIR begins
    with a slash (/), then CDPATH is not used.

    If the directory is not found, and the shell option `cdable_vars' is set,
    the word is assumed to be  a variable name.  If that variable has a value,
    its value is used for DIR.

    Options:
        -L    force symbolic links to be followed
        -P    use the physical directory structure without following symbolic
        links
        -e    if the -P option is supplied, and the current working directory
        cannot be determined successfully, exit with a non-zero status

    The default is to follow symbolic links, as if `-L' were specified.

    Exit Status:
    Returns 0 if the directory is changed, and if $PWD is set successfully when
    -P is used; non-zero otherwise.
[root@liuchao ~]#

1.3、常用快捷键

快捷键

功能

ctrl+c

停止进程

ctrl+l

清屏,等同于clear;彻底清屏是:reset

善用tab键

自动补全,防止敲错

上下键

查找执行过的命令,有些命令比较长,如果已经敲过一次可以通过上下键找到

2、文件目录类

2.1、pwd显示当前工作目录的绝对路径

  • 基本语法

pwd (功能描述:显示当前工作目录的绝对路径)

  • 实例

[root@liuchao ~]# pwd
/root
[root@liuchao ~]#

2.2、ls列出目录内容

  • 基本语法

ls [选项] [目录或是文件]

  • 选项说明

选项

说明

-a

全部的文件,连同隐藏档( 开头为 . 的文件) 一起列出来(常用)

-l

长数据串列出,包含文件的属性与权限等等数据;(常用)等价于“ll”

  • 显示说明

每行列出的信息依次是: 文件类型与权限 链接数 文件属主 文件属组 文件大小用byte

来表示 建立或最近修改的时间 名字

  • 实例

# 查看当前目录下文件、文件夹
[root@liuchao /]# ls
app  bin  boot  code  data  dev  dump.rdb  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var  work
[root@liuchao /]#
# 查看当前目录下所有文件、文件夹
[root@liuchao /]# ls -a
.  ..  app  bin  boot  code  data  dev  dump.rdb  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var  work
[root@liuchao /]#
# 长数据列出文件、文件夹  排除隐藏文件、文件夹
[root@liuchao /]# ls -l
总用量 496
drwxr-xr-x    3 root root     99 12月  2 2021 app
lrwxrwxrwx.   1 root root      7 7月   9 2018 bin -> usr/bin
dr-xr-xr-x.   4 root root   4096 9月  17 2018 boot
drwxrwxrwx    9 root root   4096 11月 26 2021 code
drwxrwxrwx    3 root root     26 10月 23 2019 data
drwxr-xr-x   18 root root   3020 8月  26 21:33 dev
-rw-r--r--    1 root root 469893 2月  12 15:51 dump.rdb
drwxr-xr-x.  74 root root   8192 1月  12 11:29 etc
drwxr-xr-x.   2 root root      6 4月  11 2018 home
lrwxrwxrwx.   1 root root      7 7月   9 2018 lib -> usr/lib
lrwxrwxrwx.   1 root root      9 7月   9 2018 lib64 -> usr/lib64
drwxr-xr-x.   2 root root      6 4月  11 2018 media
drwxr-xr-x.   2 root root      6 4月  11 2018 mnt
drwxr-xr-x.   2 root root      6 4月  11 2018 opt
dr-xr-xr-x  142 root root      0 6月  22 2022 proc
dr-xr-x---.   9 root root   4096 2月  12 11:30 root
drwxr-xr-x   21 root root    580 10月 17 11:43 run
lrwxrwxrwx.   1 root root      8 7月   9 2018 sbin -> usr/sbin
drwxr-xr-x.   2 root root      6 4月  11 2018 srv
dr-xr-xr-x   13 root root      0 6月  22 2022 sys
drwxrwxrwt.  13 root root   4096 2月  12 03:35 tmp
drwxr-xr-x.  14 root root   4096 10月 23 2019 usr
drwxr-xr-x.  19 root root   4096 7月   8 2018 var
drwxr-xr-x    3 root root     20 6月  24 2021 work
[root@liuchao /]#

# 长数据列出所有文件、文件夹
[root@liuchao /]# ls -al
总用量 504
dr-xr-xr-x.  21 root root   4096 2月  12 15:51 .
dr-xr-xr-x.  21 root root   4096 2月  12 15:51 ..
drwxr-xr-x    3 root root     99 12月  2 2021 app
lrwxrwxrwx.   1 root root      7 7月   9 2018 bin -> usr/bin
dr-xr-xr-x.   4 root root   4096 9月  17 2018 boot
drwxrwxrwx    9 root root   4096 11月 26 2021 code
drwxrwxrwx    3 root root     26 10月 23 2019 data
drwxr-xr-x   18 root root   3020 8月  26 21:33 dev
-rw-r--r--    1 root root 469893 2月  12 15:51 dump.rdb
drwxr-xr-x.  74 root root   8192 1月  12 11:29 etc
drwxr-xr-x.   2 root root      6 4月  11 2018 home
lrwxrwxrwx.   1 root root      7 7月   9 2018 lib -> usr/lib
lrwxrwxrwx.   1 root root      9 7月   9 2018 lib64 -> usr/lib64
drwxr-xr-x.   2 root root      6 4月  11 2018 media
drwxr-xr-x.   2 root root      6 4月  11 2018 mnt
drwxr-xr-x.   2 root root      6 4月  11 2018 opt
dr-xr-xr-x  142 root root      0 6月  22 2022 proc
dr-xr-x---.   9 root root   4096 2月  12 11:30 root
drwxr-xr-x   21 root root    580 10月 17 11:43 run
lrwxrwxrwx.   1 root root      8 7月   9 2018 sbin -> usr/sbin
drwxr-xr-x.   2 root root      6 4月  11 2018 srv
dr-xr-xr-x   13 root root      0 2月  12 15:54 sys
drwxrwxrwt.  13 root root   4096 2月  12 03:35 tmp
drwxr-xr-x.  14 root root   4096 10月 23 2019 usr
drwxr-xr-x.  19 root root   4096 7月   8 2018 var
drwxr-xr-x    3 root root     20 6月  24 2021 work

2.3、cd切换目录

  • 基本语法

cd [参数]

  • 参数说明

参数

说明

cd 绝对路径

切换路径

cd 相对路径

切换路径

cd ~或者 cd

回到当前用户home路径

cd -

回到上一次所在目录

cd ..

回到当前目录的上级目录

cd -P

跳转到实际物理路径,而非快捷方式路径

  • 实例

# 绝对路径切换
[root@liuchao /]# cd /usr/local/
[root@liuchao local]#
# 相对路径切换
[root@liuchao local]# cd ./lib
[root@liuchao lib]#
# 回到home目录
[root@liuchao lib]# cd ~
[root@liuchao ~]#
[root@liuchao ~]# pwd
/root
[root@liuchao ~]# cd /usr/local/
[root@liuchao local]#
# 回到home目录
[root@liuchao local]# cd
[root@liuchao ~]#
[root@liuchao ~]# pwd
/root
# 回到上次目录
[root@liuchao ~]# cd -
/usr/local
[root@liuchao local]# cd ..
[root@liuchao usr]#
[root@liuchao usr]# pwd
/usr
[root@liuchao usr]#
# 跳转到实际物理路径
[root@liuchao usr]# cd -P
[root@liuchao ~]#
[root@liuchao ~]# pwd
/root
[root@liuchao ~]#

2.4、mkdir创建一个新的目录

  • 基本语法

mkdir [选项] 要创建的目录

  • 选项说明

选项

说明

-p

创建多层目录,连同父级目录一起创建

  • 实例

# 父级目录存在创建成功
[root@liuchao /]# mkdir /root/test
[root@liuchao /]#
# test1目录不存在创建失败
[root@liuchao /]# mkdir /root/test1/test
mkdir: 无法创建目录"/root/test1/test": 没有那个文件或目录
[root@liuchao /]#
# 加上-p目录创建成功
[root@liuchao /]# mkdir -p /root/test1/test
[root@liuchao /]#

2.5、rmdir删除一个空目录

  • 基本语法

rmdir 要删除的空目录

  • 实例

# 实例说明只能删除空目录
[root@liuchao /]# rmdir /root/test1
rmdir: 删除 "/root/test1" 失败: 目录非空
[root@liuchao /]#
# 删除空目录成功
[root@liuchao /]# rmdir /root/test1/test
[root@liuchao /]#

2.6、touch创建空文件

  • 基本语法

touch 文件名称

  • 实例

[root@liuchao /]# ls
app  bin  boot  code  data  dev  dump.rdb  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var  work
[root@liuchao /]#
[root@liuchao /]#
# 新建文件
[root@liuchao /]# touch 1.txt
[root@liuchao /]#
[root@liuchao /]# ls
1.txt  app  bin  boot  code  data  dev  dump.rdb  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var  work
[root@liuchao /]#

2.7、cp复制文件或者目录

  • 基本语法

cp [选项] source dest (功能描述:复制source文件到dest)

  • 选项说明

选项

说明

-r

递归复制整个文件夹

  • 参数说明

参数

说明

source

源文件

dest

目标文件

  • 经验技巧

强制覆盖不提示的方法:\cp

  • 实例

[root@liuchao /]# ls /test1/test/
1.txt
[root@liuchao /]#
[root@liuchao /]# ls /test2/test/
[root@liuchao /]#
# 复制文件
[root@liuchao /]# cp /test1/test/1.txt /test2/test/
[root@liuchao /]#
[root@liuchao /]# ls /test2/test/
1.txt
[root@liuchao /]#
[root@liuchao /]#
# 复制文件夹
[root@liuchao /]# cp -r /test1/test/ /test2/test/
[root@liuchao /]#
[root@liuchao /]# ls /test2/test/
1.txt  test
[root@liuchao /]#

2.8、rm删除文件或者目录

  • 基本语法

rm [选项] deleteFile (功能描述:递归删除目录中所有内容)

  • 选项说明

选项

说明

-r

递归删除目录中所有内容

-f

强制执行删除操作,而不提示用于进行确认

-v

显示指令详细执行过程

  • 实例

# 查看目录内容
[root@liuchao /]# ls /test1/test/
1.txt
# 删除文件 会提示确认是否删除
[root@liuchao /]# rm /test1/test/1.txt
rm:是否删除普通空文件 "/test1/test/1.txt"?y
[root@liuchao /]#
[root@liuchao /]# ls /test1/test/
[root@liuchao /]#
[root@liuchao /]# ls /test2/test/
1.txt  test
# 强制删除无确认提示
[root@liuchao /]# rm -f /test2/test/1.txt
[root@liuchao /]#
# 强制删除目录 失败
[root@liuchao /]# rm -f /test2/test/test/
rm: 无法删除"/test2/test/test/": 是一个目录
[root@liuchao /]#
# 递归并且强制删除 成功
[root@liuchao /]# rm -fr /test2/test/test/
[root@liuchao /]#

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

  • 基本语法

(1)mv oldNameFile newNameFile (功能描述:重命名)

(2)mv /temp/movefile /targetFolder (功能描述:移动文件)

  • 实例

[root@liuchao test]# ls
1.txt
# 重名
[root@liuchao test]# mv 1.txt 2.txt
[root@liuchao test]#
[root@liuchao test]# ls
2.txt
[root@liuchao test]#
[root@liuchao test]# mkdir test
[root@liuchao test]#
[root@liuchao test]# ls
2.txt  test
# 移动文件
[root@liuchao test]# mv 2.txt ./test/
[root@liuchao test]#
[root@liuchao test]#

2.10、cat查看文件内容

查看文件内容,从第一行开始显示。

  • 基本语法

cat [选项]要查看的文件

  • 选项说明

选项

说明

-n

显示所有行的行号,包括空行

  • 经验技巧

一般查看比较小的文件,一屏幕能显示全的。

  • 实例

[root@liuchao test]# ls
2.txt
# 查看文件内容
[root@liuchao test]# cat 2.txt
first
second
third
four
# 查看文件内容并且显示行号
[root@liuchao test]# cat -n 2.txt
     1    first
     2    second
     3    third
     4    four
     5
[root@liuchao test]#

2.11、more文件内容分屏查看器

more 指令是一个基于 VI /VIM编辑器的文本过滤器,它以全屏幕的方式按页显示文本文件

的内容。more 指令中内置了若干快捷键,详见操作说明。

  • 基本语法

more 要查看的文件

  • 操作说明

操作

说明

空白键(space)

向下翻页

Enter

向下翻一行

q

代表立刻离开 more ,不再显示该文件内容。

Ctrl+F

向下滚动一屏

Ctrl+B

返回上一屏

=

输出当前行的行号

:f

输出文件名和当前行的行号

-NUM

指定每屏显示的行数为 NUM

+NUM

从文件第 NUM 行开始显示

  • 实例

# 查看文件
[root@liuchao test]# more 2.txt
# 查看文件每屏显示行数为2
[root@liuchao test]# more -2 2.txt
# 从文件第2行开始显示
[root@liuchao test]# more +2 2.txt
# 从文件第2行开始显示,每屏显示行数为2
[root@liuchao test]# more -2 +2 2.txt

2.12、less分屏显示文件内容

less 指令用来分屏查看文件内容,它的功能与 more 指令类似,但是比 more 指令更加 强大,支持各种显示终端。less 指令在显示文件内容时,并不是一次将整个文件加载之后

才显示,而是根据显示需要加载内容,对于显示大型文件具有较高的效率。

  • 基本语法

less 要查看的文件

  • 操作说明

操作

说明

空白键

向下翻一页

[pagedown]

向下翻一页

[pageup]

向上翻一页

/字符串

向下搜寻『字符串』的功能;n:向下查找;N:向上查找;

?字符串

向上搜寻『字串』的功能;n:向上查找;N:向下查找;

q

离开 less 这个程序;

  • 经验技巧

用SecureCRT时[pagedown]和[pageup]可能会出现无法识别的问题。

  • 实例

# 查看文件
[root@liuchao test]# less 2.txt

2.13、echo

echo 输出内容到控制台

  • 基本语法

echo [选项] [输出内容]

选项:

-e 支持反斜线控制的字符转换

控制字符

作用

\\

输出\本身

\n

换行符

\t

制表符,也就是tab键

  • 实例

[root@liuchao test]#
[root@liuchao test]# echo helloword
helloword
[root@liuchao test]# echo "helloword"
helloword
[root@liuchao test]# echo \\
\
[root@liuchao test]# echo "\\"
\
[root@liuchao test]#
[root@liuchao test]# echo "hello\tword"
hello\tword
[root@liuchao test]#
[root@liuchao test]# echo -e "hello\tword"
hello    word
[root@liuchao test]# echo "hello\nword"
hello\nword
[root@liuchao test]# echo -e "hello\nword"
hello
word
[root@liuchao test]#

2.14、head显示文件头部内容

head 用于显示文件的开头部分内容,默认情况下 head 指令显示文件的前 10 行内容。

  • 基本语法

head 文件 (功能描述:查看文件头10行内容)

head -n 5 文件 (功能描述:查看文件头5行内容,5可以是任意行数)

  • 选项说明

选项

说明

-n<行数>

指定显示头部内容的行数

  • 实例

[root@liuchao test]# head 2.txt
外国友人见证中国高质量发展
中新网:永不录用涉毒艺人是底线热
飞机剧烈颠簸 有乘客边哭边写遗言热
土小伙和中国救援队并肩战斗
南宁房贷年龄期限可延长至岁
茶颜悦色“罚站式”取餐引吐槽
人民网评狂飙删除涉毒演员戏份
长征中年龄最小红军、贺龙外甥逝世热
“嘎子”直播卖手机称只卖
多地回应新医改“个人吃亏”热
[root@liuchao test]#
[root@liuchao test]# head -n 2 2.txt
外国友人见证中国高质量发展
中新网:永不录用涉毒艺人是底线热
[root@liuchao test]#

2.15、tail输出文件尾部内容

tail 用于输出文件中尾部的内容,默认情况下 tail 指令显示文件的后 10 行内容。

  • 基本语法

tail 文件 (功能描述:查看文件尾部10行内容)

tail -n 5 文件 (功能描述:查看文件尾部5行内容,5可以是任意行数)

tail -f 文件 (功能描述:实时追踪该文档的所有更新)

  • 选项说明

选项

说明

-n<行数>

输出文件尾部n行内容

-f

显示文件最新追加的内容,监视文件变化

  • 实例

# 查看文件尾部10行内容
[root@liuchao test]# head 2.txt
外国友人见证中国高质量发展
中新网:永不录用涉毒艺人是底线热
飞机剧烈颠簸 有乘客边哭边写遗言热
土小伙和中国救援队并肩战斗
南宁房贷年龄期限可延长至岁
茶颜悦色“罚站式”取餐引吐槽
人民网评狂飙删除涉毒演员戏份
长征中年龄最小红军、贺龙外甥逝世热
“嘎子”直播卖手机称只卖
多地回应新医改“个人吃亏”热
[root@liuchao test]#
# 查看文件尾部2行内容
[root@liuchao test]# head -n 2 2.txt
外国友人见证中国高质量发展
中新网:永不录用涉毒艺人是底线热
[root@liuchao test]#
# 查看文件尾部10行内容,并跟踪实时更新
[root@liuchao test]# tail -f 2.txt
人民网评狂飙删除涉毒演员戏份
长征中年龄最小红军、贺龙外甥逝世热
“嘎子”直播卖手机称只卖
多地回应新医改“个人吃亏”热
奥地利军队暂停在土耳其救灾行动
央美准考证模版用肖战照片
中国已进入“长寿时代”
专家称遇上地震是命遭主持人怒斥
一手交钱一手交房还有多远热
重庆一对情侣蹦极倒挂滞留半空

^C
# 查看文件尾部2行内容并跟踪实时更新
[root@liuchao test]# tail -fn 2 2.txt
一手交钱一手交房还有多远热
重庆一对情侣蹦极倒挂滞留半空
^C
[root@liuchao test]#

2.16、">"输出重定向和">>"追加

  • 基本语法

ls -l >文件 (功能描述:列表的内容写入文件a.txt中(覆盖写))

ls -al >>文件 (功能描述:列表的内容追加到文件aa.txt的末尾)

cat 文件 1 > 文件 2 (功能描述:将文件 1 的内容覆盖到文件 2)

echo “内容” >> 文件

  • 实例

# 将ls 结果写入1.txt文件  注意 如果1.txt中有内容将会被覆盖
[root@liuchao /]# ls -l > 1.txt
# 将ls 结果追加到1.txt 文件  注意 此操作只是追加内容
[root@liuchao /]# ls -la > 1.txt
# 将hello 写入1.txt文件,1.txt内容会被覆盖
[root@liuchao /]# echo hello > 1.txt

2.17、ln软连接

软链接也称为符号链接,类似于 windows 里的快捷方式,有自己的数据块,主要存放

了链接其他文件的路径。

  • 基本语法

ln -s [原文件或目录] [软链接名] (功能描述:给原文件创建一个软链接)

  • 经验技巧

删除软链接: rm -rf 软链接名

查询:通过 ll 就可以查看,列表属性第 1 位是 l,尾部会有位置指向。

  • 实例

# 查看当前目录文件
[root@liuchao test1]# ll
总用量 0
-rw-r--r-- 1 root root 0 2月  12 17:13 1.txt
[root@liuchao test1]#
# 1.txt软连接到test2
[root@liuchao test1]# ln -s 1.txt test2
[root@liuchao test1]#
# 再次查看目录文件
[root@liuchao test1]# ll
总用量 0
-rw-r--r-- 1 root root 0 2月  12 17:13 1.txt
lrwxrwxrwx 1 root root 5 2月  12 17:16 test2 -> 1.txt
[root@liuchao test1]#
# 删除软连接
[root@liuchao test1]# rm -fr test2
[root@liuchao test1]#
# 再次查看目录文件
[root@liuchao test1]# ll
总用量 0
-rw-r--r-- 1 root root 0 2月  12 17:13 1.txt
[root@liuchao test1]#

2.18、history查看已执行过历史命令

  • 基本语法

history (功能描述:查看已经执行过历史命令)

  • 实例

# 查看历史执行命令
[root@liuchao test1]# history

重要:喜欢的朋友,收藏点赞,谢谢。下篇文章我们继续汇总时间日期类、用户管理类等分类常用命令。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

全栈行动派

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值