admin管理员组

文章数量:1558084

2019独角兽企业重金招聘Python工程师标准>>>

系统目录结构


系统目录结构

  • linux系统目录结构图:http://www.apelearn/study_v2/linux_dir_tree.jpg
/bin :里面装的是用户用的命令,root也可以用
/sbin :里面是只有root能用的命令。
/boot :系统启动的相关文件,里面有系统启动的关键文件,删除后系统无法启动
/dev :linux系统下的设备文件
/etc :配置文件的所在目录
/home :用户的家目录
/lib :存系统库文件的
/lib64 :存系统的库文件 很多命令都会依赖一些库,查看命令依赖关系:ldd
/media : 媒介目录。默认是空的。比如插一个U盘会自动放到这个目录下面去
/mnt : 临时挂载目录,默认也是空的。可以把新增的光驱或硬盘挂载这个目录下面
/opt : 空目录
/proc :里面是一些系统进程
/root :root用户的家目录
/run :一些进程产生的临时文件,重启就会消失的文件
/srv :空的,存服务产生的文件
/sys :存系统内核的相关文件
/tmp :系统的临时目录,任何一个用户都可以在这个文件夹下读写删除文件。
/usr :用户的文件,
/var :是一个很重要的目录。 经常用的/var/log ,系统日志。
  • 从centOS7系统以后,就不再区分32位和64位版本,都是64位系统。

  • 总结:/bin,/sbin,/etc,/var,/usr/local这几个目录是用的最多的。常用的。

  • tree命令:以树形的形式显示一个目录下面都有哪些目录和子文件

如果系统没有tree命令就用yum下载一下

[root@aminglinux-01 /]# yum install -y tree

tree -L 是可以设置显示最大层数。 下面试一下只显示两层目录下的结构

[root@aming1 /]# tree -L 2
.
├── bin -> usr/bin
├── boot
│   ├── config-3.10.0-514.el7.x86_64
│   ├── grub
│   ├── grub2
│   ├── initramfs-0-rescue-b1022dedd3b74686922ac31f3e680f36.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-0-rescue-b1022dedd3b74686922ac31f3e680f36
│   └── vmlinuz-3.10.0-514.el7.x86_64
├── dev
│   ├── agpgart
│   ├── autofs
│   ├── block
│   ├── bsg
│   ├── btrfs-control

ls命令

ls命令是列取目录或者文件的命令

可以列取一下系统根目录看一下Linux系统都有哪些目录

[root@aming1 /]# ls /
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

ls命令总结

ls -l :列取详细信息

ls -i :列取inode

ls -lh :看文件大小更直观

ls -la :列出隐藏文件

ls -lta: t是时间的意思,按时间顺序排列

ls -ld :只列出文件夹

ls命令详解

  • ls -l :列取详细信息(ll命令就是ls -l的缩写)

在inode使用量前面11位。第一位表示文件类型,后面九位表示文件权限,最后一个(.)是受制于selinux。如果关闭selinux,新建的文件或目录就会没有(.)

[root@aminglinux-01 ~]# ls -l
总用量 4
     -        rw-------.               1                        root   root         1422     5月  12 23:06    anaconda-ks.cfg
 文件类型    权限 .(selinux)      有几个文件使用了这个inode   所有者  所属组     文件大小    最后改动日期         文件名字
  • ls -i :列取inode

文件名字前面的数字就是inode号,inode号是储存除了文件名以外的所有文件信息,都存在inode之中。

[root@aming1 ~]# ls
123.txt  anaconda-ks.cfg  test.txt
[root@aming1 ~]# ls -i
33599795 123.txt  33574987 anaconda-ks.cfg  33599785 test.txt

  • ls -lh :显示文件大小更直观
[root@aming1 ~]# ls -l
总用量 12
-rw-r--r--. 1 root root   96 4月  25 21:33 123.txt
-rw-------. 1 root root 1422 3月  17 19:03 anaconda-ks.cfg
-rw-r--r--. 1 root root 1008 4月  26 15:30 test.txt
[root@aming1 ~]# ls -lh
总用量 12K
-rw-r--r--. 1 root root   96 4月  25 21:33 123.txt
-rw-------. 1 root root 1.4K 3月  17 19:03 anaconda-ks.cfg
-rw-r--r--. 1 root root 1008 4月  26 15:30 test.txt

  • ls -la :列出隐藏文件
