Shell命令-文件及目录操作之ls、cd

文件及目录操作 - ls、cd

1、ls:列出目录的内容及其内容属性信息

ls命令的功能说明

ls命令用于列出目录的内容及其内容属性信息。

ls命令的语法格式

ls [OPTION]... [FILE]...
ls [参数选项] [文件或目录]

ls命令的选项说明

ls 选项很多,表1为ls命令的参数及说明:

表1:ls命令的参数及说明

参数选项 解释说明(带*的为重点)
-l 长格式显示文件属性*
-a 显示所有字目录和文件(包含隐藏文件)*
-d 只显示目录*
-i 显示Inode号
-h 人类可读形式显示*
-A 列出所有内容,不包括.与..这两个目录
-p 目录结尾加斜线,用来区分目录和文件
-S 根据文件大小排序
-R 递归列出所有子目录
-F 不同文件结尾加不同标识(目录结尾加斜线)*
--color=auto 不同的文件类型显示颜色参数
--time-style=(long-iso,iso) 以不同的时间格式输出
-r 倒序排序*
-t 按修改时间排序*

ls命令的实践操作

实验环境

[root@oldboyedu  ~]# mkdir /test                 <-->在“/”下面创建一个test目录
[root@oldboyedu  ~]# cd /test                       <-->切换到test目录
[root@oldboyedu  /test]# touch file{1..3}.txt  <-->批量创建若干文件
[root@oldboyedu  /test]# mkdir dir{1..3}       <-->批量创建若干目录
[root@oldboyedu  /test]# tree                        <-->查看上述的文件和目录
.
├── dir1
├── dir2
├── dir3
├── file1.txt
├── file2.txt
└── file3.txt

3 directories, 3 files

范例1: 直接执行ls命令,不带任何参数。

[root@oldboyedu  /test]# ls              <-->不加参数,显示所有文件和目录(不包括隐藏文件)
dir1  dir2  dir3  file1.txt  file2.txt  file3.txt    

范例2: 使用-a参数显示所有文件,特别是隐藏文件。

[root@oldboyedu  /test]# touch .file4.txt      <-->创建一个隐藏文件,在开头加.即可
[root@oldboyedu  /test]# ls                           <-->没有隐藏文件
dir1  dir2  dir3  file1.txt  file2.txt  file3.txt
[root@oldboyedu  /test]# ls -a                       <-->列出所有内容,包括隐藏文件
.  ..  dir1  dir2  dir3  file1.txt  file2.txt  file3.txt  .file4.txt
[root@oldboyedu  /test]# ls -A                       <-->列出所有内容,不包括.和..
dir1  dir2  dir3  file1.txt  file2.txt  file3.txt  .file4.txt

范例3: 使用-l参数显示详细信息。

[root@oldboyedu  /test]# ls -l        <-->列出文件的详细信息,及属性及文件名。俗称以长格式显示
total 0
drwxr-xr-x 2 root root 6 Apr  4 14:51 dir1
drwxr-xr-x 2 root root 6 Apr  4 14:51 dir2
drwxr-xr-x 2 root root 6 Apr  4 14:51 dir3
-rw-r--r-- 1 root root 0 Apr  4 14:51 file1.txt
-rw-r--r-- 1 root root 0 Apr  4 14:51 file2.txt
-rw-r--r-- 1 root root 0 Apr  4 14:51 file3.txt
[root@oldboyedu  /test]# ll            <-->和 ls -l 效果一样,是因为做了别名
total 0
drwxr-xr-x 2 root root 6 Apr  4 14:51 dir1
drwxr-xr-x 2 root root 6 Apr  4 14:51 dir2
drwxr-xr-x 2 root root 6 Apr  4 14:51 dir3
-rw-r--r-- 1 root root 0 Apr  4 14:51 file1.txt
-rw-r--r-- 1 root root 0 Apr  4 14:51 file2.txt
-rw-r--r-- 1 root root 0 Apr  4 14:51 file3.txt

范例4: 显示完整时间属性参数--time-style=long-iso

[root@oldboyedu  /test]# ls -l                                        <-->不加的效果
total 0
drwxr-xr-x 2 root root 6 Apr  4 14:51 dir1
drwxr-xr-x 2 root root 6 Apr  4 14:51 dir2
drwxr-xr-x 2 root root 6 Apr  4 14:51 dir3
-rw-r--r-- 1 root root 0 Apr  4 14:51 file1.txt
-rw-r--r-- 1 root root 0 Apr  4 14:51 file2.txt
-rw-r--r-- 1 root root 0 Apr  4 14:51 file3.txt
[root@oldboyedu  /test]# ls -l --time-style=long-iso   <-->加了的效果,需结合参数-l
total 0
drwxr-xr-x 2 root root 6 2019-04-04 14:51 dir1
drwxr-xr-x 2 root root 6 2019-04-04 14:51 dir2
drwxr-xr-x 2 root root 6 2019-04-04 14:51 dir3
-rw-r--r-- 1 root root 0 2019-04-04 14:51 file1.txt
-rw-r--r-- 1 root root 0 2019-04-04 14:51 file2.txt
-rw-r--r-- 1 root root 0 2019-04-04 14:51 file3.txt

