目的

掌握创建文件、查看文件、复制文件、移动文件、删除文件、创建软链接等。

前提

先介绍下 linux 文件系统上的文件类型如下:

-:表示普通文件

d:表示目录文件

b:表示块设备文件

c:表示字符设备文件

l:表示软链接文件

p:表示管道文件

s:表示套接字文件

【例 1】查看文件类型

[root@Magedu ~]# ll
total 4
drwxr-xr-x 2 root root 54 May 23 09:00 testdir
-rw-r--r-- 1 root root 39 May 22 05:33 test.txt

显示结果中,第一个位置的符号“-”就代表了文件类型为普通文件。

命令介绍

1、pwd 命令:显示当前 shell 的工作目录

【例 2】显示当前工作目录

[root@Magedu network-scripts]#
[root@Magedu network-scripts]# pwd
/etc/sysconfig/network-scripts

2、basename 命令:取路径基名

【例 3】获取 /etc/sysconfig/ 的基名

[root@Magedu sysconfig]# basename /etc/sysconfig/
sysconfig

3、dirname 命令:取路径名

【例 4】取 /etc/sysconfig/ 的路径名

[root@Magedu sysconfig]# dirname /etc/sysconfig/
/etc

4、cd 命令:切换目录

【例 5】切换到用户家目录

[root@Magedu network-scripts]# cd
[root@Magedu ~]#

或:

[root@Magedu network-scripts]# cd ~
[root@Magedu ~]#

【例 6】切换到父目录

[root@Magedu network-scripts]# cd ..
[root@Magedu sysconfig]#

【例 7】切换到 /etc/sysconfig 目录下

[root@Magedu ~]# cd /etc/sysconfig/
[root@Magedu sysconfig]#

【例 8】切换到上一次所在的目录

[root@Magedu sysconfig]# cd -
/root
[root@Magedu ~]#

5、ls 命令:列出目录的内容

选项:

-a:包含隐藏文件;

-l:显示额外信息;

-R:目录递归通过;

-1:文件分行显示;

【例 9】显示当前目录下所有文件

[root@Magedu testdir]# ls -a
. .. test1.txt test.txt win.txt

【例 10】显示目录内容的额外信息

[root@Magedu testdir]# ls -l
total 252
-rw-r--r-- 1 root root 0 May 23 09:00 test1.txt
-rw-r--r-- 1 root root 251734 May 23 04:15 test.txt
-rw-r--r-- 1 root root 9 May 23 04:06 win.txt

或:

[root@Magedu testdir]# ll
total 252
-rw-r--r-- 1 root root 0 May 23 09:00 test1.txt
-rw-r--r-- 1 root root 251734 May 23 04:15 test.txt
-rw-r--r-- 1 root root 9 May 23 04:06 win.txt

【例 11】递归显示目录内容

[root@Magedu ~]# ls -R
.:
a b messaage.txt test test.txt
anaconda-ks.cfg fstab passwdtst testdir
./testdir:
test1.txt test.txt win.txt

【例 12】组合应用

[root@Magedu testdir]# ll -aR
.:
total 256
drwxr-xr-x 2 root root 54 May 23 09:00 .
dr-xr-x---. 10 root root 4096 May 28 07:07 ..
-rw-r--r-- 1 root root 0 May 23 09:00 test1.txt
-rw-r--r-- 1 root root 251734 May 23 04:15 test.txt
-rw-r--r-- 1 root root 9 May 23 04:06 win.txt

6、stat 命令:查看文件状态

【例 13】查看 test.txt 文件的状态,注意三个时间戳

[root@Magedu ]# stat test.txt
File: ‘test.txt’
Size: 9 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 33734497 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2018-05-25 21:55:56.298893545 -0400
Modify: 2018-05-25 08:44:11.510484658 -0400
Change: 2018-05-25 08:44:11.510484658 -0400
Birth: -

7、touch 命令:创建空文件和刷新时间

【例 14】创建空文件 test.sh

[root@Magedu ~]# touch test.sh
[root@Magedu ~]# ll test.sh
-rw-r--r-- 1 root root 0 May 29 01:55 test.sh

8、cp 命令:复制文件和目录

【例 15】把 /etc/httpd/conf/httpd.conf 文件和 /etc/my.cnf 文件拷贝到当前目录