[root@aming1 ~]# ls -la
总用量 40
dr-xr-x---.  3 root root  194 4月  26 15:30 .
dr-xr-xr-x. 17 root root  224 4月  26 15:29 ..
-rw-r--r--.  1 root root   96 4月  25 21:33 123.txt
-rw-------.  1 root root 1422 3月  17 19:03 anaconda-ks.cfg
-rw-------.  1 root root 2550 5月  11 20:55 .bash_history
-rw-r--r--.  1 root root   18 12月 29 2013 .bash_logout
-rw-r--r--.  1 root root  176 12月 29 2013 .bash_profile
-rw-r--r--.  1 root root  176 12月 29 2013 .bashrc
-rw-r--r--.  1 root root  100 12月 29 2013 .cshrc
drwx------.  2 root root   48 5月  11 10:42 .ssh
-rw-r--r--.  1 root root  129 12月 29 2013 .tcshrc
-rw-r--r--.  1 root root 1008 4月  26 15:30 test.txt
-rw-------.  1 root root 1609 4月  25 21:33 .viminfo

其中.开头的文件都是隐藏文件或者目录。

还有两个目录 点和点点 。“.”代表是当前目录 “..”代表的是上层目录

  • ls -lt ,t是时间的意思,按时间顺序排列,时间越近排在越前面
[root@aming1 ~]# ls -lt 
总用量 12
-rw-r--r--. 1 root root 1008 4月  26 15:30 test.txt
-rw-r--r--. 1 root root   96 4月  25 21:33 123.txt
-rw-------. 1 root root 1422 3月  17 19:03 anaconda-ks.cfg

  • ls -ld :只列出文件夹

  • 也可以同时使用 ls -liat

[root@aming1 ~]# ls -liat
总用量 40
33574979 -rw-------.  1 root root 2550 5月  11 20:55 .bash_history
50332748 drwx------.  2 root root   48 5月  11 10:42 .ssh
33599785 -rw-r--r--.  1 root root 1008 4月  26 15:30 test.txt
33574977 dr-xr-x---.  3 root root  194 4月  26 15:30 .
      64 dr-xr-xr-x. 17 root root  224 4月  26 15:29 ..
33599796 -rw-------.  1 root root 1609 4月  25 21:33 .viminfo
33599795 -rw-r--r--.  1 root root   96 4月  25 21:33 123.txt
33574987 -rw-------.  1 root root 1422 3月  17 19:03 anaconda-ks.cfg
33975719 -rw-r--r--.  1 root root   18 12月 29 2013 .bash_logout
33975720 -rw-r--r--.  1 root root  176 12月 29 2013 .bash_profile
33975721 -rw-r--r--.  1 root root  176 12月 29 2013 .bashrc
33975722 -rw-r--r--.  1 root root  100 12月 29 2013 .cshrc
33975723 -rw-r--r--.  1 root root  129 12月 29 2013 .tcshrc
[root@aming1 ~]# 

  • 其他命令选项使用也有很多,但是不常用,需要的时候可以man一下进行查看

文件类型

  • 文件属性前面第一排就是文件类型,就是在权限前面的

常见的就是: - 是普通文件

d 文件夹

  • 还有一些特殊的文件

c : 字符串设备

l : 软连接文件

b : 块设备,比如光盘,磁盘

s : 通信文件

[root@aminglinux-01 ~]# ls -l /dev/
总用量 0
crw-rw----. 1 root video    10, 175 8月   4 20:53 agpgart
crw-------. 1 root root     10, 235 8月   4 20:53 autofs
drwxr-xr-x. 2 root root         140 8月   4 20:53 block
drwxr-xr-x. 2 root root          80 8月   4 20:53 bsg
crw-------. 1 root root     10, 234 8月   4 20:53 btrfs-control
drwxr-xr-x. 3 root root          60 8月   4 20:53 bus
lrwxrwxrwx. 1 root root           3 8月   4 20:53 cdrom -> sr0
drwxr-xr-x. 2 root root        3020 8月   4 20:53 char
crw-------. 1 root root      5,   1 8月   4 20:53 console
srw-rw-rw-. 1 root root           0 8月   4 20:53 log
lrwxrwxrwx. 1 root root          11 8月   4 20:53 core -> /proc/kcore

