0
点赞
收藏
分享

微信扫一扫

Linux文件与目录管理

一ke大白菜 2022-03-30 阅读 160
学习linux

Linux目录管理常用命令

  • cd:切换目录(change directory)
  • cd …:回到上一层目录(只有两个.不知道为什么csdn打出来会是三个.)
  • cd ~:回到自己的家目录
  • pwd:显示当前目录(print working directory)
  • mkdir 目录名:建立一个新目录(make directory)
  • rmdir 目录名:删除一个空目录 注意,使用这个命令时,目录必须一层一层删除,而且被删除的目录里面不能存在其他的目录或者文件,如果要删除目录下的所有东西,则需要使用rm -r 目录名
  • cp:复制文件或目录(copy)
  • mv:移动文件与目录,或者重命名(重命名:mv 原目录名 新目录名)
  • ls 文件名或目录名:显示指定工作目录下的内容及属性信息(list)
    ls有很多种用法:
选项功能
-a显示所有文件及目录 (包括以“.”开头的隐藏文件)
-al显示所有文件及目录 (包括以“.”开头的隐藏文件) 详细权限与属性
-l使用长格式列出文件及目录信息
-r将文件以相反次序显示(默认依英文字母次序)
-t根据最后的修改时间排序
-A同 -a ,但不列出 “.” (当前目录) 及 “…” (父目录)
-S根据文件大小排序
-R递归列出所有子目录

VMware虚拟机实操案例
一、建立一个新目录
右键选择打开终端
在这里插入图片描述
使用pwd显示当前目录

[liyufeng@localhost ~]$ pwd
/home/liyufeng

使用mkdir建立一个新目录

[liyufeng@localhost ~]$ mkdir lixiaopang

切换到lixiaopang再新建两个目录data和empdata并切换到data

[liyufeng@localhost ~]$ cd lixiaopang
[liyufeng@localhost lixiaopang]$ mkdir data
[liyufeng@localhost lixiaopang]$ mkdir empdata
[liyufeng@localhost lixiaopang]$ cd data
[liyufeng@localhost data]$ 

二、新建一个文件test1.txt

[liyufeng@localhost data]$ touch test1.txt 

新建一个隐藏文件test2.txt

[liyufeng@localhost data]$ touch .test2.txt

ls -a显示文件

[liyufeng@localhost data]$ ls -a
.  ..  test1.txt  .test2.txt

Linux文件权限详解

使用su -切换到root,然后通过ls -al查看内容

[liyufeng@localhost lixiaopang]$ su -
密码:
ABRT has detected 1 problem(s). For more info run: abrt-cli list
[root@localhost ~]# ls -al
total 32
dr-xr-x---.  4 root root  186 Mar 26 22:22 .
dr-xr-xr-x. 17 root root  224 Mar 25 23:53 ..
-rw-------.  1 root root 2778 Mar 25 23:53 anaconda-ks.cfg
-rw-r--r--.  1 root root   18 Dec 28  2013 .bash_logout
-rw-r--r--.  1 root root  176 Dec 28  2013 .bash_profile
-rw-r--r--.  1 root root  176 Dec 28  2013 .bashrc
drwx------.  3 root root   18 Mar 26 22:22 .cache
drwxr-xr-x.  3 root root   18 Mar 26 22:22 .config
-rw-r--r--.  1 root root  100 Dec 28  2013 .cshrc
-rw-------.  1 root root 2058 Mar 25 23:53 original-ks.cfg
-rw-r--r--.  1 root root  129 Dec 28  2013 .tcshrc
-rw-------.  1 root root  132 Mar 26 22:22 .xauthMl9SBp

文件属性示意图:
在这里插入图片描述

  • 第一栏表示文件的类型和权限,第一个字符表示类型,[d]表示目录、[-]表示文件、[l]表示链接文件、[b]表示可按块随机读取的设备、[c]表示串行端口设备,比如键盘鼠标等

  • 接下来每三个字符一组,表示三组权限:文件拥有者 文件所属组 其他人
    r(read):可读 w:可写(write) x:可执行(execute)

  • Linux文件权限计算:每三个字符为一组,即rwx,r代表4,w代表2,x代表1,如果是-则代表0,把三个位置的数相加就行了,比如:

在这里插入图片描述
修改文件属性与权限

  • chgrp:更改文件用户组(change group)
  • chown:改变文件拥有者
  • chmod: 改变文件权限(change the permissions mode of a file)

VMware虚拟机实操案例
1、用root新建一个用户并设置密码,这里我设置的是123456789,比较简单,就会提示

[root@localhost ~]# useradd user1
[root@localhost ~]# passwd user1
New password: 
BAD PASSWORD: The password fails the dictionary check - it is too simplistic/systematic
Retype new password: 
passwd: all authentication tokens updated successfully.

2、修改test1.txt的文件权限为770

[liyufeng@localhost data]$ ls -l
总用量 0
-rw-rw-r--. 1 liyufeng liyufeng 0 326 22:01 test1.txt
[liyufeng@localhost data]$ chmod 770 test1.txt
[liyufeng@localhost data]$ ls -l
总用量 0
-rwxrwx---. 1 liyufeng liyufeng 0 326 22:01 test1.txt

