linux零起点之—bash
bash的基础特性
bash有很多的基础特性,能辅助我们更好的完成系统管理。
bash的基本工作特性之—- 命令历史
- 命令历史:shell进程会在其会话中保存在此前用户提交执行的命令。
history命令用于查看历史命令。在.bash.history文件中有一些常用的环境变量我们会使用得到,我们用这些环境变量来定制history的功能。
- HISTSIZE : 命令历史的条数(shell 的进程)
- HISTFILE : 持久保存命令历史文件
- HISTFILESIZE : 命令历史文件的大小
命令用法:
[OPTION]:
-c
: clear ;清除历史命令记录。-d offset
: ;(偏移量)删除。-r
:从历史文件中读取命令历史到列表中。-w
:从历史列表中的命令追加至历史文件中。#
: 显示最近的#条命令。
例子:
[root@caibird ~]# history -w #保存历史记录到文件中
[root@caibird ~]# history
1 ls
2 pwd
3 clera
4 clea
。。。。。。中间省略。。。。。。
69 ll `mkdir $(date +%F_%H_%M_%S)`
70 clear
71 history 3
72 history -w
73 history
[root@caibird ~]#
*****************************
[root@caibird ~]# history -c #清除历史记录
[root@caibird ~]# history
1 history
[root@caibird ~]#
*****************************
[root@caibird ~]# history 3 # 显示最近的3条命令
69 ll `mkdir $(date +%F_%H_%M_%S)`
70 clear
71 history 3
[root@caibird ~]#
*****************************
[root@caibird ~]# history -r # 从文件中读取历史记录文件。
[root@caibird ~]# history
1 history
2 history -r
3 ls
4 pwd
5 clera
6 clea
7 clear
8 fdisk -l
9 ls /proc/
10 ls /proc/version
*******中间省略********
67 echo '$PATH'
68 echo “$PATH”
69 mkdir $(date +%F_%H_%M_%S)
70 ll `mkdir $(date +%F_%H_%M_%S)`
71 clear
72 history 3
73 history -w
74 history
[root@caibird ~]#
如何调用命令历史列表中的命令:
使用上,下方向键选择要执行的命令
!#
:再一次执行历史列表中的第#条命令!!
: 再一次执行上一次命令。!STRING:
再一次执行命令历史中最近一个以STRING开始的命令。注意: 命令的重复执行有时候需要依赖于幂等性。
调用上次命令最后一个参数:
ESC .
, 还有!$
,ALT+.
等等.
如何控制命令历史记录的方式:
环境变量:HISTCONTROL=” $STRING ” ;
$STRING
有三种模式:- ignoredups:忽略重复的命令。
- ignorespace:忽略以上空白字符开头的命令。
- ignoreboth :既忽略重复的命令,又忽略以空白字符开头的命令。
- 修改变量的值: NAME=’VALUE’ # 只对当前SHELL有效。
bash的基本工作特性之—-命令补全
- shell程序在接收到用户执行命令的请求,分析完成之后,最左侧字符串会被当作命令。
shell命令查找处理机制
首先查找内部命令,再查找外面命令。如果没有找到,再根据PATH环境变量中设定的目录,自左向右逐个搜索目录下的文件夹。
补全机制:
文件补全:
给定的打头字符串如果能惟一标识某命令程序文件,则直接补全。
如果给定的打头字符的字符串不能惟一标识某命令程序文件,则再击一次Tab键一次,会给出列表。
路径补全:
- 根据给定的起始路径来补全,以对应路径下打头字符串逐一匹配起始路径下的文件。tab键一下,如果能惟一标识,则直接补全。否则,再按一次tab键,给出列表。
目录管理类命令
mkdir命令
- 功能 : 创建目录,make directory–>指定基名(basename)
常用选项
- -p : parent ; 自动按需创建目录,可以创建多层目录
- -v : verbose ; 显示创建的详细过程
- m : mode ; 直接给定权限
注意 :路径基名为命令的作用对角;基名之前的路径必须得存在。
例子:
[root@localhost test]# ls
[root@localhost test]# pwd
/tmp/test
[root@localhost test]# mkdir testdir
[root@localhost test]# ls
testdir
[root@localhost test]# ll
total 0
drwxr-xr-x. 2 root root 6 May 19 13:16 testdir
[root@localhost test]#
*********************************
[root@localhost test]# mkdir -pv ./testdir1/test2/test3/test4 #递归创建目录,并且显示创建过程。
mkdir: created directory ‘./testdir1’
mkdir: created directory ‘./testdir1/test2’
mkdir: created directory ‘./testdir1/test2/test3’
mkdir: created directory ‘./testdir1/test2/test3/test4’
[root@localhost test]# tree ./testdir1
./testdir1
└── test2
└── test3
└── test4
3 directories, 0 files
[root@localhost test]#
rmdir命令
- 格式:rmdir [OPTION]… DIRECTORY…
- 功能:移除一个空目录
- 选项:
- -p : 删除某目录后,如果其父目录为空,则一并删除之
- -v :显示其删除过程
例子:
[root@localhost test]# ll -d ./testdir
drwxr-xr-x. 2 root root 6 May 19 13:16 ./testdir
[root@localhost test]# rmdir ./testdir
[root@localhost test]# ll -d ./testdir
ls: cannot access ./testdir: No such file or directory
[root@localhost test]# ls
testdir1
[root@localhost test]#
****************************
[root@localhost test]# ls -dl testdir1/
drwxr-xr-x. 3 root root 19 May 19 13:18 testdir1/
[root@localhost test]# tree testdir1/
testdir1/
└── test2
└── test3
└── test4
3 directories, 0 files
***************************
[root@localhost test]# rmdir -pv testdir1/ # 从最开始一层删除将会出现错误
rmdir: removing directory, ‘testdir1/’
rmdir: failed to remove ‘testdir1/’: Directory not empty
[root@localhost test]# tree testdir1/
testdir1/
└── test2
└── test3
└── test4
3 directories, 0 files
***************************
[root@localhost test]# rmdir -pv testdir1/test2/test3/test4/ #需要从最下一层的目删起
rmdir: removing directory, ‘testdir1/test2/test3/test4/’
rmdir: removing directory, ‘testdir1/test2/test3’
rmdir: removing directory, ‘testdir1/test2’
rmdir: removing directory, ‘testdir1’
[root@localhost test]#
bash的基本工作特性之—-命令行展开功能
~ : 自动展开为用户的家目录,或指定的用户的家目录。
{,} : 可承载一个以逗号分隔的路径列表,并能够将其展开为多个路径。
{..} :可承载一个以两个点号的一组路径列表,并能够将其展开为一组路径。
例如:
(1)、创建/tmp目录下的:a_c, a_d, b_c, b_d
(2)、创建/tmp/mylinux目录下的:
mylinux/
├── bin
├── boot
│ └── grub
├── dev
├── etc
│ ├── rc.d
│ │ └── init.d
│ └── sysconfig
│ └── network-scripts
├── lib
│ └── modules
├── lib64
├── proc
├── sbin
├── sys
├── tmp
├── usr
│ └── local
│ ├── bin
│ └── sbin
└── var
├── lock
├── log
└── run
例一:
方法一:
[root@localhost tmp]# pwd
/tmp
[root@localhost tmp]# mkdir -pv {a,b}_{c,d} #显示创建过程
mkdir: created directory ‘a_c’
mkdir: created directory ‘a_d’
mkdir: created directory ‘b_c’
mkdir: created directory ‘b_d’
[root@localhost tmp]# ls
a_c
a_d
b_c
b_d
systemd-private-bd1cf1516f5147969237bb758f87b902-vmtoolsd.service-Zy7sqD
[root@localhost tmp]#
*************************************
[root@localhost tmp]# ls
a_c
a_d
b_c
b_d
systemd-private-bd1cf1516f5147969237bb758f87b902-vmtoolsd.service-Zy7sqD
[root@localhost tmp]# rm -rf a* b* #删除以a或者b开关的目录
[root@localhost tmp]# ls
systemd-private-bd1cf1516f5147969237bb758f87b902-vmtoolsd.service-Zy7sqD
[root@localhost tmp]#
方法二:
[root@localhost tmp]# mkdir -pv a_{c,d} b_{c,d}
mkdir: created directory ‘a_c’
mkdir: created directory ‘a_d’
mkdir: created directory ‘b_c’
mkdir: created directory ‘b_d’
[root@localhost tmp]# ll
total 0
drwxr-xr-x. 2 root root 6 May 19 14:02 a_c
drwxr-xr-x. 2 root root 6 May 19 14:02 a_d
drwxr-xr-x. 2 root root 6 May 19 14:02 b_c
drwxr-xr-x. 2 root root 6 May 19 14:02 b_d
drwx------. 3 root root 17 May 12 12:34 systemd-private-bd1cf1516f5147969237bb758f87b902-vmtoolsd.service-Zy7sqD
[root@localhost tmp]#
例二:
[root@localhost ~]# mkdir -pv /tmp/mylinux/{bin,boot/grup,dev,etc{/rc.d/init.d,/sysconfig/network-scripts},lib/modules,lib64,proc,sbin,sys,tmp,usr/local{/bin,/sbin},var{/lock,/log,/run}}
mkdir: created directory ‘/tmp/mylinux’
mkdir: created directory ‘/tmp/mylinux/bin’
mkdir: created directory ‘/tmp/mylinux/boot’
mkdir: created directory ‘/tmp/mylinux/boot/grup’
mkdir: created directory ‘/tmp/mylinux/dev’
mkdir: created directory ‘/tmp/mylinux/etc’
mkdir: created directory ‘/tmp/mylinux/etc/rc.d’
mkdir: created directory ‘/tmp/mylinux/etc/rc.d/init.d’
mkdir: created directory ‘/tmp/mylinux/etc/sysconfig’
mkdir: created directory ‘/tmp/mylinux/etc/sysconfig/network-scripts’
mkdir: created directory ‘/tmp/mylinux/lib’
mkdir: created directory ‘/tmp/mylinux/lib/modules’
mkdir: created directory ‘/tmp/mylinux/lib64’
mkdir: created directory ‘/tmp/mylinux/proc’
mkdir: created directory ‘/tmp/mylinux/sbin’
mkdir: created directory ‘/tmp/mylinux/sys’
mkdir: created directory ‘/tmp/mylinux/tmp’
mkdir: created directory ‘/tmp/mylinux/usr’
mkdir: created directory ‘/tmp/mylinux/usr/local’
mkdir: created directory ‘/tmp/mylinux/usr/local/bin’
mkdir: created directory ‘/tmp/mylinux/usr/local/sbin’
mkdir: created directory ‘/tmp/mylinux/var’
mkdir: created directory ‘/tmp/mylinux/var/lock’
mkdir: created directory ‘/tmp/mylinux/var/log’
mkdir: created directory ‘/tmp/mylinux/var/run’
[root@localhost ~]# tree /tmp/mylinux/
/tmp/mylinux/
├── bin
├── boot
│ └── grup
├── dev
├── etc
│ ├── rc.d
│ │ └── init.d
│ └── sysconfig
│ └── network-scripts
├── lib
│ └── modules
├── lib64
├── proc
├── sbin
├── sys
├── tmp
├── usr
│ └── local
│ ├── bin
│ └── sbin
└── var
├── lock
├── log
└── run
24 directories, 0 files
[root@localhost ~]#
例三
[root@localhost ~]# ls
[root@localhost ~]# mkdir -pv user{1..10} #创建10个目录
mkdir: created directory ‘user1’
mkdir: created directory ‘user2’
mkdir: created directory ‘user3’
mkdir: created directory ‘user4’
mkdir: created directory ‘user5’
mkdir: created directory ‘user6’
mkdir: created directory ‘user7’
mkdir: created directory ‘user8’
mkdir: created directory ‘user9’
mkdir: created directory ‘user10’
[root@localhost ~]# ls -l
total 0
drwxr-xr-x. 2 root root 6 May 19 22:58 user1
drwxr-xr-x. 2 root root 6 May 19 22:58 user10
drwxr-xr-x. 2 root root 6 May 19 22:58 user2
drwxr-xr-x. 2 root root 6 May 19 22:58 user3
drwxr-xr-x. 2 root root 6 May 19 22:58 user4
drwxr-xr-x. 2 root root 6 May 19 22:58 user5
drwxr-xr-x. 2 root root 6 May 19 22:58 user6
drwxr-xr-x. 2 root root 6 May 19 22:58 user7
drwxr-xr-x. 2 root root 6 May 19 22:58 user8
drwxr-xr-x. 2 root root 6 May 19 22:58 user9
[root@localhost ~]#
bash的基本工作特性之—-命令的执行状态结果
命令执行的状态结果:成功或失败.
bash通过状态返回值来输出此结果。
成功
–>用 0 来表示。失败
用1-255值来表示。命令执行完成之后,其状态返回值保存于bash的特殊变量 ++ ?++中,所以我们可以通过获取++ ? + + 中 , 所 以 我 们 可 以 通 过 获 取 + + ?++ 变量值,就知道命令执行后是成要功了还是失败。
- 注意:要获取命令执行状态,在命令执行后,要立即获取状态返回值,
$?
,而不能值得其他命令操作。
例如:
[root@caibird ~]# ll
总用量 40
drwxr-xr-x. 2 root root 4096 5月 16 21:28 a_c
drwxr-xr-x. 2 root root 4096 5月 16 21:28 a_d
-rw-------. 1 root root 961 4月 29 23:14 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4096 5月 16 21:28 b_c
drwxr-xr-x. 2 root root 4096 5月 16 21:28 b_d
-rw-r--r--. 1 root root 13369 4月 29 23:14 install.log
-rw-r--r--. 1 root root 3482 4月 29 23:13 install.log.syslog
[root@caibird ~]# echo $?
0 # 命令执行成功为:0
[root@caibird ~]#
*************************************
root@caibird ~]# la
-bash: la: command not found
[root@caibird ~]# echo $?
127 # 命令执行失败为:127
[root@caibird ~]#
- 命令的执行结果
- 命令正常执行时,有时还会有命令返回值,根据命令及其功能不同,结果各 不相同。
- 引用命令的执行结果:
$(COMMAND)
或者 `COMMAND` 一对单反引号。
例如:
[root@caibird ~]# echo $(date +%F_%T)
2018-05-16_22:19:05
[root@caibird ~]# echo `cat -n /etc/issue`
1 CentOS release 6.9 (Final) 2 Kernel \r on an \m 3
[root@caibird ~]#
bash的基本工作特性之—-引用
bash的基本引用有:
强引用: ’ ’ 一对单引号,功能:原样显示内部字符串,不做任何替换。
弱引用 : “ ” 一对双引号,功能: 变量值引用,会做变量替换。
命令引用: $(COMMAND) 或者 `COMMAND` :引用命令的执行结果。
例如:
[root@caibird ~]# echo '$PATH' #引用`$PATH`变量的值,强引用,原样输出。
$PATH
[root@caibird ~]# echo “$PATH” #引用`$PATH`变量的值,弱引用。
“/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin”
[root@caibird ~]#
****************************
[root@caibird ~]# mkdir $(date +%F_%H_%M_%S) #引用date命令的结果创建目录
[root@caibird ~]# ls
2018-05-16_22_24_11 a_d b_c install.log
a_c anaconda-ks.cfg b_d install.log.syslog
****************************
[root@caibird ~]# ll `mkdir $(date +%F_%H_%M_%S)` #双重引导,首先引导date创建目录 ,再把创建的结果显示出来。
总用量 48
drwxr-xr-x. 2 root root 4096 5月 16 22:24 2018-05-16_22_24_11
drwxr-xr-x. 2 root root 4096 5月 16 22:24 2018-05-16_22_24_49
drwxr-xr-x. 2 root root 4096 5月 16 21:28 a_c
drwxr-xr-x. 2 root root 4096 5月 16 21:28 a_d
-rw-------. 1 root root 961 4月 29 23:14 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4096 5月 16 21:28 b_c
drwxr-xr-x. 2 root root 4096 5月 16 21:28 b_d
-rw-r--r--. 1 root root 13369 4月 29 23:14 install.log
-rw-r--r--. 1 root root 3482 4月 29 23:13 install.log.syslog
[root@caibird ~]#
bash的基本工作特性之—快捷键
- 在bash下操作命令常用的快捷键有如下:
快捷键 | 功能 |
---|---|
CTRL+a | 跳转到命令行的首部 |
CTRL+e | 跳转到命令行的尾部 |
CTRL+u | 删除行首到光标之间的字符 |
CTRL+k | 删除光标到行尾之间的字符 |
CTRL+l | 清除屏幕的内容,同clear命令执行的结果一样 |
常用到的文件管理命令
cat 命令
功能 :显示文件内容到屏幕上,一般显示文件内容比较小文件。
格式 : cat [OPTION]… [FILE]…
选项 :
- -n : 从1开始对输出的文件行进行编号。
- -b :和-n功能相似,只是对空行不进行编号。
- -s :当遇到两个连续的空白行,就代换成一个空白行。
- -A : 显示行尾结束符 $
例子
[root@localhost ~]# cat -n a.txt
1 this is first line
2
3 this is third line
4
5 this is five line
6
7 this is last line
[root@localhost ~]#
**************************************
[root@localhost ~]# cat -b a.txt
1 this is first line
2 this is third line
3 this is five line
4 this is last line
[root@localhost ~]#
***************************************
[root@localhost ~]# cat -A a.txt
this is first line$
$
this is third line$
$
this is five line$
$
this is last line$
[root@localhost ~]#
注意:cat命令还可以向文件尾部添加多行内容,也可以创建一个新的文件。其格式为:
- cat >> filename << Eof ;其中Eof为结束标志,也可以为其他字符串。
例如:
[root@localhost ~]# cat >> a.txt << Eof
> this is first line
>
> this is third line
>
> this is five line
>
> this is last line
> Eof
[root@localhost ~]# cat a.txt
this is first line
this is third line
this is five line
this is last line
tac 命令
功能 :将文件以行为单位逆向输出。即第一行最后输出,最后一行先显示。
格式 : tac [OPTION]… [FILE]…
例子
[root@localhost ~]# cat /etc/issue
\S
Kernel \r on an \m
[root@localhost ~]# tac /etc/issue
Kernel \r on an \m
\S
[root@localhost ~]#
head 命令
功能 :显示文件内容到屏幕上,默认显示文件的前10行。
格式 : head [OPTION]… [FILE]…
选项 :
-n<数字> : 指定显示头部内容的行数。
-<数字> : 同上面功能一样。
例子
[root@localhost ~]# head /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]#
[root@localhost ~]# head -n 3 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@localhost ~]# head -3 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@localhost ~]#
tail 命令
功能 :显示文件尾部的后几行,默认显示10行。
格式 : tail [OPTION]… [FILE]…
选项 :
-n<数字> : 指定显示尾部内容的行数。
-<数字> : 同上面功能一样。
-n : 指定尾部要显示的行数。
-f : 动态显示文件尾部内容,结束后,不退出命令,跟随显示新增的内容。
例子
[root@localhost ~]# tail /etc/passwd
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin
[root@localhost ~]# tail -n 4 /etc/passwd
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin
[root@localhost ~]#
more 命令
more命令是一个基于vi编辑器文本过滤器,它以全屏幕的方式按页显示文本文件的内容,支持vi中的关键字定位操作。more名单中内置了若干快捷键,常用的有H(获得帮助信息),Enter(向下翻滚一行),空格(向下滚动一屏),Q(退出命令)。命令一次显示一屏文本,满屏后停下来,并且在屏幕的底部出现一个提示信息,给出至今己显示的该文件的百分比:–More–(XX%)可以用下列不同的方法对提示做出回答:
- space键:显示文本的下一屏内容。
- 按Enier键:只显示文本的下一行内容。
- 按斜线符/:接着输入一个模式,可以在文本中寻找下一个相匹配的模式。
- 按H键:显示帮助屏,该屏上有相关的帮助信息。
- 按B键:显示上一屏内容。
- 按Q键:退出rnore命令。
功能 : 分页显示文件的内容。
格式 : more [options] file […]
选项 :
- -<数字> :指定每屏显示的行数。
- -d :显示“[press space to continue,’q’ to quit.]”和“[Press ‘h’ for instructions]”。
- -c :不进行滚屏操作。每次刷新这个屏幕。
- -s :将多个空行压缩成一行显示。
- +<数字> :从指定数字的行开始显示。
例子
[root@localhost ~]# more +15 /etc/passwd
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd
daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin
[root@localhost ~]#
注意:more命令:翻屏至文件尾部后会自动退出。
less 命令
less命令的作用与more十分相似,都可以用来浏览文字档案的内容,不同的是less命令允许用户向前或向后浏览文件,而more命令只能向前浏览。用less命令显示文件时,用PageUp键向上翻页,用PageDown键向下翻页。要退出less程序,应按Q键。
功能 : 分页显示文件的内容。
格式 : less [options] file […]
选项 :
- -e :文件内容显示完毕后,自动退出;
- -f :强制显示文件;
- -g :不加亮显示搜索到的所有关键词,仅显示当前显示的关键字,以提高显示速度;
- -i :搜索时忽略大小写的差异;
- -N :每一行行首显示行号;
- -s :将连续多个空行压缩成一行显示;
例子
[root@localhost ~]# less /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
*************************************************
[root@localhost ~]# less -N /etc/passwd
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6 sync:x:5:0:sync:/sbin:/bin/sync
cp 命令 : (copy)
功能 : 复制文件和目录。
格式 :
cp [OPTION] … [-T] SOURCE DESTINATION
cp [OPTION] … SOURCE … DIRECTORY
cp [OPTION] … -t DIRECTORY SOURCE
选项:
- -i : interactive; 交互式复制,即覆盖之前提醒用户确认。
- -f :force ; 强制覆盖目标文件,不会出现提示用户确认。
- -R,-r:recursive ; 递归复制目录。
- -d : 如果源文件是符号链接,则只复制链接文件本身,而不是链接文件指向的源文件内容。
用法:
单源复制
格式 : cp [OPTION] … [-T] SOURCE DESTINATION
- 如果目标(DESTINATION)不存在,则会先创建该文件,并复制源文件的数据流至目标中。
如果目标存在:
- 如果目标是非目录文件,则覆盖目标文件。
- 如果目标是目标文件,则先在目标目录下创建一个与源文件同名的文件,并复制其数据 流至目标文件。
例如:
[root@localhost ~]# ls /tmp/
systemd-private-bd1cf1516f5147969237bb758f87b902-vmtoolsd.service-Zy7sqD
[root@localhost ~]# cp /etc/issue /tmp/
[root@localhost ~]# ls /tmp/
issue
systemd-private-bd1cf1516f5147969237bb758f87b902-vmtoolsd.service-Zy7sqD
[root@localhost ~]#
[root@localhost ~]# pwd
/root
[root@localhost ~]# ll
total 4
-rw-------. 1 root root 1307 May 1 17:46 anaconda-ks.cfg
[root@localhost ~]# cp /boot/*.* .
[root@localhost ~]# ls
anaconda-ks.cfg
config-3.10.0-514.el7.x86_64
initramfs-0-rescue-7dc3daceda194bc1bef7f8a84dafe887.img
initramfs-3.10.0-514.el7.x86_64.img
initramfs-3.10.0-514.el7.x86_64kdump.img
initrd-plymouth.img
symvers-3.10.0-514.el7.x86_64.gz
System.map-3.10.0-514.el7.x86_64
vmlinuz-3.10.0-514.el7.x86_64
[root@localhost ~]#
多源复制
cp [OPTION] … SOURCE … DIRECTORY
cp [OPTION] … -t DIRECTORY SOURCE
如果DESTINATION不存在,则将会出现错误。
如果DESTInation存在:
- 如果DESTInation是非目录文件,则将出现错误。
- 如果DESTInation是目录文件,则会分别复制每个文件至目标目录中,并保持原名。
例如:
[root@localhost ~]# mkdir -pv /tmp/test #创建目录/tmp/test
mkdir: created directory ‘/tmp/test’
[root@localhost ~]# ls /tmp/test/ #/tmp/test/目录下为空
[root@localhost ~]# ls # 当前目录下的内容
anaconda-ks.cfg
config-3.10.0-514.el7.x86_64
initramfs-0-rescue-7dc3daceda194bc1bef7f8a84dafe887.img
initramfs-3.10.0-514.el7.x86_64.img
initramfs-3.10.0-514.el7.x86_64kdump.img
initrd-plymouth.img
symvers-3.10.0-514.el7.x86_64.gz
,sysconfig
System.map-3.10.0-514.el7.x86_64
vmlinuz-3.10.0-514.el7.x86_64
[root@localhost ~]# ls /tmp/test/
[root@localhost ~]# cp i* s* /tmp/test/ #复制当前目录下以i和s开始的文件到/tmp/test/目录下
[root@localhost ~]# ls /tmp/test/ #查看/tmp/test/目录下的文件
initramfs-0-rescue-7dc3daceda194bc1bef7f8a84dafe887.img
initramfs-3.10.0-514.el7.x86_64.img
initramfs-3.10.0-514.el7.x86_64kdump.img
initrd-plymouth.img
symvers-3.10.0-514.el7.x86_64.gz
[root@localhost ~]#
mv 命令 :(move)
功能 : 移动文件或者文件更名。
格式 :
mv [OPTION]… [-T] SOURCE DEST
mv [OPTION]… SOURCE… DIRECTORY
mv [OPTION]… -t DIRECTORY SOURCE…
选项:
-i : interactive; 交互式移动,即移动之前提醒用户确认。
-f :force ; 强制移动目标文件,不会出现提示用户确认。
例如:
[root@localhost ~]# ls
user1 user2 user4 user6 user8
user10 user3 user5 user7 user9
[root@localhost ~]# mkdir move
[root@localhost ~]# ls
move user10 user3 user5 user7 user9
user1 user2 user4 user6 user8
[root@localhost ~]# mv -i user{1..5} ./move/
[root@localhost ~]# ls
move user10 user6 user7 user8 user9
[root@localhost ~]# ls ./move/
user1 user2 user3 user4 user5
[root@localhost ~]#
rm 命令 :(remove)
功能 :删除文件和目录。
格式 :rm [OPTION]… FILE…
选项:
- -i : interactive: 交互式删除。
- -f : force :强制删除。
- -r : recursive :删除目录及目录下的文件。
例子一:
[root@localhost ~]# ls
move user10 user6 user7 user8 user9
[root@localhost ~]# rm -r user{6,8,9}
rm: remove directory ‘user6’? y
rm: remove directory ‘user8’? y
rm: remove directory ‘user9’? y
[root@localhost ~]# ls
move user10 user7
[root@localhost ~]#
**************************************
[root@localhost ~]# ls
move user10 user7
[root@localhost ~]# rm -rf user10
[root@localhost ~]# ls
move user7
[root@localhost ~]#
- 人生格言:希望是成功之路上的明灯,希望是达到目的第一步,如果一个人没有希望,那么他决不会按计划去完成任何事情,最终必然一事无成。加油。。。。。坚持。