alias命令

  • alias 就是 命令的别名,是命令加选线得组合。可以理解给一个很长的命令设置一个简单外号,这样可以方便操作。

  • alias的作用就是,可以简写命令。

  • 用which来查看命令的别名

[root@aminglinux-01 ~]# which ls 
alias ls='ls --color=auto'
	/usr/bin/ls
[root@aminglinux-01 ~]# which ll
alias ll='ls -l --color=auto'
	/usr/bin/ls
  • 可以输入alias来看系统有别名的命令有哪些
[root@aming1 ~]# 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 aming='ls -lha' ”
[root@aminglinux-01 ~]# aming
-bash: aming: 未找到命令
[root@aminglinux-01 ~]# alias aming='ls -lha'
[root@aminglinux-01 ~]# aming
总用量 28K
dr-xr-x---.  3 root root  147 8月   3 09:24 .
dr-xr-xr-x. 17 root root  224 8月   3 21:06 ..
-rw-------.  1 root root 1.4K 7月  31 23:06 anaconda-ks.cfg
-rw-------.  1 root root 1.4K 8月   4 16:45 .bash_history
-rw-r--r--.  1 root root   18 12月 29 2013 .bash_logout
-rw-r--r--.  1 root root  176 12月 29 2013 .bash_profile
-rw-r--r--.  1 root root  176 12月 29 2013 .bashrc
-rw-r--r--.  1 root root  100 12月 29 2013 .cshrc
drwx------.  2 root root   80 8月   3 22:17 .ssh
-rw-r--r--.  1 root root  129 12月 29 2013 .tcshrc

可以看到没有设置别名的时候直接输入aming是不起作用的。可以用which来看一下

[root@aminglinux-01 ~]# which aming
alias aming='ls -lha'
	/usr/bin/ls
  • 取消aming的别名 用unalias命令
[root@aminglinux-01 ~]# unalias aming
[root@aminglinux-01 ~]# aming
-bash: aming: 未找到命令
  • which命令:

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

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

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


相对和绝对路径

  • 绝对路径 :以根“/”开头的路径都是绝对路径。
[root@aminglinux-01 ~]# ls /root/.ssh/authorized_keys 
/root/.ssh/authorized_keys

不管在那个路径下,都能都能通过绝对路径找到文件。从根“/”开始

任何文件都有绝对路径。

  • 相对路径 :相对于当前目录的路径,不是从根“/”开始的路径
[root@aminglinux-01 ~]# cd /etc/sysconfig/
[root@aminglinux-01 sysconfig]# ls network-scripts/ifcfg-ens33 
network-scripts/ifcfg-ens33
[root@aminglinux-01 sysconfig]# pwd
/etc/sysconfig
  • pwd命令是用来显示当前路径,当前的目录是哪里。
  • cd 是切换目录的命令

cd命令

cd 切换目录命令。change directory 的缩写

  • cd - :这个命令的就是切换上一次目录的所在位置。切换非常方便。
[root@aminglinux-01 sysconfig]# cd -
/root
[root@aminglinux-01 ~]# cd -
/etc/sysconfig
[root@aminglinux-01 sysconfig]# cd -
/root
[root@aminglinux-01 ~]# cd -
/etc/sysconfig
[root@aminglinux-01 sysconfig]# 
  • 直接输入命令:cd

无论在哪个目录下,输入cd都会回到登陆系统的用户的家目录下。

[root@aminglinux-01 sysconfig]# pwd
/etc/sysconfig
[root@aminglinux-01 sysconfig]# cd
[root@aminglinux-01 ~]# pwd
/root
[root@aminglinux-01 ~]# 
  • 命令:cd ~

同样是直接返回用户的家目录

  • 命令:cd ..

返回上一层目录

  • 其他选项不常用,可以man cd 来查看用法。

创建和删除目录

创建目录 mkdir

mkdir是make directory的缩写

[root@aming1 ~]# ls
123.txt  anaconda-ks.cfg  test.txt
[root@aming1 ~]# mkdir mulu
[root@aming1 ~]# ls
123.txt  anaconda-ks.cfg  mulu  test.txt

  • mkdir -p 创建连续的文件夹

正常用mkdir创建连续的文件夹是不可以的,如下

[root@aming1 ~]# mkdir 111/222/333/444/555
mkdir: 无法创建目录"111/222/333/444/555": 没有那个文件或目录
[root@aming1 ~]# 

加-p 属性即可创建成功

