yum install -y tree
选择列出目录最大深度为2tree -L 2
存放用户命令目录/bin、/sbin、/usr/bin、/usr/sbin
详细目录介绍 /sbin 一般存放Root用户命令 /bin 存放普通用户命令 /boot 系统启动相关文件 /dev linux系统相关设备文件,光盘、鼠标、键盘等 /etc 系统配置文件,网卡配置文件、ssh配置文件、selinux配置文件 /home 用户家目录 /lib、lib64 存放库文件,查看命令依赖的库:ldd /bin/ls /media 媒介目录,插入U盘自动挂载至该目录 /mnt 临时挂载目录,可将光驱、新增设备挂载在该目录 /opt 主要存放可选程序,安装在/opt目录下的程序,所有的数据、库文件都会保存在该目录下 /proc 系统启动进程,每个进程对应PID /run 进程产生的临时文件,关机重启后会消失 /srv 存放服务产生的相关文件 /sys 系统内核相关文件 /tmp 临时目录,权限很大 /usr 存放用户文件,比如/usr/bin、/usr/sbin。/usr/local编译安装的软件默认安装在此目录 /var /var/log存放日志、/var/run某些进程PID所在目录==d==rwxr-xr-x 2 root root 6 5月 30 21:27 123
- 表示普通文件、二进制文件 d 表示目录 c 表示字符串设备 l 表示软链接文件 b 表示块设备,光盘、磁盘 s 套接字文件,进程之间通信使用,如musql.sockwhich ls or alias ls 2. 设置别名
alias gbj = ls -lha /root
取消别名ualias gbj
which 2. 全局更改PATH值,每次终端自动添加自定义变量
修改/etc/profile,添加 PTAH=$PATH:/目录cp 源文件/目录 目的文件/目录
cp -r 用于拷贝目录 如果目标目录存在,则将源文件/目录放到目标目录中 如果目标文件不存在,则将源文件/目录改名后放置指定路径中mv 源文件/目录 目标文件/目录
目标文件为目录,并且目标目录不存在,则将源目录重命名 目标文件为目录,并且目标目录存在,则将源目录移动目标目录中 目标文件不是目录,且不存在,则重命名源文件cat、tac、more、less、head、tail
cat -A 查看文档中所有字符 cat -n 显示文档行号 wc -l 查看行数 tac 倒序查看文档内容 more 部分查看文档,空格键往下翻屏,ctrl+f网上翻页,ctrl+b往下翻页 less 部分查看文档,按空格键往下翻屏,ctrl+f往上翻页,ctrl+b往下翻页,shift+G定位行末、shift+g定位行首,/从当前页面从前往后搜索,?从当前页面从后往前搜索,n/N搜索内容后往前往后定位,支持方向键 head 查看文件开头10行 -n:指定数字 tail 查看文件尾部10行。-n:指定数字;-f:动态显示文档,一般用于动态查看某个文件。chmod == change mode
root@centos7-1 ~]# ls -l 总用量 28 -rw-r--r-- 1 root root 13244 5月 30 23:05 1 drwxr-xr-x 2 root root 6 5月 30 21:27 123 权限位 rw-r--r-- == 属主 属组 其他 r = 可读 w = 可写 x = 可执行 数字权限位表示 r = 4 w = 2 x = 1rw-r–r– == 644
chmod 权限更改 Each MODE is of the form ‘[ugoa]*( -+=)+|[-+=][0-7]+’. 更改文件1权限为rwx------ chmod 700 1 更改文件1权限为rwx------ chmod u=rwx,g=,o= 1 更改文件1权限为rwxrwxrwx chmod a=rwx 1 去除文件1可执行权限 chmod a-x 1 chmod -R 级联更改目录及目录下所有子目录和文件所有文件权限 特殊点 [root@centos7-1 ~]# ls -l /tmp/ 总用量 12 drwx - - - - - -**.** 2 root root 6 5月 29 17:27 akonadi-root.f3NFIq -rw-r- -r- -**.** 1 root root 1018 5月 29 17:23 anaconda.log **.** 表示该文件受制于selinux,关闭后重启设备,不再生成该类型文件chown == change owner
查看系统用户 cat /etc/passwd 更改文件1 属主为gbj [root@centos7-1 ~]# chown gbj 1 更改文件属组为gbjgp chgrp == change group [root@centos7-1 ~]# chgrp gbjgp 1 [root@centos7-1 ~]# chown :gbjgp 1 更改文件1属主为gbj,属组为gbjgp [root@centos7-1 ~]# chown gbj:gbjgp 1 chown -R 级联更改目录及目录下所有子目录和文件所有文件gbj属主、root属组 chown -R gbj:root 123/登录系统之后创建一个文件总是有一个默认权限的。umask设置了用户创建文件的默认权限,它与chmod的效果刚好相反,umask设置的是权限“补码”,而chmod设置的是文件权限码。一般可在/etc/profile
默认权限值\ 系统默认root用户umask值为022,最前面0表示set Uid、set Gid、粘着位(sticky)、有效的设定值只有后三位数。 在 bash中,虽然可带入四位数字,但第一个数字,必须为 0。 [root@Temence ~]# umask 0022 默认创建文件权限 644 ,文件完全权限为666 默认创建目录权限 755 ,目录权限权限为777 umask值定义文件/etc/profile # 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 ] && [ "`id -gn`" = "`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 更改umask值 更改定义文件 /etc/profileumask 003 umask值定义的文件默认权限不能单纯的认为是完全权限减去umask值,需要逐位相减。比如: 666-003 = (rw-rw-rw-)-(-------wx) = rw-rw-r-- = 664chattr -R files -R 级联修改目录及目录下面的文件隐藏权限 更改目录权限时,如果不使用-R参数,目录下的文件或者目录不会拥有隐藏权限,文件依旧可以追加更改,但不能删除
目录添加 a权限,使用和不使用-R参数区别: [root@centos7-1 ~]# lsattr -d test/ ---------------- test/ [root@centos7-1 ~]# lsattr test/1 ---------------- test/1 [root@centos7-1 ~]# chattr +a test/ [root@centos7-1 ~]# lsattr test/ ---------------- test/1 [root@centos7-1 ~]# lsattr -d test/ -----a---------- test/ [root@centos7-1 ~]# echo 'qwer' > test/1 [root@centos7-1 ~]# rm test/1 rm:是否删除普通文件 "test/1"?y rm: 无法删除"test/1": 权限不够 [root@centos7-1 ~]# chattr +a -R test/ [root@centos7-1 ~]# lsattr test/ -----a---------- test/1 [root@centos7-1 ~]# lsattr -d test/ -----a---------- test/ [root@centos7-1 ~]# echo 'wer' > test/1 -bash: test/1: 不允许的操作 目录添加==i==权限,使用和不使用-R参数区别: [root@centos7-1 ~]# lsattr -d test/ ---------------- test/ [root@centos7-1 ~]# lsattr test/2 ---------------- test/2 [root@centos7-1 ~]# chattr +i test/ [root@centos7-1 ~]# lsattr test/ ---------------- test/1 ---------------- test/2 [root@centos7-1 ~]# lsattr -d test/ ----i----------- test/ [root@centos7-1 ~]# echo 'qwer' > test/2 [root@centos7-1 ~]# cat test/2 qwer [root@centos7-1 ~]# rm test/2 rm:是否删除普通文件 "test/2"?y rm: 无法删除"test/2": 权限不够 [root@centos7-1 ~]# chattr -R +i test/ [root@centos7-1 ~]# lsattr -R test/ ----i----------- test/1 ----i----------- test/2 [root@centos7-1 ~]# lsattr -d test ----i----------- test [root@centos7-1 ~]# echo 'asdf' > test/2 -bash: test/2: 权限不够 [root@centos7-1 ~]# cat test/2 qwer让普通用户可以以root用户的角色运行只有root帐号才能运行的程序或命令
查看set_uid 保证普通用户临时拥有该命令所有者的身份
[root@Temence ~]# ll /usr/bin/passwd -rw s r-xr-x 1 root root 27832 Jun 10 2014 /usr/bin/passwd
设置set_uid
[root@centos7-1 ~]# cp /bin/ls . [root@centos7-1 ~]# ls 1 1.t anaconda-ks.cfg ls test 模板 图片 下载 桌面 123 1.txt initial-setup-ks.cfg passwd 公共 视频 文档 音乐 [root@centos7-1 ~]# chmod u+s ls [root@centos7-1 ~]# ll ls -rwsr-xr-x 1 root root 117656 6月 8 07:29 ls 取消set_uid [root@centos7-1 ~]# chmod u-s ls [root@centos7-1 ~]# ll ls -rwxr-xr-x 1 root root 117656 6月 8 07:29 ls 使用chmod u=rws设置set_uid [root@centos7-1 ~]# chmod u=rws ls [root@centos7-1 ~]# ll ls -rwSr-xr-x 1 root root 117656 6月 8 07:29 ls此时ls属主权限位变成S,而不是s。是因为没有可执行的权限,但是此时仍然ls命令,有两个原因: 1.root拥有s权限,s有一层可执行含义。 2.普通用户其他权限位有可执行权限
bash [root@centos7-1 ~]# chmod u+x ls [root@centos7-1 ~]# ll ls -rwsr-xr-x 1 root root 117656 6月 8 07:29 ls
让普通用户组可以以root用户组的角色运行只有root帐号才能运行的程序或命令
设置set_gid Set_gid权限如果给文件设置,是让运行此文件的其它用户具有这个文件的属组特性;==给目录设置Set_gid权限,任何用户在该目录下创建的文件,则该文件属组都和目录的属组一致。== [root@centos7-1 ~]# ll ls -rwxr-xr-x 1 root root 117656 6月 8 07:29 ls [root@centos7-1 ~]# chmod g+s ls [root@centos7-1 ~]# ll ls -rwxr-sr-x 1 root root 117656 6月 8 07:29 ls [root@centos7-1 ~]# ll -d 123 drwxr-xr-x 2 root root 6 5月 30 21:27 123 [root@centos7-1 ~]# chown :gbj 123/ [root@centos7-1 ~]# ll -d 123 drwxr-xr-x 2 root gbj 6 5月 30 21:27 123 [root@centos7-1 ~]# touch 123/1 [root@centos7-1 ~]# ll 123/1 -rw-r--r-- 1 root root 0 6月 8 07:56 123/1 [root@centos7-1 ~]# chmod g+s 123/ [root@centos7-1 ~]# ll -d 123 drwxr-sr-x 2 root gbj 15 6月 8 07:56 123 [root@centos7-1 ~]# touch 123/2 [root@centos7-1 ~]# ll 123 总用量 0 -rw-r--r-- 1 root root 0 6月 8 07:56 1 -rw-r--r-- 1 root gbj 0 6月 8 07:59 2 **设置目录set_gid之后,在目录下创建子文件和子目录,创建的子文件和子目录属主会和父目录属主保持一致**符号链接,这个文件包含了另一个文件的路径名。可以是任意文件或目录,可以链接==不同文件系统==的文件 - 软链接文件 usr/bin
[root@centos7-1 ~]# ls -l /bin lrwxrwxrwx. 1 root root 7 5月 29 16:44 /bin -> usr/bin 创建软链接 ln -s 源文件 软链接文件创建软链接文件时,如果源文件和软链接文件在同一目录,可以使用相对路径。
用例:
系统/data目录磁盘不够,但是www服务需要一直使用/data目录中数据,此时可以将/data/www/目录mv至空间大的目录中,然后将mv的/www目录软链接至/data中。
硬链接==只能在同一文件系统==中的文件之间进行链接,不能对目录进行创建。如果删除硬链接对应的源文件,则硬链接文件仍然存在
创建硬链接 ln 源文件 硬链接文件关于搜索文件命令还有:which、whereis、locate(yum安装 mlocate,更新库updatedb)
语法:find [搜索路径] [搜索条件],支持通配符搜索
常用搜索条件 -atime +n/-n 访问或执行时间大于/小于 n 天的文件 -ctime +n/-n 写入、更改 inode 属性(例如更改所有者、权限或者链接)时间大于/小于 n 天的文件 -mtime +n/-n 写入时间大于/小于 n 天的文件 -name filename 直接查找该文件名的文件,这个选项使用很多 -type filetype 通过文件类型查找。文件类型在前面部分已经简单介绍过,相信你已经大体上了解了。filetype包含了f、 b,、 c、 d、 l、 s 等 -size 搜索文件大小 -inum 搜索指定inode号 -exec 一般查找出来内容需要进一步的操作,参数后面跟的是command命令,{}代表前面find查找出来的结果。exec以;为结束标志的,考虑到各个系统中分号会有不同的意义,所以前面加反斜杠转义。 -name 搜索条件 搜索passwd文件所在路径: [root@Temence ~]# find / -name passwd /etc/pam.d/passwd /etc/passwd /usr/bin/passwd [root@Temence ~]# find / -name passw? /etc/pam.d/passwd /etc/passwd /usr/bin/passwd [root@Temence ~]# -type 搜索条件 搜索123名称的目录: [root@Temence ~]# find / -type d -name 123 /data/123 -atime、ctime、mtime搜索条件 Access time也就是atime 是在读取文件或者执行文件时更改的。 Modified time也就是mtime 是在写入文件时随文件内容的更改而更改的。 Create time也就是ctime 是在写入文件、更改所有者、权限或链接设置时随inode的内容更改而更改的。 stat命令可用来列出文件的atime、ctime和mtime。 atime不一定在访问文件之后被修改,因为: 这三个time属性值都放在了inode中。若mtime、atime修改inode就一定会改,既然inode改了,那ctime也就跟着要改了。 更改时间在一天以内后缀为.conf的所有文件: [root@Temence ~]# find / -type f -ctime -1 -name "*.conf" /run/systemd/system/session-296801.scope.d/50-SendSIGHUP.conf /run/systemd/system/session-296801.scope.d/50-After-systemd-user-sessions\x2eservice.conf /run/systemd/system/session-296801.scope.d/50-After-systemd-logind\x2eservice.conf /run/systemd/system/session-296801.scope.d/50-Description.conf /run/systemd/system/session-296801.scope.d/50-Slice.conf /run/systemd/system/session-296542.scope.d/50-SendSIGHUP.conf /run/systemd/system/session-296542.scope.d/50-After-systemd-user-sessions\x2eservice.conf /run/systemd/system/session-296542.scope.d/50-After-systemd-logind\x2eservice.conf /run/systemd/system/session-296542.scope.d/50-Description.conf /run/systemd/system/session-296542.scope.d/50-Slice.conf 更改时间在一天以内或者文件后缀为.txt的文件: [root@Temence ~]# find / -ctime -1 -o -name "*.txt" 使用find命令查找硬链接文件 1. 创建新的硬链接文件: [root@Temence ~]# ln 1.txt /tmp/1.txt.bak 2. 查看此时源文件inode值: [root@Temence ~]# ll 1.txt -rw-r--r-- 2 root root 0 Jun 7 10:55 1.txt [root@Temence ~]# ls -i 1.txt 23502493 1.txt 3. 使用-inum查找两个硬链接文件: [root@Temence ~]# find / -inum 23502493 /root/1.txt /tmp/1.txt.bak -mmin搜索条件 1. 查找root目录200分钟以内修改过的文件: [root@Temence ~]# find /root/ -type f -mmin -200 /root/1.txt /root/.viminfo -exec 参数 1. 查找root目录200分钟以内修改过的文件,并且将文件详细属性列出: [root@Temence ~]# find /root/ -type f -mmin -200 -exec ls -l {} \; -rw-r--r-- 2 root root 48 Jun 9 22:40 /root/1.txt -rw------- 1 root root 3853 Jun 9 22:40 /root/.viminfo 2.查root目录找200分钟以内修改过的文件,并且将文件重命名{}.bak: [root@Temence ~]# find /root/ -type f -mmin -200 -exec mv {} {}.bak \; [root@Temence ~]# ll total 4 -rw-r--r-- 2 root root 48 Jun 9 22:40 1.txt.bak -rw-r--r-- 1 root root 0 Jun 1 20:10 test size 搜索条件 查找/目录下大于10M的文件: [root@Temence ~]# find / -size +10M -type f find: '/proc/2273/task/2273/fdinfo/6': No such file or directory find: '/proc/2273/fdinfo/6': No such file or directory /usr/lib/locale/locale-archive /var/cache/yum/x86_64/7/base/gen/primary_db.sqlite /var/cache/yum/x86_64/7/updates/gen/primary_db.sqlite /var/lib/rpm/Packages方法有很多,比如ftp、samba服务器,这里使用的是lrzsz工具,无法在putty下使用。
[root@Temence ~]# yum install -y lrzsz 从linux下载文件至windows [root@centos7-1 ~]# sz 1.txt 从windows上传文件至linux [root@centos7-1 ~]# rz -be 1.txt-b 以二进制方式,默认为文本方式。 -e 对所有控制字符转义。 如果要保证上传的文件内容在服务器端保存之后与原始文件一致,最好同时设置这两个参数
