Linux系统文件与目录管理(小白的“升级打怪”成长之路)

目录

一、Linux系统目录结构

1、查看目录结构命令

2、目录结构操作使用方法

2.1、绝对路径

2.2、相对路径

3、目录结构中重要文件数据

3.1、网卡地址信息配置文件

3.2、网络域名服务配置文件

3.3、网络本地解析配置文件

3.4、开机自动运行脚本或命令文件

二、文件与目录操作命令

1、打印工作目录命令

2、目录切换命令

3、ls命令

4、文件查看命令

4.1、cat命令

重定向符号:

1、正确重定向

2、错误重定向

3、混合重定向

4.2、more命令

4.3、less命令

4.4、head命令

4.5、tail命令

4.6、nl命令

5、创建目录命令

6、创建文件命令

6.1、touch命令da

6.2、echo命令

7、文件或目录复制命令

8.文件或目录移动命令

9、文件或目录删除命令

10、别名命令

11、文件属性查看命令

12、目录结构查看命令

13、dd命令

功能:

14、链接指令

15、file 命令

三、 VIM编辑器

1、VIM操作模式

1.1、命令模式

1.2、插入模式

1.3、末行模式

1.4、视图模式

2、vim高级用法


一、Linux系统目录结构

1、查看目录结构命令

tree 目标目录

查看指定目录内容

2、目录结构操作使用方法

在对目录结构进行操作管理时,可有三种方法对目录进行操作管理:

1.采用绝对路径方式进行操作管理

2.采用相对路径方式进行操作管理

3.采用设备挂载方式进行管理

2.1、绝对路径

表示从根路径开始,进入指定的路径或子目录中,获取指定的数据资源信息

采用绝对路径方式进入到network-scripts目录中

[root@huang ~]# cd /etc/sysconfig/network-scripts/ [root@huang network-scripts]# ls ifcfg-ens33 ifdown-ppp ifup-ib ifup-Team ifcfg-lo ifdown-routes ifup-ippp ifup-TeamPort ifdown ifdown-sit ifup-ipv6 ifup-tunnel ifdown-bnep ifdown-Team ifup-isdn ifup-wireless ifdown-eth ifdown-TeamPort ifup-plip init.ipv6-global ifdown-ib ifdown-tunnel ifup-plusb network-functions ifdown-ippp ifup ifup-post network-functions-ipv6 ifdown-ipv6 ifup-aliases ifup-ppp ifdown-isdn ifup-bnep ifup-routes ifdown-post ifup-eth ifup-sit

说明:采用绝对路径的方式切换目录可以更有效地保证操作的准确性

2.2、相对路径

表示从当前路径开始,进入指定的路径中或子目录中,获取指定的数据资源信息

采用相对路径方式进入到network-scripts目录中

[root@huang network-scripts]# cd /etc/sysconfig/ [root@huang sysconfig]# cd network-scripts/ [root@huang network-scripts]# ls ifcfg-ens33 ifdown-ppp ifup-ib ifup-Team ifcfg-lo ifdown-routes ifup-ippp ifup-TeamPort ifdown ifdown-sit ifup-ipv6 ifup-tunnel ifdown-bnep ifdown-Team ifup-isdn ifup-wireless ifdown-eth ifdown-TeamPort ifup-plip init.ipv6-global ifdown-ib ifdown-tunnel ifup-plusb network-functions ifdown-ippp ifup ifup-post network-functions-ipv6 ifdown-ipv6 ifup-aliases ifup-ppp ifdown-isdn ifup-bnep ifup-routes ifdown-post ifup-eth ifup-sit

说明:采用相对路径的方式切换目录可以提升操作效率

3、目录结构中重要文件数据

3.1、网卡地址信息配置文件

/ect/sysconfig/network-scripts/ifcfg-ens33

说明:网卡文件名称结尾的数字编号n,即为第n+1块网卡信息,ens33表示第一块网卡,ens34则表示第二块网卡,以此类推;

3.2、网络域名服务配置文件

/etc/resolve.conf

说明:此文件中配置信息,也会影响主机和外网域名地址信息进行通讯。

3.3、网络本地解析配置文件

/etc/hosts

说明:此文件中配置信息,主要用于测试访问指定网站服务器,以及可以利用域名地址和其他服务器建立通讯。

3.4、开机自动运行脚本或命令文件

/etc/rc.local

说明:此文件中产生信息,为系统或某些服务运行状态信息,会有正常运行状态输出信息,也会有错误输出信息,主要用于排错使用。

二、文件与目录操作命令

1、打印工作目录命令

pwd命令来查看“当前工作目录”的完整路径。简单得说,每当你在终端进行操作时,你都会有一个当前工作目录。在不太确定当前位置时,就会使用pwd来判定当前目录在文件系统内的确切位置。

语法:pwd [选项]

[root@huang etc]# pwd /etc

2、目录切换命令

cd命令可以说是Linux中最基本的命令语句,其他的命令语句要进行操作,都是建立在使用cd命令上的。

语法:

cd [目录]

选项:

cd:返回当前登录用户的家目录

cd..:返回上级目录