[root@aming1 ~]# mkdir -p 111/222/333/444/555
[root@aming1 ~]# tree 
.
├── 111
│   └── 222
│       └── 333
│           └── 444
│               └── 555
├── 123.txt
├── anaconda-ks.cfg
├── mulu
└── test.txt

6 directories, 3 files
[root@aming1 ~]# 

  • mkdir -pv 创建连续的文件夹并显示创建过程
[root@aming1 ~]# mkdir -pv 1111/2222/3333/4444/5555
mkdir: 已创建目录 "1111"
mkdir: 已创建目录 "1111/2222"
mkdir: 已创建目录 "1111/2222/3333"
mkdir: 已创建目录 "1111/2222/3333/4444"
mkdir: 已创建目录 "1111/2222/3333/4444/5555"
[root@aming1 ~]# 

删除目录 rmdir

rmdir是remove directory的缩写

  • rmdir 直接运行时不能删除非空目录,也就是只能删除空文件夹,如下
[root@aminglinux-01 ~]# rmdir /tmp/aminglinux/
rmdir: 删除 "/tmp/aminglinux/" 失败: 目录非空
[root@aminglinux-01 ~]# touch /tmp/aminglinux/222/aaa/1.txt
[root@aminglinux-01 ~]# rmdir /tmp/aminglinux/222/aaa/
rmdir: 删除 "/tmp/aminglinux/222/aaa/" 失败: 目录非空
  • rmdir -p 也是可以连续删除空文件夹的。但必须是空的,如果是非空则删不掉,只删除空的
[root@aminglinux-01 ~]# mkdir -p /tmp/aminglinux/222/yyyy/uuuu/iiii/
[root@aminglinux-01 ~]# rmdir -pv /tmp/aminglinux/222/yyyy/uuuu/iiii/
rmdir: 正在删除目录 "/tmp/aminglinux/222/yyyy/uuuu/iiii/"
rmdir: 正在删除目录 "/tmp/aminglinux/222/yyyy/uuuu"
rmdir: 正在删除目录 "/tmp/aminglinux/222/yyyy"
rmdir: 正在删除目录 "/tmp/aminglinux/222"
rmdir: 删除目录 "/tmp/aminglinux/222" 失败: 目录非空

rmdir只能删除目录,而且必须是空目录,要想全部删除只能先删除里面的文件夹后者是文件。-p 比较危险,一般不用。


rm命令

之前学习的rmdir 删除局限性是比较大的。这回试试rm命令能否顺利删除有文件的目录呢

 [root@aminglinux-01 ~]# tree /tmp/aminglinux/
/tmp/aminglinux/
├── 111
│   └── 222
│       └── 333
└── 222
    └── aaa
        ├── 1.txt
        └── bbb
            └── ccc

7 directories, 1 file
[root@aminglinux-01 ~]# rm /tmp/aminglinux/222/
rm: 无法删除"/tmp/aminglinux/222/": 是一个目录
[root@aminglinux-01 ~]# rm -r /tmp/aminglinux/222/
rm:是否进入目录"/tmp/aminglinux/222/"? y
rm:是否进入目录"/tmp/aminglinux/222/aaa"? y
rm:是否进入目录"/tmp/aminglinux/222/aaa/bbb"? y
rm:是否删除目录 "/tmp/aminglinux/222/aaa/bbb/ccc"?y
rm:是否删除目录 "/tmp/aminglinux/222/aaa/bbb"?y
rm:是否删除普通空文件 "/tmp/aminglinux/222/aaa/1.txt"?y
rm:是否删除目录 "/tmp/aminglinux/222/aaa"?y
rm:是否删除目录 "/tmp/aminglinux/222/"?y
[root@aminglinux-01 ~]# tree /tmp/aminglinux/
/tmp/aminglinux/
└── 111
    └── 222
        └── 333

3 directories, 0 files
[root@aminglinux-01 ~]# 

可以看到rm 是不能直接删有文件的目录的。但是加上-r指令,就可以可以全部删除啦。

可以看到文件1.txt也是被直接删除了。

如果加上-f命令就会强制性删除,不会问是否删除文件,这样省去如果文件夹或文件过多一直问下去的麻烦。

  • 总结

    rm 只删除目录里面的文件

    rm -r删除所有目录以及文件

    rm -f强制删除文件,不提示是否删除

    rm -v显示删除过程

    rm -rfv 指令可以叠加使用,效果叠加。

  • touch命令

