2:CentOS7文件管理-上

xiaoxiao2021-02-28  63

1.目录结构&tree命令 目录结构tree命令: 2.ls命令3.命令别名–alias&ualias alias命令: 4.文件类型5.绝对路径&相对路径6.cd切换目录7.创建和删除目录8.创建和删除文件9.PATH环境变量、which10.复制、移动和重命名11.文档查看cat /more/less/head/tail more命令:less命令:head命令:tail命令: 12.文件权限13.修改文件属性

1.目录结构&tree命令


目录结构

Linux 的目录结构是一个有层次的倒挂着的树型目录结构

根“/”是所有目录的顶点。

目录结构和分区设备是没有关系的。也就是不同的目录可以跨越不同的磁盘设备或分区

所有的目录都是按照一定的类别有规律的组织和命名的

详细目录结构参考:

http://www.apelearn.com/study_v2/linux_dir_tree.jpg

CentOS7部分主要目录的作用:

/bin,/usr/bin #存放普通用户使用的命令 /sbin,/usr/sbin #存放管理员使用的命令 /dev #设备文件 /root #root用户的家目录 /home #普通用户的家目录 /proc #虚拟的文件系统,反映出来是内核,进程信息,或实时状态 #程序运行相关的文件都存放于以进程号命名的目录中. /usr #系统文件,相当于C:\Windows /usr/local #软件安装的目录,相当于C:\program /usr/lib #32位库文件 /usr/lib64 #64位库文件 /boot #存放系统启动相关的文件,例如kernel.grub等 /etc #配置文件(系统相关,如网络/etc/sysconfig/network #应用程序相关配置文件如/etc/ssh/sshd_config) /tmp #临时文件目录(全局可见,进程产生的临时文件) /var #存放的是一些变化的文件,比如数据库, 日志,邮件等..

tree命令:

tree命令主要功能为查看目录结构。

最小安装的系统是不带tree命令的,需要安装后才能使用,

yum install -y tree#yum安装tree。

tree命令不加选项和参数默认是查看当前目录的结构

[root@long01 ~]# ls anaconda-ks.cfg dir1 dir2 [root@long01 ~]# tree . |-- anaconda-ks.cfg |-- dir1 | `-- file1 `-- dir2 `-- file2 2 directories, 3 files

也可以查看指定目录的目录结构,我当前也在/root目录下,所以结果跟上面一样

[root@long01 ~]# tree /root /root |-- anaconda-ks.cfg |-- dir1 | `-- file1 `-- dir2 `-- file2 2 directories, 3 files

也可以指定查看目录结构的层数,比如我只想查看根目录下第一层的目录和文件。

这里需要用到 -L 选项,选项后加一个数字表示列出目录结构的层数。

[root@long01 ~]# tree -L 1 / / |-- bin -> usr/bin |-- boot |-- dev |-- etc |-- home |-- lib -> usr/lib |-- lib64 -> usr/lib64 |-- media |-- mnt |-- opt |-- proc |-- root |-- run |-- sbin -> usr/sbin |-- srv |-- sys |-- tmp |-- usr `-- var 19 directories, 0 files

常用的选项就一个-L,详细的用法参考:http://man.linuxde.net/tree

2.ls命令


ls命令是list的缩写,用来查看当前目录或指定目录下的文件信息

直接输入ls命令就可以列出当前目录下的文件和目录

[root@long01 ~]# ls anaconda-ks.cfg dir1 dir2

ls后跟一个路径,就会列出指定路径下的文件和子目录

[root@long01 ~]# ls /usr bin etc games include lib lib64 libexec local sbin share src tmp

-l 参数可以显示文件和目录的详细信息

[root@long01 ~]# ls -l /usr total 100 dr-xr-xr-x. 2 root root 20480 May 12 07:31 bin drwxr-xr-x. 2 root root 6 Nov 5 2016 etc drwxr-xr-x. 2 root root 6 Nov 5 2016 games drwxr-xr-x. 3 root root 23 May 10 04:41 include dr-xr-xr-x. 27 root root 4096 May 10 04:41 lib dr-xr-xr-x. 38 root root 20480 May 10 06:47 lib64 drwxr-xr-x. 19 root root 4096 May 10 04:41 libexec drwxr-xr-x. 12 root root 131 May 10 04:40 local dr-xr-xr-x. 2 root root 12288 May 10 04:41 sbin drwxr-xr-x. 76 root root 4096 May 10 06:47 share drwxr-xr-x. 4 root root 34 May 10 04:40 src lrwxrwxrwx. 1 root root 10 May 10 04:40 tmp -> ../var/tmp

-a 参数可以显示目录下的所有子目录和文件,包括隐藏文件

Linux文件名或目录名以小数点开头的都为隐藏文件。ls 不加-a参数是不会列出来的

[root@long01 ~]# ls -a . .bash_logout .bashrc .pki .viminfo dir1 .. .bash_profile .cshrc .tcshrc anaconda-ks.cfg dir2

-h 参数可以给文件和目录的大小加上一个合适的单位,方便阅读。只有配合-l参数才有效。

[root@long01 ~]# ls -lh /usr total 100K dr-xr-xr-x. 2 root root 20K May 12 07:31 bin drwxr-xr-x. 2 root root 6 Nov 5 2016 etc drwxr-xr-x. 2 root root 6 Nov 5 2016 games drwxr-xr-x. 3 root root 23 May 10 04:41 include dr-xr-xr-x. 27 root root 4.0K May 10 04:41 lib dr-xr-xr-x. 38 root root 20K May 10 06:47 lib64 drwxr-xr-x. 19 root root 4.0K May 10 04:41 libexec drwxr-xr-x. 12 root root 131 May 10 04:40 local dr-xr-xr-x. 2 root root 12K May 10 04:41 sbin drwxr-xr-x. 76 root root 4.0K May 10 06:47 share drwxr-xr-x. 4 root root 34 May 10 04:40 src lrwxrwxrwx. 1 root root 10 May 10 04:40 tmp -> ../var/tmp

-i 参数会显示文件或目录的inode号码。并且也只能配合-l参数才能方便阅读

[root@long01 ~]# ls -ih 33574978 anaconda-ks.cfg 33625905 dir1 50792825 dir2 [root@long01 ~]# ls -lih total 4.0K 33574978 -rw-------. 1 root root 1.4K May 10 04:43 anaconda-ks.cfg 33625905 drwxr-xr-x. 2 root root 19 May 12 07:35 dir1 50792825 drwxr-xr-x. 2 root root 19 May 12 07:35 dir2

-t 列出的文件和目录按修改或创建时间进行排序,此为顺序排列,最新的在上边,最老的在下边。

[root@long01 ~]# ls -lt 总用量 4 drwxr-xr-x. 2 root root 6 5月 12 09:37 dir3 drwxr-xr-x. 2 root root 19 5月 12 07:35 dir2 drwxr-xr-x. 2 root root 19 5月 12 07:35 dir1 -rw-------. 1 root root 1366 5月 10 04:43 anaconda-ks.cfg