cd~:返回当前登陆用户的家目录

cd-:返回上次跳转到当前目录的目录

cd /指定目录:切换到指定目录

[root@huang ~]# cd
[root@huang ~]# cd ..
[root@huang /]# cd ~
[root@huang ~]# cd -
/
​
[root@huang /]# cd /etc
[root@huang etc]# 
​
### 3、目录内容查看

3、ls命令

ls命令是linux下最常用的命令。ls命令就是list的缩写,缺省下ls用来打印当前目录的清单,如果ls指定其他目录,那么就会显示指定目录里的文件及文件夹清单。通过ls命令不仅可以查看linux文件夹包含的文件,而且可以查看文件权限(包括目录、文件夹、文件权限)查看目录信息等等。

语法:ls []

选项:

-a,--all:显示所有文件(包括以.开头的隐藏文件)

-l:使用详细列表格式显示文件或目录的详细信息

-h,--human-readable:使用人类可读的格式显示文件大小(例如1K、2M)

-r,--reverse:以相反的顺序列出文件

-t:按照修改时间(最新的文件在前)排序

-S:按照文件大小排序

-d,--directory:显示目录本身,而非目录下的文件列表

-i,--inode:显示每个文件的inode号

--color=auto:自动根据文件类型显示不同的颜色

-R,--recursive:递归列出目录及其子目录中的文件

[root@huang ~]# ls -a
.                .bash_history  .bashrc  .cshrc     .ICEauthority         .tcshrc   模板  文档  桌面
..               .bash_logout   .cache   .dbus      initial-setup-ks.cfg  .viminfo  视频  下载
anaconda-ks.cfg  .bash_profile  .config  .esd_auth  .local                公共      图片  音乐
​
[root@huang ~]# ls -l
总用量 8
-rw-------. 1 root root 1700 5月   7 23:15 anaconda-ks.cfg
-rw-r--r--. 1 root root 1748 5月   7 23:21 initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 5月   7 23:24 公共
drwxr-xr-x. 2 root root    6 5月   7 23:24 模板
drwxr-xr-x. 2 root root    6 5月   7 23:24 视频
drwxr-xr-x. 2 root root    6 5月   7 23:24 图片
drwxr-xr-x. 2 root root    6 5月   7 23:24 文档
drwxr-xr-x. 2 root root    6 5月   7 23:24 下载
drwxr-xr-x. 2 root root    6 5月   7 23:24 音乐
drwxr-xr-x. 2 root root    6 5月   7 23:24 桌面
[root@huang ~]# ls -lh
总用量 8.0K
-rw-------. 1 root root 1.7K 5月   7 23:15 anaconda-ks.cfg
-rw-r--r--. 1 root root 1.8K 5月   7 23:21 initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 5月   7 23:24 公共
drwxr-xr-x. 2 root root    6 5月   7 23:24 模板
drwxr-xr-x. 2 root root    6 5月   7 23:24 视频
drwxr-xr-x. 2 root root    6 5月   7 23:24 图片
drwxr-xr-x. 2 root root    6 5月   7 23:24 文档
drwxr-xr-x. 2 root root    6 5月   7 23:24 下载
drwxr-xr-x. 2 root root    6 5月   7 23:24 音乐
drwxr-xr-x. 2 root root    6 5月   7 23:24 桌面
​
[root@huang ~]# ls -r
桌面  音乐  下载  文档  图片  视频  模板  公共  initial-setup-ks.cfg  anaconda-ks.cfg
[root@huang ~]# ls -t
公共  模板  视频  图片  文档  下载  音乐  桌面  initial-setup-ks.cfg  anaconda-ks.cfg
[root@huang ~]# ls -tl
总用量 8
drwxr-xr-x. 2 root root    6 5月   7 23:24 公共
drwxr-xr-x. 2 root root    6 5月   7 23:24 模板
drwxr-xr-x. 2 root root    6 5月   7 23:24 视频
drwxr-xr-x. 2 root root    6 5月   7 23:24 图片
drwxr-xr-x. 2 root root    6 5月   7 23:24 文档
drwxr-xr-x. 2 root root    6 5月   7 23:24 下载
drwxr-xr-x. 2 root root    6 5月   7 23:24 音乐
drwxr-xr-x. 2 root root    6 5月   7 23:24 桌面
-rw-r--r--. 1 root root 1748 5月   7 23:21 initial-setup-ks.cfg
-rw-------. 1 root root 1700 5月   7 23:15 anaconda-ks.cfg
[root@huang ~]# ls -S
initial-setup-ks.cfg  公共  视频  文档  音乐
anaconda-ks.cfg       模板  图片  下载  桌面
​
[root@huang ~]# ls -Sl
总用量 8
-rw-r--r--. 1 root root 1748 5月   7 23:21 initial-setup-ks.cfg
-rw-------. 1 root root 1700 5月   7 23:15 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 5月   7 23:24 公共
drwxr-xr-x. 2 root root    6 5月   7 23:24 模板
drwxr-xr-x. 2 root root    6 5月   7 23:24 视频
drwxr-xr-x. 2 root root    6 5月   7 23:24 图片
drwxr-xr-x. 2 root root    6 5月   7 23:24 文档
drwxr-xr-x. 2 root root    6 5月   7 23:24 下载
drwxr-xr-x. 2 root root    6 5月   7 23:24 音乐
drwxr-xr-x. 2 root root    6 5月   7 23:24 桌面
​
[root@huang ~]# ls -d
.
[root@huang ~]# ls -dl
dr-xr-x---. 14 root root 4096 5月   9 14:36 .
[root@huang ~]# ls -il
总用量 8
100663362 -rw-------. 1 root root 1700 5月   7 23:15 anaconda-ks.cfg
100663363 -rw-r--r--. 1 root root 1748 5月   7 23:21 initial-setup-ks.cfg
 68134271 drwxr-xr-x. 2 root root    6 5月   7 23:24 公共
 33713981 drwxr-xr-x. 2 root root    6 5月   7 23:24 模板
 68134272 drwxr-xr-x. 2 root root    6 5月   7 23:24 视频
 33713982 drwxr-xr-x. 2 root root    6 5月   7 23:24 图片