范例5: 执行ls命令带显示内容的访问时间属性的参数。

[root@oldboyedu  /test]# stat file1.txt                       <-->显示文件的属性及状态信息
  File: ‘file1.txt’
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: 803h/2051d  Inode: 51524120    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-04-04 14:51:43.623772862 +0800        <-->文件的访问时间
Modify: 2019-04-04 14:51:43.623772862 +0800
Change: 2019-04-04 14:51:43.623772862 +0800
 Birth: -
[root@oldboyedu  /test]# date                                   <-->查看当下系统时间
Thu Apr  4 15:17:13 CST 2019
[root@oldboyedu  /test]# cat file1.txt                        <-->查看文件内容即表示访问文件了
[root@oldboyedu  /test]# stat file1.txt                       <-->重新查看文件的访问时间
  File: ‘file1.txt’
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: 803h/2051d  Inode: 51524120    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-04-04 15:17:21.452639089 +0800        <-->file1.txt的访问时间已经变化了
Modify: 2019-04-04 14:51:43.623772862 +0800
Change: 2019-04-04 14:51:43.623772862 +0800
 Birth: -
[root@oldboyedu  /test]# ls -l --time-style=long-iso --time=atime
total 0
drwxr-xr-x 2 root root 6 2019-04-04 14:52 dir1
drwxr-xr-x 2 root root 6 2019-04-04 14:52 dir2
drwxr-xr-x 2 root root 6 2019-04-04 14:52 dir3
-rw-r--r-- 1 root root 0 2019-04-04 15:17 file1.txt     <-->看,只有file1.txt的文件的访问时间发生变化
-rw-r--r-- 1 root root 0 2019-04-04 14:51 file2.txt
-rw-r--r-- 1 root root 0 2019-04-04 14:51 file3.txt
[root@oldboyedu  /test]# ls -l --time-style=long-iso 
total 0
drwxr-xr-x 2 root root 6 2019-04-04 14:51 dir1
drwxr-xr-x 2 root root 6 2019-04-04 14:51 dir2
drwxr-xr-x 2 root root 6 2019-04-04 14:51 dir3         <-->这里是文件的默认修改时间列
-rw-r--r-- 1 root root 0 2019-04-04 14:51 file1.txt
-rw-r--r-- 1 root root 0 2019-04-04 14:51 file2.txt
-rw-r--r-- 1 root root 0 2019-04-04 14:51 file3.txt
<-->结论:--time=atime是显示访问时间,而非默认的修改时间

范例6: 执行ls命令,带-F参数(与tree命令的-F非常相似)。

[root@oldboyedu  /test]# ls -F                 <-->所有目录后面加了 / 
dir1/  dir2/  dir3/  file1.txt  file2.txt  file3.txt
[root@oldboyedu  /test]# ls -F|grep /      <-->过滤目录
dir1/
dir2/
dir3/
[root@oldboyedu  /test]# ls -F|grep -v /  <-->过滤文件
file1.txt
file2.txt
file3.txt

范例7: 使用-d参数只显示目录本身信息

[root@oldboyedu  /test]# ls -l dir1      <-->无法查到dir1目录的信息
total 0
[root@oldboyedu  /test]# ls -ld dir1    <-->显示dir1目录本身的信息
drwxr-xr-x 2 root root 6 Apr  4 14:51 dir1

范例8: 使用-R参数递归查看目录

[root@oldboyedu  /test]# mkdir dir1/sub1/test -p    <-->递归创建目录
[root@oldboyedu  /test]# ls -R dir1                            <-->类似tree,但是没tree好用
dir1:
sub1

dir1/sub1:
test

dir1/sub1/test:
[root@oldboyedu  /test]# ls dir1
sub1

范例8: ls命令的别名相关知识及设置ls别名

[root@oldboyedu  /test]# alias |grep ls                                         <-->后续会讲alias,这里只是演示
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
[root@oldboyedu  /test]# alias lst='ls -l --time-style=long-iso'  <-->设置别名
[root@oldboyedu  /test]# alias |grep lst                                        <-->检查是否生效
alias lst='ls -l --time-style=long-iso'
[root@oldboyedu  /test]# lst                                                          <-->测试结果(这里只是临时生效)
total 0
drwxr-xr-x 3 root root 18 2019-04-05 18:52 dir1
drwxr-xr-x 2 root root  6 2019-04-04 14:51 dir2
drwxr-xr-x 2 root root  6 2019-04-04 14:51 dir3
-rw-r--r-- 1 root root  0 2019-04-04 14:51 file1.txt
-rw-r--r-- 1 root root  0 2019-04-04 14:51 file2.txt
-rw-r--r-- 1 root root  0 2019-04-04 14:51 file3.txt