-r 列出的文件和目录进行倒序排列,配合 -t 参数使用最佳

[root@long01 ~]# ls -ltr 总用量 4 -rw-------. 1 root root 1366 5月 10 04:43 anaconda-ks.cfg drwxr-xr-x. 2 root root 19 5月 12 07:35 dir1 drwxr-xr-x. 2 root root 19 5月 12 07:35 dir2 drwxr-xr-x. 2 root root 6 5月 12 09:37 dir3

-d 只列出指定文件或目录的信息。

[root@long01 ~]# ls -ldh /root dr-xr-x---. 6 root root 225 512 10:07 /root [root@long01 ~]# ls -ldh /etc/profile -rw-r--r--. 1 root root 1.9K 512 08:59 /etc/profile

还有一个选项 –color=auto,给不同类型的文件加上颜色,

这个选项系统默认定义在了别名中,不需要我们自己输入。alias可以查看已经定义的别名。

[root@long01 ~]# alias alias cp='cp -i' alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' 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

ls命令的常用参数也只有这么几个,详细用法参考:http://man.linuxde.net/ls

3.命令别名–alias&ualias


alias命令:

用来设置命令的别名。我们可以使用该命令可以将一些较长的命令进行简化。

比如:我们修改网卡配置文件的时候,每次都需要输入很长的路径。

这样我们就可以使用alias命令来给编辑网卡配置文件这条命令定义一个别名,

下次直接使用别名就相当于输入了一长串的命令

直接使用alias命令可以查看系统当前所有已定义的别名信息。

[root@long01 ~]# alias alias cp='cp -i' alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' 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'

下面我来给编辑网卡配置文件的命令定义一个别名

定义别名的格式: alias 别名=’真实命令’,真实命令一定要使用双引号或单引号括起来。不然有可能会报错

[root@long01 ~]# alias vieth='vi /etc/sysconfig/network-scripts/ifcfg-ens33' [root@long01 ~]# alias alias cp='cp -i' alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' 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 vieth='vi /etc/sysconfig/network-scripts/ifcfg-ens33' alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

下面我直接使用vieth就可以编辑网卡配置文件了

[root@long01 ~]# vieth TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static DEFROUTE=yes IPV4_FAILURE_FATAL=no NAME=ens33 DEVICE=ens33 ONBOOT=yes DNS1=223.5.5.5 DNS2=61.139.2.69 IPADDR=10.1.1.25 PREFIX=24 NETMASK=255.255.255.0 GATEWAY=10.1.1.1 ~

当然,这样设置的别名只是临时生效的,重新登录终端,或重启系统就会失效。

那么怎样才能使它永久生效呢?

我们需要将定义别名写入到一个配置文件中,/etc/profile,这个配置文件是会开机自动加载的一个配置文件。

写入配置文件的方法有两个:

1:编辑/etc/profile文件,在文件末尾加上定义别名的命令。

2:一条命令:这条命令的意思是将双引号中的内容追加到/etc/profile文件的末尾。

echo "alias vieth='vi /etc/sysconfig/network-scripts/ifcfg-ens33'" >> /etc/profile

注意命令中是两个大于号,写成一个大于号就会覆盖掉/etc/profile文件,这样就杯具了。←。←

接下来检查是否添加成功,这条命令的意思是查看/etc/profile文件最后三行内容。

[root@long01 ~]# tail -n3 /etc/profile unset i unset -f pathmunge alias vieth='vi /etc/sysconfig/network-scripts/ifcfg-ens33'

接下来我重新登录看是否还能使用vieth这个别名。

[C:\~]$ Connecting to 10.1.1.25:22... Connection established. To escape to local shell, press 'Ctrl+Alt+]'. Last login: Sat May 12 04:32:01 2018 from 10.1.1.169 [root@long01 ~]# vieth TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static DEFROUTE=yes IPV4_FAILURE_FATAL=no NAME=ens33 DEVICE=ens33 ONBOOT=yes DNS1=223.5.5.5 DNS2=61.139.2.69 IPADDR=10.1.1.25 PREFIX=24 NETMASK=255.255.255.0 GATEWAY=10.1.1.1

OK,如果现在我不需要这个别名了怎样把它取消了呢?

使用ualias命令即可。ualias 跟上需要取消的别名就可以了。

[root@long01 ~]# unalias vieth [root@long01 ~]# vieth -bash: vieth: 未找到命令

当然,如果写入过/etc/profile配置文件中的话,这样取消也是临时的。重新登录后还会存在。

还需要将/etc/profile文件中添加的命令删除才可以永久性的取消。

如果只是临时不想使用别名的效果有两个方式。

1:使用命令的绝对路径。

2:在命令前面加上一个 \ 就可以忽略这个命令的别名了。

4.文件类型


在linux系统中,可以说一切皆文件。文件类型包含有普通文件、目录、字符设备文件、块设备文件、符号链接文件等等。

ls -l 命令所显示的详细信息中,其中每一行第一个字符就表示该文件的文件类型。

下面为Linux中所有文件类型:

b:block (buffered)special #块设备文件 c:character(unbuffered)special #字符设备文件 d:directory #目录 f :regular file #普通文件,表现为 “ - ” l :symbolic link #符号链接文件 p:named pipe (FIFO) #管道文件 s:socket #套接字文件

首先介绍两个命令,file命令和stat命令 。

file命令可以查看指定文件的文件类型,直接file+文件名即可

[root@long01 ~]# file /root/dir1/file1 /root/dir1/file1: ASCII text #普通文本文件会显示为ASCII test [root@long01 ~]# file /root /root: directory #目录 [root@long01 ~]# file /root/dir2/file2 /root/dir2/file2: empty #文件没有任何内容会显示为empty

stat命令可以查看文件或目录的详细属性。

[root@long01 ~]# stat /root File: '/root' Size: 196 Blocks: 0 IO Block: 4096 directory Device: 803h/2051d Inode: 33574977Links: 3 Access: (0550/dr-xr-x---) Uid: (0/root) Gid: (0/root) Context: system_u:object_r:admin_home_t:s0 Access: 2018-05-12 22:04:04.936392782 +0800 Modify: 2018-05-12 14:15:07.408143463 +0800 Change: 2018-05-12 14:15:07.408143463 +0800 Birth: - [root@long01 ~]# stat anaconda-ks.cfg File: 'anaconda-ks.cfg' Size: 1366 Blocks: 8 IO Block: 4096 regular file Device: 803h/2051d Inode: 33574978Links: 1 Access: (0600/-rw-------) Uid: (0/root) Gid: (0/root) Context: system_u:object_r:admin_home_t:s0 Access: 2018-05-10 04:43:19.839017021 +0800 Modify: 2018-05-10 04:43:19.839017021 +0800 Change: 2018-05-10 04:43:19.839017021 +0800 Birth: - [root@long01 ~]#

块设备:

就是存储数据供系统及程序访问的接口设备,如硬盘、光驱等。

块设备文件第一个属性为“b”。

[root@long01 ~]# ls -ld /dev/sda brw-rw----. 1 root disk 8, 0 512 18:00 /dev/sda [root@long01 ~]# file /dev/sda /dev/sda: block special