100663421 drwxr-xr-x. 2 root root    6 5月   7 23:24 文档
    10908 drwxr-xr-x. 2 root root    6 5月   7 23:24 下载
    10909 drwxr-xr-x. 2 root root    6 5月   7 23:24 音乐
100663420 drwxr-xr-x. 2 root root    6 5月   7 23:24 桌面
​
[root@huang ~]# ls -R
.:
anaconda-ks.cfg  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
​
./公共:
​
./模板:
​
./视频:
​
./图片:
​
./文档:
​
./下载:
​
./音乐:
​
./桌面:

4、文件查看命令

4.1、cat命令

cat命令的用途是连接文件或标准输入并打印。这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读出内容并显示,它常用与重定向符号配合使用。

cat [选项] [文件]...

功能:

1、一次显示整个文件:cat filename

2、从键盘创建一个文件:cat>filename只能创建新文件,不能编辑已有文件

3、将几个文件合并为一个文件:cat file1 file2 > file3

4、追加内容到指定文件

[root@huang ~]# cat <<e>> 3.txt
​
> 123456
> e
> [root@huang ~]# cat 3.txt
> 123456
重定向符号:
1、正确重定向
1、覆盖重定向:>
​
2、追加重定向:>>
[root@huang ~]# cat > 1.txt
123
456
789[root@huang ~]# cat 1.txt
123
456
789[root@huang ~]# cat > 2.txt
qwe
asd
zxc
[root@huang ~]# cat 2.txt
qwe
asd
zxc
​
[root@huang ~]# cat 1.txt > 3.txt
[root@huang ~]# cat 3.txt
123
456
​
789[root@huang ~]# cat 2.txt >> 3.txt
[root@huang ~]# cat 3.txt
123
456
789qwe
asd
zxc
2、错误重定向
1、覆盖重定向:2>
2、追加重定向:2>>
[root@huang ~]# mkdir dir3/dir03 2> ./error.txt
[root@huang ~]# ls
1.txt  3.txt            dir1  error.txt             公共  视频  文档  音乐
2.txt  anaconda-ks.cfg  dir2  initial-setup-ks.cfg  模板  图片  下载  桌面
[root@huang ~]# cat error.txt
mkdir: 无法创建目录"dir3/dir03": 没有那个文件或目录
3、混合重定向
1、覆盖重定向:&>
2、追加重定向:&>>
[root@huang ~]# cat 2.txt 02.txt &>> error
[root@huang ~]# cat error
qwe
asd
zxc
cat: 02.txt: 没有那个文件或目录

选项:

-n:显示行号

-e:在输出的行尾添加制表符和换行号

[root@huang ~]# cat -n 3.txt 1 123 2 456

3 789[root@huang ~]# cat -e 3.txt 123$ 456$

4.2、more命令

用于逐页查看文本内容的命令,当需要查看大型文件时,more命令能够按页显示文件内容,允许用户逐页浏览文件内容。

1、more [选项][文件名]
[root@huang ~]# more 3.txt
123
456
789

操作:

空格键:向下翻一页

enter键:向下翻一行

Q键:退出more命令

/pattern:在文件中搜索指定的模式(pattern)

:n :跳转到第n行

:f :显示文件名和当前行号

4.3、less命令

看文本文件内容的命令,类似于more命令,但提供了更多的功能和操作选项。less命令可以让用户在文件中进行搜索、前后翻页、跳转到指定行等操作。

1、less [选项][文件名]
    [root@huang ~]# less 3.txt
123
456
789
~
~
~
~
~
~
~
~
~
~
~
(END)

空格键:向下滚动一屏

page up或b:向上滚动一屏

page down或空格键:向下滚动一屏

/pattern :在文件中搜索指定模式

n :找到下一个匹配的搜索结果

N :找到上一个匹配的搜索结果

g :跳转到文件的起始处

G :跳转到文件的末尾

q :退出less命令

4.4、head命令

head命令用于显示文件的开头部分,默认情况下显示文件的前10行。head命令在查看文件的头部信息时非常有用,尤其适合用于快速查看文件的开头几行内容。

