0
点赞
收藏
分享

微信扫一扫

Linux 命令 (文件和目录操作命令)

心智的年轮 2022-04-21 阅读 135
linux

Linux 命令 (文件和目录操作命令)

1,pwd:显示当前所在的位置

[root@oldboy ~]# echo $PWD  #<==echo命令能够输出指定变量,具体用法见本书echo命令章节。
/root
[root@oldboy ~]# pwd -L    #<==显示逻辑(忽略软链接文件)路径。
/root

2,cd:切换目录
3,tree :以树形结构显示目录下的内容
语法格式:
tree 选项 目录
安装tree命令

[root@oldboy ~]# rpm -qa tree            #<==查询tree命令是否安装。
tree-1.5.3-2.el6.x86_64                  #<==如果没有显示就执行下面的命令。
[root@oldboy ~]# yum -y install tree      #<==安装tree命令的yum命令。

调整系统字符集

[root@oldboy ~]# tree /boot/
/boot/
├── config-2.6.32-573.el6.x86_64
├── efi
│?? └── EFI
│??     └── redhat
│??         └── grub.efi
[root@oldboy ~]# LANG=en_US.UTF-8

在这里插入图片描述

4,mkdir:创建目录
语法格式:
mkdir 选项 目录

在这里插入图片描述

5,touch:创建空文件或改变文件的时间戳属性
toch 选项 文件
在这里插入图片描述
例1,更改文件的时间戳属性。

[root@oldboy ~]# stat oldboy.txt #<==stat命令可以查看文件的时间戳属性,具体用法见stat
                                 命令讲解。
  File: 'oldboy.txt'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 802h/2050d      Inode: 272247      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2015-07-30 17:37:32.295105308 +0800
Modify: 2015-07-30 17:37:32.295105308 +0800
Change: 2015-07-30 17:37:32.295105308 +0800
#<==说明:文件的时间戳属性分为访问时间、修改时间、状态改变时间。
[root@oldboy ~]# touch -a oldboy.txt #<==-a参数更改最后访问的时间。
[root@oldboy ~]# stat oldboy.txt
  File: 'oldboy.txt'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 802h/2050d      Inode: 272247      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2015-07-30 17:48:20.502156890 +0800
Modify: 2015-07-30 17:37:32.295105308 +0800
Change: 2015-07-30 17:48:20.502156890 +0800
[root@oldboy ~]# touch -m oldboy.txt  #<==-m参数更改最后修改的时间。
[root@oldboy ~]# stat oldboy.txt
  File: 'oldboy.txt
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 802h/2050d      Inode: 272247      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2015-07-30 17:48:20.502156890 +0800
Modify: 2015-07-30 17:48:45.006106223 +0800
Change: 2015-07-30 17:48:45.006106223 +0800

例2,指定时间属性创建/修改文件。
可利用选项-d指定创建文件后的文件修改时间:

[root@oldboy ~]# ls -lh oldboy.txt
-rw-r--r-- 1 root root 0 Oct 23 20:20 oldboy.txt      #<==修改前的文件修改时间10

                                                   月23日。
[root@oldboy ~]# touch -d 20201001 oldboy.txt            #<==指定创建文件后的文件修改

                                                   时间为2020年10月01日。
[root@oldboy ~]# ls -lh oldboy.txt
-rw-r--r-- 1 root root 0 Oct  1  2020 oldboy.txt      #<==文件修改时间已改为2020                                                   年10月01日。
也可利用选项-r,修改oldboy.txt的时间属性,使其和a.txt的时间属性一致:

[root@oldboy ~]# ls -lh a.txt
-rw-r--r-- 1 root root 0 Oct 23 20:20 a.txt            #<==查看a.txt的修改时间。
[root@oldboy ~]# touch -r a.txt oldboy.txt            #<==使用-r参数让oldboy.txt
                                                    的时间属性和a.txt一致。
[root@oldboy ~]# ls -lh oldboy.txt
-rw-r--r-- 1 root root 0 Oct 23 20:20 oldboy.txt      #<==oldboy.txt文件的修改
                                                    时间已和a.txt一致了。
还可以利用选项-t,将文件设置为201512312234.50时间格式:

[root@oldboy ~]# touch -t 201512312234.50 oldboy.txt
[root@oldboy ~]# ls -lh --full-time oldboy.txt
-rw-r--r-- 1 root root 0 2015-12-31 22:34:50.000000000 +0800 oldboy.txt      #<==查看设置的属性。

GNU/Linux的文件有3种类型的时间戳:

Access: 2015-07-30 17:48:20.502156890 +0800 #<==最后访问文件的时间。
Modify: 2015-07-30 17:48:45.006106223 +0800 #<==最后修改文件的时间。
Change: 2015-07-30 17:48:45.006106223 +0800 #<==最后改变文件状态的时间。
对应ls命令,查看上述时间戳的选项如下:

mtime: 最后修改时间(ls -lt) #<==修改文件内容,文件的修改时间(modify time)会改变。
ctime: 状态改变时间(ls -lc) #<==修改文件内容、移动文件或改变文件属性等,文件的change时间会改变。
atime: 最后访问时间(ls -lu) #<==查看文件内容时,文件的访问时间(access time)会改变。

6,ls:显示目录下的内容及相关属性信息
ls 选项 文件或目录

在这里插入图片描述
7,CP:复制文件或目录
cp 选项 源文件 目标文件
在这里插入图片描述
cp覆盖文件之前不提示是否覆盖的几种方法。
解题思路:屏蔽系统默认的对应的命令别名。
第一种方法,使用命令全路径:

[root@oldboy test]# cp file1.txt file2.txt
cp: overwrite 'file2.txt'? y  #<==提示若输入y则确认覆盖,若输入n则不覆盖。
[root@oldboy test]# which cp  #<==查看cp的系统别名。
alias cp='cp -i'
        /bin/cp
[root@oldboy test]# /bin/cp file1.txt file2.txt      #<==使用cp命令的绝对路径,
                                                   就可以屏蔽别名。

第二种方法,命令开头使用反斜线(\):

[root@oldboy test]# \cp file1.txt file2.txt #<==使用“\”屏蔽系统别名。
[root@oldboy test]#

第三种方法,取消cp的别名,但重启系统失效:

[root@oldboy test]# unalias cp #<==alias设置别名,unalias取消别名,具体用法请参见对应
                                章节。
[root@oldboy test]# cp file1.txt file2.txt
[root@oldboy test]#

第四种为杀鸡取卵的方法,用于开拓思路,不建议采用:

[root@oldboy ~]# cat ~/.bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i' #<==编辑这个文件,注释掉本行,重启也是生效的。在开机时会加载.bashrc这
                      个文件。
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

快速备份文件案例。
我们备份文件通常使用如下所示的格式:

[root@oldboy ~]# cp /etc/ssh/sshd_config /etc/ssh/sshd_config.ori
#<==但观察上面的格式,我们可以发现有一部分路径重复了。因此有了下面的快捷写法,使用了“{}”的用法,这两种方法是等效的。
[root@oldboy ~]# cp /etc/ssh/sshd_config{,.ori}
#<==这种方法的原理是bash对大括号的展开操作,/etc/ssh/sshd_config{,.ori}展开成/etc/ssh/sshd_config /etc/ssh/sshd_config.ori再传给cp命令。

8,mv:移动或重命名文件
mv 选项 源文件 目标文件
在这里插入图片描述

给文件改名的例子。

[root@oldboy test]# ls
dir1  dir3  file1.txt  file3.txt  file5.txt
dir2  dir4  file2.txt  file4.txt  file6.txt
[root@oldboy test]# mv file6.txt file7.txt #<==若file7.txt不存在,则将file6.
txt重命名为file7.txt。
[root@oldboy test]# ls
dir1  dir3  file1.txt  file3.txt  file5.txt
dir2  dir4  file2.txt  file4.txt  file7.txt
[root@oldboy test]# mv file5.txt file7.txt #<==若file7.txt存在,则将file5.txt
                                           覆盖为file7.txt。
mv: overwrite 'file7.txt'? y #<==由于系统默认给mv设置了别名,因此会提示是否覆盖。
[root@oldboy test]# alias mv
alias mv='mv -i' #<==-i参数的功能是若目标文件已经存在,就会询问是否覆盖。
[root@oldboy test]# ls
dir1  dir2  dir3  dir4  file1.txt  file2.txt  file3.txt  file4.txt  file7.txt
[root@oldboy test]# \mv file4.txt file7.txt #<==使用\屏蔽系统别名就不会询问是否覆
                                            盖了。
[root@oldboy test]# ls
dir1  dir2  dir3  dir4  file1.txt  file2.txt  file3.txt  file7.txt
[root@oldboy test]#

移动文件的例子。
移动单个文件:

[root@oldboy test]# ls dir1/
sub1
[root@oldboy test]# mv file7.txt dir1/ #<==dir1为目录且存在,则移动file7.txt到
                                        dir1下,若dir1不存在,则重命名为dir1的
                                        普通文件。
[root@oldboy test]# ls dir1/
file7.txt  sub1

移动多个文件:

[root@oldboy test]# mv file1.txt file2.txt dir1/  #<==第一种方式,多个文件在前,目录
                                                在后。
[root@oldboy test]# ls dir1/
file1.txt  file2.txt  file7.txt  sub1
[root@oldboy test]# mv dir1/file* . #<==还原试验环境,file*匹配所有以file开头的
                                     文件。

将源和目标调换移动到文件目录(-t参数)。

[root@oldboy test]# ls
dir1  dir2  dir3  dir4  file1.txt  file2.txt  file3.txt  file7.txt
[root@oldboy test]# mv -t dir1/ file1.txt  file2.txt  file3.txt  file7.txt #<==使用-t参数将源和目标调换,-t后接目录,最后是要移动的文件。
[root@oldboy test]# ls dir1/
file1.txt  file2.txt  file3.txt  file7.txt  sub1

移动目录的例子。

[root@oldboy test]# ls
dir1  dir2  dir3  dir4
[root@oldboy test]# mv dir1 dir5 #<==目录dir5不存在,将目录dir1改名为dir5。
[root@oldboy test]# ls
dir2  dir3  dir4  dir5
[root@oldboy test]# ls dir5/
file1.txt  file2.txt  file3.txt  file7.txt  sub1
[root@oldboy test]# mv dir2 dir5 #<==目录dir5存在,将dir2移动到dir5中。
[root@oldboy test]# ls dir5/
dir2  file1.txt  file2.txt  file3.txt  file7.txt  sub1
[root@oldboy test]# mv dir3/ dir5/ #<==源目录结尾加“/”不影响结果,但为了规范和简单
                                     起见,不加“/”。


[root@oldboy test]# ls
dir4  dir5
[root@oldboy test]# ls dir5/
dir2  dir3  file1.txt  file2.txt  file3.txt  file7.txt  sub1

关于mv命令的使用小结
表2-11针对mv命令的使用进行了总结。

在这里插入图片描述

举报

相关推荐

0 条评论