LINUX核心命令实战总结二——文件和目录操作命令一

1、文件和目录操作命令

 1.1pwd:显示当前所在位置

【功能说明】

 Pwd  命令是“print  working  directory

 【语法格式】

pwd [OPTION]... 
pwd [选项]...

 【参数选项】

Pwd 命令参数选项及说明

参数选项

解释说明

-L

Logical首字母缩写,表示显示逻辑路径,取pwd系统环境变量的值,极少用

-P

Physical首字母缩写,表示显示物理路径如果当前目录路径是软连接文件,则会显示软连接文件对应的源文件,极少用

命令pwd   -L echo  $PWD 二者的功能是等价

[root@web01 ~]# echo $PWD 
/root [root@web01 ~]# pwd -L /root [root@web01 ~]#

 【使用范例】

不带任何选项执行pwd 命令

 [root@web01 ~]# pwd #不带任何选项执行pwd 命令 
/root [root@web01 ~]# cd /etc/init.d [root@web01 init.d]# pwd /etc/init.d [root@web01 init.d]#

 对比使用-L-P参数

[root@web01 init.d]# ls -l /etc/init.d  
lrwxrwxrwx. 1 root root 11 7月   5 2016 /etc/init.d -> rc.d/init.d
[root@web01 init.d]# pwd -L
/etc/init.d
[root@web01 init.d]# echo $PWD
/etc/init.d
[root@web01 init.d]# pwd -P         #显示链接对应的源文件的目录路径
/etc/rc.d/init.d
[root@web01 init.d]# 

1.2 cd:切换目录

【功能说明】

 cd 命令是“change  directory

【语法格式】

cd [option] [dir]
cd [选项] [目录]

【选项说明】
cd 命令的参数选项及说明

参数选项

解释说明(带*的为重点)

-P

如果切换的目标目录是一个链接,则会直接切换软连接指向的真正物理目标目录,和pwd命令的-p选项功能类似,该参数不常用

-L

功能与-P相反,如果切换的目标目录是一个软连接,则直接切换到软连接所在的目录,和pwd命令的-L 选项功能类似,该参数常用

-

当只使用“-”选项时,将会从当前目录切换到系统环境变量“OLDPWD”对应值的目录路径,即当前用户上一次所在的目录路径。

~

当只使用“~”选项时,将会从当前目录切换到系统环境变量“HOME”对应值得目录路径,即当前用户的家目录所在的路径

..

当前使用“..”选项时,将会从当前目录切换到当前目录的上一级目录所在路径

【使用范例】

进入目录

[root@web01 ~]# pwd
/root
[root@web01 ~]# cd /usr/local        #切换到/usr/local目录
[root@web01 local]# pwd
/usr/local

切换到当前目录的上一级目录

[root@web01 local]# pwd
/usr/local
[root@web01 local]# cd ..
[root@web01 usr]# pwd
/usr

返回当前用户上一次所在的目录

[root@web01 usr]# pwd
/usr
[root@web01 usr]# cd -
/usr/local
[root@web01 local]# 

进入当前用户的家目录

[root@web01 local]# pwd
/usr/local
[root@web01 local]# cd ~
[root@web01 ~]# pwd
/root

1.3tree:以树形结构显示目录下的内容

【功能说明】

tree  命令中文“树”的意思。

【语法格式】

 tree  [option]  [directory] 
 tree  [选项]    [目录] 

【选项说明】

Tree 命令的参数选项及说明

参数选项

解释说明(带*的为重点)

-a

显示所有文件,包括隐藏文件(以“.”开头的文件)

-d