语法:

1、head[选项][文件名]
root@huang ~]# head 3.txt
123
456
7891111111
2222222
333333
44444444
5555555
6666666
777
888888888

选项:

-n :显示行数

[root@huang ~]# head -n 3 3.txt
123
456
7891111111
[root@huang ~]# head -3 3.txt
123
456
7891111111

管道符(|):

将前一命令的结果作为后一命令的输入进行再次处理;相当于流水线工作。

[root@huang ~]# cat -n 3.txt | head -3 
     1  123
     2  456
     3  7891111111
4.5、tail命令

tail命令用于查看文件的尾部内容,默认情况下显示文件的最后10行。tail命令在查看日志文件或需要关注文件末尾

信息时非常有用。使用tail-f选项可以实时监控文件的变化,这对于监控系统日志或等待文件尾部的特定信息非常方便。

语法:

1、tail [选项][文件名]
[root@huang ~]# tail 1.txt
qqqqqqqqq
wwwwwwwwwwwwww
eeeeeeeeeeee
rrrrrrrrrrrr
ttttttttttttt
yyyyyyyyyy
uuuuuuuuu
iiiiiiiiiiii
oooooooo
ppppppppp

跟head命令配合使用,事先指定功能

root@huang ~]# head 1.txt | tail -2
77777777777
88888888888

选项:

-f :循环读取(实时监控文件内容的变化)

-n :显示行数

[root@huang ~]# tail -f 1.txt
qqqqqqqqq
wwwwwwwwwwwwww
eeeeeeeeeeee
rrrrrrrrrrrr
ttttttttttttt
yyyyyyyyyy
uuuuuuuuu
iiiiiiiiiiii
oooooooo
ppppppppp
​
4.6、nl命令

nl命令是Linux和类Unix系统中的一个用于将文本文件中的行号添加到内容的命令。它主要用于向文本文件中的每一行添加行号,便于阅读和查找

语法:

1、nl [选项][文件名]
[root@huang ~]# nl 1.txt
     1  123
     2  456
     3  7891111111
     4  222222222
     5  333333333
     6  444444444444
     7  555555555555
     8  666666666
     9  77777777777
    10  88888888888
    11  999999999999
    12  000000000
    13  qqqqqqqqq
    14  wwwwwwwwwwwwww
    15  eeeeeeeeeeee
    16  rrrrrrrrrrrr
    17  ttttttttttttt
    18  yyyyyyyyyy
    19  uuuuuuuuu
    20  iiiiiiiiiiii
    21  oooooooo
    22  ppppppppp
       
       
       
​

5、创建目录命令

mkdir命令用来创建指定的名称的目录,要求创建目录的用户在当前目录中具有写权限,并且指定的目录名不能是当前目录中已有的目录。如果需要创建多级目录,可以使用通配符*或多个目录名。还支持使用{}来进行创建多个目录。

语法:

1、mkdir [选项] [目录名]
[root@huang ~]# mkdir dir1
[root@huang ~]# ls
1.txt            initial-setup-ks.cfg  文档
2.txt            公共                  下载
3.txt            模板                  音乐
anaconda-ks.cfg  视频                  桌面
dir1             图片

选项:

-p :递归创建目录,如果父目录不存在也会创建

[root@huang ~]# mkdir dir2/dir02 -p
[root@huang ~]# ls
1.txt            dir2                  图片
2.txt            initial-setup-ks.cfg  文档
3.txt            公共                  下载
anaconda-ks.cfg  模板                  音乐
dir1             视频                  桌面
[root@huang ~]# tree dir2
dir2
└── dir02

创建多个目录

[root@huang ~]# ls
01.txt  1.txt  3.txt  error.txt  passwd    wang  模板  图片  下载  桌面
02.txt  2.txt  5.txt  file1      test.txt  公共  视频  文档  音乐
[root@huang ~]# mkdir dir{1..10}
[root@huang ~]# ls
01.txt  2.txt  dir1   dir3  dir6  dir9       passwd    公共  图片  音乐
02.txt  3.txt  dir10  dir4  dir7  error.txt  test.txt  模板  文档  桌面
1.txt   5.txt  dir2   dir5  dir8  file1      wang      视频  下载

6、创建文件命令

查看文件的时间戳:stat文件名称

[root@huang ~]# stat 2.txt
  文件:"2.txt"
  大小:12              块:8          IO 块:4096   普通文件
设备:fd00h/64768d      Inode:100936218   硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2025-05-12 15:02:24.873656305 +0800
最近更改:2025-05-09 21:13:54.359638653 +0800
最近改动:2025-05-09 21:13:54.359638653 +0800
创建时间:-
6.1、touch命令da

用于创建一个新的空文件或修改现有文件的时间戳。它的全称是"touch file",即触摸文件。

语法:

1、touch [文件]    文件大小为0
[root@huang ~]# touch file1
[root@huang ~]# ls
1.txt  anaconda-ks.cfg  error      initial-setup-ks.cfg  视频  下载
2.txt  dir1             error.txt  公共                  图片  音乐
3.txt  dir2             file1      模板                  文档  桌面
[root@huang ~]# ls -l
总用量 28
-rw-r--r--. 1 root root  243 5月  12 16:22 1.txt
-rw-r--r--. 1 root root   12 5月   9 21:13 2.txt
-rw-r--r--. 1 root root  107 5月  12 15:23 3.txt
-rw-------. 1 root root 1700 5月   7 23:15 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 5月  12 16:34 dir1
drwxr-xr-x. 3 root root   19 5月  12 16:37 dir2
-rw-r--r--. 1 root root   53 5月  12 18:39 error
-rw-r--r--. 1 root root   54 5月  12 18:34 error.txt
-rw-r--r--. 1 root root    0 5月  12 18:49 file1
-rw-r--r--. 1 root root 1748 5月   7 23:21 initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 5月   7 23:24 公共
drwxr-xr-x. 2 root root    6 5月   7 23:24 模板
drwxr-xr-x. 2 root root    6 5月   7 23:24 视频
drwxr-xr-x. 2 root root    6 5月   7 23:24 图片
drwxr-xr-x. 2 root root    6 5月   7 23:24 文档
drwxr-xr-x. 2 root root    6 5月   7 23:24 下载
drwxr-xr-x. 2 root root    6 5月   7 23:24 音乐
drwxr-xr-x. 2 root root    6 5月   7 23:24 桌面

批量创建文件

[root@huang ~]# touch fi{1..15}
[root@huang ~]# ls
01.txt           fi1   fi3    file2                 文档
02.txt           fi10  fi4    initial-setup-ks.cfg  下载
1.txt            fi11  fi5    passwd                音乐
2.txt            fi12  fi6    wang                  桌面
3.txt            fi13  fi7    公共
5.txt            fi14  fi8    模板
anaconda-ks.cfg  fi15  fi9    视频
error.txt        fi2   file1  图片

选项:

-t或--time=date :使用指定的日期和时间更新文件的访问和修改时间(黑客常用来修改访问记录)

6.2、echo命令

用于在终端输出指定的文本内容

1、echo [选项] [文本内容]

新建文件

[root@huang ~]# echo > file2
[root@huang ~]# ls
1.txt  anaconda-ks.cfg  file1                 公共  图片  音乐
2.txt  dir1             file2                 模板  文档  桌面
3.txt  error.txt        initial-setup-ks.cfg  视频  下载
[root@huang ~]# echo 1 > file3
[root@huang ~]# cat file3
1

选项:

-n :在输出内容后不换行

-e :启用转义字符的解析。“\”转移符号;\n:换行;\r:回车;\t:水平制表符(tab)

[root@huang ~]# echo 1
1
[root@huang ~]# echo -n 1
1[root@huang ~]# echo -e "1"
1
[root@huang ~]# echo "1\t"
1\t
[root@huang ~]# echo -e "1\t"
1
[root@huang ~]# echo -e "1\t2"
1       2
[root@huang ~]# echo "1\t2"
1\t2
[root@huang ~]# echo -e "1\t2"
1       2
[root@huang ~]# echo -e "1\n2"
1
2
[root@huang ~]# echo -e "1\r2"
2
[root@huang ~]# echo -e "1\v2"
1
 2
[root@huang ~]# echo -e "111331\v123122"
111331
      123122

7、文件或目录复制命令

cp [options] source destination 
[root@huang ~]# ls
1.txt  anaconda-ks.cfg  file1                 公共  图片  音乐
2.txt  dir1             file2                 模板  文档  桌面
3.txt  error.txt        initial-setup-ks.cfg  视频  下载
[root@huang ~]# cat 1.txt
123
456
7891111111
222222222
333333333
444444444444
555555555555
666666666
77777777777
​
[root@huang ~]# cp 1.txt 6.txt
[root@huang ~]# ls
1.txt  6.txt            error.txt  initial-setup-ks.cfg  视频  下载
2.txt  anaconda-ks.cfg  file1      公共                  图片  音乐
3.txt  dir1             file2      模板                  文档  桌面
[root@huang ~]# cat 6.txt
123
456
7891111111
222222222
333333333
444444444444
555555555555
666666666
77777777777
​

选项:

-i(交互式) :如果在目标位置存在一个文件且该文件会被覆盖时,cp命令会询问用户是否继续

-n(不覆盖) :在目标位置存在文件时,不会覆盖该文件,即不会进行复制操作

-r或-R(递归) :用于复制目录以及目录内所有子目录和文件

-p :(保留属性) :复制文件的同时保留源文件的属性如权限和时间戳

-v (详细模式) :显示详细的复制过程信息

