Linux基础知识学习——第六天:文件目录类命令详解(1)
一、pwd
1.1 含义
pwd:显示当前工作目录的绝对路径
1.2 实例
[root@localhost centos]# pwd
/home/centos
二、ls
2.1 含义
ls:列出目录的内容
2.2 语法
ls [选项] [目录或是文件]
2.3 选项说明
选项 | 说明 |
---|---|
-a | 全部文件,连同隐藏文件(开头为.的文件)一起列出来(常用) |
-l | 长数据串列出,包含文件的属性与权限等数据(常用),等价于ll命令 |
目录参数非必须 | 命令后面的目录可以省略,省略时,查看的是当前目录 |
2.4 显示说明
-
每行出的信息依次是:
-
文件类型与权限
连接数
文件属主
文件属组
文件大小(byte)
创建或最近修改的时间
名字
2.4 实例
//查看当前目录的所有信息
[root@localhost centos]# ls -al
total 28
drwx------. 15 centos centos 4096 Nov 1 13:58 .
drwxr-xr-x. 3 root root 20 Nov 1 13:53 ..
-rw-r--r--. 1 centos centos 18 Apr 1 2020 .bash_logout
-rw-r--r--. 1 centos centos 193 Apr 1 2020 .bash_profile
-rw-r--r--. 1 centos centos 231 Apr 1 2020 .bashrc
drwx------. 14 centos centos 4096 Nov 1 14:00 .cache
drwxr-xr-x. 14 centos centos 261 Nov 1 13:58 .config
drwx------. 3 centos centos 25 Nov 1 13:58 .dbus
drwxr-xr-x. 2 centos centos 6 Nov 1 13:58 Desktop
drwxr-xr-x. 2 centos centos 6 Nov 1 13:58 Documents
drwxr-xr-x. 2 centos centos 6 Nov 1 13:58 Downloads
-rw-------. 1 centos centos 16 Nov 1 13:58 .esd_auth
-rw-------. 1 centos centos 310 Nov 1 13:58 .ICEauthority
drwx------. 3 centos centos 19 Nov 1 13:58 .local
drwxr-xr-x. 4 centos centos 39 Nov 1 13:45 .mozilla
drwxr-xr-x. 2 centos centos 6 Nov 1 13:58 Music
drwxr-xr-x. 2 centos centos 6 Nov 1 13:58 Pictures
drwxr-xr-x. 2 centos centos 6 Nov 1 13:58 Public
drwxr-xr-x. 2 centos centos 6 Nov 1 13:58 Templates
drwxr-xr-x. 2 centos centos 6 Nov 1 13:58 Videos
//查看sys文件的信息
[root@localhost centos]# ls /sys
block bus class dev devices firmware fs hypervisor kernel module power
三、cd
3.1 含义
cd:切换目录
3.2 基本语法
cd [参数]
3.3 参数说明
参数 | 功能 |
---|---|
cd 绝对路径 | 切换路径 |
cd 相对路径 | 切换路径 |
cd ~或cd | 回到自己的家目录 |
cd - | 回到上一次所在目录,可以用来在2个目录来回跳转 |
cd … | 回到当前目录的上一级目录 |
cd -P | 跳转到实际物理路径,而非快捷方式路径 |
3.4 实例
//使用绝对路径切换到/usr/local目录
[root@localhost /]# cd /home/centos/
//使用相对路径切换到“公共的”目录
//如下,当前在/root目录,我们想到root目录下面的 桌面 目录,那么可以直接使用 cd /home/ 即可,这种情况下使用相对路径更方便,相对路径是相对于当前目录来说的。
[root@localhost ~]# pwd
/root
[root@localhost ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg
[root@localhost ~]# cd /home/
[root@localhost home]#
//回到当前用户自己的家目录
//如下,当前是root用户,目前在 /home 目录,执行cd直接回到了root用户自己的目录,即 /root 目录
[root@localhost home]# pwd
/home
[root@localhost home]# cd
[root@localhost ~]# pwd
/root
//cd - 回到上一次所在目录
[root@localhost ~]# pwd
/root
[root@localhost ~]# cd /home/centos/
[root@localhost centos]# pwd
/home/centos
[root@localhost centos]# cd /etc/sysconfig/
[root@localhost sysconfig]# cd -
/home/centos
[root@localhost centos]# cd -
/etc/sysconfig
[root@localhost sysconfig]# cd -
/home/centos
//cd ..回到上一级目录
[root@localhost centos]# pwd
/home/centos
[root@localhost centos]# cd ..
[root@localhost home]# pwd
/home
四、mkdir
4.1 含义
mkdir:创建一个新的目录
4.2 基本语法
mkdir 要创建的目录
4.3 实例
//创建一个目录data
[root@localhost home]# ls
centos
[root@localhost home]# mkdir data
[root@localhost home]# ls
centos data
//创建多级目录,mkdir -p 要创建的目录
[root@localhost home]# mkdir /a/b
mkdir: cannot create directory ‘/a/b’: No such file or directory
[root@localhost home]# mkdir -p a/b
[root@localhost home]# ls
a centos data
五、rmdir
5.1 含义
rmdir:删除一个空目录
5.2 实例
[root@localhost home]# rmdir a
//删除非空目录会失败
[root@localhost home]# rmdir b
rmdir: failed to remove ‘b’: Directory not empty
六、touch
6.1 含义
touch:创建空文件
6.2 实例
[root@localhost home]# touch word.txt
[root@localhost home]# ls
centos word.txt
七、cp
7.1 含义
cp:复制文件或目录
7.2 基本语法
cp file folder
复制file文件到folder
//复制文件
//当前目录创建word.txt文件
[root@localhost home]# touch word.txt
[root@localhost home]# ls
centos word.txt
//将文件复制到/tmp/目录
[root@localhost home]# cp word.txt /tmp/
//递归复制整个文件夹
[root@localhost ~]# mkdir a
[root@localhost ~]# touch a/word01.txt
[root@localhost ~]# touch a/word02.txt
[root@localhost ~]# ls a
word01.txt word02.txt
[root@localhost ~]# cp -r a b
[root@localhost ~]# ls b
word01.txt word02.txt
// \cp:复制,覆盖不提示
[root@localhost ~]# touch 1.txt
[root@localhost ~]# cp 1.txt /tmp/
[root@localhost ~]# cp 1.txt /tmp/
cp: overwrite ‘/tmp/1.txt’? y
[root@localhost ~]# \cp 1.txt /tmp/
八、rm
8.1 含义
rm:删除文件或目录
8.2 基本语法
rm [选项] 文件1 文件2 …
选项 | 说明 |
---|---|
-r | 递归删除目录中所有内容 |
-f | 强制执行删除操作,不会提示用户是否确认删除 |
-v | 显示命令的详细执行过程 |
8.3 实例
//删某个文件
[root@localhost ~]# rm 1.txt
rm: remove regular empty file ‘1.txt’? y
//使用rm -f可以强制删除,不会提示确认信息
[root@localhost ~]# rm -f 2.txt
//递归删除目录所有内容
[root@localhost ~]# mkdir -p /1/2/3/4
[root@localhost ~]# rm -rf 1
//删除多个文件,且输出提示信息
[root@localhost home]# mkdir a b c
[root@localhost home]# ls
a b c centos
[root@localhost home]# rm -rfv a b c
removed directory: ‘a’
removed directory: ‘b’
removed directory: ‘c’
//*通配符删除
//rm -rf /*:此命令慎用
[root@localhost home]# mkdir a1 a2 a3
[root@localhost home]# ls
a1 a2 a3 centos
//强制或者递归删除以文件名称以a开头的所有文件
[root@localhost home]# rm -rf a*
[root@localhost home]# ls
centos