用法:touch+目录+想要创建的文件 创建空文件命令

[root@aming1 ~]# ls
anaconda-ks.cfg
[root@aming1 ~]# touch 123.txt
[root@aming1 ~]# ls
123.txt  anaconda-ks.cfg

  • history命令 : 查看历史输入命令记录

  • !命令

用法:!+命令

感叹号加一个命令的意思是这个命令在命令历史里最近的一次执行的内容。


环境变量PATH

平时在敲命令的时候可以在任何路径下直接执行。而输入其他 文本会提示找不到该文件,这是为什么呢?

  • 如果命令在以下目录的时候,可以不敲绝对路径。就能生效。这就是:环境变量
[root@aminglinux-01 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
  • 当然直接输入命令的绝对路径肯定也是可以生效的。拷贝到别的地方输入绝对路径一样可以生效。
[root@aminglinux-01 ~]# which ls
alias ls='ls --color=auto'
	/usr/bin/ls
[root@aminglinux-01 ~]# ls /usr/bin/ls 
/usr/bin/ls
[root@aminglinux-01 ~]# cp /usr/bin/ls /tmp/ls2
[root@aminglinux-01 ~]# /tmp/ls2 
anaconda-ks.cfg
[root@aminglinux-01 ~]# 
  • 但是直接输入ls2会生效吗?是不可以的,看下面输出
[root@aminglinux-01 ~]# ls2
-bash: ls2: 未找到命令

结果是并没有生效,这是因为ls2所在的/tmp/目录并不在(PATH)环境变量里面,现在把目录加到环境变量里面。

  • PATH=$PATH:/tmp/ 这是个shell意思是把tmp加入到PATH
[root@aminglinux-01 ~]# PATH=$PATH:/tmp/
[root@aminglinux-01 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/tmp/
  • 这样在输入ls2,就可以生效了。
[root@aminglinux-01 ~]# ls2
anaconda-ks.cfg
[root@aminglinux-01 ~]#
  • 这时候如果在打开新的终端连接系统,在输入ls2,就不再生效了。如果需要在任何时候都会生效,永久生效。需要加到系统配置/etc/profile文件里。添加到最后一行就可以了,如下:
 [root@aminglinux-01 ~]# vi /etc/profile
...


 if [ "$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth
else
    export HISTCONTROL=ignoredups
fi

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL

# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
    umask 002
else
    umask 022
fi

for i in /etc/profile.d/*.sh ; do
    if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done

unset i
unset -f pathmunge
PATH=$PATH:/tmp/                           *** 在最后加上:PATH=$PATH:/tmp/
  • 如果想恢复之前的PATH,就去掉/tmp/,复制之前的PATH。命令如下:
[root@aminglinux-01 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/tmp/
[root@aminglinux-01 ~]#PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@aminglinux-01 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@aminglinux-01 ~]# ls2
-bash: ls2: 未找到命令
  • 如果设置了永久保存,就在vi /etc/profile里面把PATH=$PATH:/tmp/删掉重启就可以了。

cp 拷贝命令

  • cp ,copy的意思,把源文件拷贝到目标文件,并且可以重命名目标文件名字
[root@aming1 ~]# ls
123.txt  anaconda-ks.cfg
[root@aming1 ~]# cp /etc/passwd  456.txt
[root@aming1 ~]# ls
123.txt  456.txt  anaconda-ks.cfg

  • cp 目录也是需要加上-r的。
[root@aming1 ~]# cp -r /tmp/aminglinux/   /tmp/aming/
[root@aming1 ~]# tree /tmp/aming 
/tmp/aming
└── 111
    └── 222
        └── 333
[root@aming1 ~]# tree /tmp/aminglinux/
/tmp/aminglinux/
└── 111
    └── 222
        └── 333
  • 当复制文件已经存在的时候会问是否覆盖
[root@aming1 ~]# ls
123.txt  anaconda-ks.cfg
[root@aming1 ~]# cp /etc/passwd  456.txt
[root@aming1 ~]# ls
123.txt  456.txt  anaconda-ks.cfg
[root@aming1 ~]# cp /etc/passwd  456.txt
cp:是否覆盖"456.txt"? y
[root@aming1 ~]# 

  • 那么当复制目录的时候会不会提示覆盖呢

经过测试是如果目录都是空的,就不会有提示,如果目录里有文件就会提示是否覆盖。


mv移动文件命令

  • mv :move的缩写,移动文件命令

  • 同一个目录下移动文件或者目录会给文件或目录更改名字

[root@aminglinux-01 ~]# ls
anaconda-ks.cfg
[root@aminglinux-01 ~]# mv anaconda-ks.cfg    anaconda-ks.cfg.1
[root@aminglinux-01 ~]# ls
anaconda-ks.cfg.1
  • 也可以移动文件或目录并且改名字。
[root@aminglinux-01 tmp]# mv 12.txt     /root/2.txt
[root@aminglinux-01 tmp]# ls /root/
2.txt  anaconda-ks.cfg.1
[root@aminglinux-01 tmp]# 
  • 如果移动的位置已经有一个相同文件名的文件,也是会提示覆盖的。输入mv的绝对路径就不会提示了。
[root@aming1 ~]# mv 123.txt 111.txt
[root@aming1 ~]# mv 111.txt 111.txt
mv: "111.txt" 与"111.txt" 为同一文件
[root@aming1 ~]# mv 456.txt 111.txt
mv:是否覆盖"111.txt"? y

另外如果移动相同文件夹里面如果有文件,那么是会报错目录非空。无法覆盖。


文件查看cat_more_less_head_tail

cat

  • 用cat查看一个文件的内容
[root@aming1 ~]# cat 111.txt 
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
games:x:12:100:games:/usr/games:/sbin/nologin
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@aming1 ~]# 

  • cat -n 显示行号
[root@aming1 ~]# cat -n 111.txt 
     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
     7	shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
     8	halt:x:7:0:halt:/sbin:/sbin/halt
     9	mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    10	operator:x:11:0:operator:/root:/sbin/nologin
    11	games:x:12:100:games:/usr/games:/sbin/nologin
    12	ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    13	nobody:x:99:99:Nobody:/:/sbin/nologin
    14	systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
    15	systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    16	dbus:x:81:81:System message bus:/:/sbin/nologin
    17	polkitd:x:998:996:User for polkitd:/:/sbin/nologin
    18	tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
    19	postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    20	sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    21	chrony:x:997:995::/var/lib/chrony:/sbin/nologin
[root@aming1 ~]# 

  • tac 倒序查看一个文件
[root@aming1 ~]# tac 111.txt 
chrony:x:997:995::/var/lib/chrony:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
halt:x:7:0:halt:/sbin:/sbin/halt
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
sync:x:5:0:sync:/sbin:/bin/sync
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
root:x:0:0:root:/root:/bin/bash

  • 其他用法可以man cat一下。

more

  • 也是查看文件内容的,但是不会像cat一样全部显示出来。
  • 它会一屏一屏的显示。按空格键下翻页。
  • 看完之后就自动退出来了。
  • 按Ctrl+b键是向上翻页。
[root@aminglinux-01 ~]# more /etc/profile
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

pathmunge () {
    case ":${PATH}:" in
        *:"$1":*)
            ;;
        *)
            if [ "$2" = "after" ] ; then
                PATH=$PATH:$1
            else
                PATH=$1:$PATH
            fi
    esac
}




--More--(66%)

less命令

  • less的命令的用法包含了more的用法,同时可以用方向键来一行一行的翻页。翻到尾部不会自动退出来。

  • 按q可以退出。

  • 按“g”定位到行首

  • 按“G”定位到行尾

  • 还可以搜索内容

输入:/或者?“单词” 比如/and或?and 搜索下面文件内容所有的and都会高亮显示。 按“n”像下查询查找内容 按“N”从下向上查询查找内容

head 命令 tail 命令

  • head 命令查看文件的头十行

  • tail 命令查看文件的尾十行

  • tail 和head都是可以输入-n 来指定看几行的。比如只看两行。

[root@aming1 ~]# head -n2 111.txt 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@aming1 ~]# 

wc -l 命令,显示一个文件的行数

[root@aming1 ~]# wc -l 111.txt 
21 111.txt

追加重定向:前面命令的输出内容 >> 追加到目标文件内容的后面。

  • 分别查看1.txt与2.txt
[root@aming1 ~]# cat 1.txt 
11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
[root@aming1 ~]# cat 2.txt 
22222222222222222222
  • 把查看1.txt的输出追加重定向到2.txt后查看,
[root@aming1 ~]# cat 1.txt >> 2.txt 
[root@aming1 ~]# cat 2.txt 
22222222222222222222
11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111


chmod文件或目录权限

  • 之前学习的ls -l命令时候,学习了列出每个位置都是什么。
[root@aming1 ~]# ls -l 111.txt 
-rw-r--r--. 1 root root 1008 5月  13 22:26 111.txt

  • 开头除了第一位是文件类型外,后面九位都是权限位。

  • 在inode使用量前面11位。第一位表示文件类型,后面九位表示文件权限,最后一个(.)是受制于selinux。如果关闭selinux,新建的文件或目录就会没有(.)

  • 在九位权限位置里,前三位代表所有者权限,中间三位是所属组权限,后三位是其他用户权限。

  • 其中rwx

r 读 数字用4代表

w 写 数字用2代表

x 执行 数字用1代表

  • 权限一般也用数字表示,比如764,就是所有者(u)rwx,所属组(g)rw-,其他用户(o)r--
r+w+x=7
rwxrwxrwx=777

修改权限命令: chmod

chmod 更改权限 change mode 的缩写

  • 语法:chmod 777 文件/目录名字
[root@aming1 ~]# ls -l 111.txt 
-rw-r--r--. 1 root root 1008 5月  13 22:26 111.txt
[root@aming1 ~]# chmod 777 111.txt 
[root@aming1 ~]# ls -l 111.txt 
-rwxrwxrwx. 1 root root 1008 5月  13 22:26 111.txt

  • 或者:chmod u=rwx,g=rw,o=r 文件/目录名字
[root@aming1 ~]# ls -l 111.txt 
-rw-r--r--. 1 root root 1008 5月  13 22:26 111.txt
[root@aming1 ~]# chmod 777 111.txt 
[root@aming1 ~]# ls -l 111.txt 
-rwxrwxrwx. 1 root root 1008 5月  13 22:26 111.txt
[root@aming1 ~]# chmod u=rwx,g=rw,o=r 111.txt
[root@aming1 ~]# ls -l 111.txt 
-rwxrw-r--. 1 root root 1008 5月  13 22:26 111.txt


  • chmod -R

操作只对目录或文件本身生效。如需目录及子文件子目录全部生效需要加-R

加上-R 命令可以同时更改目录及目录下所有的权限。

[root@aminglinux-01 ~]# chmod 700 /tmp/aming1/
[root@aminglinux-01 ~]# ls -l /tmp/
总用量 236
-rwxr-xr-x. 1 root root 117656 8月   9 09:19 111
drwx------. 3 root root     17 8月   9 09:23 aming1
drwxr-xr-x. 4 root root     31 8月   9 09:34 aming2

[root@aminglinux-01 ~]# ls -l /tmp/aming1/
总用量 0
drwxr-xr-x. 3 root root 30 8月   9 09:40 111
[root@aminglinux-01 ~]# chmod -R 777 /tmp/aming1/
[root@aminglinux-01 ~]# ls -l /tmp/aming1/
总用量 0
drwxrwxrwx. 3 root root 30 8月   9 09:40 111
[root@aminglinux-01 ~]# ls -l /tmp/
总用量 236
-rwxr-xr-x. 1 root root 117656 8月   9 09:19 111
drwxrwxrwx. 3 root root     17 8月   9 09:23 aming1
drwxr-xr-x. 4 root root     31 8月   9 09:34 aming2
  • 还有一个用法是 chmod a+x 文件/目录名字

意思是u,g,o同时加上x权限。

a-x,就是全部去掉x权限。

也是可以,u+w,u-x,g+w,g-w等等都可以用。

[root@aminglinux-01 ~]# chmod 700 /tmp/aming1/

[root@aminglinux-01 ~]# ls -l /tmp/
总用量 236

drwx------. 3 root root     17 8月   9 09:23 aming1



[root@aminglinux-01 ~]# chmod a+x /tmp/aming1/
[root@aminglinux-01 ~]# ls -l /tmp/
总用量 236

drwx--x--x. 3 root root     17 8月   9 09:23 aming1


[root@aminglinux-01 ~]# chmod a-x /tmp/aming1/
[root@aminglinux-01 ~]# ls -l /tmp/
总用量 236

drw-------. 3 root root     17 8月   9 09:23 aming1

[root@aminglinux-01 ~]# 


chown 更改所有者和所属组

  • chown = change owner 更改所有者

  • chown 不仅可以更改所有者,也可以更改所属组。

  • cat /etc/passwd 来查看系统用户都有哪些。

  • 用法:chown 所有者:所属组 文件名/目录

下面修改一下新建文件的所有者。

[root@aminglinux-01 ~]# touch /tmp/lunyu.log
[root@aminglinux-01 ~]# ls -l /tmp/lunyu.log 
-rw-r--r--. 1 root root 0 8月   9 21:45 /tmp/lunyu.log
[root@aminglinux-01 ~]# chown linyu /tmp/lunyu.log 
[root@aminglinux-01 ~]# ls -l /tmp/lunyu.log 
-rw-r--r--. 1 linyu root 0 8月   9 21:45 /tmp/lunyu.log
[root@aminglinux-01 ~]# 
  • 更改所属组命令:chgrp,用法同上。但是chown既可以更改所有者也可以同时或者单独更改所属组

  • chown 同时或者单独更改所属组

[root@aminglinux-01 ~]# chown linyu:games /tmp/lunyu.log 
[root@aminglinux-01 ~]# ls -l /tmp/lunyu.log 
-rw-r--r--. 1 linyu games 0 8月   9 21:45 /tmp/lunyu.log

[root@aminglinux-01 ~]# chown   :root /tmp/lunyu.log 
[root@aminglinux-01 ~]# ls -l /tmp/lunyu.log 
-rw-r--r--. 1 linyu root 0 8月   9 21:45 /tmp/lunyu.log

  • chown -R 命令

同样可以一次性更改目录及子目录和文件的所有者和所属组。


umask

linux在创建一个文件或者目录时候都会有一个默认的权限。这个是怎么来的呢。 运行一下 umask

[root@aminglinux-01 ~]# umask
0022

这个是root用户下的umask值,通过这个值可以确定文件以及目录默认是什么权限。

首先创建一个目录和文件。然后修改一下啊umask值,在创建一个目录和文件。然后对比一下看有什么联系。

[root@aminglinux-01 ~]# touch 123.txt
[root@aminglinux-01 ~]# mkdir aaa
[root@aminglinux-01 ~]# ls -l 
总用量 8
-rw-r--r--. 1 root root    0 8月   9 22:08 123.txt
drwxr-xr-x. 2 root root    6 8月   9 22:08 aaa

在0022时候,文件默认是:644,目录默认是:755
[root@aminglinux-01 ~]# umask 002
[root@aminglinux-01 ~]# umask 
0002
[root@aminglinux-01 ~]# touch 456.txt
[root@aminglinux-01 ~]# mkdir bbb
[root@aminglinux-01 ~]# ls -l
总用量 8
-rw-r--r--. 1 root root    0 8月   9 22:08 123.txt
-rw-rw-r--. 1 root root    0 8月   9 22:14 456.txt
drwxr-xr-x. 2 root root    6 8月   9 22:08 aaa
drwxrwxr-x. 2 root root    6 8月   9 22:14 bbb
[root@aminglinux-01 ~]# 
在0002时候,文件默认是:664,目录默认是:775
  • 得出答案是:

在0022时候,文件默认是:644,目录默认是:755

在0002时候,文件默认是:664,目录默认是:775

首先目录,必须会有x权限,因为想查看这个目录,首先都要进入查看,所以有x权限。所以默认权限最大的话就是7.文件不需要必须有x权限,所以默认6就可以满足。

由此规律得出:目录:777-022=755
                    777-002=775
              文件:666-022=644
                    666-002=664
  • 这个规律合乎逻辑但是不一定是准确的。

  • 如果我们设置umask 为 003。按照之前的算法666-003=663。实际上经过测试是664

[root@aminglinux-01 ~]# umask 003
[root@aminglinux-01 ~]# touch 888.txt
[root@aminglinux-01 ~]# ls -l 888.txt 
-rw-rw-r--. 1 root root 0 8月   9 22:27 888.txt
[root@aminglinux-01 ~]# 
  • 那么之前的算法就是有漏洞的。

  • 正确的算法是:

文件 666(rw-rw-rw-)- 003(-------wx)=rw-rw-r-- 也就是664

目录 777(rwxrwxrwx)- 002(-------w-)=rwxrwxr-x 也就是775

转载于:https://my.oschina/u/3852961/blog/1811719

本文标签: 文件类型权限结构目录系统