[root@huang ~]# ls
1.txt  5.txt            dir1       file2                 模板  文档  桌面
2.txt  6.txt            error.txt  initial-setup-ks.cfg  视频  下载
3.txt  anaconda-ks.cfg  file1      公共                  图片  音乐
[root@huang ~]# cp -r /home/huang/ ./
[root@huang ~]# ls
1.txt  5.txt            dir1       file2                 公共  图片  音乐
2.txt  6.txt            error.txt  huang                 模板  文档  桌面
3.txt  anaconda-ks.cfg  file1      initial-setup-ks.cfg  视频  下载
[root@huang ~]# cd /home/
[root@huang home]# ls -l
总用量 0
drwx------. 3 huang huang 78 5月   7 23:10 huang
[root@huang home]# cd
[root@huang ~]# ls
1.txt  5.txt            dir1       file2                 公共  图片  音乐
2.txt  6.txt            error.txt  huang                 模板  文档  桌面
3.txt  anaconda-ks.cfg  file1      initial-setup-ks.cfg  视频  下载
[root@huang ~]# ls -l
总用量 36
-rw-r--r--. 1 root root  243 5月  12 16:22 1.txt
-rw-r--r--. 1 root root  243 5月  12 19:50 2.txt
-rw-r--r--. 1 root root  107 5月  12 15:23 3.txt
-rw-r--r--. 1 root root  243 5月  12 19:45 5.txt
-rw-r--r--. 1 root root  243 5月  12 19:41 6.txt
-rw-------. 1 root root 1700 5月   7 23:15 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 5月  12 16:34 dir1
-rw-r--r--. 1 root root   54 5月  12 18:34 error.txt
-rw-r--r--. 1 root root    0 5月  12 18:49 file1
-rw-r--r--. 1 root root    1 5月  12 19:06 file2
drwx------. 3 root root   78 5月  12 20:10 huang
[root@huang ~]# rm -rf huang
[root@huang ~]# ls
1.txt  5.txt            dir1       file2                 模板  文档  桌面
2.txt  6.txt            error.txt  initial-setup-ks.cfg  视频  下载
3.txt  anaconda-ks.cfg  file1      公共                  图片  音乐
[root@huang ~]# cp -pr /home/huang/ ./
[root@huang ~]# ls
1.txt  5.txt            dir1       file2                 公共  图片  音乐
2.txt  6.txt            error.txt  huang                 模板  文档  桌面
3.txt  anaconda-ks.cfg  file1      initial-setup-ks.cfg  视频  下载
[root@huang ~]# ls -l
总用量 36
-rw-r--r--. 1 root  root   243 5月  12 16:22 1.txt
-rw-r--r--. 1 root  root   243 5月  12 19:50 2.txt
-rw-r--r--. 1 root  root   107 5月  12 15:23 3.txt
-rw-r--r--. 1 root  root   243 5月  12 19:45 5.txt
-rw-r--r--. 1 root  root   243 5月  12 19:41 6.txt
-rw-------. 1 root  root  1700 5月   7 23:15 anaconda-ks.cfg
drwxr-xr-x. 2 root  root     6 5月  12 16:34 dir1
-rw-r--r--. 1 root  root    54 5月  12 18:34 error.txt
-rw-r--r--. 1 root  root     0 5月  12 18:49 file1
-rw-r--r--. 1 root  root     1 5月  12 19:06 file2
drwx------. 3 huang huang   78 5月   7 23:10 huang

8.文件或目录移动命令

mv命令用来移动文件或目录,或者重命名文件和目录的命令

语法:

1、mv [options] source destination
[root@huang ~]# ls
01.txt  5.txt            dir1       file2                 公共  图片  音乐
2.txt   6.txt            error.txt  huang                 模板  文档  桌面
3.txt   anaconda-ks.cfg  file1      initial-setup-ks.cfg  视频  下载
[root@huang ~]# mv huang wang
[root@huang ~]# ls
01.txt  5.txt            dir1       file2                 公共  图片  音乐
2.txt   6.txt            error.txt  initial-setup-ks.cfg  模板  文档  桌面
3.txt   anaconda-ks.cfg  file1      wang                  视频  下载

选项:

-i (交互式):如果移动或重命名目标位置已存在切会覆盖时,mv命令会询问用户是否继续

-n (不覆盖) :在目标位置存在文件时,不会覆盖该文件,即不会进行移动或重命名操作

-v (详细模式) :显示详细的移动或重命名过程信息

9、文件或目录删除命令

rm命令用于删除文件的命令。它的全称是”remove“,意思是”删除“

语法:

1.rm [options] file

选项:

-i或 --interactive :在删除之前,显示每个文件是否确认删除

-f或 --force :强制删除,即使文件正在被使用,或者存在打开的文件描述符

-r或--recursive :递归删除目录及其内容

-v或--verbose :详细模式,显示删除的每个文件

[root@huang ~]# rm 5.txt
rm:是否删除普通文件 "5.txt"?^C
[root@huang ~]# rm -f 5.txt
[root@huang ~]# rm dir1
rm: 无法删除"dir1": 是一个目录
[root@huang ~]# rm -r dir1
rm:是否删除目录 "dir1"?^C
[root@huang ~]# rm -rf dir1  

10、别名命令

用于创建或显示当前目录下的别名。别名是一种用户定义的命令或函数,可以用来简化复杂的命令序列。使用unalias name可以删除一个别名

