linux目录文件基础操作命令

目录相关操作

几个特殊目录

.:当前目录
..:上一层目录
-:前一个工作目录
~:当前使用者所在家目录
~account:account使用者的家目录

操作目录

cd: 切换目录
pwd [-P]:显示当前目录,-P显示实际工作目录,而非链接目录
mkdir [-mp]:建立新目录,-m设置权限,-p将所需目录递归创建
rmdir [-pr]:删除空目录,-p连同上层空目录一起删除,-r将目录下的东西全部删除

echo $PATH: echo有打印的意思,$后面节变量,即整句为打印变量的意思

添加路径

PATH="${PATH}:/root" : 将/root将入PATH中

dirname:获取路径名
basename:获取文件名

创建文件时候的默认权限

umask: 查看(或者使用umask -S
文件的默认权限:-rw-rw-rw-,即文件默认666
目录的默认权限:drwxrwxrwx,即目录默认777
umask的后三位为上面权限需要减去的值

文件隐藏属性

chattr [+-=] [Sai...]:±=增加或减少或设置属性;S:修改文件时随时sync到磁盘;a:只能增加数据,不能修改或者删除数据,唯root可设;i:不能被删除、修改、增加链接……,唯root可设
lsattr [adR]:列出隐藏属性,a将隐藏文件也显示出来,d如是目录,仅显示目录,R将目录和子文件数据一同显示

文件的特殊权限

  • SUID:set uid,ownner的特殊执行权,对二进制程序有效,当不具有x时,其为空,为大写S,当用户有x并执行该程序的时候,可以获得ownner身份(进而去访问ownner可以访问或执行的文件目录等)
  • SGID:set gid,group的特殊执行权,对二进制程序有效,当不具有x时,其为空,为大写S,当用户具有x并执行该程序时,可以获得group身份,(进而去访问group可以访问或执行的文件目录等)
  • SBIT:sticky bit,other的特殊执行权,对目录有效,当不具有x时,其为空,为大写T,当其他用户具有该目录x,并进一步具有该目录t之后,在该目录中只能操作(新建、删除、移动)自己建立的文件

上面三种权限对应数字,依次是:4, 2, 1
例如:

[root@VM-0-14-centos tmp]# ll test
-rwxrwxrwx 1 root root 0 Jun 24 18:07 test
[root@VM-0-14-centos tmp]# chmod 4755 test
[root@VM-0-14-centos tmp]# ls -l test
-rwsr-xr-x 1 root root 0 Jun 24 18:07 test
[root@VM-0-14-centos tmp]# chmod 6755 test; ll test
-rwsr-sr-x 1 root root 0 Jun 24 18:07 test
[root@VM-0-14-centos tmp]# chmod 1755 test; ll test
-rwxr-xr-t 1 root root 0 Jun 24 18:07 test
[root@VM-0-14-centos tmp]# chmod 7666 test; ll test
-rwSrwSrwT 1 root root 0 Jun 24 18:07 test

或者使用符号法:ugo当中添加s, s, t
例如:

[root@VM-0-14-centos tmp]# chmod u=rwxs,go=x test; ll test
-rws--x--x 1 root root 0 Jun 24 18:07 test
[root@VM-0-14-centos tmp]# chmod g+s,o+t test; ll test
-rws--s--t 1 root root 0 Jun 24 18:07 test

查看文件类型

[root@VM-0-14-centos ~]# file ~/.bashrc
/root/.bashrc: ASCII text
[root@VM-0-14-centos ~]# file /usr/bin/passwd 
/usr/bin/passwd: setuid ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=dee1b9ab6618c6bfb84a14f85ba258c742cf4aec, stripped
[root@VM-0-14-centos ~]# file /var/lib/mlocate/mlocate.db 
/var/lib/mlocate/mlocate.db: data

命令(可执行程序)与文件查找

which [-a] command: 用which查找命令,其中-a表示列出所有,而不只找到的第一个
例如:

[root@VM-0-14-centos ~]# which ifconfig
/usr/sbin/ifconfig
[root@VM-0-14-centos ~]# which which
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
        /usr/bin/alias
        /usr/bin/which
[root@VM-0-14-centos ~]# which history
/usr/bin/which: no history in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
[root@VM-0-14-centos ~]# history --help
-bash: history: --: invalid option
history: usage: history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg [arg...]

whereis [-bmsu] file_or_directory_name: whereis是从特定的一些目录进行查找,因此速度较快,可用-l查看其遍历的目录
例如:

[root@VM-0-14-centos ~]# whereis ifconfig
/usr/sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz
[root@VM-0-14-centos ~]# whereis passwd
passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.gz
[root@VM-0-14-centos ~]# whereis -m passwd
passwd: /usr/share/man/man1/passwd.1.gz

locate [-ir] keyword: 可以进行关键词的查询,是利用/var/lib/mlocate内的数据库文件查询,因此速度很快,i忽略大小写,r后可接正则表达式的显示方式,c不输出文件名,只计算找到的文件数量
例如:

[root@VM-0-14-centos ~]# locate -l 5 passwd
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
/etc/security/opasswd
/usr/bin/gpasswd
[root@VM-0-14-centos ~]# locate -S
Database /var/lib/mlocate/mlocate.db:
        10,997 directories
        80,223 files
        4,208,535 bytes in file names
        1,869,419 bytes used to store database

updatedb:更新locate的数据库文件,根据/etc/updatedb.conf的设置更新/var/lib/mlocate内的数据库文件

find [PATH] [option] [action]

时间有关选项

-mtime n:n*24小时之前的24小时内被修改的文件
-mtime -n:n*24小时之内,直到现在的时间内,被修改的文件
-mtime +n:(n+1)*24小时之前,也就是在-mtime n之前的时间被修改的文件
-newer finename:比filename文件还要更新的文件
例如:
24小时之内被修改的文件:root@VM-0-14-centos ~]# find / -mtime 0