字符设备:

字符设备就是串行端口的接口设备,如键盘,鼠标等串口设备。、

字符设备文件第一个属性为“c”。

[root@long01 ~]# ls -l /dev/tty crw-rw-rw-. 1 root tty 5, 0 512 04:00 /dev/tty

目录:

目录在Linux是一个比较特殊的文件。

目录的第一个属性是为“d”

[root@long01 ~]# ls -ld /etc drwxr-xr-x. 74 root root 8192 512 18:00 /etc [root@long01 ~]# file /etc /etc: directory

普通文件:

Linux系统中,普通文件又有三种

即纯文本文件,二进制文件和数据格式的文件。

纯文本文件,其内容可以用cat命令查看,使用vi来编辑它,比如配置文件几乎都是这种文件

[root@long01 ~]# ls -dl /etc/profile -rw-r--r--. 1 root root 1855 512 08:59 /etc/profile [root@long01 ~]# file /etc/profile /etc/profile: ASCII text

二进制文件,Linux中的可执行文件,比如命令ls、cat、vi等等,不能直接使用cat等命令查看文件的内容类似于Windows的.exe文件;

[root@long01 ~]# ls -dl /usr/bin/ls /usr/bin/cat -rwxr-xr-x. 1 root root 54080 116 2016 /usr/bin/cat -rwxr-xr-x. 1 root root 117656 116 2016 /usr/bin/ls [root@long01 ~]# file /usr/bin/ls /usr/bin/cat /usr/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=3d705971a4c4544545cb78fd890d27bf792af6d4, stripped /usr/bin/cat: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=fac04659ab9a437b5384c09f4731023373821a39, stripped

数据格式文件,比较少见,是一种具有特定格式的文件。比如用户登录的信息的存放文件:/var/log/wtmp

如果直接cat查看这个文件就会出现乱码

[root@long01 ~]# ls -ld /var/log/wtmp -rw-rw-r--. 1 root utmp 9600 512 18:01 /var/log/wtmp [root@long01 ~]# file /var/log/wtmp /var/log/wtmp: data

使用特定的命令就可以查看它的内容

[root@long01 ~]# last /var/log/wtmp wtmp begins Thu May 10 04:44:43 2018

符号链接文件:

符号链接文件就是跟Windows快捷方式类似的文件,它会指向另外一个文件。

符号链接文件在详细信息中第一个字符为 l

[root@long01 ~]# ls -dl /etc/rc.local lrwxrwxrwx. 1 root root 13 5月 10 04:41 /etc/rc.local -> rc.d/rc.local [root@long01 ~]# file /etc/rc.local /etc/rc.local: symbolic link to `rc.d/rc.local'

管道文件:

管道文件(FIFO,pipe)也是一种特殊的文件类型,他主要的目的在解决多个程序同时存取一个文件所造成的错误问题。 FIFO是first-in-first-out的缩写。第一个属性为 p

[root@long01 ~]# ls -ld /run/systemd/initctl/fifo prw-------. 1 root root 0 512 18:00 /run/systemd/initctl/fifo [root@long01 ~]# file /run/systemd/initctl/fifo /run/systemd/initctl/fifo: fifo (named pipe)

套接字文件:

套接字文件(.sock)是一类特殊的文件,这类文件通常用在网络之间进行数据连接,如我们可以启动一个程序来监听客户端的请求,客户端可以通过套接字来进行数据通信。第一个属性为 s

[root@long01 ~]# ls -ld /dev/log srw-rw-rw-. 1 root root 0 512 18:00 /dev/log [root@long01 ~]# file /dev/log /dev/log: socket

5.绝对路径&相对路径


在Linux中,绝对路径是从/(也被称为根目录)开始的,比如/usr、/etc/profile,如果一个路径是从/开始的,它一定是绝对路径,这样就好理解了。

pwd命令可以查看用户当前所在目录的绝对路径:

[root@long01 ~]# pwd /root [root@long01 ~]# cd /etc/sysconfig/ #切换用户当前所在的目录 [root@long01 sysconfig]# pwd /etc/sysconfig

而相对路径则表示以相对于当前目录来说的一个路径表示方式。

ls -a命令中,会列出两个特殊的目录“.”和“..”:

[root@long01 ~]# ls -al #这里会列出很多内容,我只复制了两个特殊的文件夹 总用量 152 dr-xr-x---. 6 root root 225 512 10:07 . #当前目录 dr-xr-xr-x. 17 root root 224 512 04:00 .. #上一级目录

其中以一个点命名的目录,表示的是当前目录

[root@long01 ~]# pwd /root [root@long01 ~]# ls . anaconda-ks.cfg dir1 dir2 dir3 ls [root@long01 ~]# ls /root anaconda-ks.cfg dir1 dir2 dir3 ls

以两个点命名的目录表示的是上一级目录:

[root@long01 selinux]# cd /etc/selinux/tmp #随意切换到一个目录 [root@long01 tmp]# pwd #查看当前所在目录 /etc/selinux/tmp [root@long01 tmp]# cd .. #切换到上一级目录 [root@long01 selinux]# pwd #再次查看当前所在的目录 /etc/selinux #结果为/etc/selinux

切换到上级目录下的另外一个目录,和切换上上上….级目录:

[root@long01 /]# cd /etc/sysconfig/network-scripts/ [root@long01 network-scripts]# pwd /etc/sysconfig/network-scripts [root@long01 network-scripts]# cd ../console/ [root@long01 console]# pwd /etc/sysconfig/console [root@long01 console]# cd ../../../ [root@long01 /]# pwd / #/为所有文件的顶点,所以在根目录下的 .. 还是表示根目录

还有一个特殊的相对路径 “~”,

“~”表示的是当前用户的家目录,家目录(/home)相当于Windows的C:\users

如果是root用户它则表示为/root/

[root@long01 /]# cd ~/dir1 [root@long01 dir1]# pwd /root/dir1

如果是普通用户,它则表示普通用户的家目录 /home/用户名

[root@long01 ~]# su - user1 #切换到user1用户下。 这命令为切换用户的命令,暂时不详细解释。 [user1@long01 ~]$ pwd /home/user1 [user1@long01 ~]$ mkdir test [user1@long01 ~]$ cd ~/test [user1@long01 test]$ pwd /home/user1/test

6.cd切换目录


cd命令用于切换用户当前所在的目录,

用法就是 cd加上一个绝对路径或者相对路径,就可以把用户当前所在目录切换到指定的路径下

[root@long01 ~]# cd /root/dir1 [root@long01 dir1]# pwd /root/dir1 [root@long01 dir1]# cd .. [root@long01 ~]# pwd /root [root@long01 ~]# cd ~/dir2 [root@long01 dir2]# pwd /root/dir2 [root@long01 dir2]#

cd有一个特殊的用法 cd - 切换到上次所在的目录,不停的cd - 就会在两个目录间来回切换

