1. Linux - 目录操作

1.1. cd

cd:进入对应目录,类似 windows 打开文件夹

  • 命令语法
1
cddir
  • 操作示例

cd ~ :回到当前登录账号的home目录

1
2
3
4
[root@VM-0-9-centos ~]# cd /usr/local/
[root@VM-0-9-centos local]# cd ~
[root@VM-0-9-centos ~]# pwd
/root

cd .. || cd ./.. :进到当前目录的父级目录

1
2
3
4
5
6
7
8
[root@VM-0-9-centos /]# cd /usr/local/
[root@VM-0-9-centos local]# cd ./..
[root@VM-0-9-centos usr]# pwd
/usr
[root@VM-0-9-centos usr]# cd /usr/local/
[root@VM-0-9-centos local]# cd ..
[root@VM-0-9-centos usr]# pwd
/usr

cd - :跳转到之前所在目录

1
2
3
4
5
[root@VM-0-9-centos home]# cd /usr/local/
[root@VM-0-9-centos local]# pwd
/usr/local
[root@VM-0-9-centos local]# cd -
/home

1.2. cp

cp:文件拷贝

  • 命令语法
1
cp 参数 源文件 目标位置
  • 参数说明

-r/R:递归,使用于目录复制

-n:不覆盖,不加该参数默认覆盖

  • 操作示例

【cp 源文件 目标位置】:覆盖复制当前目录下bbb文件至/tmp目录下,命名为aaa文件

1
2
3
4
5
6
7
8
9
10
11
[root@VM-0-9-centos local]# touch bbb.md
[root@VM-0-9-centos local]# ll
...
-rw-r--r-- 1 root root 0 May 9 12:01 bbb.md
...
[root@VM-0-9-centos local]# cp bbb.md /tmp/aaa
[root@VM-0-9-centos local]# cd /tmp/
[root@VM-0-9-centos tmp]# ll
...
-rw-r--r-- 1 root root 0 May 9 12:01 aaa
...

【cp -r 源文件 目标位置】:覆盖复制ccc目录至/tmp目录下

1
2
3
4
5
6
7
[root@VM-0-9-centos local]# pwd
/usr/local
[root@VM-0-9-centos local]# mkdir ccc
[root@VM-0-9-centos local]# cp -r ccc/ /tmp/
[root@VM-0-9-centos local]# cd /tmp/
[root@VM-0-9-centos tmp]# ll
drwxr-xr-x 2 root root 4096 May 9 13:32 ccc

【cp -n 源文件 目标位置】:复制bbb文件至/tmp目录下,并跳过重名文件

1
2
3
4
5
[root@VM-0-9-centos local]# touch bbb.md
[root@VM-0-9-centos local]# cp -n bbb.md /tmp/
[root@VM-0-9-centos local]# cd /tmp/
[root@VM-0-9-centos tmp]# ll
-rw-r--r-- 1 root root 0 May 9 13:36 bbb.md

1.3. find

find:查找文件

  • 命令语法
1
find 查找目录 参数
  • 参数说明

-name:名称匹配

-mindepth:目录深度最小限制

-maxdepth:目录深度最大限制

-type:文件类型匹配(f 普通文件 l 符号连接 d 目录)

  • 操作示例

find 路径 -mindepth -maxdepth:查找目录下目录深度2层与3层的文件

1
2
3
4
5
[root@VM-0-9-centos home]# find /home -mindepth 2 -maxdepth 3
/home/www/.bashrc
/home/www/.bash_logout
/home/www/.bash_profile
...

find 路径 -type f -name filename:查找/home目录下名称为aaa的普通文件

1
2
3
4
[root@VM-0-9-centos home]# find /home -type f -name aaa
[root@VM-0-9-centos home]# touch aaa
[root@VM-0-9-centos home]# find /home -type f -name aaa
/home/aaa

find 路径 -name *.jpg:查找/home目录下后缀为.jpg的文件