可见,修改完后从-rw-rw-r–变成了-rwxrwx—

3、使用root用户登录,修改文件拥有者:chown [-R] 账号名称 文件名
直接用liyufeng是改不了的
其中,[-R]表示下面的子文件也一起更改

[root@localhost ~]# cd /home/liyufeng
[root@localhost liyufeng]# cd lixiaopang
[root@localhost lixiaopang]# cd data
[root@localhost data]# ls -l
total 0
-rwxrwxrwx. 1 liyufeng liyufeng 0 Mar 26 22:01 test1.txt
[root@localhost data]# chown user1 test1.txt
[root@localhost data]# ls -l
total 0
-rwxrwxrwx. 1 user1 liyufeng 0 Mar 26 22:01 test1.txt

可见,文件拥有者已经增加了。注意,只有在/etc/passwd这个文件中有记录的用户名才能更改,那么怎么查看这个文件的内容呢?vi /etc/passwd

vi /etc/passwd
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-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
libstoragemgmt:x:998:995:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
colord:x:997:994:User for colord:/var/lib/colord:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
saned:x:996:993:SANE scanner daemon user:/usr/share/sane:/sbin/nologin
gluster:x:995:992:GlusterFS daemons:/run/gluster:/sbin/nologin
saslauth:x:994:76:Saslauthd user:/run/saslauthd:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
setroubleshoot:x:993:990::/var/lib/setroubleshoot:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
chrony:x:992:987::/var/lib/chrony:/sbin/nologin
unbound:x:991:986:Unbound DNS resolver:/etc/unbound:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
sssd:x:990:984:User for sssd:/:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
geoclue:x:989:983:User for geoclue:/var/lib/geoclue:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
gnome-initial-setup:x:988:982::/run/gnome-initial-setup/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
liyufeng:x:1000:1000:李小胖:/home/liyufeng:/bin/bash
user1:x:1001:1001::/home/user1:/bin/bash

可以看到user1是在里边的
这里还有很多没有见过的用户,这些用户是Linux系统默认的。这些用户中的绝大多数是系统或服务正常运行所必需的用户,这种用户通常称为系统用户或伪用户。系统用户无法用来登录系统,但也不能删除,因为一旦删除,依赖这些用户运行的服务或程序就不能正常执行,会导致系统问题。
每行用户信息都以 “:” 作为分隔符,划分为 7 个字段,每个字段所表示的含义如下:
用户名:密码:UID(用户ID):GID(组ID):描述性信息:主目录:默认Shell

4、更改文件用户组

[root@localhost data]# ls -l
total 0
-rwxrwxrwx. 1 user1 liyufeng 0 Mar 26 22:01 test1.txt
[root@localhost data]# chgrp user1 test1.txt
[root@localhost data]# ls -l
total 0
-rwxrwxrwx. 1 user1 user1 0 Mar 26 22:01 test1.txt

可见,文件拥有者已经改变了。注意,只有在/etc/group这个文件中有记录的用户名才能更改,那么怎么查看这个文件的内容呢?vi /etc/group,与上面的类似
另外,也可以同时改变文件拥有者和所属组:chown 拥有者:用户组 文件名

[root@localhost data]# ls -l
total 0
-rwxrwxrwx. 1 user1 user1 0 Mar 26 22:01 test1.txt
[root@localhost data]# chown liyufeng:liyufeng test1.txt
[root@localhost data]# ls -l
total 0
-rwxrwxrwx. 1 liyufeng liyufeng 0 Mar 26 22:01 test1.txt

3、如果只想添加部分权限的话,chmod其实完整表述是这样子的
chmod [可选项] <file…>
其中mode 为权限设定字串,详细格式如下:
[ugoa…][[±=][rwxX]…][,…],其中
[ugoa…]
u 表示该档案的拥有者,g 表示与该档案的拥有者属于同一个群体(group)者,o 表示其他以外的人,a 表示所有(包含上面三者)。
[±=]
+表示增加权限,- 表示取消权限,= 表示唯一设定权限。
[rwxX]
r 表示可读取,w 表示可写入,x 表示可执行,X 表示只有当该档案是个子目录或者该档案已经被设定过为可执行。

比如,设置所有用户增加读权限

[root@localhost data]# ls -l
total 0
-rwxr-----. 1 liyufeng liyufeng 0 Mar 26 22:01 test1.txt
[root@localhost data]# chmod a+r test1.txt
[root@localhost data]# ls -l
total 0
-rwxr--r--. 1 liyufeng liyufeng 0 Mar 26 22:01 test1.txt
[root@localhost data]# 

设置权限为拥有者与其所属同一个群组 可读写,其它组不可写

[root@localhost data]# ls -l
total 0
-rwxr--r--. 1 liyufeng liyufeng 0 Mar 26 22:01 test1.txt
[root@localhost data]# chmod ug+w,o-w test1.txt
[root@localhost data]# ls -l
total 0
-rwxrw-r--. 1 liyufeng liyufeng 0 Mar 26 22:01 test1.txt
[root@localhost data]# 
举报

相关推荐

0 条评论