[root@long01 ~]# cd /dev [root@long01 dev]# cd /root [root@long01 ~]# cd - /dev [root@long01 dev]# cd - /root [root@long01 ~]# cd - /dev [root@long01 dev]# cd - /root [root@long01 ~]# cd - /dev [root@long01 dev]#

7.创建和删除目录


创建目的命令为:mkdir

创建目录默认为在当前所在的目录下创建。也可以使用绝对路径或相对路径创建目录

[root@long01 ~]# mkdir test1 #在当前目录下创建test1目录 [root@long01 ~]# ls -l 总用量 4 -rw-------. 1 root root 1366 510 04:43 anaconda-ks.cfg drwxr-xr-x. 2 root root 6 512 20:24 test1 [root@long01 ~]# cd test1/ #切换到test1目录下 [root@long01 test1]# mkdir ../test2 #在上一级目录创建test2目录 [root@long01 test1]# cd .. #切换到上一级目录 [root@long01 ~]# ls -l #查看当前目录下的文件和目录 总用量 4 -rw-------. 1 root root 1366 510 04:43 anaconda-ks.cfg drwxr-xr-x. 2 root root6 512 20:24 test1 drwxr-xr-x. 2 root root6 512 20:27 test2

mkdir有一个常用的选项 -p,

它的作用是:当创建一个目录时,路径中有某个目录不存在时,使用-p选项后会自动创建路径中不存在的目录。

例如:

[root@long01 ~]# mkdir test1/dir1/dir2/dir3 #其中dir1和dir2都不存在,不加-p选项则无法创建dir3 mkdir: 无法创建目录"test1/dir1/dir2/dir3": 没有那个文件或目录 [root@long01 ~]# ls test1/ [root@long01 ~]# mkdir -p test1/dir1/dir2/dir3 #加上 -p 选项就可以创建成功 [root@long01 ~]# tree test1/ test1/ └── dir1 └── dir2 └── dir3 3 directories, 0 files

删除目录的命令为rmdir:

这个命令只能删除空目录,当目录下存在文件时不能删除

[root@long01 ~]# ls test1 dir1 [root@long01 ~]# rmdir test1 rmdir: 删除 "test1" 失败: 目录非空

rmdir也有-p选项,他可以删除一个没有其他分支的路径。

[root@long01 ~]# mkdir -p test2/dir1/dir2/dir3/dir4/dir5 [root@long01 ~]# mkdir test2/dir1/dir2/dir3/test [root@long01 ~]# tree test2 test2 └── dir1 └── dir2 └── dir3 ├── dir4 │ └── dir5 └── test #dir3目录下有dir4和test两个目录 6 directories, 0 files [root@long01 ~]# rmdir -p test2/dir1/dir2/dir3/dir4/dir5/#删除这个路径 rmdir: 删除目录 "test2/dir1/dir2/dir3" 失败: 目录非空 [root@long01 ~]# tree test2 test2 └── dir1 └── dir2 └── dir3 └── test #结果只删除了dir4目录 9 directories, 1 file [root@long01 ~]# rmdir test2/dir1/dir2/dir3/test/ #删除test目录 [root@long01 ~]# rmdir -p test2/dir1/dir2/dir3/ #再删除这个路径 [root@long01 ~]# ls anaconda-ks.cfg test1 #连同test2目录也一起删除掉了

rmdir命令有一定局限性,使用起来很不方便。

还有一个删除命令:rm ,它可以删文件,也可以删目录,删除目录需要加一个选项 -r 。

[root@long01 ~]# ls anaconda-ks.cfg test1 [root@long01 ~]# rm test1 rm: 无法删除"test1": 是一个目录 [root@long01 ~]# rm -r test1 rm:是否进入目录"test1"? y rm:是否进入目录"test1/dir1"? y rm:是否进入目录"test1/dir1/dir2"? y rm:是否删除目录 "test1/dir1/dir2/dir3"?y rm:是否删除目录 "test1/dir1/dir2"?y rm:是否删除目录 "test1/dir1"?y rm:是否删除目录 "test1"?y

删除的时候需要进行确认,如果目录下有很多文件岂不是很麻烦?

这里还有一个选项 -f ,意为force,强制删除,不提示确认。

[root@long01 ~]# rm -r test1 rm:是否进入目录"test1"? [root@long01 ~]# rm -rf test1 [root@long01 ~]# ls anaconda-ks.cfg [root@long01 ~]#

系统为了安全起见,给rm定义了一个带 -i 选项的别名,

-i选项的作用就是在删除一个已存在的文件或目录时系统会提示确认。

我们可以使用 \rm 来忽略这个别名,这样也不会提示确认了。

[root@long01 ~]# mkdir -p test1/dir1/dir2/dir3 [root@long01 ~]# tree test1 test1 └── dir1 └── dir2 └── dir3 3 directories, 0 files [root@long01 ~]# \rm -r test1 [root@long01 ~]# ls anaconda-ks.cfg

-v选项可以显示命令执行的过程,部分命令有这个选项。

[root@long01 ~]# mkdir -pv test1/dir1/dir2/dir3 mkdir: 已创建目录 "test1" mkdir: 已创建目录 "test1/dir1" mkdir: 已创建目录 "test1/dir1/dir2" mkdir: 已创建目录 "test1/dir1/dir2/dir3" [root@long01 ~]# rm -rfv test1 已删除目录:"test1/dir1/dir2/dir3" 已删除目录:"test1/dir1/dir2" 已删除目录:"test1/dir1" 已删除目录:"test1"

注:使用rm命令时一定要再三确认。

8.创建和删除文件


Linux创建文件有很多种方式:touch,vi等等都可以创建文件

其中最常用的就是touch了,比如创建一个test1.txt文件:

[root@long01 ~]# touch test1.txt [root@long01 ~]# ls anaconda-ks.cfg test1.txt

如果touch的文件已经存在了则会改变该文件的时间标签为当前系统时间。

[root@long01 ~]# ls -l total 4 -rw-------. 1 root root 1366 May 12 14:27 anaconda-ks.cfg -rw-r--r--. 1 root root 0 May 12 14:15 test1.txt [root@long01 ~]# touch anaconda-ks.cfg [root@long01 ~]# ls -l total 4 -rw-------. 1 root root 1366 May 12 14:29 anaconda-ks.cfg -rw-r--r--. 1 root root 0 May 12 14:15 test1.txt

touch还可以单独修改文件或目录的访问时间和修改时间。

修改时间的方式有两个选项:

-t(时间格式:201805121200 表示2018年5月12日12:00)

-d(时间格式:“20180512 12:00”需用双引号括起来)和-t选项只是时间格式不同。

还有两个选项 -a 修改访问时间(access) -m修改文件内容的修改时间(modify)

例:

[root@long01 ~]# touch -at 201805011200 test1.txt #使用-t的时间格式修改test1.txt文件的访问时间 [root@long01 ~]# stat test1.txt File: 'test1.txt' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 803h/2051d Inode: 33574992 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: (0/root) Context: unconfined_u:object_r:admin_home_t:s0 Access: 2018-05-01 12:00:00.000000000 +0800 Modify: 2018-05-12 14:15:07.408143463 +0800 Change: 2018-05-12 15:04:50.180815164 +0800 Birth: - [root@long01 ~]# touch -md "20180502 12:00" test1.txt #使用-d的时间格式修改test1.txt文件的修改时间 [root@long01 ~]# stat test1.txt File: 'test1.txt' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 803h/2051d Inode: 33574992 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: (0/root) Context: unconfined_u:object_r:admin_home_t:s0 Access: 2018-05-01 12:00:00.000000000 +0800 Modify: 2018-05-02 12:00:00.000000000 +0800 Change: 2018-05-12 15:05:35.061810224 +0800 Birth: -

使用vi编辑一个不存在的文件时,当执行保存退出的操作后也会创建对应的文件。

[root@long01 ~]# ls 10:00 anaconda-ks.cfg test1.txt [root@long01 ~]# vi test2.txt #使用Vi编辑后执行保存退出操作。如果不执行保存退出则不会创建该文件。 [root@long01 ~]# ls 10:00 anaconda-ks.cfg test1.txt test2.txt

删除文件使用rm命令,其操作跟删除目录大同小异。删除文件时不用加-r选项

[root@long01 ~]# rm test1.txt rm: remove regular empty file 'test1.txt'? y #当rm不加任何选项时,也会带有别名的-i选项,需要输入y进行确认

也可以使用-f选项、命令绝对路径或使用 \rm 的方式跳过确认删除的操作。

9.PATH环境变量、which


PATH说简单点就是一个字符串变量,当输入命令的时候LINUX会去查找PATH里面记录的路径。

PATH中每个路径使用 : 分隔

查看当前系统的PATH环境变量:

[root@long01 ~]# echo $PATH #这里echo是一个输出命令,先不细说,$符的作用是获取一个变量的值, /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

例:

首先我们创建一个目录 /test 然后将/usr/bin/ls命令复制到/test目录下并且改名为list,

[root@long01 ~]# mkdir /test [root@long01 ~]# cp /usr/bin/ls /t test/ tmp/ [root@long01 ~]# cp /usr/bin/ls /test/list #复制并改名,后面会写 [root@long01 ~]# ls -l /test/ total 116 -rwxr-xr-x. 1 root root 117656 May 12 15:56 list

然后直接执行list命令,发现并不能执行。

[root@long01 ~]# list -bash: list: 未找到命令

接下来将/test/目录加入环境变量。

[root@long01 ~]# PATH=$PATH:/test #Linux系统可以这样定义变量和给变量重新赋值,关于变量这里就不细说了 [root@long01 ~]# echo $PATH /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/test

然后list命令就可以直接执行了。

[root@long01 ~]# list /usr bin etc games include lib lib64 libexec local sbin share src tmp

当然这样修改的环境变量也只是临时的,重启系统或者重新登录后就会失效。

也需要将命令写入/etc/profile文件中。

[root@long01 ~]# echo 'PATH=$PATH:/test' >> /etc/profile [root@long01 ~]# tail -n3 /etc/profile unset -f pathmunge alias vieth='vi /etc/sysconfig/network-scripts/ifcfg-ens33' PATH=$PATH:/test

这里echo的内容使用单引号会将内容原封不动的写入文件中,而使用双引号会解析变量后再写入文件中。

例如:

[root@long01 ~]# echo '$PATH' #使用单引号不会解析$变量 $PATH [root@long01 ~]# echo "$PATH" #使用双引号会解析变量再输出。 /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/tmp:/test:/test

但是/etc/profile文件是对全局生效的配置文件,修改后对每一个用户都会生效,这样就不是很安全。

用户的家目录下有一个文件 .bashrc。这个文件每个用户的家目录下都有。 这是用户私有的配置文件

[root@long01 ~]# ls -lad .bashrc -rw-r--r--. 1 root root 176 12月 29 2013 .bashrc

修改这个文件只对单独一个用户生效。所以我们在这个文件里更改环境变量更安全。

先用vi将/etc/profile文件中我们自己添加的内容删除了。

[root@long01 ~]# cp /etc/profile /etc/profile.bak #需要执行删除操作前进行备份。 [root@long01 ~]# vi /etc/profile #编辑/etc/profile文件,删除我们自己添加的内容 [root@long01 ~]# tail -n3 /etc/profile #查看/etc/profile文件,确认没有了自己添加的内容, unset i unset -f pathmunge

此时重新登录后再次查看PATH变量,已经没有了/test路径了。list也不能执行了

[root@long01 ~]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin [root@long01 ~]# list -bash: list: 未找到命令

将配置命令写入.bashrc配置文件中

[root@long01 ~]# echo 'PATH=$PATH:/test/' >> /root/.bashrc [root@long01 ~]# tail -n3 /root/.bashrc . /etc/bashrc fi PATH=$PATH:/test/

重新加载.bashrc配置文件

[root@long01 ~]# source .bashrc #source命令可以将一个文件当做一个命令来执行,重新执行.bashrc,就相当于重载了.bashrc的配置。 [root@long01 ~]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/test/ [root@long01 ~]# list 10:00 anaconda-ks.cfg test2.txt

切换用户后环境变量并没有被更改。

[user1@long01 ~]$ echo $PATH /usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/user1/.local/bin:/home/user1/bin [user1@long01 ~]$ list -bash: list: 未找到命令

which命令:

which命令可以查看一个或多个命令、可执行文件的绝对路径。并且会显示命令的别名

which命令是通过遍历PATH环境变量路径的方式来查找文件的,

如果一个命令不存在于PATH中的任何一个路径下,使用which命令是查找不到的。

[root@long01 ~]# which ls cat mv alias ls='ls --color=auto' /usr/bin/ls alias mv='mv -i' /usr/bin/mv /usr/bin/cat

例:

我在/test/路径下创建一个普通文件看which是否能查找到,目前PATH中有/test/这个路径

[root@long01 test]# touch test1 [root@long01 test]# which test1 /usr/bin/which: no test1 in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/test/)

这样的普通文件which并不会查找。那么给这个文件加上执行权限呢?

[root@long01 test]# chmod +x test1 #chmod 修改文件权限,后面会写到。 [root@long01 test]# which test1 /test/test1

加上执行权限后就可以被which查找到了。

在不存在于PATH的路径中创建有执行权限的文件,which也找不到。

[root@long01 ~]# pwd /root [root@long01 ~]# touch test1 [root@long01 ~]# which test1 /usr/bin/which: no test1 in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/test/) [root@long01 ~]# chmod +x test1 [root@long01 ~]# which test1 /usr/bin/which: no test1 in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/test/)

10.复制、移动和重命名


复制操作后会有两个内容一样的文件或目录。跟Windows的复制粘贴一样。

复制命令:cp。

例:

先在/root目录下创建两个目录dir1 dir2,并且在dir1下创建一个file1文件,在dir2下创建一个dir2文件

[root@long01 ~]# mkdir dir1 dir2 [root@long01 ~]# ls dir1 dir2 [root@long01 ~]# touch dir1/file1 [root@long01 ~]# touch dir2/file2 [root@long01 ~]# tree . . ├── dir1 │ └── file1 └── dir2 └── file2 2 directories, 2 files