[root@VM-0-14-centos ~]# find /etc -newer /etc/passwd
/etc
/etc/group
/etc/ssh/sshd_config
/etc/sudoers.d/90-cloud-init-users
/etc/gshadow
/etc/shadow
/etc/resolv.conf
/etc/subgid
/etc/subuid
/etc/shadow-
/etc/tuned/active_profile
/etc/tuned/profile_mode
使用者,用户组有关选项

-uid n:n means user id stored in /etc/passed
-gid n:n means group id stored in /etc/group
-user name:
nouser: find file whose owner is not in /etc/passwd
nogroup: find file whose group is not in /etc/group
eg:

[root@VM-0-14-centos ~]# find /home -user dmtsai
/home/dmtsai
/home/dmtsai/.lesshst
/home/dmtsai/.bashrc
/home/dmtsai/.bash_logout
/home/dmtsai/.cache
/home/dmtsai/.cache/abrt
/home/dmtsai/.cache/abrt/lastnotification
/home/dmtsai/.bash_history
/home/dmtsai/text.txt∫
/home/dmtsai/bashrc
/home/dmtsai/.config
/home/dmtsai/.config/abrt
/home/dmtsai/.bash_profile
与文件权限属性有关的参数

-name filename:find file whose name is filename
-size [+-]SIZE:find file whose size is greater(+) or less(-) than SIZE, c:Bytes, K:kB, -size +50k:greater than 50kB
-type TYPE:file(f), device(b,c), directory(d), link(l), socket(s), FIFO§
-perm mode:find file whose chmod attribute is mode
-perm -mode:find file whose chmod attribute includes mode
-perm /mode:find file whose chmod attribute includes part of mode
eg:

[root@VM-0-14-centos ~]# find / -name passwd
/etc/passwd
/etc/pam.d/passwd
/usr/share/bash-completion/completions/passwd
/usr/bin/passwd
[root@VM-0-14-centos ~]# find / -name "*passwd*"
/etc/passwd
/etc/security/opasswd
/etc/pam.d/passwd
/etc/passwd-
/usr/lib/firewalld/services/kpasswd.xml
/usr/lib/python2.7/site-packages/cloud_init-17.1-py2.7.egg/cloudinit/sources/helpers/vmware/imc/config_passwd.py
/usr/lib/python2.7/site-packages/cloud_init-17.1-py2.7.egg/cloudinit/sources/helpers/vmware/imc/config_passwd.pyc
/usr/share/man/cs/man1/gpasswd.1.gz
.....
[root@VM-0-14-centos ~]# find /run -type s
/run/acpid.socket
/run/dbus/system_bus_socket
/run/lsm/ipc/sim
/run/lsm/ipc/simc
/run/lvm/lvmetad.socket
/run/lvm/lvmpolld.socket
/run/udev/control
/run/systemd/shutdownd
/run/systemd/private
/run/systemd/journal/socket
/run/systemd/journal/stdout
/run/systemd/cgroups-agent
/run/systemd/notify
[root@VM-0-14-centos ~]# find / -perm /7000
/usr/lib/polkit-1/polkit-agent-helper-1
/usr/local/qcloud/YunJing/ydsvc.err
/usr/bin/pkexec
/usr/bin/locate
/usr/bin/mount
/usr/bin/passwd
/usr/bin/umount
/usr/bin/newgrp
......
[root@VM-0-14-centos ~]# find /usr/bin /usr/sbin -perm /6000
/usr/bin/pkexec
/usr/bin/locate
/usr/bin/mount
/usr/bin/passwd
/usr/bin/umount
/usr/bin/newgrp
/usr/bin/ssh-agent
/usr/bin/write
/usr/bin/chsh
/usr/bin/crontab
/usr/bin/at
/usr/bin/staprun
/usr/bin/gpasswd
/usr/bin/sudo
/usr/bin/chage
/usr/bin/chfn
/usr/bin/su
/usr/bin/wall
/usr/sbin/userhelper
/usr/sbin/unix_chkpwd
/usr/sbin/postqueue
/usr/sbin/netreport
/usr/sbin/pam_timestamp_check
/usr/sbin/postdrop
/usr/sbin/usernetctl
额外命令参数

-exec command \;: execute other command, begin with -exec end with \;
eg:

[root@VM-0-14-centos ~]# find /usr/bin /usr/sbin -perm /7000 -exec ls -l {} \;
-rwsr-xr-x 1 root root 23576 Apr  1  2020 /usr/bin/pkexec
-rwx--s--x. 1 root slocate 40520 Apr 11  2018 /usr/bin/locate
-rwsr-xr-x 1 root root 44264 Oct  1  2020 /usr/bin/mount
-rwsr-xr-x 1 root root 27856 Apr  1  2020 /usr/bin/passwd
-rwsr-xr-x 1 root root 31984 Oct  1  2020 /usr/bin/umount
-rwsr-xr-x 1 root root 41936 Aug  9  2019 /usr/bin/newgrp
---x--s--x 1 root nobody 382216 Aug  9  2019 /usr/bin/ssh-agent
-rwxr-sr-x 1 root tty 19544 Oct  1  2020 /usr/bin/write
-rws--x--x 1 root root 23880 Oct  1  2020 /usr/bin/chsh
-rwsr-xr-x 1 root root 57656 Aug  9  2019 /usr/bin/crontab
-rwsr-xr-x. 1 root root 53048 Oct 31  2018 /usr/bin/at
---s--x--- 1 root stapusr 212080 Oct 14  2020 /usr/bin/staprun
-rwsr-xr-x 1 root root 78408 Aug  9  2019 /usr/bin/gpasswd
---s--x--x 1 root root 147336 Jan 27 05:56 /usr/bin/sudo
-rwsr-xr-x 1 root root 73888 Aug  9  2019 /usr/bin/chage
-rws--x--x 1 root root 23968 Oct  1  2020 /usr/bin/chfn
-rwsr-xr-x 1 root root 32128 Oct  1  2020 /usr/bin/su
-r-xr-sr-x. 1 root tty 15344 Jun 10  2014 /usr/bin/wall
-rws--x--x 1 root root 40328 Aug  9  2019 /usr/sbin/userhelper
-rwsr-xr-x 1 root root 36272 Apr  1  2020 /usr/sbin/unix_chkpwd
-rwxr-sr-x 1 root postdrop 264128 Apr  1  2020 /usr/sbin/postqueue
-rwxr-sr-x 1 root root 11224 Nov 17  2020 /usr/sbin/netreport
-rwsr-xr-x 1 root root 11232 Apr  1  2020 /usr/sbin/pam_timestamp_check
-rwxr-sr-x 1 root postdrop 218560 Apr  1  2020 /usr/sbin/postdrop
-rwsr-xr-x 1 root root 11296 Nov 17  2020 /usr/sbin/usernetctl

{}:output of find

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ML--小小白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值