更改目录命令: cd (Change directory )
功能:改变现在目录到别的目录
语法:cd [-L| [-P] [dir]
参数:
-P,如果目录是符号链接,则进入实际的目录;
-L,如果目录是符号链接,则进行链接目录;
举例:
1.切换到根目录:cd /
[root@wCentos7 ~]# cd /==> 使用绝对路径切换目录
[root@wCentos7 /]# pwd
/
2.切换目录到 testdir/shell目录下:cd testdir/shell ==>使用相对路径切换的,由于testdir本身在根目录下
[root@wCentos7 /]# cd testdir/shell/
[root@wCentos7 shell]# pwd
/testdir/shell
3.Linux 中有些特殊符号表示特定的含义:
..: 表示上级目录
.: 表示当前目录
-: 表示上一次工作目录
~: 表示当前家目录
~username : 表示指定用户的家目录
[root@wCentos7 shell]# pwd
/testdir/shell
[root@wCentos7 shell]#cd .. ==> 切换到上级目录
[root@wCentos7 testdir]# pwd
/testdir
[root@wCentos7 testdir]#cd ./shell/ ==> 切换到下级目录方式
[root@wCentos7 shell]# pwd
/testdir/shell
[root@wCentos7 shell]#cd -==> 切换到上此工作目录
/testdir
[root@wCentos7 testdir]# cd ~==> 切换到自己家目录
[root@wCentos7 ~]# pwd
/root
[root@wCentos7 ~]# cd ~chen==> 切换到陈的家目录
[root@wCentos7 ChenJiaShun]# pwd
/home/ChenJiaShun
列出目录内容命令 : ls
功能说明:最基本的就是显示系统上有哪些文件
语法:ls [OPTION]... [FILE]...
参数:
ls -a包含隐藏文件(隐藏文件以.号开始)
ls -l显示额外的信息(显示文件权限,大小,属组,属主等信息)
ls -R目录递归通过(显示目录下面的文件)
ls -ld目录和符号链接信息
ls -1 文件分行显示
ls –S按从大到小排序
ls –u配合-t选项,显示并按atime从新到旧排序
ls –U不排序按目录存放顺序显示
举例:
[root@wCentos7 ~]# ls-->正常ls显示不会显示隐藏文件和文件其它属性
anaconda-ks.cfg Downloads initial-setup-ks.cfg Public zzzzzzz
Desktop f11 Music Templates
Documents f1.txt Pictures Videos
[root@wCentos7 ~]# ls -a-->显示该目录下是所有的文件和文件夹(. ..是当前目录和上级目录的 )
. .config f1.txt Templates
.. .cshrc .ICEauthority Videos
anaconda-ks.cfg .dbus initial-setup-ks.cfg .viminfo
.bash_history Desktop .local .Xauthority
.bash_logout Documents Music zzzzzzz
.bash_profile Downloads Pictures
.bashrc .esd_auth Public
.cache f11 .tcshrc
[root@wCentos7 ~]# ls -R-->显示该目录下是所有的文件和文件夹下面的文件和目录.:
anaconda-ks.cfg Downloads initial-setup-ks.cfg Public zzzzzzz
Desktop f11 Music Templates
Documents f1.txt Pictures Videos
./Desktop:
./Documents:
test
./Documents/test:
123 text2
./Documents/test/text2:
456 test3
./Documents/test/text2/test3:
789
./Downloads:
fuse.conf
./Music:
./Pictures:
./Public:
./Templates:
./Videos:
[root@wCentos7 ~]# ls -ld-->显示当前目录的文件详细信息
dr-xr-x---. 14 root root 4096 Jul 28 08:36 .
[root@wCentos7 ~]# ls -Sl-->当前目录下文件按文件大小排序(注意没有统计目录下文件大小的合计)
total 16
-rw-------. 1 root root 1465 Jul 21 11:35 initial-setup-ks.cfg
-rw-------. 1 root root 1417 Jul 21 11:34 anaconda-ks.cfg
drwxr-xr-x. 2 root root 22 Jul 28 11:39 Downloads
drwxr-xr-x. 3 root root 17 Jul 27 23:14 Documents
-rwxrw-rw-. 1 root root 15 Jul 27 10:15 f1.txt
-rw-r--r--. 1 root root 12 Jul 27 10:19 f11
drwxr-xr-x. 2 root root 6 Jul 21 11:54 Desktop
drwxr-xr-x. 2 root root 6 Jul 21 11:54 Music
drwxr-xr-x. 2 root root 6 Jul 21 11:54 Pictures
drwxr-xr-x. 2 root root 6 Jul 21 11:54 Public
drwxr-xr-x. 2 root root 6 Jul 21 11:54 Templates
drwxr-xr-x. 2 root root 6 Jul 21 11:54 Videos
-rw-r--r--. 1 root root 0 Jul 27 23:24 zzzzzzz
[root@wCentos7 ~]# ls -utl-->当前目录下文件按访问时间前后顺序排序的
total 16
drwxr-xr-x. 2 root root 22 Jul 28 11:39 Downloads
-rw-r--r--. 1 root root 0 Jul 27 23:24 zzzzzzz
drwxr-xr-x. 3 root root 17 Jul 27 23:14 Documents
drwxr-xr-x. 2 root root 6 Jul 27 16:38 Music
drwxr-xr-x. 2 root root 6 Jul 27 16:38 Pictures
drwxr-xr-x. 2 root root 6 Jul 27 16:38 Videos
drwxr-xr-x. 2 root root 6 Jul 27 16:38 Desktop
-rw-------. 1 root root 1465 Jul 27 16:38 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 6 Jul 27 16:38 Public
drwxr-xr-x. 2 root root 6 Jul 27 16:38 Templates
-rw-------. 1 root root 1417 Jul 27 16:38 anaconda-ks.cfg
-rw-r--r--. 1 root root 12 Jul 27 10:19 f11
-rwxrw-rw-. 1 root root 15 Jul 27 10:15 f1.txt
关于文件名通配符的描述
匹配模式:元字符
* : 匹配任意长度任意字符;
例如: pa* = paaa,pab,pa,pac,pawwsd。
? :匹配任意单个字符(不包含零字符);
例如:pa?=pas,pa1,pab,pa2(不包含pa)。
[] :范围内的任意单个字符;
格式: [a-z]所有的字母,不区分大小写;[0-9]所有的数字
例如:pa[0-9][0-9] = pa00~~pa99
2[0-9][0-9] = 200~~299
特殊的文件格式:
[[:upper:]] :大写 的A-Z
[[:lower:]] :小写的a-z
[[:alpha:]] :所有的字母
[[:digit:]] :所有的数字
[[:alnum:]] :所有的字母与数字
[[:space:]] :空白字符
[[:punct:]] :所有的标点符号
[^] :匹配指定范围外的任意字符
练习题熟练对命令和通配符的理解
1、显示/var目录下所有以l开头,以一个小写字母结尾,且中间出现至少一位数字的文件或目录
ll -ad /var/l*[[:digit:]]*[[:lower:]]==> l开头+任意字符+所有数字+任意字符+小写字母
ll -ad /var/l*[[:digit:]*[a-z]==> l开头+任意字符+所有数字+任意字符+a-z
ll -ad /var/l*[0-9]*[a-z]==> l开头+任意字符+0-9+任意字符+a-z(只有这个可以达到我需要的效果)
注意:理论上三个通配符都可以达到我的要求,我就奇怪为什么只有最下面的一个可以,难道是我电脑问题。
[root@wCentos7 var]# ll -d /var/l*[0-9]*[a-z]
-rw-r--r--. 1 root root 0 Jul 28 12:28 /var/l123name
2、显示/etc目录下以任意一位数字开头,且以非数字结尾的文件或目录
ll-ad /etc/[0-9]*[^0-9]==>0-9开头+任意字符+非0-9结尾
[root@wCentos7 var]# touch /etc/{222ww,333txt,444uu5}
[root@wCentos7 var]# ll -d /etc/[0-9]*[^0-9]
-rw-r--r--. 1 root root 0 Jul 28 12:47 /etc/222ww
-rw-r--r--. 1 root root 0 Jul 28 12:47 /etc/333txt
[root@wCentos7 var]#
3、显示/etc/目录下以非字母开头,后面跟了一个字母及其它任意长度任意字符的文件或目录
ll -ad /etc/[^[:alpha:]][[:alpha:]]*==>非字母开头+任意一个字母
[root@wCentos7 var]# ll -ad /etc/[^[:alpha:]][[:alpha:]]*
-rw-r--r--. 1 root root 0 Jul 28 12:52 /etc/1wwq
4、显示/etc目录下所有以m开头以非数字结尾的文件或目录
ll -ad /etc/m*[^[::digit]]==>m开头+任意字符+非数字结尾
[root@wCentos7 var]#ll -ad /etc/m*[^[:digit:]]
-rw-r--r--. 1 root root 0 Jul 28 12:57 /etc/m12b
-rw-r--r--. 1 root root 0 Jul 28 12:57 /etc/m1qw
-r--r--r--. 1 root root 33 Jul 21 11:27 /etc/machine-id
-rw-r--r--. 1 root root 111 Nov 20 2015 /etc/magic
-rw-r--r--. 1 root root 1968 Dec 17 2014 /etc/mail.rc
-rw-r--r--. 1 root root 5122 Nov 21 2015 /etc/makedumpfile.conf.sample
-rw-r--r--. 1 root root 5171 Jun 10 2014 /etc/man_db.conf
drwxr-xr-x. 2 root root 6 Nov 21 2015 /etc/maven
-rw-r--r--. 1 root root 936 Mar 6 2015 /etc/mke2fs.conf
drwxr-xr-x. 2 root root 22 Jul 21 11:27 /etc/modprobe.d
drwxr-xr-x. 2 root root 6 Nov 20 2015 /etc/modules-load.d
-rw-r--r--. 1 root root 0 Jun 7 2013 /etc/motd
lrwxrwxrwx. 1 root root 17 Jul 21 11:22 /etc/mtab -> /proc/self/mounts
-rw-r--r--. 1 root root 2620 Jun 10 2014 /etc/mtools.conf
drwxr-xr-x. 2 root root 6 Nov 20 2015 /etc/multipath
-rw-r--r--. 1 root root 570 Nov 21 2015 /etc/my.cnf
drwxr-xr-x. 2 root root 30 Jul 21 11:25 /etc/my.cnf.d
5、显示/etc目录下,所有以.d结尾的文件或目录
ll -ad /etc/*.d==>任意字符+.d结尾
[root@wCentos7 var]#ll -ad /etc/*.d
drwxr-xr-x. 2 root root 4096 Jul 21 11:31 /etc/bash_completion.d
drwxr-xr-x. 2 root root 6 Nov 20 2015 /etc/binfmt.d
drwxr-xr-x. 2 root root 6 Nov 20 2015 /etc/chkconfig.d
drwxr-xr-x. 2 root root 51 Jul 21 11:30 /etc/cron.d
drwxr-xr-x. 2 root root 22 Jul 21 11:27 /etc/depmod.d
drwxr-xr-x. 2 root root 6 Aug 6 2015 /etc/dnsmasq.d
drwxr-xr-x. 2 root root 6 Nov 20 2015 /etc/dracut.conf.d
drwxr-xr-x. 2 root root 6 Nov 20 2015 /etc/exports.d
drwxr-xr-x. 2 root root 6 Sep 11 2015 /etc/gdbinit.d
drwx------. 2 root root 4096 Jul 21 11:34 /etc/grub.d
lrwxrwxrwx. 1 root root 11 Jul 21 11:23 /etc/init.d -> rc.d/init.d
drwx------. 5 root root 4096 Jul 21 11:27 /etc/ipsec.d
drwxr-xr-x. 2 root root 4096 Jul 21 11:27 /etc/ld.so.conf.d
drwxr-xr-x. 2 root root 6 Nov 20 2015 /etc/libibverbs.d
drwxr-xr-x. 2 root root 4096 Jul 21 11:30 /etc/logrotate.d
drwxr-xr-x. 2 root root 22 Jul 21 11:27 /etc/modprobe.d
drwxr-xr-x. 2 root root 6 Nov 20 2015 /etc/modules-load.d
drwxr-xr-x. 2 root root 30 Jul 21 11:25 /etc/my.cnf.d
drwxr-xr-x. 2 root root 68 Jul 21 11:27 /etc/oddjobd.conf.d
drwxr-xr-x. 2 root root 4096 Jul 21 17:11 /etc/pam.d
drwxr-xr-x. 2 root root 6 Jun 10 2014 /etc/popt.d
drwxr-xr-x. 2 root root 101 Jul 21 11:27 /etc/prelink.conf.d
drwxr-xr-x. 2 root root 4096 Jul 21 11:30 /etc/profile.d
lrwxrwxrwx. 1 root root 10 Jul 21 11:23 /etc/rc0.d -> rc.d/rc0.d
lrwxrwxrwx. 1 root root 10 Jul 21 11:23 /etc/rc1.d -> rc.d/rc1.d
lrwxrwxrwx. 1 root root 10 Jul 21 11:23 /etc/rc2.d -> rc.d/rc2.d
lrwxrwxrwx. 1 root root 10 Jul 21 11:23 /etc/rc3.d -> rc.d/rc3.d
lrwxrwxrwx. 1 root root 10 Jul 21 11:23 /etc/rc4.d -> rc.d/rc4.d
lrwxrwxrwx. 1 root root 10 Jul 21 11:23 /etc/rc5.d -> rc.d/rc5.d
lrwxrwxrwx. 1 root root 10 Jul 21 11:23 /etc/rc6.d -> rc.d/rc6.d
drwxr-xr-x. 10 root root 4096 Nov 20 2015 /etc/rc.d
drwxr-xr-x. 2 root root 29 Jul 21 11:27 /etc/request-key.d
drwxr-xr-x. 2 root root 51 Jul 21 11:27 /etc/rsyslog.d
drwxr-xr-x. 2 root root 6 Nov 20 2015 /etc/rwtab.d
drwxr-xr-x. 3 root root 4096 Jul 21 11:29 /etc/sane.d
drwxr-xr-x. 2 root root 4096 Jul 21 11:30 /etc/setuptool.d
drwxr-xr-x. 2 root root 6 Nov 20 2015 /etc/statetab.d
drwxr-x---. 2 root root 6 Nov 21 2015 /etc/sudoers.d
drwxr-xr-x. 2 root root 27 Jul 21 11:27 /etc/sysctl.d
drwxr-xr-x. 2 root root 24 Nov 20 2015 /etc/tmpfiles.d
drwxr-xr-x. 2 root root 8192 Jul 21 11:27 /etc/usb_modeswitch.d
drwxr-xr-x. 2 root root 6 Aug 12 2015 /etc/xinetd.d
drwxr-xr-x. 2 root root 4096 Dec 3 2015 /etc/yum.repos.d
6、显示/etc目录下,所有.conf结尾,且以m,n,r,p开头的文件或目录
ll -ad /etc/[mnrp]*.conf==>mnrp任意一个字符开头+任意字符+.conf结尾
[root@wCentos7 var]#ll -ad /etc/[mnrp]*.conf
-rw-r--r--. 1 root root 5171 Jun 10 2014 /etc/man_db.conf
-rw-r--r--. 1 root root 936 Mar 6 2015 /etc/mke2fs.conf
-rw-r--r--. 1 root root 2620 Jun 10 2014 /etc/mtools.conf
-rw-r--r--. 1 root root 3390 Nov 20 2015 /etc/nfsmount.conf
-rw-r--r--. 1 root root 1717 Jul 21 11:34 /etc/nsswitch.conf
-rw-r--r--. 1 root root 91 Dec 3 2012 /etc/numad.conf
-rw-r--r--. 1 root root 1362 Jun 10 2014 /etc/pbm2ppa.conf
-rw-r--r--. 1 root root 6300 Jun 10 2014 /etc/pnm2ppa.conf
-rw-r--r--. 1 root root 433 Sep 9 2015 /etc/radvd.conf
-rw-r--r--. 1 root root 1787 Jun 10 2014 /etc/request-key.conf
-rw-r--r--. 1 root root 63 Jul 28 08:36 /etc/resolv.conf
-rw-r--r--. 1 root root 458 Nov 21 2015 /etc/rsyncd.conf
-rw-r--r--. 1 root root 3232 Sep 8 2015 /etc/rsyslog.conf
查看文件状态
详细请见 博客关于文件 atime mtime gtime专题
创建空文件和刷新时间:touch命令
vtouch命令:
功能:创建空文件和刷新时间
语法:touch [OPTION]... FILE...
参数:
-a: 仅改变atime
-m: 仅改变mtime
-t:STAMP:
[[CC]YY]MMDDhhmm[.ss]
-c: 如果文件不存在,则不予创建
举例:
[root@wCentos7 touch.dir]# ll -a-->当前目录下没有任何文件
total 4
drwxr-xr-x. 2 root root 6 Jul 28 15:32 .
drwxr-xr-x. 14 root root 4096 Jul 28 15:32 ..
[root@wCentos7 touch.dir]# t ouch 11-->touch一个不存在的文件
[root@wCentos7 touch.dir]# ll -a-->当前目录下新建了一个文件
total 4
drwxr-xr-x. 2 root root 15 Jul 28 15:32 .
drwxr-xr-x. 14 root root 4096 Jul 28 15:32 ..
-rw-r--r--. 1 root root 0 Jul 28 15:32 11
[root@wCentos7 touch.dir]#stat 11#查看当前文件的状态为当前时间
File: ‘11’
Size: 0Blocks: 0 IO Block: 4096 regular empty file
Device: 805h/2053dInode: 3142 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2016-07-28 15:32:38.911943667 +0800
Modify: 2016-07-28 15:32:38.911943667 +0800
Change: 2016-07-28 15:32:38.911943667 +0800
Birth: -
[root@wCentos7 touch.dir]#touch -a -m 11#修改atime mtime的时间
[root@wCentos7 touch.dir]# stat 11
File: ‘11’
Size: 0Blocks: 0 IO Block: 4096 regular empty file
Device: 805h/2053dInode: 3142 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2016-07-28 15:33:33.499942685 +0800
Modify: 2016-07-28 15:33:33.499942685 +0800# 文件的三个时间都有变化
Change: 2016-07-28 15:33:33.499942685 +0800
Birth: -
[root@wCentos7 touch.dir]#touch -t 200808080808.08 11#修改时间的为2008年
[root@wCentos7 touch.dir]# stat 11
File: ‘11’
Size: 0Blocks: 0 IO Block: 4096 regular empty file
Device: 805h/2053dInode: 3142 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2008-08-08 08:08:08.000000000 +0800
Modify: 2008-08-08 08:08:08.000000000 +0800#atime mtime的时间都变为修改的时间
Change: 2016-07-28 15:34:12.920941975 +0800
Birth: -
[root@wCentos7 touch.dir]#touch -c 22#文件不存在,就不会创建文件,文件存在就会修改文件时间
[root@wCentos7 touch.dir]# ll -a
total 4
drwxr-xr-x. 2 root root 15 Jul 28 15:32 .
drwxr-xr-x. 14 root root 4096 Jul 28 15:32 ..
-rw-r--r--. 1 root root 0 Jul 28 15:40 11
[root@wCentos7 touch.dir]#
复制文件和目录cp命令
功能:cp copy复制文件和目录,并且可以改名
语法:
cp [OPTION]... [-T] SOURCE DEST
vcp [OPTION]... SOURCE... DIRECTORY (第二个语法常用)
vcp [OPTION]... -t DIRECTORY SOURCE...
注意:举例说明可以比较好说明
如果目标不存在:新建目标文件,并将源文件中内容填充至目标中
如果目标存在:
如果目标是文件:将源文件中的内容覆盖至目标文件中
基于安全,建议为cp命令使用-i选项
如果目标是目录:在目标下新建与原文件同名的文件,并将源文件中内容填充至新文件中
参数:
-i:交互式 (目标文件有同名,提示需要覆盖吗?)
-r, -R: 递归复制目录及内部的所有内容;(主要用于目录拷贝)
-d:--no-dereference --preserv=links不复制原文件,只复制链接名
--preserv[=ATTR_LIST] (复制的时候对于文件的属性是否变更问题)
mode: 权限
ownership: 属主属组
timestamp:
links
xattr
context
all
-a: 归档,相当于-dR--preserv=all
-p: 等同--preserv=mode,ownership,timestamp(拷贝后文件属主 属组 时间戳不改变)
-v: --verbose (显示其拷贝过程)
-f: --force(强制)
举例:
1.拷贝单个文件,目标不存在(会在目标目录下新建一个同名文件)
[root@wCentos7 touch.dir]#cp /etc/issue ./#拷贝文件到当前目录下
[root@wCentos7 touch.dir]# ll
total 4
-rw-r--r--. 1 root root 0 Jul 28 15:40 11
-rw-r--r--. 1 root root 126 Jul 28 15:58 issue
2.如果目标文件存在,提示会覆盖目标文件,没有使用-i参数为什么提示我覆盖文件呢?
[root@wCentos7 touch.dir]# cp /etc/issue
issue issue.net
[root@wCentos7 touch.dir]# cp /etc/issue ./
cp: overwrite ‘./issue’? Y
[root@wCentos7 touch.dir]# alias#由于是管理员登录,默认cp命令的别名有-i参数,所以提示覆盖,安全设
alias cls='clear'置而已,如果是普通用户登录,目标存在,是不会提醒覆盖,就是没有-i选项
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias ip='ifconfig'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias vimnet='vim /etc/sysconfig/network-scripts/ifcfg-eno16777736'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
3.如果源文件为目录,目标不存在,会在目标目录下新建一个同名的目录
[root@wCentos7 touch.dir]#cp /etc/sysconfig/network-scripts/ ./#拷贝目录一定记得要带-r参数,不让会报错
cp: omitting directory ‘/etc/sysconfig/network-scripts/’
[root@wCentos7 touch.dir]#cp -r /etc/sysconfig/network-scripts/ ./#加上-r参数就不报错了
[root@wCentos7 touch.dir]# ll
total 8
-rw-r--r--. 1 root root 0 Jul 28 15:40 11
-rw-r--r--. 1 root root 126 Jul 28 15:59 issue
drwxr-xr-x. 2 root root 4096 Jul 28 16:08 network-scripts#创建了一个同名的目录
[root@wCentos7 touch.dir]#
4.如果目标目录存在,则源目录会拷贝至目标目录的同名目录文件里面去
[root@wCentos7 touch.dir]# cp -r /etc/sysconfig/network-scripts/ ./network-scripts/
[root@wCentos7 touch.dir]# tree -d -L 2
.
└── network-scripts
└── network-scripts
其实我们的初衷是想让network-scripts/下的文件夹覆盖掉我们目标目录/network-scripts/下的所有文件,下面我就介绍下如何让源和目标同名的目录,源目录下的文件覆盖目标目录下的文件。
5.如果目标目录存在,且源目录和目标目录同名,我们如何让源目录下的文件覆盖目标同名目录下的文件
[root@wCentos7 touch.dir]#cp -r /etc/sysconfig/network-scripts/ ./
cp: overwrite ‘./network-scripts/ifdown-Team’? y
cp: overwrite ‘./network-scripts/ifdown-TeamPort’? y
cp: overwrite ‘./network-scripts/ifup-Team’? y
cp: overwrite ‘./network-scripts/ifup-TeamPort’? y
cp: overwrite ‘./network-scripts/ifcfg-eno16777736’? y
[root@wCentos7 touch.dir]#\cp -rf /etc/sysconfig/network-scripts/ ./
[root@wCentos7 touch.dir]#
注意:cp -r /etc/sysconfig/network-scripts/ ./
这个命令是可以更新目标目录下的文件,不过由于默认情况下管理员账号是有 -i的选项,需要提示你是不是确定覆盖文件,如果有100个是不是很烦的,那我们就可以使用下面的方法。
\cp -rf /etc/sysconfig/network-scripts/ ./
让cp使用原本没有-i选项 带上-f参数即可强制覆盖目录文件目录下的文件
-p参数的使用,不改变文件的属主 属组和时间戳(用于打包和备份的时候使用居多)
[root@wCentos7 ~]# cp ~chen/bash.sh ./#默认复制后 文件的属组和属组都变为root用户的
[root@wCentos7 ~]# stat bash.sh
File: ‘bash.sh’
Size: 0Blocks: 0 IO Block: 4096 regular empty file
Device: 802h/2050dInode: 207797038 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2016-07-28 16:33:58.340877444 +0800
Modify: 2016-07-28 16:33:58.340877444 +0800
Change: 2016-07-28 16:33:58.340877444 +0800
Birth: -
[root@wCentos7 ~]# cp -p ~chen/bash.sh ./bash1.sh#-p参数拷贝一份后
[root@wCentos7 ~]# stat bash1.sh#保留原来的属主和属组
File: ‘bash1.sh’
Size: 0Blocks: 0 IO Block: 4096 regular empty file
Device: 802h/2050dInode: 207797039 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ chen) Gid: ( 1000/ChenJiaShun)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2016-07-28 16:33:58.340877444 +0800
Modify: 2016-07-28 16:33:11.242878292 +0800
Change: 2016-07-28 16:34:24.988876964 +0800
Birth: -
-d参数的使用,不复制源文件,只复制链接文件,默认情况下复制连接的源文件,
[root@wCentos7 etc]# cp system-release /testdir/
# 正常拷贝连接文件system-release到目录目录下
[root@wCentos7 etc]# cp -d system-release /testdir/system-release2
# -p拷贝连接文件system-release到目录目录下
[root@wCentos7 etc]# cd /testdir/
[root@wCentos7 testdir]# ll
-rw-r--r--. 1 root root 38 Jul 28 16:38 system-release
# 正常拷贝连接文件system-release到目录目录下,为正常的普通文件
lrwxrwxrwx. 1 root root 14 Jul 28 16:39 system-release2 -> centos-release
# -p拷贝连接文件system-release到目录目录下,为连接文件,由于源文件找不到,所以会一致是红色的闪闪的
[root@wCentos7 testdir]#
命令别名: alias命令详解
alias命令
功能:可以给一个较长的命令设置一个让我们好记忆的名称
语法:
alisa NAME=”VALUE”设置别名
unalias NAME取消别名
注意:在命令行中定义的别名,仅对当前shell进程有效
v如果想永久有效,要定义在配置文件中
仅对当前用户:~/.bashrc
对所有用户有效:/etc/bashrc
bash进程重新读取配置文件:
source /path/to/config_file
. /path/to/config_file
参数:
-p : 打印出系统现有设置的别名
举例:
1.查看系统现有别名
[root@wCentos7 testdir]# alias -p
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias ip='ifconfig'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
2.设置别名(最好不要设置别名名称和现有命令名称一样的别名)
alias cls='clear'#cls 代替命令clear清屏命令 , 原来的clear命令还是可用
alias vimnet='vim /etc/sysconfig/network-scripts/ifcfg-eno16777736'
#设置vimnet别名代替配置网卡信息的命令+路径
注意:只对当前shell进程用户有效,需要永久有效就修改配置文件
3.修改配置文件让别名永久有效(仅对当前用户:~/.bashrc对所有用户有效:/etc/bashrc)
[root@wCentos7 testdir]# cat ~/.bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias cls="clear"
alias ip="ifconfig"#定义alias别名
alias vimnet="vim /etc/sysconfig/network-scripts/ifcfg-eno16777736"
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
[root@wCentos7 testdir]#
[root@wCentos7 testdir]#source /root/.bashrc#使配置文件立即生效
移动和重命名文件
移动和重命名文件:mv
功能:mv命令:移动文件或目录 还可以改名
语法:mv [OPTION]... SOURCE... DIRECTORY
参数:
-i: 交互式 (目标文件存在提示覆盖,需要确认)
-f: 强制删除 (不确认)
举例:
[root@wCentos7 mydate]# mv system-release2 system-release#移动覆盖提示确认
mv: overwrite ‘system-release’? Y
[root@wCentos7 mydate]# mv -f 111 system-release#移动覆盖不提示
[root@wCentos7 mydate]# ll
total 0
-rw-r--r--. 1 root root 0 Jul 28 17:51 system-release
dr-xr-xr-x. 2 root root 93 Jul 26 13:25 text-dir
删除文件或目录 rm
功能:删除文件或目录
语法:rm [OPTION]... FILE...
参数:
-i: 交互式
-f: 强制删除
-r: 递归
举例:
1.删除文件:(默认别名有 -i选项)
[root@wCentos7 mydate]# rm system-release
rm: remove regular empty file ‘system-release’? y
2.删除目录:(删除目录需要添加 -r选项)
[root@wCentos7 mydate]# rm bashtest/
rm: cannot remove ‘bashtest/’: Is a directory
[root@wCentos7 mydate]# rm -r bashtest/
rm: descend into directory ‘bashtest/’? y
rm: remove regular file ‘bashtest/1.sh’? y
rm: remove regular file ‘bashtest/.1.sh.swp’? y
rm: remove regular empty file ‘bashtest/222’? y
rm: remove regular empty file ‘bashtest/333’? y
rm: remove regular empty file ‘bashtest/444’? y
rm: remove regular empty file ‘bashtest/555’? y
rm: remove regular empty file ‘bashtest/666’? y
rm: remove regular empty file ‘bashtest/777’? y
rm: remove directory ‘bashtest/’? y
3.强制删除目录:(删除文件和目录不确认通用使用rm -rf命令 )
[root@wCentos7 mydate]# touch system.txt
[root@wCentos7 mydate]# rm -rf system.txt
[root@wCentos7 mydate]#
目录操作命令:tree rmdir mkdir
vtree命令
功能:显示目录树,显示目录结构
语法: tree [option] [directory ...]
参数:
-d: 只显示目录
-L level:指定显示的层级数目
-P pattern: 只显示由指定pattern匹配到的路径
举例:
1.显示家目录结构
[root@wCentos7 ~]# tree
.
├── anaconda-ks.cfg
├── bash1.sh
├── bash.sh
├── Desktop
├── Documents
│└──test
│├──123
│└──text2
│├──456
│└──test3
│└──789
├── Downloads
│└──fuse.conf
├── f11
├── f1.txt
├── initial-setup-ks.cfg
├── Music
├── Pictures
├── Public
├── Templates
├── Videos
└── zzzzzzz
2.只显示目录,不显示文件
[root@wCentos7 ~]# tree -d
.
├── Desktop
├── Documents
│└──test
│└──text2
│└──test3
├── Downloads
├── Music
├── Pictures
├── Public
├── Templates
└── Videos
3.只显示当前目录下的目录结构,不显示目录下的目录
[root@wCentos7 ~]# tree -d -L 1
.
├── Desktop
├── Documents
├── Downloads
├── Music
├── Pictures
├── Public
├── Templates
└── Videos
vmkdir创建目录
功能:创建命令
语法: mkdir [OPTION]... DIRECTORY...
参数:
-p: 存在于不报错,且可自动创建所需的各目录;
-v: 显示详细信息
-m MODE: 创建目录时直接指定权限;(前期没有讲解,后期讲解)
举例:
1.批量创建空目录并显示其建立目录的过程
[root@wCentos7 dir3]# mkdir dir1/dir2/dir3/dir4#不加-p参数是无法创建目录中的目录的
mkdir: cannot create directory ‘dir1/dir2/dir3/dir4’: No such file or directory
[root@wCentos7 dir3]# mkdir-pv dir1/dir2/dir3/dir4
bash: mkdir-pv: command not found...
[root@wCentos7 dir3]# mkdir -pv dir1/dir2/dir3/dir4#加上-pv参数,批量创建目录中的目录并显示器创建过程
mkdir: created directory ‘dir1’
mkdir: created directory ‘dir1/dir2’
mkdir: created directory ‘dir1/dir2/dir3’
mkdir: created directory ‘dir1/dir2/dir3/dir4’
[root@wCentos7 dir3]#
vrmdir删除空目录
功能:删除空目录
语法:mkdir [OPTION]... DIRECTORY...
参数:
-p: 递归删除父空目录
-v: 显示详细信息
vrm-r递归删除目录树
举例:只用于删除空目录,实际用得比较少常用 :rm-r递归删除目录树