1
2
3
4
[root@VM-0-9-centos home]# find /home -name *.jpg
[root@VM-0-9-centos home]# touch aa.jpg
[root@VM-0-9-centos home]# find /home -name *.jpg
/home/aa.jpg

1.4. ls

ls:显示当前目录下的信息

  • 命令语法
1
ls
  • 参数说明

-a :显示所有文件,包括隐藏文件

-l :显示文件属性,文件大小以字节显示

-h :按K\M\G的形式显示文件大小信息

-S :按文件大小排序

  • 操作示例

ls:显示当前目录下的信息

1
2
[root@VM-0-9-centos home]# ls # 显示当前目录下的信息
aaa aa.jpg lighthouse www

ls -a:显示所有文件,包括隐藏文件

1
2
[root@VM-0-9-centos home]# ls -a # 显示所有文件,包括隐藏文件
. .. aaa aa.jpg lighthouse www

ls -l:显示文件属性,文件大小以字节显示

1
2
3
4
5
6
[root@VM-0-9-centos home]# ls -l # 显示文件属性,文件大小以字节显示
total 8
-rw-r--r-- 1 root root 0 May 9 13:44 aaa
-rw-r--r-- 1 root root 0 May 9 13:46 aa.jpg
drwx------ 5 lighthouse lighthouse 4096 Oct 12 2022 lighthouse
drwx------ 2 www www 4096 Aug 11 2022 www

ls -al:显示所有文件,包括隐藏文件,文件属性,文件大小

1
2
3
4
5
6
7
8
[root@VM-0-9-centos home]# ls -al # 显示所有文件,包括隐藏文件,文件属性,文件大小
total 16
drwxr-xr-x. 4 root root 4096 May 9 13:46 .
dr-xr-xr-x. 21 root root 4096 May 9 13:50 ..
-rw-r--r-- 1 root root 0 May 9 13:44 aaa
-rw-r--r-- 1 root root 0 May 9 13:46 aa.jpg
drwx------ 5 lighthouse lighthouse 4096 Oct 12 2022 lighthouse
drwx------ 2 www www 4096 Aug 11 2022 www

ls -lh(按K\M\G的形式显示文件大小信息):显示文件属性,文件大小以K显示

1
2
3
4
5
6
[root@VM-0-9-centos home]# ls -lh # 显示文件属性,文件大小以K显示
total 8.0K
-rw-r--r-- 1 root root 0 May 9 13:44 aaa
-rw-r--r-- 1 root root 0 May 9 13:46 aa.jpg
drwx------ 5 lighthouse lighthouse 4.0K Oct 12 2022 lighthouse
drwx------ 2 www www 4.0K Aug 11 2022 www

ls -lhS:显示文件属性,文件大小以K显示,按文件大小排序

1
2
3
4
5
6
[root@VM-0-9-centos home]# ls -lhS  # 显示文件属性,文件大小以K显示,按文件大小排序
total 8.0K
drwx------ 5 lighthouse lighthouse 4.0K Oct 12 2022 lighthouse
drwx------ 2 www www 4.0K Aug 11 2022 www
-rw-r--r-- 1 root root 0 May 9 13:44 aaa
-rw-r--r-- 1 root root 0 May 9 13:46 aa.jpg

1.5. mkdir

创建文件目录

  • 命令语法
1
mkdir 参数 文件名
  • 参数说明

-p :嵌套关系目录创建;

  • 操作示例

mkdir dir:创建空白目录;

1
2
3
4
5
6
7
8
[root@VM-0-9-centos home]# ls
lighthouse www
[root@VM-0-9-centos home]# mkdir v1
[root@VM-0-9-centos home]# ls
lighthouse v1 www
[root@VM-0-9-centos home]# mkdir v2 v3 v4
[root@VM-0-9-centos home]# ls
lighthouse v1 v2 v3 v4 www
1
2
3
4
5
6
[root@VM-0-9-centos home]# rm -rf v1 v2 v3 v4
[root@VM-0-9-centos home]# mkdir -p v1/v2/v3
[root@VM-0-9-centos home]# ls
lighthouse v1 www
[root@VM-0-9-centos home]# find v1 -mindepth 2 -maxdepth 3
v1/v2/v3