范例9: 查找最近更新过的文件

[root@oldboyedu  /test]# touch /etc/test.txt    <-->创建一个新文件
[root@oldboyedu  /test]# ls -lrt /etc/
total 1096                                                              <-->有1096行文件
***省略若干文件行***
drwxr-xr-x   2 root root     38 Apr  4 18:14 ntp
-rw-r--r--   1 root root     51 Apr  5 18:41 resolv.conf
-rw-r--r--   1 root root      0 Apr  5 19:03 test.txt <-->看,我们新创建的就排在最后,这样,就不用翻屏看了
[root@oldboyedu  /test]# ls -lrt /etc/|tail -1        <-->显示最后一行,很方便吧
-rw-r--r--   1 root root      0 Apr  5 19:03 test.txt  

范例10: 显示inode号并且以人类可读的方式显示

[root@oldboyedu  /test]# ls -lhi          <-->第一列就是inode号,并且以M为单位显示
total 51M
  870845 drwxr-xr-x 3 root root  18 Apr  5 18:52 dir1
17517441 drwxr-xr-x 2 root root   6 Apr  4 14:51 dir2
33636287 drwxr-xr-x 2 root root   6 Apr  4 14:51 dir3
51524120 -rw-r--r-- 1 root root 51M Apr  5 19:16 file1.txt
51524122 -rw-r--r-- 1 root root   0 Apr  4 14:51 file2.txt
51524123 -rw-r--r-- 1 root root   0 Apr  4 14:51 file3.txt
[root@oldboyedu  /test]# ls -l              <-->没有inode号,并且以b为单位显示
total 51832
drwxr-xr-x 3 root root       18 Apr  5 18:52 dir1
drwxr-xr-x 2 root root        6 Apr  4 14:51 dir2
drwxr-xr-x 2 root root        6 Apr  4 14:51 dir3
-rw-r--r-- 1 root root 53075968 Apr  5 19:16 file1.txt
-rw-r--r-- 1 root root        0 Apr  4 14:51 file2.txt
-rw-r--r-- 1 root root        0 Apr  4 14:51 file3.txt

2、cd:切换目录

cd命令的功能说明

cd命令用于从当前目录切换到指定的工作目录。

cd命令的语法格式

cd [-L|[-P [-e]]] [dir]
cd [参数选项] [目录]

cd命令的选项说明

cd 选项对于初学者可能容易记混,不过勤加练习就好了,表1为cd命令的参数及说明:

表1cd命令的参数及说明

参数选项 解释说明
. 当前目录
.. 上一级目录
~ 用户家目录
- 上一次所在目录,可来回切换
空格 不加任何参数,不管在哪,都直接回到用户家目录

cd命令的实践操作

案例1: 进入系统/etc目录(cd /etc

[root@oldboyedu  /test]# pwd             <-->打印当前路径
/test
[root@oldboyedu  /test]# cd /etc/       <-->切换到/etc目录
[root@oldboyedu  /etc]# pwd              <-->打印当前路径
/etc

案例2: 切换到当前目录的上一级目录(cd ..

[root@oldboyedu  /etc]# pwd
/etc
[root@oldboyedu  /etc]# cd ..     <-->".."等同于"../"
[root@oldboyedu  /]# pwd
/

案例3: 进入当前目录的父目录的父目录中(cd ../../

[root@oldboyedu  /]# cd /test/dir1
[root@oldboyedu  /test/dir1]# pwd
/test/dir1
[root@oldboyedu  /test/dir1]# cd ../../
[root@oldboyedu  /]# pwd
/

案例4: 返回当前目录上一次所在的目录(cd -

[root@oldboyedu  /]# cd /test/dir1
[root@oldboyedu  /test/dir1]# pwd
/test/dir1
[root@oldboyedu  /test/dir1]# cd
[root@oldboyedu  ~]# pwd
/root
[root@oldboyedu  ~]# cd -
/test/dir1

案例4: 进入当前用户的家目录(cd ~

[root@oldboyedu  ~]# cd /test/dir1
[root@oldboyedu  /test/dir1]# pwd
/test/dir1
[root@oldboyedu  /test/dir1]# cd ~     <-->回到当前root用户的家目录
[root@oldboyedu  ~]# pwd
/root
[root@oldboyedu  ~]# cd /etc/sysconfig/network-scripts/  
[root@oldboyedu  /etc/sysconfig/network-scripts]# pwd
/etc/sysconfig/network-scripts              <-->这个目录够长吧
[root@oldboyedu  /etc/sysconfig/network-scripts]# cd
[root@oldboyedu  ~]# pwd                  <-->cd不加参数,直接回到家目录
/root

今天就写到这里,有什么疑问或出现什么错误,随时欢迎大神们发表评论指点迷津

猜你喜欢

转载自blog.51cto.com/14068656/2375083