只显示目录(*

-f

显示每个文件的全路径

-i

不显示树枝,常与-f参数配合使用

-L  level

遍历目录的最大层数,level为大于0的正数(*

-F

在执行文件、目录、Socket、符号链接、管道名称等不同类型文件的结尾,各自加上“*”、“/”、“=”、“@”、“|”号,类似ls命令的-F选项

【使用范例】

安装查看安装版本

[root@client ~]# rpm -qa tree
tree-1.5.3-3.el6.x86_64
[root@client ~]# yun -y install tree

解决乱码问题

[root@client ~]# tree /boot      
/boot
|-- System.map-2.6.32-504.el6.x86_64
|-- config-2.6.32-504.el6.x86_64
|-- efi
|   `-- EFI
|       `-- redhat
|           `-- grub.efi
[root@client ~]# LANG=en_US_UTF-8
[root@client ~]# tree /boot
/boot
├── config-2.6.32-504.el6.x86_64
├── efi
│   └── EFI
│       └── redhat
│           └── grub.efi

不带任何参数

[root@web01 ~]# tree
.
├── aa
├── anaconda-ks.cfg
├── eth0 -> /etc/sysconfig/network-scripts/ifcfg-eth0
├── eth1 -> /etc/sysconfig/network-scripts/ifcfg-eth1
├── install.log
└── install.log.syslog
0 directories, 6 files

以树型结构显示目录下的所有内容

[root@web01 ~]# tree -a
.
├── aa
├── anaconda-ks.cfg
├── .bash_history
├── .bash_logout
├── .bash_profile
├── .bashrc
├── .cshrc
├── eth0 -> /etc/sysconfig/network-scripts/ifcfg-eth0
├── eth1 -> /etc/sysconfig/network-scripts/ifcfg-eth1
├── install.log
├── install.log.syslog
├── .tcshrc
└── .viminfo
0 directories, 13 files

只列出根目录下第一层目录结构

[root@web01 ~]# tree -L 1 /
/
├── app
├── backup
├── bin
├── boot
省略.......若干......
├── server
├── srv
├── sys
├── tmp
├── usr
└── var
26 directories, 0 files

只显示所有的目录(不显示文件和其他文件类型)

[root@web01 ~]# tree -d /etc/
/etc/
├── abrt
│   └── plugins
├── acpi
│   ├── actions
│   └── events
├── alsa
├── alternatives
248 directories

-f 选项和-i 选项的使用

使用-f选项课显示完整的路径名称,使用-i选项则不显示树枝部分。

[root@web01 ~]# tree -L 1 -f /boot
/boot
├── /boot/config-2.6.32-504.el6.x86_64
├── /boot/efi
├── /boot/grub
├── /boot/initramfs-2.6.32-504.el6.x86_64.img
├── /boot/initrd-2.6.32-504.el6.x86_64kdump.img
├── /boot/lost+found
├── /boot/symvers-2.6.32-504.el6.x86_64.gz
├── /boot/System.map-2.6.32-504.el6.x86_64
└── /boot/vmlinuz-2.6.32-504.el6.x86_64
3 directories, 6 files
[root@web01 ~]# tree -L 1 -fi /boot
/boot
/boot/config-2.6.32-504.el6.x86_64
/boot/efi
/boot/grub
/boot/initramfs-2.6.32-504.el6.x86_64.img
/boot/initrd-2.6.32-504.el6.x86_64kdump.img
/boot/lost+found
/boot/symvers-2.6.32-504.el6.x86_64.gz
/boot/System.map-2.6.32-504.el6.x86_64
/boot/vmlinuz-2.6.32-504.el6.x86_64

使用tree命令区分目录和文件的方法

[root@web01 ~]# tree -L 1 -F /boot 
/boot
├── config-2.6.32-504.el6.x86_64
├── efi/
├── grub/
├── initramfs-2.6.32-504.el6.x86_64.img
├── initrd-2.6.32-504.el6.x86_64kdump.img
├── lost+found/
├── symvers-2.6.32-504.el6.x86_64.gz
├── System.map-2.6.32-504.el6.x86_64
└── vmlinuz-2.6.32-504.el6.x86_64*

3 directories, 6 files
[root@web01 ~]# tree -L 1 -F /boot/|grep /$
/boot/
├── efi/
├── grub/
├── lost+found/

1.4mkdir:创建目录

【功能说明】

Mkdir 命令是“make dirctories

【语法格式】

mkdir     [OPTION]...     [DIRECTORY]...
mkdir     [选项]...        [ 目录]...

【选项说明】

Mkdir命令的参数选项和说明

参数选项

解释说明(带*的为重点)

-p

递归创建目录,递归的意思是父目录及其子目录的子目录(*

-m

设置新创建的默认目录对应的权限

-v

显示创建目录的过程

【使用范例】

Mkdir 命令创建目录

[root@web01 ~]# tree -d
.
0 directories
[root@web01 ~]# mkdir data
[root@web01 ~]# tree -d   
.
└── data
1 directory
[root@web01 ~]# 

使用-p参数递归创建目录

[root@web01 ~]# mkdir  /hh/han   
mkdir: 无法创建目录"/hh/han": 没有那个文件或目录
[root@web01 ~]# mkdir -p yhh/han 
[root@web01 ~]# tree -d
.
├── data
└── yhh
    └── han
3 directories

-v参数显示创建目录过程

[root@web01 ~]# mkdir -pv yan/yui/huihuang
mkdir: 已创建目录 "yan"
mkdir: 已创建目录 "yan/yui"
mkdir: 已创建目录 "yan/yui/huihuang"

创建目录时可使用-m参数设置目录的默认权限

[root@web01 ~]# mkdir dir1
[root@web01 ~]# ls -ld dir1
drwxr-xr-x 2 root root 4096 1月  18 12:17 dir1
[root@web01 ~]# mkdir -m 333 dir2
[root@web01 ~]# ls -ld dir2
d-wx-wx-wx 2 root root 4096 1月  18 12:18 dir2

【技巧性范例】

同时创建多个目录及其多级子目录

[root@web01 ~]# mkdir -pv yhh/{dir_1,dir_2}/{dir_2.1,dir_2.2}
mkdir: 已创建目录 "yhh/dir_1"
mkdir: 已创建目录 "yhh/dir_1/dir_2.1"
mkdir: 已创建目录 "yhh/dir_1/dir_2.2"
mkdir: 已创建目录 "yhh/dir_2"
mkdir: 已创建目录 "yhh/dir_2/dir_2.1"
mkdir: 已创建目录 "yhh/dir_2/dir_2.2"
[root@web01 ~]# tree -d yhh
yhh
├── dir_1
│   ├── dir_2.1
│   └── dir_2.2
├── dir_2
│   ├── dir_2.1
│   └── dir_2.2
└── han
7 directories
[root@web01 ~]# mkdir -p test/dir{1..5} test1/{a..g}
[root@web01 ~]# tree -d test test1
test
├── dir1
├── dir2
├── dir3
├── dir4
└── dir5
test1
├── a
├── b
├── c
├── d
├── e
├── f
└── g
12 directories
View Code

1.5touch:创建空文件或改变文件的时间戳属性

【功能说明】

touch命令有两个功能:一是创建新的空文件,二是改变已有文件的时间戳属性。

【语法格式】

touch  [OPTION]... [FILE]...
touch  [选项]... [文件]...

【选项说明】

touch命令参数选项及说明

参数选项

解释说明(带*的为重点)

-a

只更改指定文件的最后访问时间

-d STRING

使用字符串STRING代表的时间作为模板设置指定文件的时间属性

-m

只更改指定文件的最后修改时间

-r file

将指定文件的时间属性设置为与模板文件file的时间属性相同

-t STAMP

使用[[CC]YY]MMDDhhmm[.ss]格式的时间设置文件的时间属性。格式的含义从左到右依次为:世纪、年、月、日、时、分、秒

 【使用范例】

创建文件(文件不存在)

[root@web01 test]# touch yhh.txt

[root@web01 test]# ls

yhh.txt

[root@web01 test]# touch a.txt b.txt

[root@web01 test]# ls

a.txt  b.txt  yhh.txt

[root@web01 test]# touch stu{01..05}

[root@web01 test]# ls

a.txt  b.txt  stu01  stu02  stu03  stu04  stu05  yhh.txt
View Code

更改文件的时间戳属性

[root@web01 test]# stat yhh.txt

  File: "yhh.txt"

  Size: 0               Blocks: 0          IO Block: 4096   普通空文件

Device: fd00h/64768d    Inode: 1703979     Links: 1

Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2018-01-18 14:41:46.865161519 +0800

Modify: 2018-01-18 14:41:46.865161519 +0800

Change: 2018-01-18 14:41:46.865161519 +0800

[root@web01 test]# touch -a yhh.txt

[root@web01 test]# stat yhh.txt    

  File: "yhh.txt"

  Size: 0               Blocks: 0          IO Block: 4096   普通空文件

Device: fd00h/64768d    Inode: 1703979     Links: 1

Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2018-01-18 14:51:17.706155181 +0800

Modify: 2018-01-18 14:41:46.865161519 +0800

Change: 2018-01-18 14:51:17.706155181 +0800

[root@web01 test]# touch -m yhh.txt

[root@web01 test]# stat yhh.txt    

  File: "yhh.txt"

  Size: 0               Blocks: 0          IO Block: 4096   普通空文件

Device: fd00h/64768d    Inode: 1703979     Links: 1

Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2018-01-18 14:51:17.706155181 +0800

Modify: 2018-01-18 14:51:54.638152169 +0800

Change: 2018-01-18 14:51:54.638152169 +0800
View Code

指定时间属性创建/修改文件(指定选项-d指定创建文件后的文件修改时间)

[root@web01 test]# ls -lh yhh.txt

-rw-r--r-- 1 root root 0 1月  18 14:51 yhh.txt

[root@web01 test]# touch -d 20201001 yhh.txt

[root@web01 test]# ls -lh yhh.txt           

-rw-r--r-- 1 root root 0 10月  1 2020 yhh.txt

利用选项-r,修改yhh.txt的时间属性,使其和a.txt的时间属性一致

[root@web01 test]# ls -lh yhh.txt

-rw-r--r-- 1 root root 0 10月  1 2020 yhh.txt

[root@web01 test]# touch a.txt yhh.txt

[root@web01 test]# ls -lh yhh.txt

-rw-r--r-- 1 root root 0 1月  18 14:59 yhh.txt
View Code

还可以利用选项-t ,将文件设置201712312234.50时间格式

[root@web01 test]# touch -t 201712312234.50 yhh.txt
[root@web01 test]# ls -lh yhh.txt
-rw-r--r-- 1 root root 0 12月 31 22:34 yhh.txt

【扩展知识】

Linux文件有3钟类型的时间戳:

Access: 2018-01-18 14:51:17.706155181 +0800 #最后访问文件的时间

Modify: 2018-01-18 14:51:54.638152169 +0800 #最后修改文件的时间

Change: 2018-01-18 14:51:54.638152169 +0800 #最后改变文件的时间

对应ls 命令查看上述时间选项如下:

[root@web01 test]# ls -lt yhh.txt #修改文件内容,文件的修改时间会改变
-rw-r--r-- 1 root root 0 12月 31 22:34 yhh.txt
[root@web01 test]# ls -lc yhh.txt #修改文件内容,移动文件或改变文件属性等
-rw-r--r-- 1 root root 0 1月  18 15:02 yhh.txt
[root@web01 test]# ls -lu yhh.txt #查看文件内容时,文件的访问时间会改变
-rw-r--r-- 1 root root 0 12月 31 22:34 yhh.txt

1.6ls:显示目录下的内容及相关属性信息

【功能说明】

ls 命令可以理解“list”缩写。

【语法格式】

ls         [OPTION]...     [FILE]...
ls         [选项]...         [文件]...

【选项说明】

ls命令的参数选项及说明

参数选项

解释说明(带*的为重点)

-l

使用长格式列出文件及目录信息

-a

显示目录下的所有文件,包括以“.”字符开始的隐藏文件(*

-t

根据最后的修改时间(mtime)排序,默认是以文件名排序

-r

以相反次序排列

-F

在条目加上文件类型的指示符(*/=@|、其中的一个)(*

-p

只在目录后面加上“/

-i

显示inode节点信息

-d

当遇到目录时,列出目录本身而非目内容的文件,并且不跟随符号链接(*

-h

以人类可读的信息显示文件或目录大小。

-A

列出所有文件,包括隐藏文件,但不包括“.”、“..”这两个目录

-S

根据文件大小排序

-R

递归列出所有子目录

-x

逐行列出项目而不是逐栏列出

-X

根据扩展名排序

-c

根据状态改变时间(ctime)排序

-u

根据最后访问时间(atime)排序

--color={never,always,auto}

不同的文件类型显示不同的颜色参数,never表示不显示,always表示总是显示,auto表示自动显示

-full-time

以完整的时间格式输出

--time-style={full-iso,long-iso,locale}

以不同的格式输出,long-iso效果最好

--time={atime,ctime}

按照不同的时间属性输出,atime表示按访问时间,ctime 表示按改变权限属性时间,如果不加此参数,则默认为最后修改时间

【使用范例】

实例前做好一些准备

[root@web01 ~]# mkdir /test
[root@web01 ~]# cd /test
[root@web01 test]# touch file1.txt file2.txt file3.txt
[root@web01 test]# mkdir dir1 dir2 dir3
[root@web01 test]# tree
.
├── dir1
├── dir2
├── dir3
├── file1.txt
├── file2.txt
└── file3.txt
3 directories, 3 files
View Code

基础范例

直接执行ls命令,不带任何参数

[root@web01 test]# ls
dir1  dir2  dir3  file1.txt  file2.txt  file3.txt

使用-a参数显示所有文件,特别是隐藏文件

[root@web01 test]# ls -a
.  ..  dir1  dir2  dir3  file1.txt  file2.txt  file3.txt  .file4.txt

使用-l参数显示详细信息

[root@web01 test]# ls -l
总用量 12
drwxr-xr-x 2 root root 4096 1月  18 15:48 dir1
drwxr-xr-x 2 root root 4096 1月  18 15:48 dir2
drwxr-xr-x 2 root root 4096 1月  18 15:48 dir3
-rw-r--r-- 1 root root    0 1月  18 15:48 file1.txt
-rw-r--r-- 1 root root    0 1月  18 15:48 file2.txt
-rw-r--r-- 1 root root    0 1月  18 15:48 file3.txt
View Code

显示完整时间属性的参数--time-style=long-iso

[root@web01 test]# ls -l --time-style=long-iso
总用量 12
drwxr-xr-x 2 root root 4096 2018-01-18 15:48 dir1
drwxr-xr-x 2 root root 4096 2018-01-18 15:48 dir2
drwxr-xr-x 2 root root 4096 2018-01-18 15:48 dir3
-rw-r--r-- 1 root root    0 2018-01-18 15:48 file1.txt
-rw-r--r-- 1 root root    0 2018-01-18 15:48 file2.txt
-rw-r--r-- 1 root root    0 2018-01-18 15:48 file3.txt
View Code

执行ls命令,带显示内容的访问时间属性的参数

[root@web01 test]# stat file1.txt
  File: "file1.txt"
  Size: 0               Blocks: 0          IO Block: 4096   普通空文件
Device: fd00h/64768d    Inode: 262147      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-01-18 15:48:38.247743143 +0800
Modify: 2018-01-18 15:48:38.247743143 +0800
Change: 2018-01-18 15:48:38.247743143 +0800
[root@web01 test]# date
2018年 01月 18日 星期四 16:04:59 CST
[root@web01 test]# cat file1.txt
[root@web01 test]# stat file1.txt
  File: "file1.txt"
  Size: 0               Blocks: 0          IO Block: 4096   普通空文件
Device: fd00h/64768d    Inode: 262147      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-01-18 16:05:17.699956221 +0800
Modify: 2018-01-18 15:48:38.247743143 +0800
Change: 2018-01-18 15:48:38.247743143 +0800
[root@web01 test]# ls -l --time-style=long-iso --time=atime   
总用量 12
drwxr-xr-x 2 root root 4096 2018-01-18 15:48 dir1
drwxr-xr-x 2 root root 4096 2018-01-18 15:48 dir2
drwxr-xr-x 2 root root 4096 2018-01-18 15:48 dir3
-rw-r--r-- 1 root root    0 2018-01-18 16:05 file1.txt
-rw-r--r-- 1 root root    0 2018-01-18 15:48 file2.txt
-rw-r--r-- 1 root root    0 2018-01-18 15:48 file3.txt
[root@web01 test]# ls -l --time-style=long-iso 
总用量 12
drwxr-xr-x 2 root root 4096 2018-01-18 15:48 dir1
drwxr-xr-x 2 root root 4096 2018-01-18 15:48 dir2
drwxr-xr-x 2 root root 4096 2018-01-18 15:48 dir3
-rw-r--r-- 1 root root    0 2018-01-18 15:48 file1.txt
-rw-r--r-- 1 root root    0 2018-01-18 15:48 file2.txt
-rw-r--r-- 1 root root    0 2018-01-18 15:48 file3.txt
View Code

执行ls命令,带-F参数(这一点与tree命令的-F很类似)

[root@web01 test]# ls -F
dir1/  dir2/  dir3/  file1.txt  file2.txt  file3.txt
[root@web01 test]# ls -F|grep /
dir1/
dir2/
dir3/

使用-d参数只显示目录本身的信息

[root@web01 test]# ls -l dir1
总用量 0
[root@web01 test]# ls -ld dir1
drwxr-xr-x 2 root root 4096 1月  18 15:48 dir1

使用-R参数递归查看目录

[root@web01 test]# mkdir dir1/sub1/test -p
[root@web01 test]# ls -R dir1
dir1:
sub1
dir1/sub1:
test
dir1/sub1/test:
View Code

【技巧性示范】

使用ls命令别名的相关知识及设置ls别名

[root@web01 test]# alias lst='ls -l --time-style=long-iso'
[root@web01 test]# alias |grep lst
alias lst='ls -l --time-style=long-iso'
[root@web01 test]# lst
总用量 12
drwxr-xr-x 3 root root 4096 2018-01-18 16:29 dir1
drwxr-xr-x 2 root root 4096 2018-01-18 15:48 dir2
drwxr-xr-x 2 root root 4096 2018-01-18 15:48 dir3
-rw-r--r-- 1 root root    0 2018-01-18 15:48 file1.txt
-rw-r--r-- 1 root root    0 2018-01-18 15:48 file2.txt
-rw-r--r-- 1 root root    0 2018-01-18 15:48 file3.txt
View Code

查找最近更新过的文件

[root@web01 ~]# ls -lrt /etc|tail -1
-rw-r--r--   1 root root      0 1月  18 16:35 test.txt

【生产案例】

生产场景数据库备份,获取数据库名表

[root@web01 test]# ls -lhi
总用量 12K
262150 drwxr-xr-x 3 root root 4.0K 1月  18 16:29 dir1
262151 drwxr-xr-x 2 root root 4.0K 1月  18 15:48 dir2
262152 drwxr-xr-x 2 root root 4.0K 1月  18 15:48 dir3
262147 -rw-r--r-- 1 root root    0 1月  18 15:48 file1.txt
262148 -rw-r--r-- 1 root root    0 1月  18 15:48 file2.txt
262149 -rw-r--r-- 1 root root    0 1月  18 15:48 file3.txt
View Code

第一列:inode索引节点编号

第二列:文件类型及权限

第三列:硬链接个数

第四列:文件或目录所属的用户

第五列:文件或目录所属的组

第六列:文件或目录的大小

第七、八、九列:文件或目录的修改时间

第十列:实际的文件或目录名

1.7cp:复制文件或目录

【功能说明】

cp命令可以理解为英文单词copy缩写。

【语法格式】

 cp         [OPTION]...     [SOURCE]...     [DIRECTORY]
 cp         [选项]...         [源文件]...     [目标文件]

【选项说明】

参数选项

解释说明(带*的为重点)

-p

复制文件保持源文件的所有者、权限信息及时间属性

-d

如果复制的源文件的符号链接,那么复制符号链接本身,而且保留符合链接所指向的目标文件或目录

-r

递归复制目录,即复制目录下的所有层级的子目录及文件

-a

等同于上面的pdr3各参数的功能总和

-i

覆盖已有文件前提示用户确认

-t

默认情况下命令格式是“cp”源文件,目标文件,使用用-t 可以颠倒顺序,格式变为“cp -t”目标文件 源文件

【使用范例】

[root@web01 test]# pwd
/test
[root@web01 test]# ll -h
总用量 12K
drwxr-xr-x 3 root root 4.0K 1月  18 16:29 dir1
drwxr-xr-x 2 root root 4.0K 1月  18 15:48 dir2
drwxr-xr-x 2 root root 4.0K 1月  18 15:48 dir3
-rw-r--r-- 1 root root    0 1月  18 15:48 file1.txt
-rw-r--r-- 1 root root    0 1月  18 15:48 file2.txt
-rw-r--r-- 1 root root    0 1月  18 15:48 file3.txt
[root@web01 test]# cp file1.txt file4.txt
[root@web01 test]# cp -a file1.txt file5.txt
[root@web01 test]# ll -h
总用量 12K
drwxr-xr-x 3 root root 4.0K 1月  18 16:29 dir1
drwxr-xr-x 2 root root 4.0K 1月  18 15:48 dir2
drwxr-xr-x 2 root root 4.0K 1月  18 15:48 dir3
-rw-r--r-- 1 root root    0 1月  18 15:48 file1.txt
-rw-r--r-- 1 root root    0 1月  18 15:48 file2.txt
-rw-r--r-- 1 root root    0 1月  18 15:48 file3.txt
-rw-r--r-- 1 root root    0 1月  18 17:17 file4.txt
-rw-r--r-- 1 root root    0 1月  18 15:48 file5.txt
View Code

使用-i参数的例子

[root@web01 test]# cp -i file1.txt file5.txt
cp:是否覆盖"file5.txt"? y
[root@web01 test]# alias cp
alias cp='cp -i'

使用-r参数复制目录

[root@web01 test]# cp dir1 dir4
cp: 略过目录"dir1"
[root@web01 test]# cp dir1 dir4 -r
[root@web01 test]# ls
dir1  dir2  dir3  dir4  file1.txt  file2.txt  file3.txt  file4.txt  file5.txt

【技巧性范例】

命令cp覆盖文件之前不提示是否覆盖的几种方法

[root@web01 test]# /bin/cp file1.txt file4.txt            #全路径
[root@web01 test]# cp file1.txt file4.txt             #命令开头用反斜线
[root@web01 test]# unalias cp                    #重启会失效
[root@web01 test]# cat ~/.bashrc 
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'            #删除这个
alias mv='mv -i'   

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi
View Code

快速备份文件

[root@web01 test]# cp /etc/ssh/ssh
ssh_config            ssh_host_dsa_key      ssh_host_key          ssh_host_rsa_key      
sshd_config           ssh_host_dsa_key.pub  ssh_host_key.pub      ssh_host_rsa_key.pub  
[root@web01 test]# cp /etc/ssh/sshd_config{,.org}
[root@web01 test]# ls -l /etc/ssh/sshd_config{,.org}
-rw------- 1 root root 3877 12月  5 10:47 /etc/ssh/sshd_config
-rw------- 1 root root 3877 1月  18 21:21 /etc/ssh/sshd_config.org

1.8 mv 移动或重命名文件

【功能说明】

Mv 命令可以理解为英文“move”的缩写。

【语法格式】

 mv         [OPTION]...  [SOURCE ]     [DEST]
 mv         [命令]...       [ 源文件]    [目标文件]

【选项说明】

Mv 命令的参数选项及说明

参数选项

解释说明(带*的为重点)

-f

若目标文件存在,则不会询问而是直接覆盖

-i

若目标文件存在,则会询问是否直接覆盖

-n

不覆盖已经存在文件

-t

指定mv的目标目录,适用于移动多个源文件到一个目录的情况,此时目标目录在前,源文件在后,和cp命令的-t选项功能一致。

-u

在源文件比目标文件新,或者目标文件不存在时才会进行移动

【使用范例】

给文件改名字:

[root@web01 test]# ls
dir1  dir2  dir3  dir4  file1.txt  file2.txt  file3.txt  file4.txt  file5.txt
[root@web01 test]# mv file5.txt file6.txt
[root@web01 test]# ls
dir1  dir2  dir3  dir4  file1.txt  file2.txt  file3.txt  file4.txt  file6.txt
[root@web01 test]# mv file4.txt file6.txt  
mv:是否覆盖"file6.txt"? y
[root@web01 test]# alias mv
alias mv='mv -i'
[root@web01 test]# ls
dir1  dir2  dir3  dir4  file1.txt  file2.txt  file3.txt  file6.txt
[root@web01 test]# mv file1.txt file6.txt 
[root@web01 test]# ls
dir1  dir2  dir3  dir4  file2.txt  file3.txt  file6.txt
View Code

移动文件:

[root@web01 test]# ls dir1
sub1
[root@web01 test]# mv file6.txt dir/ 
mv: 无法将"file6.txt" 移动至"dir/": 不是目录
[root@web01 test]# mv file6.txt dir1/
[root@web01 test]# ls
dir1  dir2  dir3  dir4  file2.txt  file3.txt
[root@web01 test]# ls dir1 
file6.txt  sub1
View Code

移动多个文件:

[root@web01 test]# ls dir1 
file6.txt  sub1
[root@web01 test]# touch yhh{00..05}
[root@web01 test]# mv yhh00 yhh01 yhh03 yhh04 dir1/
[root@web01 test]# ls
dir1  dir2  dir3  dir4  file2.txt  file3.txt  yhh02  yhh05
[root@web01 test]# ls dir1
file6.txt  sub1  yhh00  yhh01  yhh03  yhh04

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

[root@web01 test]# mv dir1/ yhh02 yhh05
mv: 目标"yhh05" 不是目录
[root@web01 test]# mv -t  dir1/ yhh02 yhh05
View Code

移动目录的例子:

[root@web01 test]# ls dir1
file6.txt  sub1  yhh00  yhh01  yhh02  yhh03  yhh04  yhh05
[root@web01 test]# mv dir1 dir5
[root@web01 test]# ls dir5
file6.txt  sub1  yhh00  yhh01  yhh02  yhh03  yhh04  yhh05
[root@web01 test]# mv dir3 dir5
[root@web01 test]# ls dir5
dir3  file6.txt  sub1  yhh00  yhh01  yhh02  yhh03  yhh04  yhh05
View Code

命令mv 小总结:

源文件

目标文件

结果

一个普通文件A

目录B

文件A移动到目录B

多个文件A1A2......

目录B

在目录B下有文件A1A2......,有两种写法

一个普通文件A

普通文件C

将文件A重命名为文件C,如果文件C已经存在则会提醒是否覆盖。

多个普通文件A1A2.....

目录B

若目录B不会存在,则将目录D改名为B;若目录B存在,则将D移动到B

一个目录D

目录B

如目录B不存在,则将目录D改成为B;若目录B存在,则将D移动到B中。

多个目录D1D2......

目录B

如果目录B不存在则会报错;若目录B存在则将D1D2.....移动到目录B中。

一个目录D

普通文件C

报错,提示不能将目录复制成文件

多个目录D1D2......

普通文件C

报错,提示不能将目录复制成文件

1.9 rm:删除文件或目录

【功能说明】

命令rm可以理解为英文单词remove的缩写。

【语法格式】

rm [OPTION]... [FILE]...
rm [选项]...            [文件或目录]...

【选项说明】

命令rm选项说明

参数选项

解释说明(带*的为重点)

-f

强制删除,忽略不存在文件,不提示确认(*

-i

在删除前需要确认

-I

在删除超过三个文件或者递归删除前要确认

-r

递归删除目录及内容(*

【使用范例】

[root@web01 ~]# mkdir -p /data/{dir1,dir2,dir3}
[root@web01 ~]# touch /data/{file1.txt,file2.txt,file3.txt}
[root@web01 ~]# tree /data
/data
├── dir1
├── dir2
├── dir3
├── file1.txt
├── file2.txt
└── file3.txt
3 directories, 3 files
View Code

不带参数删除

[root@web01 ~]# cd /data
[root@web01 data]# ls
dir1  dir2  dir3  file1.txt  file2.txt  file3.txt
[root@web01 data]# rm file3.txt
rm:是否删除普通空文件 "file3.txt"?n
[root@web01 data]# alias rm
alias rm='rm -i'
View Code

强制删除

[root@web01 data]# rm -f file3.txt
[root@web01 data]# ls
dir1  dir2  dir3  file1.txt  file2.txt

递归删除

[root@web01 data]# mkdir -p dir1/a/b
[root@web01 data]# tree dir1
dir1
└── a
    └── b

2 directories, 0 files
[root@web01 data]# rm dir1
rm: 无法删除"dir1": 是一个目录
[root@web01 data]# rm  -r dir1
rm:是否进入目录"dir1"? y
rm:是否进入目录"dir1/a"? y
rm:是否删除目录 "dir1"?y
rm: 无法删除"dir1": 目录非空
View Code

【删除的实战经验】

1、mv替代rm,不要着急删除,而是先移到回收站/tmp.

2、删除前务必备份,最好是异机备份,若出现问题随时可以回滚。

3、如果非要删除,那么请用find替代rm,包括通过系统定时任务等清理文件方法。

4、如果非要通过rm删除,那先切换到当前目录再删除,能不用通配符的就不用通配符。对文件的删除禁止使用“rm   -rf 文件名”,因为“rm   -rf  ” 误删目录时并不会有提示,非常危险。最多使用“rm  -rf  文件名”,推荐用“rm    文件名”。

5、慎用rsync  --delete

1.10 rmdir:删除空目录

【功能说明】

rmdir  命令用于删除空目录(remove  empty  directiories)当目录不为空时,命令不起作用。

【语法格式】

rmdir     [OPTION]...     [DIRECTORY]...
rmdir     [选项]...         [目录]...

【选项说明】

命令rmdir的参数选项及说明

参数选项

解释说明(带*的为重点)

-p

递归删除目录,当子目录删除后其父目录为空时,也一并删除。如果整个路径被删除或者由于某种原因保留部分路径,则系统在标准输出上显示相应的信息。

-v

显示命令的执行过程

【使用范例】

[root@web01 data]# tree dir1
dir1
└── a

1 directory, 0 files
[root@web01 data]# rmdir dir1
rmdir: 删除 "dir1" 失败: 目录非空

参数-p 递归删空目录

[root@web01 data]# rmdir -pv dir1
rmdir: 正在删除目录 "dir1"
rmdir: 删除 "dir1" 失败: 目录非空
[root@web01 data]# rmdir -pv dir1/a
rmdir: 正在删除目录 "dir1/a"
rmdir: 正在删除目录 "dir1"
原文地址:https://www.cnblogs.com/huihuangyan/p/13650484.html