然后使用cp命令将file1复制到dir2目录下,将file2复制到dir1目录下,

复制文件时,如果目标是一个目录则会将文件放到该目录下,

[root@long01 ~]# cp dir1/file1 dir2 [root@long01 ~]# cp dir2/file2 dir1 [root@long01 ~]# tree . ├── dir1 │ ├── file1 │ └── file2 └── dir2 ├── file1 └── file2 2 directories, 4 files

复制文件时,如果目标目录不存在,则会将源文件复制到目标的位置并且重命名为目标名。

例:如果我想把dir2/file2文件复制到dir1下面的dir3目录,但是dir3目录并不存在,这样会把文件复制到dir1下重命名为dir3.

[root@long01 ~]# cp dir2/file2 dir1/dir3 [root@long01 ~]# tree . ├── dir1 │ ├── dir3 │ ├── file1 │ └── file2 └── dir2 ├── file1 └── file2 3 directories, 7 files [root@long01 ~]# ls -l dir1 总用量 0 -rw-r--r--. 1 root root 0 5月 13 20:11 dir3 #普通文件。复制dir2/file2 重命名为dir3 -rw-r--r--. 1 root root 0 5月 13 19:51 file1 -rw-r--r--. 1 root root 0 5月 13 19:53 file2

但是如果目标不存在,并且没有上一级目录,则无法复制。

[root@long01 ~]# cp dir1/file1 dir2/dir3/dir4 #dir2下不存在dir3,自然也无法创建dir4 cp: 无法创建普通文件"dir2/dir3/dir4": 没有那个文件或目录

将dir1目录复制到dir2目录下,复制目录需要加一个选项 -r或者-R都可以

[root@long01 ~]# cp -r dir1 dir2 [root@long01 ~]# tree . ├── dir1 │ ├── file1 │ └── file2 └── dir2 ├── dir1 │ ├── file1 │ └── file2 ├── file1 └── file2 3 directories, 6 files

再次将dir1复制到dir2下就会提示覆盖,需要输入y确认才会进行覆盖目标目录与源目录中相同位置的同名文件。

[root@long01 ~]# cp -r dir1 dir2 cp:是否覆盖"dir2/dir1/file1"y cp:是否覆盖"dir2/dir1/file2"y

同样可以使用\cp 和绝对路径来忽略系统定义的-i选项的别名,直接覆盖,不提示确认。

-v 选项可以显示命令的操作过程。

[root@long01 ~]# \cp -vr dir1 dir2/ "dir1/file1" -> "dir2/dir1/file1" "dir1/file2" -> "dir2/dir1/file2"

移动是将一个文件或目录移动到另外一个指定的路径下。跟Windows的剪切,粘贴类似。

mv的操作方式跟cp很相似,与cp不通的是mv像是将一个物体挪到另一个地方,物品的数量没有变化,

例:在dir1目录下创建一个test.txt文件,然后将test.txt文件移动到dir2目录下

[root@long01 ~]# touch dir1/test.txt [root@long01 ~]# ls -l dir1 总用量 0 -rw-r--r--. 1 root root 0 5月 13 19:51 file1 -rw-r--r--. 1 root root 0 5月 13 19:53 file2 -rw-r--r--. 1 root root 0 5月 13 20:23 test.txt [root@long01 ~]# ls -l dir2 总用量 0 -rw-r--r--. 1 root root 0 5月 13 19:53 file1 -rw-r--r--. 1 root root 0 5月 13 19:53 file2 [root@long01 ~]# mv dir1/test.txt dir2/ [root@long01 ~]# ls -l dir1 dir2 dir1: 总用量 0 -rw-r--r--. 1 root root 0 5月 13 19:51 file1 -rw-r--r--. 1 root root 0 5月 13 19:53 file2 dir2: 总用量 0 -rw-r--r--. 1 root root 0 5月 13 19:53 file1 -rw-r--r--. 1 root root 0 5月 13 19:53 file2 -rw-r--r--. 1 root root 0 5月 13 20:23 test.txt

移动文件时,如果目标不是一个目录,也不存在同名文件,这样的操作就是移动后重命名

例:将dir2下的test.txt移动到dir1下重命名为aaaa.txt:

[root@long01 ~]# mv dir2/test.txt dir1/aaaa.txt [root@long01 ~]# ls -l dir1 dir2 dir1: 总用量 0 -rw-r--r--. 1 root root 0 5月 13 20:23 aaaa.txt -rw-r--r--. 1 root root 0 5月 13 19:51 file1 -rw-r--r--. 1 root root 0 5月 13 19:53 file2 dir2: 总用量 0 -rw-r--r--. 1 root root 0 5月 13 19:53 file1 -rw-r--r--. 1 root root 0 5月 13 19:53 file2

如果目标存在同名文件则会提示覆盖,当然不能用目录覆盖一个文件mv

[root@long01 ~]# mv dir1/file1 dir2/file2 #将dir1/file1复制到dir2下重命名为file2 mv:是否覆盖"dir2/file2"y #dir2/file2已经存在,输入y确认后将会用file1覆盖掉file2 [root@long01 ~]# ls -l dir1 dir2 dir1: 总用量 0 -rw-r--r--. 1 root root 0 513 20:23 aaaa.txt -rw-r--r--. 1 root root 0 513 19:53 file2 dir2: 总用量 0 -rw-r--r--. 1 root root 0 513 19:53 file1 -rw-r--r--. 1 root root 0 513 19:51 file2 [root@long01 ~]# mv dir1 dir2/file1 #移动目录时,目标为一个文件则无法覆盖。 mv:是否覆盖"dir2/file1"y mv: 无法以目录"dir1" 来覆盖非目录"dir2/file1"

移动目录时,目标下存在同名的非空目录也是无法覆盖的。

[root@long01 ~]# cp -r dir1 dir2 #先将dir1 复制一份到dir2下。 [root@long01 ~]# ls -l dir1 dir2 dir1: 总用量 0 -rw-r--r--. 1 root root 0 513 2018 aaaa.txt -rw-r--r--. 1 root root 0 513 2018 file2 dir2: 总用量 0 drwxr-xr-x. 2 root root 35 513 12:38 dir1 -rw-r--r--. 1 root root 0 513 2018 file1 -rw-r--r--. 1 root root 0 513 2018 file2 [root@long01 ~]# mv dir1/ dir2/ #dir2下已经存在一个dir1 在将当前目录的dir1移动到dir2下,就提示无法覆盖了。 mv:是否覆盖"dir2/dir1"y mv: 无法将"dir1/" 移动至"dir2/dir1": 文件已存在

重命名是更改文件或目录的名字,并不会对文件或目录的内容造成影响。

上面的cp,mv命令都可以实现重命名的操作。 还用一个重命名专用的命令rename

renam是通过字符串匹配来重命名的,

它需要3个参数,1:原文件名中存在的字符串,2:目标字符串,3:原文件名或路径。