1.6. mv

mv:移动文件,可以操作目录和文件。

  • 命令格式
1
mv 源路径 目标路径
  • 操作示例
1
2
3
4
5
[root@VM-0-9-centos home]# touch ccc
[root@VM-0-9-centos home]# mv ccc /tmp/aaa
[root@VM-0-9-centos home]# cd /tmp/
[root@VM-0-9-centos tmp]# ls
aaa
1
2
3
4
5
[root@VM-0-9-centos local]# mkdir bbb
[root@VM-0-9-centos local]# mv bbb/ /tmp/
[root@VM-0-9-centos local]# cd /tmp/
[root@VM-0-9-centos tmp]# ls
bbb

1.7. pwd

显示当前工作目录

  • 命令格式
1
pwd
  • 操作示例
1
2
3
[root@VM-0-9-centos tmp]# pwd
/tmp

1.8. rm

rm:删除目录或文件

  • 命令格式
1
rm 参数 路径
  • 参数说明

-r:递归,用于目录

-f:强制删除,不显示任何信息(会使前边的参数 i 失效)

-i:进行任何操作前需确认,只能操作文件(会使前边的参数 f 失效)

  • 操作示例

rm -ir 路径:删除目录下所有文件,并在每一步执行操作前进行确认

1
2
[root@VM-0-9-centos tmp]# rm -ir bbb/
rm: remove directory ‘bbb/’? y

rm -rf 路径:强制删除目录下所有文件,中间不会产生任何提示

1
2
[root@VM-0-9-centos tmp]# rm -rf ccc/
[root@VM-0-9-centos tmp]#

rm -i 路径:删除test1文件,在删除前进行确认

1
2
3
4
[root@VM-0-9-centos tmp]# touch ccc.md
[root@VM-0-9-centos tmp]# rm -i ccc.md
rm: remove regular empty file ‘ccc.md’? y
[root@VM-0-9-centos tmp]#

2. Linux - 文件编辑

vi:自带文本编辑器

  • 命令格式
1
vi filename
  • 操作示例
  • 第一步:进入文件编辑器后,按 i 进入插入模式

  • 第二步:插入模式下,文本编辑按常规模式进行操作

  • 第三步:按 ESC 退出插入模式

  • 移动光标

    • ctrl + b 屏幕向后移动一页

    • ctrl + f 屏幕向前移动一页

    • ctrl + u 屏幕向后移动半页

    • ctrl + d 屏幕向前移动半页

    • $ 光标移动到行尾

    • ^ 光标移动到行首

  • 删除操作

    • x 删除光标后一位字符
    • #x 删除光标后#位字符
    • dd 删除光标所在行
  • 撤回操作

    • u 撤回上一步操作
  • 命令模式下,按 进入底行模式

  • 字符查找

    • /keywords 命令模式下按 / ,接着输入查找关键字后回车进行查找,接着按 n 转到下一个查找结果
  • 保存文件

    • :w filenamefilename 为文件名另存为

    • :wq 保存并退出

  • 退出编辑

    • :q! 不存盘强制退出

3. Linux - 文件查看

3.1. cat

  • 命令格式
1
2
cat filename # 查看文件
cat -n filename # 查看文件内容并附带行号
  • 操作示例
1
2
3
4
5
6
[root@VM-0-9-centos tmp]# touch Readme.md
[root@VM-0-9-centos tmp]# vi Readme.md
[root@VM-0-9-centos tmp]# cat Readme.md
this is Readme
[root@VM-0-9-centos tmp]# cat -n Readme.md
1 this is Readme

3.2. less

  • 命令格式
1
less filename
  • 参数说明

SpacePageUp:向前一屏

BPageDown:向后一屏

Enter :向后一行

K :向前一行

3.3. tail

  • 命令格式
1
tail 参数 filename
  • 参数说明

-n :指定显示文件的最后多少行。

-f :动态显示,默认显示文件末尾10行,并且一直保持显示状态。