Linux之文件目录类命令

文件目录类命令

pwd:显示当前工作目录的绝对路径

基本语法

pwd

案例

[root@Demo-Server ~]# pwd
/root

ls:列出目录的内容

基本语法

ls [选项] [目录或是文件]

选项说明

选项说明
-a全部文件,连同隐藏文件(开头为.的文件)一起列出来(常用)
-l长数据串列出,包含文件的属性与权限等数据(常用),等价于ll命令
目录参数非必须命令后面的目录可以省略,省略时,查看的是当前目录

显示说明

每行出的信息依次是:

  • 文件类型与权限
  • 连接数
  • 文件属主
  • 文件属组
  • 文件大小(byte)
  • 创建或最近修改的时间
  • 名字

案例

查看当前目录所有的内容信息
[root@Demo-Server ~]# ls -al
总用量 64
dr-xr-x---.  7 root root 4096 929 16:27 .
dr-xr-xr-x. 17 root root  224 84 14:35 ..
-rw-------.  1 root root 1728 84 14:36 anaconda-ks.cfg
-rw-------.  1 root root 1567 929 11:36 .bash_history
-rw-r--r--.  1 root root   18 1229 2013 .bash_logout
-rw-r--r--.  1 root root  176 1229 2013 .bash_profile
-rw-r--r--.  1 root root  176 1229 2013 .bashrc
drwx------.  4 root root   31 85 15:03 .cache
drwxr-xr-x.  3 root root   18 85 15:03 .config

查看目录/usr的文件信息
[root@Demo-Server ~]# ls /usr/
bin  etc  games  include  lib  lib64  libexec  local  sbin  share  src  tmp

cd:切换目录

基本语法

cd [参数]

参数说明

参数功能
cd 绝对路径切换路径
cd 相对路径切换路径
cd ~或cd回到自己的家目录
cd -回到上一次所在目录,可以用来在2个目录来回跳转
cd …回到当前目录的上一级目录
cd -P跳转到实际物理路径,而非快捷方式路径

案例

使用绝对路径切换到/usr/local目录
[root@Demo-Server ~]# cd /usr/local/
[root@Demo-Server local]#
使用相对路径切换到“公共的”目录

当前在/root目录,我们想到root目录下面的桌面目录,那么可以直接使用cd 桌面即可,这种情况下使用相对路径更方便,相对路径是相对于当前目录来说的。