[root@Magedu dir1]# cp /etc/httpd/conf/httpd.conf /etc/my.cnf ./
[root@Magedu dir1]# ll
total 16
-rw-r--r-- 1 root root 11766 May 29 02:13 httpd.conf
-rw-r--r-- 1 root root 570 May 29 02:13 my.cnf

【例 16】把 /etc/nginx 目录及其下面所有文件和子目录拷贝到当前目录

[root@Magedu dir1]# cp -R /etc/nginx/ ./
[root@Magedu dir1]# ll
total 20
-rw-r--r-- 1 root root 11766 May 29 02:13 httpd.conf
-rw-r--r-- 1 root root 570 May 29 02:13 my.cnf
drwxr-xr-x 4 root root 4096 May 29 02:16 nginx

【例 17】复制 httpd.conf 文件并重命名为 httpd.conf.bak

[root@Magedu dir1]# cp httpd.conf httpd.conf.bak
[root@Magedu dir1]# ll
total 32
-rw-r--r-- 1 root root 11766 May 29 02:13 httpd.conf
-rw-r--r-- 1 root root 11766 May 29 02:19 httpd.conf.bak
-rw-r--r-- 1 root root 570 May 29 02:13 my.cnf
drwxr-xr-x 4 root root 4096 May 29 02:16 nginx

【例 18】复制 /etc 目录下所有文件及其子目录到当前目录,并重命名为 etc_bak

[root@Magedu dir1]# cp -R /etc ./etc_bak
[root@Magedu dir1]# ll
total 44
drwxr-xr-x 143 root root 8192 May 29 02:32 etc_bak
-rw-r--r-- 1 root root 11766 May 29 02:13 httpd.conf
-rw-r--r-- 1 root root 11766 May 29 02:19 httpd.conf.bak
-rw-r--r-- 1 root root 570 May 29 02:13 my.cnf
drwxr-xr-x 4 root root 4096 May 29 02:16 nginx

9、mv 命令:移动文件或目录

注意:移动目录时,无需添加 - R 递归选项,要与 cp 命令区别。

【例 19】把当前目录下 nginx 命令重命名为 nginx_bak

[root@Magedu dir1]# mv nginx/ nginx_bak
[root@Magedu dir1]# ll
total 44
drwxr-xr-x 143 root root 8192 May 29 02:32 etc_bak
-rw-r--r-- 1 root root 11766 May 29 02:13 httpd.conf
-rw-r--r-- 1 root root 11766 May 29 02:19 httpd.conf.bak
-rw-r--r-- 1 root root 570 May 29 02:13 my.cnf
drwxr-xr-x 4 root root 4096 May 29 02:16 nginx_bak

【例 20】把 httpd.conf 文件移动到 /tmp 目录下

[root@Magedu dir1]# mv httpd.conf /tmp
[root@Magedu dir1]# ll
total 32
drwxr-xr-x 143 root root 8192 May 29 02:32 etc_bak
-rw-r--r-- 1 root root 11766 May 29 02:19 httpd.conf.bak
-rw-r--r-- 1 root root 570 May 29 02:13 my.cnf
drwxr-xr-x 4 root root 4096 May 29 02:16 nginx_bak

10、rm 命令:删除文件或目录

【例 21】删除当前目录下所有文件

[root@Magedu dir1]# rm -rf *
[root@Magedu dir1]# ll
total 0

11、mkdir 命令:创建目录

【例 22】创建目录 a,其下包含 b 和 c 两目录,且 b 和 c 目录下都有一个目录 d

[root@Magedu ~]# mkdir -p a/{b,c}/d

12、tree 命令:显示目录树

【例 23】显示 a 目录的目录树

[root@Magedu ~]# tree a
a
├── b
│ └── d
└── c
└── d
4 directories, 0 files

【例 24】查看 /usr/local 目录树,但仅查看 2 级的目录深度

[root@Magedu ~]# tree -L 2 /usr/local
/usr/local
├── bin
├── etc
├── games
├── include
├── lib
├── lib64
├── libexec
├── sbin
├── share
│ ├── applications
│ ├── info
│ └── man
└── src
13 directories, 0 files

13、ln 命令:创建链接文件

【例 25】把 /usr/sbin/apachectl 文件在当前目录下创建软连接文件为 apachectl

[root@Magedu dir1]# ln -s /usr/sbin/apachectl apachectl
[root@Magedu dir1]# ll
total 0
lrwxrwxrwx 1 root root 19 May 29 02:57 apachectl -> /usr/sbin/apachectl
文章来源于网络,侵删!