1、alias [options] name=command
[root@huang ~]# alias kan='ls -l'
[root@huang ~]# kan
总用量 28
-rw-r--r--. 1 root  root   243 5月  12 16:22 01.txt
-rw-r--r--. 1 root  root   243 5月  12 19:50 2.txt
-rw-r--r--. 1 root  root   107 5月  12 15:23 3.txt
-rw-------. 1 root  root  1700 5月   7 23:15 anaconda-ks.cfg
-rw-r--r--. 1 root  root    54 5月  12 18:34 error.txt
-rw-r--r--. 1 root  root     0 5月  12 18:49 file1
-rw-r--r--. 1 root  root     1 5月  12 19:06 file2
-rw-r--r--. 1 root  root  1748 5月   7 23:21 initial-setup-ks.cfg
drwx------. 3 huang huang   78 5月   7 23:10 wang
drwxr-xr-x. 2 root  root     6 5月   7 23:24 公共
drwxr-xr-x. 2 root  root     6 5月   7 23:24 模板
drwxr-xr-x. 2 root  root     6 5月   7 23:24 视频
drwxr-xr-x. 2 root  root     6 5月   7 23:24 图片
drwxr-xr-x. 2 root  root     6 5月   7 23:24 文档
drwxr-xr-x. 2 root  root     6 5月   7 23:24 下载
drwxr-xr-x. 2 root  root     6 5月   7 23:24 音乐
drwxr-xr-x. 2 root  root     6 5月   7 23:24 桌面
[root@huang ~]# unalias kan
[root@huang ~]# kan
bash: kan: 未找到命令...

选项:

-p :显示当前目录下的所有已定义的别名列表

11、文件属性查看命令

stat命令是Unix和Linux系统中的一个实用程序,用于显示关于文件的状态信息。stat命令提供比ls -l更详细的文件信息,包括文件的大小、创建时间、最后访问时间、最后修改时间等。

语法:

1、stat [options] file
[root@huang ~]# stat .
  文件:"."
  大小:4096            块:8          IO 块:4096   目录
设备:fd00h/64768d      Inode:100663361   硬链接:15
权限:(0550/dr-xr-x---)  Uid:(    0/    root)   Gid:(    0/    root)
环境:system_u:object_r:admin_home_t:s0
最近访问:2025-05-12 20:49:59.131122503 +0800
最近更改:2025-05-12 20:49:58.422120319 +0800
最近改动:2025-05-12 20:49:58.422120319 +0800
创建时间:-

12、目录结构查看命令

tree命令是一个用于以树状结构显示目录及其内容的实用程序,常用于显示文件系统的层次结构。tree命令会递归地列出指定目录下地所有子目录和文件,并以树的形式展示它们的关系

语法:

tree [options] [directory]
[root@huang ~]# tree .
.
├── 01.txt
├── 2.txt
├── 3.txt
├── anaconda-ks.cfg
├── error.txt
├── file1
├── file2
├── initial-setup-ks.cfg
├── wang
├── \345\205\254\345\205\261
├── \346\250\241\346\235\277
├── \350\247\206\351\242\221
├── \345\233\276\347\211\207
├── \346\226\207\346\241\243
├── \344\270\213\350\275\275
├── \351\237\263\344\271\220
└── \346\241\214\351\235\242
​
9 directories, 8 files

13、dd命令

用于复制文件和转换数据。它用于将源文件复制到目标文件,或者用于在目标文件上进行数据交换

功能:

1、生成块文件

2、复制文件内容

3、测试I/O性能

语法:

dd [选项] [输出选项] [输入选项]
[root@huang ~]# dd if=./01.txt of=./5.txt 
记录了0+1 的读入
记录了0+1 的写出
243字节(243 B)已复制,0.000120653 秒,2.0 MB/秒

选项:

if=filename :指定输入文件

of=filename :指定输出文件

bs=size :指定块大小

count=num :指定要复制的块数

​
[root@huang ~]# dd if=./01.txt of=./3.txt bs=1K count=1
记录了0+1 的读入
记录了0+1 的写出
243字节(243 B)已复制,0.000262674 秒,925 kB/秒

14、链接指令

ln命令用于在文件系统中创建链接(links)。链接分为两种类型:硬链接(hard link)和符号链接(symbolic link)

硬链接(hard link):(目录不允许创建硬链接)

硬链接是文件系统中的一个目录条目,它指向另外一个文件的数据块

硬链接和原始文件共享一个inode,因此对文件的修改会影响到所有硬链接

硬链接不能跨越文件系统边界

符号(软)链接(symbolic link):

符号链接是一个特殊的文件,其内容是指向另外一个文件的路径

符号链接和原始文件不共享inode,因此删除原始文件后,符号链接仍然存在,但无法访问

符号链接可以跨越文件系统边界

语法:

1、ln [options] source destination
[root@huang ~]# ln 01.txt ./1.txt
[root@huang ~]# ls -l
总用量 36
-rw-r--r--. 2 root  root   243 5月  12 16:22 01.txt
-rw-r--r--. 2 root  root   243 5月  12 16:22 1.txt
-rw-r--r--. 1 root  root   243 5月  12 19:50 2.txt
-rw-r--r--. 1 root  root   243 5月  12 21:20 3.txt
-rw-r--r--. 1 root  root   243 5月  12 21:21 5.txt
-rw-------. 1 root  root  1700 5月   7 23:15 anaconda-ks.cfg
-rw-r--r--. 1 root  root    54 5月  12 18:34 error.txt
-rw-r--r--. 1 root  root     0 5月  12 18:49 file1
-rw-r--r--. 1 root  root     1 5月  12 19:06 file2
-rw-r--r--. 1 root  root  1748 5月   7 23:21 initial-setup-ks.cfg
drwx------. 3 huang huang   78 5月   7 23:10 wang

选项:

-s 或 --symbolic :创建符号链接而不是硬链接

[root@huang ~]# ln -s 2.txt 02.txt
root@huang ~]# ls -l
总用量 36
-rw-r--r--. 2 root  root   243 5月  12 16:22 01.txt
lrwxrwxrwx. 1 root  root     5 5月  12 21:57 02.txt -> 2.txt
-rw-r--r--. 2 root  root   243 5月  12 16:22 1.txt
-rw-r--r--. 1 root  root   243 5月  12 19:50 2.txt
-rw-r--r--. 1 root  root   243 5月  12 21:20 3.txt
-rw-r--r--. 1 root  root   243 5月  12 21:21 5.txt
-rw-------. 1 root  root  1700 5月   7 23:15 anaconda-ks.cfg
-rw-r--r--. 1 root  root    54 5月  12 18:34 error.txt

15、file 命令

在UNIX和Linux系统中,file命令用于确定文件的类型或属性。它可以提供有关文件的内容、文件系统类型、文件权限等信息

语法:

1、file [选项] 文件名
[root@huang ~]# ls
01.txt  1.txt  3.txt  anaconda-ks.cfg  file1  initial-setup-ks.cfg  公共  视频  文档  音乐
02.txt  2.txt  5.txt  error.txt        file2  wang                  模板  图片  下载  桌面
[root@huang ~]# file /usr/bin/ls
/usr/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=aaf05615b6c91d3cbb076af81aeff531c5d7dfd9, stripped

三、 VIM编辑器

1、VIM操作模式

1.1、命令模式

G :跳转到文件末尾

gg :返回文件首行

home键或者^ :跳到首字符

end键或者$ :跳到尾字符

yy :单行复制

nyy :n行复制

P :将光标移动到想要粘贴位置的上一行

p :将光标移动到想要粘贴位置的下一行

u :撤销操作,注意:进行保存且退出后无效

dd :单行删除

ndd :n行删除

D或者d$ :从所在行的光标处删除到末尾

d^ :从所在的光标处删除到行首

r :替换某一个字符

del键 :删除光标所处字符

/关键字 :根据关键字查找,n:上翻 ;N:下翻

ZZ :保存且退出

1.2、插入模式

作用:进行文件内容编辑

a :进行插入模式,在当前光标的后一个字符插入

o :在当前光标的下一行插入

i :在当前光标所在字符插入,返回命令模式时会将光标迁移一个字符

A :在光标所在行的首字符插入

I(大写) :在光标所在行的首字符插入

O :在当前光标的上一行插入

ESC(按键) :返回命令模式

1.3、末行模式

作用:保存、退出及字符串替换

:w :保存

:q :不保存退出

:wq或:x :保存退出

! :强制

:X->输入密码->:wq :设置密码并保存退出

:X->回车两次->:wq<br或者 :set key=""->:wq :取消密码

:set nu :设置行号

:nohl :取消高亮

:行号 :跳转到指定行I#

:%s/keyword/neword/ :全文替换,首个匹配替换,当包含路径时,#替换/作为匹配符号

:%s/keyword/neword/g :全文替换,所有匹配替换,当包含路径时,#替换/作为匹配符号

:指定行号s/keyword/neword/ :指定行替换,首个匹配替换,当包含路径时,#替换/作为匹配符号

:指定行号s/keyword/neword/g :指定行替换,所有匹配替换,当包含路径时,#替换/作为匹配符号

:起始行号,结束行号s/keyword/neword/ : 范围替换,首个匹配替换,当包含路径时,#替换/作为匹配符号

:起始行号,结束行号s/keyword/neword/g :范围替换,所有匹配替换,当包含路径时,#替换/作为匹配符号

1.4、视图模式

v :进行行选中

1.5、视图块模式

ctrl+v :进行列选中

选中要删除的“#”,按del :批量删除注释

选中要添加的列。输入大写I,输入#,按两次ESC :批量增加注释

2、vim高级用法

:r!command :将命令结果插入文件,command命令的结果插入光标下一行

:nr! command :将命令结果插入文件,command命令的结果插入n行后

:m,n! command :使用页面内容作为输入,command以m,n之间的行作为标准输入,标准输出会代替m,n之 间 的行. 注意这个操作似乎是不可用ctrl+r撤销的。要谨慎使用。

:! /bin/bash :暂时离开vim来执行命令,使用完sh后exit又可以返回vim

看到感觉有帮助的,动动发财的小手给博主点个赞,你们的支持是我最大的创作动力

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值