[root@Demo-Server ~]# pwd
/root
[root@Demo-Server ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg  mysql
[root@Demo-Server ~]# cd mysql
[root@Demo-Server mysql]#
回到当前用户自己的家目录

当前是root用户,目前在/usr/local目录,执行cd直接回到了root用户自己的目录,即/root目录

[root@Demo-Server local]# pwd
/usr/local
[root@Demo-Server local]# cd
[root@Demo-Server ~]# pwd
/root
cd - 回到上一次所在目录

先通过cd跳到/usr/local目录,然后又通过cd跳到/root目录,执行cd - 回到上一次所在的目录,即/usr/local目录,多次执行cd -,可以在两个目录之间来回切换,还是很实用的一个命令

[root@Demo-Server ~]# cd /usr/local/
[root@Demo-Server local]# pwd
/usr/local
[root@Demo-Server local]# cd /root
[root@Demo-Server ~]# cd -
/usr/local
[root@Demo-Server local]# cd -
/root
[root@Demo-Server ~]# cd -
/usr/local
[root@Demo-Server local]# cd -
/root
cd …回到上一级目录

从/usr/local目录通过cd …回到上一级目录,即/usr目录

[root@Demo-Server local]# pwd
/usr/local
[root@Demo-Server local]# cd ..
[root@Demo-Server usr]#

mkdir:创建一个新的目录

基本语法

mkdir [选项] 要创建的目录

选项说明

选项说明
-p创建多层目录

案例

创建一个目录test
[root@Demo-Server local]# mkdir test
[root@Demo-Server local]# ls
bin  redis  test
创建多级目录

递归创建多级目录需要加-p选项,否则,父目录不存在时会报错

[root@Demo-Server local]# mkdir a/b/c/d
mkdir: 无法创建目录"a/b/c/d": 没有那个文件或目录
[root@Demo-Server local]# mkdir -p a/b/c/d

rmdir:删除一个空目录

基本语法

rmdir 要删除的空目录

案例

删除一个空目录
[root@Demo-Server local]# rmdir test
删除非空目录会失败
[root@Demo-Server local]# rmdir a
rmdir: 删除 "a" 失败:目录非空

touch:创建空文件

基本语法

touch 文件名称

案例

当前目录创建1.txt文件

[root@Demo-Server local]# touch test.txt

cp:复制文件或目录

基本语法

复制source文件到dest

cp [选项] source dest

选项说明

选项说明
-r递归复制整个文件夹

参数说明

参数说明
source源文件
dest目标文件

经验技巧

案例

复制文件
[root@Demo-Server local]# touch test.txt
[root@Demo-Server local]# cp test.txt /tmp
递归复制整个文件夹
[root@Demo-Server local]# mkdir a
[root@Demo-Server local]# touch a/test.txt
[root@Demo-Server local]# touch a/text1.txt
[root@Demo-Server local]# ls a
test.txt  text1.txt
[root@Demo-Server local]# cp -r a b
[root@Demo-Server local]# ls b
test.txt  text1.txt
\cp:复制,覆盖不提示
[root@Demo-Server local]# touch test.txt
[root@Demo-Server local]# cp test.txt /tmp
cp:是否覆盖"/tmp/test.txt"? y
[root@Demo-Server local]# touch test.txt
[root@Demo-Server local]# \cp test.txt /tmp

rm:删除文件或目录

基本语法

删除文件或目录

rm [选项] 文件1 文件2 [文件N...]

选项说明

选项说明
-r递归删除目录中所有内容
-f强制执行删除操作,不会提示用户是否确认删除
-v显示命令的详细执行过程

案例

删某个文件
[root@Demo-Server a]# touch 1.txt 2.txt
[root@Demo-Server a]# ls
1.txt  2.txt  test.txt  text1.txt
[root@Demo-Server a]# rm 1.txt
rm:是否删除普通空文件 "1.txt"?y
[root@Demo-Server a]# rm 2.txt
rm:是否删除普通空文件 "2.txt"?y
[root@Demo-Server a]# rm -f 2.txt
递归删除目录所有内容
[root@Demo-Server a]# mkdir -p a/b/c/d
[root@Demo-Server a]# rm -rf a
删除多个文件,且输出提示信息
[root@Demo-Server a]# mkdir a1 a2 a3
[root@Demo-Server a]# rm -rfv a1 a2 a3
已删除目录:"a1"
已删除目录:"a2"
已删除目录:"a3"
通配符删除

rm -rf /*:此命令慎用,否则永无回头之日。

[root@Demo-Server a]# mkdir a1 a2 a3
[root@Demo-Server a]# ls
a1  a2  a3  test.txt  text1.txt
[root@Demo-Server a]# rm -rf a*
[root@Demo-Server a]# ls
test.txt  text1.txt

mv:移动文件与目录或重命名

基本语法

mv oldNameFile newNameFile(重命名文件)
mv /temp/movefile /targetFolder (移动文件)

案例

重命名

将1.txt重命名为2.txt

[root@Demo-Server a]# ls
test.txt  text1.txt
[root@Demo-Server a]# mv test.txt 1.txt
[root@Demo-Server a]# ls
1.txt  text1.txt
移动文件
[root@Demo-Server a]# touch 1.txt
[root@Demo-Server a]# ls
1.txt  text1.txt
[root@Demo-Server a]# mkdir a
[root@Demo-Server a]# ls
[root@Demo-Server a]# mv 1.txt a
[root@Demo-Server a]# ls a
1.txt
mv a b
  • 若a和b都是目录,b不存在时,相当于把a目录命令为b目录
  • 若a和b都是目录,b存在的时,相当于把a目录移动到b目录中

cat:查看文件内容

基本语法

cat [选项] 要查看的文件

选项说明

选项说明
-n显示所有行的行号,也包含空行

经验技巧

一般查看比较小的文件,一屏幕能显示全的。

案例

[root@Demo-Server a]# ls
a  text1.txt
[root@Demo-Server a]# cat text1.txt
[root@Demo-Server a]# vi text1.txt
我是张三
[root@Demo-Server a]# cat text1.txt
我是张三
[root@Demo-Server a]# cat -n text1.txt
     1  我是张三
     2

more:文件内容分屏查看器

适合查看内容超过一个屏幕的文件,支持上下翻页查看文件内容。

more命令是一个基于VI编辑器的文本过滤器,它以全屏幕的方式按页显示文本文件的内容,more命令中内置了若干快捷键,详见操作说明。

基本语法

more 要查看的文件

操作说明

操作说明
空白键(space)向下翻一页
b向上翻一页
Enter向下翻【一行】
q代表立刻离开more,不再显示该文件内容
=输出当前行的行号
:f输出文件名和当前行的行号

示例

使用more查看2.txt文件内容

[root@Demo-Server a]# more text1.txt

less:分屏显示文件内容

less指令用来分屏查看文件内容,它的功能与more指令类似,但是比more指令更加强大,支持各种显示终端,less指令在显示文件内容时,并不是一次将整个文件加载之后才显示,而是根据显示需要去加载的内容,对于显示大型文件具有较高的效率。

基本语法

less 需要查看的文件

操作说明

操作说明
空白键(space)向下翻动一页
[pagedown] 或 f上下翻动一页
[pageup] 或 b向上翻动一页
/字符串向下搜寻【字符串】的功能;n:向下查找;N:向上查找
?字符串向上搜索【字符串】的功能,n:向上查找,N:向下查找
q离开less这个程序

经验技巧

用SecureCRT时候[pagedown]和[pageup]可能会出现无法识别的问题

案例

使用less查看2.txt的内容,这个文件比较大

[root@Demo-Server a]# less text1.txt

echo:输出内容到控制台

基本语法

echo [选项] [输出内容]

选项:

-e:支持反斜线的字符转换

控制字符作用
\\输出\本身
\n换行符
\t制表符,也就是Tab键

案例

[root@Demo-Server a]# echo "hello\tworld"
hello\tworld
[root@Demo-Server a]# echo -e "hello\tworld"
hello   world
[root@Demo-Server a]# echo -e "hello\nworld"
hello
world

head:显示文件头部内容

head用于显示文件的开头部分内容,默认情况下head指令显示文件的前10行内容。

基本语法

head 文件    (功能描述:查看文件头10行内容)
head -n 5 文件 (功能描述:查看文件头5行内容,5可以是任意行数)

选项说明

选项功能
-n 行数指定显示头部内容的行数

案例

使用head命令查看1.txt前1行内容

[root@Demo-Server a]# cat text1.txt
我是张三
[root@Demo-Server a]# head -n 1 text1.txt
我是张三

tail:输出文件尾部内容

tail用于输出文件中尾部的内容,默认情况下tail指令显示文件后10行内容。

还可以用来实时监控文件的变更。

基本语法

语法说明
tail 文件查看文件尾部10行内容
tail -n 5 文件查看文件尾部5行内容,5可以是任意行数
tail -f 文件实时追踪该文档的所有更新

案例

(1)查看1.txt文件末尾1行内容

[root@Demo-Server a]# tail -n 1 text1.txt

(2)实时追踪1.txt的内容,当有更新的时候,当前屏幕会自动输出所有的更新,通常用来查看日志文件变化,非常有用

[root@Demo-Server a]# tail -f  text1.txt

>输出重定向 和 >>追加

基本语法

语法说明
ls -l > 目标文件将ls -l命令的结果写入到目标文件中(会覆盖目标文件内容)
ls -al >> 目标文件将ls -al命令的结果追加到目标文件中
cat 文件1 > 文件2将文件1的内容覆盖到文件2中
echo “内容” >> 目标文件将一段文字追加到目标文件

案例

(1)将ll命令结果结果写入到1.txt文件
[root@Demo-Server a]# ll
总用量 4
drwxr-xr-x. 2 root root 19 929 17:07 a
-rw-r--r--. 1 root root 14 929 17:10 text1.txt
[root@Demo-Server a]# ll > text1.txt
[root@Demo-Server a]# cat text1.txt
总用量 0
drwxr-xr-x. 2 root root 19 929 17:07 a
-rw-r--r--. 1 root root  0 929 17:25 text1.txt
(2)将ls命令结果追加到1.txt文件
[root@Demo-Server a]# ls >> text1.txt
[root@Demo-Server a]# cat text1.txt
总用量 0
drwxr-xr-x. 2 root root 19 929 17:07 a
-rw-r--r--. 1 root root  0 929 17:25 text1.txt
a
text1.txt
(3)采用echo将hello单词追加到1.txt中
[root@Demo-Server a]# echo hello >> text1.txt
[root@Demo-Server a]# cat text1.txt
总用量 0
drwxr-xr-x. 2 root root 19 9月  29 17:07 a
-rw-r--r--. 1 root root  0 9月  29 17:25 text1.txt
a
text1.txt
hello

ln:创建软连接

软连接也称为符号链接,类似于windows里的快捷方式,有自己的数据块,主要存放了连接其他文件的路径。

基本语法

给源文件创建一个软连接

ln -s [源文件或目录] [软连接名称]

经验

  • 删除软连接:rm -rf 软连接名称,而不是 rm -rf 软连接名称/

  • 如果使用rm -rf 软连接名/ 删除,会把软连接对应的真实目录下内容删除。

  • 查询:通过ll就可以查看,列表属性第1位是1,尾部会有位置指向。

案例

创建软连接

在/tmp目录创建一个快捷方式desktop,指向/root/桌面

[root@Demo-Server a]# ls
a  text1.txt
[root@Demo-Server a]# ln -s /root/a ./desktop
[root@Demo-Server a]# ll desktop
lrwxrwxrwx. 1 root root 7 929 17:31 desktop -> /root/a
cd -P:进入软连接实际物理路径
[root@Demo-Server a]# ln -s /root/a ./desktop
[root@Demo-Server a]# cd -P desktop/
[root@Demo-Server desktop]#
/usr/local/a/desktop

history:查看已执行过的历史命令

基本语法

history

案例

查看已执行过的命令

history可以查看已执行过的历史命令,返回的结果:命令编号 命令

[root@Demo-Server a]# history
    1  cd /usr/local/redis/bin/
    2  ./redis-server /usr/local/redis/etc/redis.conf
    3  ./redis-cli
    4  clear
    5  uname -r
    6  cat
    7  cat /etc/os-release
    8  images
    9  imgage
!命令编号,可以执行对应编号的命令

上面编号为5对应的命令是uname -r,那么我们可以使用!5再次把uname -r命令执行一遍

[root@Demo-Server a]# !5
uname -r
history n:查看最近的n行历史命令
[root@Demo-Server a]# history
    1  cd /usr/local/redis/bin/
    2  ./redis-server /usr/local/redis/etc/redis.conf
    3  ./redis-cli
    4  clear
    5  uname -r
    6  cat
    7  cat /etc/os-release
[root@Demo-Server a]# history 3
  212  uname -r
  213  history
  214  history 3
history -c:清除命令历史记录

如果我们不想让别人看到命令的历史记录,可以使用history -c来清除命令记录。

[root@Demo-Server a]# history
    1  cd /usr/local/redis/bin/
    2  ./redis-server /usr/local/redis/etc/redis.conf
    3  ./redis-cli
    4  clear
    5  uname -r
    6  cat
    7  cat /etc/os-release
[root@Demo-Server a]# history -c
[root@Demo-Server a]# history
    1  history

tree:树形显示目录所有文件

tree命令在系统中默认是不存在的,所以使用前需要先安装,然后才可以使用。

安装tree软件包

执行下面命令进行安装,过程中提示输入的地方输入y就可以

[root@Demo-Server a]# yum install tree
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: ftp.sjtu.edu.cn
 * extras: mirror.lzu.edu.cn
 * updates: mirrors.aliyun.com
base                                                                   | 3.6 kB  00:00:00
docker-ce-stable                                                       | 3.5 kB  00:00:00
extras                                                                 | 2.9 kB  00:00:00
updates                                                                | 2.9 kB  00:00:00
(1/3): docker-ce-stable/7/x86_64/primary_db                            |  82 kB  00:00:03
(2/3): extras/7/x86_64/primary_db                                      | 250 kB  00:00:04
(3/3): updates/7/x86_64/primary_db                                     |  17 MB  00:00:14
正在解决依赖关系
--> 正在检查事务
---> 软件包 tree.x86_64.0.1.6.0-10.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

==============================================================================================
 Package            架构                 版本                        源                  大小
==============================================================================================
正在安装:
 tree               x86_64               1.6.0-10.el7                base                46 k

事务概要
==============================================================================================
安装  1 软件包

总下载量:46 k
安装大小:87 k
Is this ok [y/d/N]: y
Downloading packages:
tree-1.6.0-10.el7.x86_64.rpm                                           |  46 kB  00:00:02
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : tree-1.6.0-10.el7.x86_64                                                  1/1
  验证中      : tree-1.6.0-10.el7.x86_64                                                  1/1

已安装:
  tree.x86_64 0:1.6.0-10.el7

完毕!

语法

tree [选项] [目录]

树形显示目录中的文件。

目录可以省略,默认为当前目录

选项

选项功能
-h以人容易理解的方式显示文件大小(GBytes、MBytes、KBytes)
-L nn用来指定显示的树的深度

tree命令的使用

[root@Demo-Server a]# tree
.
├── a
│   └── 1.txt
├── desktop -> /root/a
└── text1.txt

1 directory, 3 files
[root@Demo-Server a]# tree -h
.
├── [  19]  a
│   └── [   0]  1.txt
├── [   7]  desktop -> /root/a
└── [ 126]  text1.txt

1 directory, 3 files
[root@Demo-Server a]# tree -L 2
.
├── a
│   └── 1.txt
├── desktop -> /root/a
└── text1.txt

1 directory, 3 files
[root@Demo-Server a]# tree -Lh 2
.
├── [  19]  a
│   └── [   0]  1.txt
├── [   7]  desktop -> /root/a
└── [ 126]  text1.txt

1 directory, 3 files
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陆卿之

你的鼓励将是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值