文件及目录操作 - 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

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

Shell命令-文件及目录操作之ls、cd的更多相关文章

  1. Shell命令-文件及目录操作之cp、find

    文件及目录操作 - cp.find 1.cp:复制文件或目录 cp命令的功能说明 cp命令用于复制文件或目录. cp命令的语法格式 cp [OPTION]... SOURCE... DIRECTORY ...

  2. Shell命令-文件及目录操作之mkdir、mv

    文件及目录操作 - mkdir.mv 1.mkdir:创建目录 mkdir命令的功能说明 mkdir命令用于创建目录,默认情况下,要创建的目录已存在,会提示文件存在,不会继续创建目录. mkdir命令 ...

  3. Shell命令-文件及目录操作之pwd、rm

    文件及目录操作 - pwd.rm 1.pwd:显示当前所在位置信息 pwd命令的功能说明 pwd命令用于显示当前工作目录的绝对路径,以便在各个目录间来回切换. pwd命令的语法格式 pwd [OPTI ...

  4. Shell命令-文件及目录操作之touch、tree

    文件及目录操作 - touch.tree 1.touch:创建文件或更改文件时间戳 touch命令的功能说明 touch命令用于创建新的空文件或改变已有文件的时间戳属性. touch命令的语法格式 t ...

  5. Shell命令-文件及目录操作之file、md5sum

    文件及目录操作 - file.md5sum 1. file:显示文件的类型 file命令的功能说明 用于辨识文件类型.通过 file 指令,我们得以辨识该文件的类型. file命令的语法格式 file ...

  6. Shell命令-文件及目录操作之chattr、lsattr

    文件及目录操作 - chattr.lsattr 1. chattr:改变文件属性 chattr命令的功能说明 chattr命令用于改变文件属性.这项指令可改变存放在ext2文件系统上的文件或目录属性, ...

  7. Linux操作系统常用命令合集——第一篇-文件和目录操作(40个命令)

    一.选项和参数的区别        在经过上一次的系统安装后我们已经成功登陆,登陆LInux后,我们就可以在#或$符后面去输入命令,有的时候命令后面还会跟着“选项”(英文名:options)或“参数” ...

  8. Python中的文件和目录操作实现

    Python中的文件和目录操作实现 对于文件和目录的处理,虽然可以通过操作系统命令来完成,但是Python语言为了便于开发人员以编程的方式处理相关工作,提供了许多处理文件和目录的内置函数.重要的是,这 ...

  9. Python之文件与目录操作及压缩模块(os、shutil、zipfile、tarfile)

    Python中可以用于对文件和目录进行操作的内置模块包括: 模块/函数名称 功能描述 open()函数 文件读取或写入 os.path模块 文件路径操作 os模块 文件和目录简单操作 zipfile模 ...

随机推荐

  1. C#添加PDF页眉——添加文本、图片到页眉

    页眉常用于显示文档的附加信息,我们可以在页眉中插入文本或者图形,例如,页码.日期.公司徽标.文档标题.文件名或作者名等等.那么我们如何以编程的方式添加页眉呢?今天,这篇文章向大家分享如何使用了免费组件 ...

  2. vs2013中集成Git

    一:为什么不用2013中自带的? 我的2013自带的没法用,连最基本的克隆都用不了,网上看着下  好像说都不能用:不知道各位的如何. 二:如何自己讲Git集成到vs2013中? 需要的工具:     ...

  3. 基础环境系列:MySQL8.0.12

    机型与版本:windows10(64-bits) Mysql环境配置:mysql8.0.12 一.MySQL安装 Mysql的安装有两种方法,一种是通过.msi一种是通过压缩包.穷呢,大家就老实下社区 ...

  4. Python使用Plotly绘图工具,绘制气泡图

    今天来讲讲如何使用Python 绘图工具,Plotly来绘制气泡图. 气泡图的实现方法类似散点图的实现.修改散点图中点的大小,就变成气泡图. 实现代码如下: import plotly as py i ...

  5. 使用 Nexus Repository Manager 搭建 npm 私服

    目录 环境 下载与安装 添加npm仓库 配置与验证npm仓库 发布自己的包 Nexus开启启动 脚注 环境 windows10(1803) Nexus Repository Manager OSS 3 ...

  6. UE3中的时间

    为了管理时间,Unreal将游戏运行时间片分隔为"Ticks".一个Tick是关卡中所有Actors更新的最小时间单位.一个tick一般是10ms-100ms(CPU性能越好,游戏 ...

  7. Python第三天 序列 5种数据类型 数值 字符串 列表 元组 字典 各种数据类型的的xx重写xx表达式

    Python第三天 序列  5种数据类型  数值  字符串  列表  元组  字典 各种数据类型的的xx重写xx表达式 目录 Pycharm使用技巧(转载) Python第一天  安装  shell ...

  8. mssql 存储过程调用另一个存储过程中的结果的方法分享

    转自:http://www.maomao365.com/?p=6801 摘要: 下文将分享"一个存储过程"中如何调用"另一个存储过程的返回结果",并应用到自身的 ...

  9. 20180831-Linux环境下Python 3.6.6 的安装说明

    20180831-Linux环境下Python 3.6.6 的安装说明 摘要:Python3 安装部署,普通用户,编译安装 Author: andy_yhm@yeah.net Date: 201808 ...

  10. linux-arm 安装 dotnetcore

    X86或者X64 安装.net core runtime  可以参照   https://www.cnblogs.com/nnhy/p/netcore_centos.html#4122354 而   ...