通常用法:将aaaa.txt重命名为bbbb.txt

[root@long01 ~]# rename aaaa bbbb dir1/aaaa.txt #rename会将aaaa与文件名匹配,匹配上的第一个字符串换成bbbb [root@long01 ~]# ls -l dir1 总用量 0 -rw-r--r--. 1 root root 0 513 2018 bbbb.txt -rw-r--r--. 1 root root 0 513 2018 file2 [root@long01 ~]#

rename只会更改与文件名中第一个匹配的字符串

[root@long01 ~]# rename b cccc dir1/bbbb.txt [root@long01 ~]# ls -l dir1 总用量 0 -rw-r--r--. 1 root root 0 5月 13 2018 ccccbbb.txt -rw-r--r--. 1 root root 0 5月 13 2018 file2

rename还支持通配符,正则等操作,单是也不常用,这里就不详细写了。详细参考:http://man.linuxde.net/rename


11.文档查看cat /more/less/head/tail

这里的文件查看只能查看普通的文本文件,二进制文件,和数据文件是不支持直接查看的。

cat:

cat会一次将整个文件的内容都显示出来,只适合用来查看小文件,用来查看大文件就不是很方便了。

例:查看/etc/sysconfig/network-scripts/ifcfg-ens33网卡配置文件:

[root@long01 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33 TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static DEFROUTE=yes IPV4_FAILURE_FATAL=no NAME=ens33 DEVICE=ens33 ONBOOT=yes DNS1=223.5.5.5 DNS2=61.139.2.69 IPADDR=10.1.1.25 PREFIX=24 NETMASK=255.255.255.0 GATEWAY=10.1.1.1

cat有一个常用选项 -n 用于显示文件内容的行号:

[root@long01 ~]# cat -n /etc/sysconfig/network-scripts/ifcfg-ens33 1 TYPE=Ethernet 2 PROXY_METHOD=none 3 BROWSER_ONLY=no 4 BOOTPROTO=static 5 DEFROUTE=yes 6 IPV4_FAILURE_FATAL=no 7 NAME=ens33 8 DEVICE=ens33 9 ONBOOT=yes 10 DNS1=223.5.5.5 11 DNS2=61.139.2.69 12 IPADDR=10.1.1.25 13 PREFIX=24 14 NETMASK=255.255.255.0 15 GATEWAY=10.1.1.1

与cat对应的还有一个tac命令,它的作用是以行为单位倒序列出文件内容,即从最后一行开始倒序列出。

[root@long01 ~]# tac /etc/sysconfig/network-scripts/ifcfg-ens33 GATEWAY=10.1.1.1 NETMASK=255.255.255.0 PREFIX=24 IPADDR=10.1.1.25 DNS2=61.139.2.69 DNS1=223.5.5.5 ONBOOT=yes DEVICE=ens33 NAME=ens33 IPV4_FAILURE_FATAL=no DEFROUTE=yes BOOTPROTO=static BROWSER_ONLY=no PROXY_METHOD=none TYPE=Ethernet

more命令:

more命令查看的文件内容如果不足一屏,其效果跟cat类似。

查看大文件时可以进行上下翻页等操作

空格键:向后翻一屏内容 B键:向前翻一屏内容 回车键:向后翻一行内容 F键:直接跳到文件末尾。 more查看文件到末尾会自动退出 所以F键和Q键的功能相似 Q键:直接退出more命令。

less命令:

less命令跟more命令相似,less浏览到文件末尾不会自动退出,而且支持关键字搜索功能。 less有一个常用的选项,-N 可以显示行号

退出:Q键 到文件头:G键 到文件尾:Shift+G 向下翻页:空格键,F键,PageDown键 向上翻页:B键,PageUp键,U键,W键 向下一行:回车键,下方向键,K键 向上一行:上方向键,J键,E键 显示帮助页面:H键 q键退出帮助页面 /关键字:向后搜索文件内容中的关键字。 ?关键字:向前搜索文件内容中的关键字。 N键:搜索模式下跳到下一个搜索到的关键字 Shift+N:搜索模式下跳到上一个搜索到的关键字

head命令:

head命令查看指定文件头部的内容,默认为十行。

[root@long01 dev]# head test.txt | cat -n #这里使用的“|”管道符,它的作用是将前面命令的结果交给下一个命令来处理 1 # /etc/profile 2 3 # System wide environment and startup programs, for login setup 4 # Functions and aliases go in /etc/bashrc 5 6 # It's NOT a good idea to change this file unless you know what you 7 # are doing. It's much better to create a custom.sh shell script in 8 # /etc/profile.d/ to make custom changes to your environment, as this 9 # will prevent the need for merging in future updates. 10

还可以指定查看多少行:下面三种命令格式效果都是一样的

[root@long01 dev]# head -3 test.txt | cat -n 1 # /etc/profile 2 3 # System wide environment and startup programs, for login setup [root@long01 dev]# head -n3 test.txt | cat -n 1 # /etc/profile 2 3 # System wide environment and startup programs, for login setup [root@long01 dev]# head -n 3 test.txt | cat -n 1 # /etc/profile 2 3 # System wide environment and startup programs, for login setup

还可以指定查看多少个字符的内容,这里要用到-c选项。Linux中字母和数字都为一个字符,汉字为3个字符。换行符也会算一个字符

[root@long01 dev]# head -c 3 test2.txt 123[root@long01 dev]# head -c 5 test2.txt #head使用-c选项时可能不会换行,所以输出的内容是这样的。 123[root@long01 dev]# head -c 6 test2.txt 123一[root@long01 dev]#

tail命令:

tail命令跟head命令相反,它查看的是文件尾部的内容。默认为10行

[root@long01 ~]# tail test.txt | cat -n 1 if [ "${-#*i}" != "$-" ]; then 2 . "$i" 3 else 4 . "$i" >/dev/null 5 fi 6 fi 7 done 8 9 unset i 10 unset -f pathmunge

查看指定行数的方式跟head一样

[root@long01 ~]# tail -n2 test.txt unset i unset -f pathmunge [root@long01 ~]# tail -n 2 test.txt unset i unset -f pathmunge [root@long01 ~]# tail -2 test.txt unset i unset -f pathmunge

-c选项也跟head类似,tail的-c选项是从文件的最后一个字符开始计算的。

[root@long01 ~]# tail -c 10 test.txt #查看test.txt文件中最后十个字符。 pathmunge #结果只有9个字符,其实还包含了一个换行符。

-f选项可以实时监控一个文件尾部增加的每一行内容,Ctrl+C可以结束监控。

12.文件权限


[root@long01 ~]# ls -l /usr 总用量 100 一列 二列 三列 四列 五列 六列 七列 八列 九列 dr-xr-xr-x. 2 root root 20480 512 07:31 bin drwxr-xr-x. 2 root root 6 115 2016 etc drwxr-xr-x. 2 root root 6 115 2016 games drwxr-xr-x. 3 root root 23 510 04:41 include ls -l 显示的文件或目录信息中 第一位为文件类型, 后面九位为文件权限 每三位分为一段,第一段表示该文件拥有者的权限,第二段表示文件所属组的权限,第三段表示其他人的权限 每段第一位表示读取权限,以 r 表示,可以读取文件内容, 用ls可以查看目录下问文件等。 每段第二位表示写入权限,以 w 表示,可以对编辑修改文件,目录下可以创建与删除文件和目录。 每段第三为表示执行权限,以 x 表示,可以直接执行文件,目录可以使用cd命名切换到改目录下。 如果某一位为 - ,则表示没有相应的权限,无法执行该权限对应的操作。 第一列第十位的小数点,为SELINUX相关的标识,关掉SELINUX后创建的文件就没有了。 第二列的数字表示该文件存在多少个硬链接,也就是有多少个相同inode号的文件 第三列表示文件的所属用户,第四列表示文件所属的用户组。 第五列表示该文件或目录的大小。 第六、七、八列表示文件的创建或修改时间。 第九列是该文件的文件名。

13.修改文件属性


chmod:修改文件权限

为了方便我们通常用数字的方式来表示文件的权限信息,

r=4 x=2 x=1 -=0 例如一个文件的权限是:rw-rw-r = 664 ,r-x--x--x =511

chmod命令可以修改一个文件或目录的读写执行的权限。

例:

[root@long01 ~]# ls -l 总用量 0 -rw-r--r--. 1 root root 0 514 02:48 test.txt [root@long01 ~]# chmod 666 test.txt #将test.txt文件的权限修改为666 [root@long01 ~]# ls -l 总用量 0 -rw-rw-rw-. 1 root root 0 514 02:48 test.txt

还可以指定修改一个小段的权限,这里有几个特殊含义的字母:

a:所有人 u:文件的所属用户的权限 g:文件的所属用户组 o:其他人 [root@long01 ~]# chmod a=- test.txt #取消test.txt文件中所有人的所有权限。 [root@long01 ~]# ls -l 总用量 0 ----------. 1 root root 0 514 02:48 test.txt [root@long01 ~]# chmod a=rx test.txt #将txt.txt文件所有人的权限都设置为r-x [root@long01 ~]# ls -l 总用量 0 -r-xr-xr-x. 1 root root 0 514 02:48 test.txt [root@long01 ~]# chmod u+w test.txt #给test.txt文件的所属用户加上 w 权限 [root@long01 ~]# ls -l 总用量 0 -rwxr-xr-x. 1 root root 0 514 02:48 test.txt [root@long01 ~]# chmod o-x test.txt #去掉test.txt文件其他用户的 x 权限。 [root@long01 ~]# ls -l 总用量 0 -rwxr-xr--. 1 root root 0 514 02:48 test.txt

-c选项,用于显示被修改的信息,类似cp的-v选项

[root@long01 ~]# chmod -c o+w test.txt mode of "test.txt" changed from 0754 (rwxr-xr--) to 0756 (rwxr-xrw-)

-R选项,递归处理,修改目录的权限时,将目录下的所有文件和子目录的权限一并修改

[root@long01 ~]# ls -dl test/ drwxr-xr-x. 2 root root 22 514 03:19 test/ [root@long01 ~]# ls -l test/ 总用量 0 -rwxr-xrw-. 1 root root 0 514 02:48 test.txt [root@long01 ~]# chmod -R 777 test/ [root@long01 ~]# ls -dl test/ drwxrwxrwx. 2 root root 22 514 03:19 test/ [root@long01 ~]# ls -l test/ 总用量 0 -rwxrwxrwx. 1 root root 0 514 02:48 test.txt

umask:修改创建文件或目录时的默认权限的权限掩码 直接执行umask可以查看当前系统的权限掩码。

[root@long01 ~]# umask 0022 #显示为0022,其中第一个0并没有什么实际作用。

CentOS7中,创建一个文件的默认权限为666,创建一个目录的默认权限为777 但是系统的umask默认为022,所以创建一个文件后初始的权限就是644,目录为755

计算方式: 系统默认权限: 目录:rwxrwxrwx = 777 文件:rw-rw-rw- = 666 减去umask掩码: ----wx-wx = 033 ----wx-wx = 033 等于最终创建文件的权限:rwxr--r-- =744 rw-r--r-- 644 由此看来如果系统默认权限中某些权限位没有权限,则不会参与权限掩码的计算

下面将umask 改为044再创建文件和目录看看。

[root@long01 ~]# umask 044 [root@long01 ~]# umask 0044 [root@long01 ~]# touch test.txt [root@long01 ~]# mkdir test [root@long01 ~]# ls -l 总用量 4 -rw-------. 1 root root 1314 513 20:06 anaconda-ks.cfg drwx-wx-wx. 2 root root 6 513 20:34 test -rw--w--w-. 1 root root 0 513 20:34 test.txt

改了umaask后创建目录的默认权限变成了733,文件的权限变成的622。

chown:修改文件所属用户和组

chown命令可以更改文件或目录的用户和和用户组,

例:先创建两个用户 user1、user2 将test.txt文件的所属用户改为user1

[root@long01 ~]# useradd user1 [root@long01 ~]# useradd user2 [root@long01 ~]# ls -d test.txt test.txt [root@long01 ~]# ls -dl test.txt -rw--w--w-. 1 root root 0 513 20:34 test.txt [root@long01 ~]# chown user1 test.txt [root@long01 ~]# ls -dl test.txt -rw--w--w-. 1 user1 root 0 513 20:34 test.txt

再将test.txt的所属用户组改为user2

[root@long01 ~]# chown :user2 test.txt [root@long01 ~]# ls -dl test.txt -rw--w--w-. 1 user1 user2 0 513 20:34 test.txt

将test.txt文件的所属用户和组同时改回root

[root@long01 ~]# chown root:root test.txt [root@long01 ~]# ls -dl test.txt -rw--w--w-. 1 root root 0 513 20:34 test.txt

chown也有一个-R选项,可以在修改目录的用户和组的同时,将目录下的所有子目录和文件也一并修改

[root@long01 ~]# ls -l test 总用量 0 -rw-------. 1 root root 0 513 20:47 test.txt [root@long01 ~]# ls -ld test/ drwx-wx-wx. 2 root root 22 513 20:47 test/ [root@long01 ~]# chown -R user1:user1 test #-R选项修改test目录同时也会修改test目录下的文件。 [root@long01 ~]# ls -ld test/ drwx-wx-wx. 2 user1 user1 22 513 20:47 test/ [root@long01 ~]# ls -l test 总用量 0 -rw-------. 1 user1 user1 0 513 20:47 test.txt

chgrp: 修改文件所属用户组

chgrp命令只能修改文件和目录的所属组。 同样也有-R选项,跟chown的-R选项的功能同理。

[root@long01 ~]# chgrp -R root test [root@long01 ~]# ls -ld test/ drwx-wx-wx. 2 user1 root 22 513 20:47 test/ [root@long01 ~]# ls -l test 总用量 0 -rw-------. 1 user1 root 0 513 20:47 test.txt
转载请注明原文地址: https://www.6miu.com/read-2150318.html

最新回复(0)