Linux学习笔记5

Linux学习笔记5

系统拥有哪些组

cat /etc/group

更改所属组

chgrp

例:更改test目录的所属组

[root@localhost ~]# cd /tmp/
[root@localhost tmp]# mkdir test
[root@localhost tmp]# ls -ld test/
drwxr-xr-x 2 root root 6 3月 29 18:37 test/
[root@localhost tmp]# groupadd user1
[root@localhost tmp]# chgrp user1 test/
[root@localhost tmp]# ls -ld test/
drwxr-xr-x 2 root user1 6 3月 29 18:37 test/
[root@localhost tmp]#

PS:默认的更改命令是不会将目录中的文件的所属组更改的,如果要级联更改,那么可以加上“-R”选项。

更改所属主

chown

例:更改test的所属组

[root@localhost tmp]# useradd user2
[root@localhost tmp]# ls -ld test/
drwxr-xr-x 3 root user1 16 3月 29 18:42 test/
[root@localhost tmp]# chown user2 test/
[root@localhost tmp]# ls -ld test/
drwxr-xr-x 3 user2 user1 16 3月 29 18:42 test/
[root@localhost tmp]#

PS:默认的更改命令是不会将目录中的文件的所属主更改的,如果要级联更改,那么可以加上“-R”选项。

例:更改test及目录下所有文件的属主和属组

[root@localhost tmp]# ls -ld test/
drwxr-xr-x 3 user2 user1 16 3月 29 18:42 test/
[root@localhost tmp]# ls -ld test/123/
drwxr-xr-x 2 root root 20 3月 29 18:43 test/123/
[root@localhost tmp]# chown -R user2:user1 test/
[root@localhost tmp]# ls -ld test/
drwxr-xr-x 3 user2 user1 16 3月 29 18:42 test/
[root@localhost tmp]# ls -ld test/123/
drwxr-xr-x 2 user2 user1 20 3月 29 18:43 test/123/

PS:属主是可以省略的。

特殊权限

chattr

例:给test加上不可修改权限。

[root@localhost tmp]# lsattr -d test/
---------------- test/
[root@localhost tmp]# chattr +i test/
[root@localhost tmp]# lsattr -d test/
----i----------- test/
[root@localhost tmp]#

PS:设置之后,不能被删除、重命名、写入和新增数据,可以用”-i”选项删除不可修改权限。

PS:级联也是假”-R”选项。

选项:

A:表示文件或目录的atime将不可被修改。

s:数据同步写入磁盘中。

a:只能追加不能删除,非root用户不能设定该属性。

c:自动压缩该文件,读取时会自动解压。

i:文件不能被删除、重命名、设定链接、写入以及新增数据。

例:级联读取test目录下的特殊权限。

[root@localhost tmp]# lsattr -R test/
---------------- test/123
test/123:
---------------- test/123/321.txt
[root@localhost tmp]#

强制位(set uid和set gid)与冒险位(sticky)的作用都有什么

http://www.cnblogs.com/wanggd/p/3195114.html

文件搜索

whereis

例:查找文件

[root@localhost ~]# whereis vim
vim: /usr/bin/vim /usr/share/vim /usr/share/man/man1/vim.1.gz
[root@localhost ~]# whereis bin
bin: /usr/local/bin
[root@localhost ~]#

PS:该命令是通过预生成的一个文件列表库去查找与给出文件名相关的文件。

locate

PS:通过查找预生成的文件列表库来告诉用户要查找的文件在哪里,一般用于知道具体信息,才去搜索。

PS:如果没有locate命令,使用yum install -y mlocate安装。

find(常用)

语法 : find [路径] [参数]

‘-atime +n/-n’ : 访问或执行时间大于/小于n天的文件

‘-ctime +n/-n’ : 写入、更改inode属性(例如更改所有者、权限或者链接)时间大于/小于n天的文件

‘-mtime +n/-n’ : 写入时间大于/小于n天的文件

[root@localhost ~]# find /tmp/ -mtime -1
/tmp/
/tmp/.ICE-unix
/tmp/test
[root@localhost ~]# find /tmp/ -atime +10
[root@localhost ~]# find /tmp/ -atime +1
/tmp/yum.log
/tmp/.bash_history

文件的 Access time也就是 ‘atime’ 是在读取文件或者执行文件时更改的。

文件的 Modified time也就是 ‘mtime’ 是在写入文件时随文件内容的更改而更改的。

文件的 Change time也就是 ‘ctime’ 是在写入文件、更改所有者、权限或链接设置时随inode的内容更改而更改的。

因此,更改文件的内容即会更改mtime和ctime,但是文件的ctime可能会在 mtime 未发生任何变化时更改,例如,更改了文件的权限,但是文件内容没有变化。

‘stat’ 命令可用来列出文件的 atime、ctime 和 mtime。

[root@localhost ~]# stat test/test2
File: 'test/test2'
Size: 0 Blocks: 0 IO Block: 4096 普通空文件
Device: 803h/2051d Inode: 261657 Links: 1
Access: (0744/-rwxr--r--) Uid: ( 500/ user1) Gid: ( 500/testgroup)
Access: 2013-05-10 09:00:36.092000531 +0800
Modify: 2013-05-10 09:00:36.092000531 +0800
Change: 2013-05-10 09:30:58.788996594 +0800

atime不一定在访问文件之后被修改,因为:使用ext3文件系统的时候,如果在mount的时候使用了noatime参数那么就不会更新atime的信息。总之, 这三个 time stamp 都放在 inode 中。若 mtime, atime 修改inode 就一定會改, 既然 inode 改了, 那 ctime 也就跟着要改了。

‘find’常用选项:

‘-name filename’ 直接查找该文件名的文件,这个选项使用很多。

[root@localhost ~]# find . -name test2
./test/test2
./test2

‘-type filetype’ 通过文件类型查找。filetype 包含了 f, b, c, d, l, s 等。

[root@localhost ~]# find /tmp/ -type d
/tmp/
/tmp/.ICE-unix
/tmp/test
[root@localhost ~]# find /tmp/ -type f
/tmp/yum.log
/tmp/.bash_history
/tmp/ip.txt

综合案例:查找修改时间小于10天的以”.txt”结尾的文件

[root@localhost ~]# find /root/ -mtime -10 -type f -name "*.txt"
/root/.cache/tracker/db-version.txt
/root/.cache/tracker/db-locale.txt
/root/.cache/tracker/miner-applications-locale.txt
/root/.cache/tracker/last-crawl.txt
/root/.cache/tracker/first-index.txt
/root/234.txt
/root/2/3/1.txt
/root/123.txt
[root@localhost ~]#

Linux的链接文件

链接文件分为两种,硬链接(hard link)和软链接(symbolic link)。两种链接的本质区别关键点在于inode。

Hard Links : 当系统要读取一个文件时,就会先去读inode table,然后再去根据inode中的信息到块区域去将数据取出来。而hard link 是直接再建立一个inode链接到文件放置的块区域。也就是说,进行hard link的时候实际上该文件内容没有任何变化,只是增加了一个指到这个文件的inode, hard link 有两个限制:(1)不能跨文件系统,因为不同的文件系统有不同的inode table; (2) 不能链接目录。

Symbolic Links : 跟hard link不同,这个是建立一个独立的文件,而这个文件的作用是当读取这个链接文件时,它会把读取的行为转发到该文件所link的文件上。这样讲,也许比较绕口,那么就来举一个例子。现在有文件a,做了一个软链接文件b(只是一个链接文件,非常小),b指向了文件a。当读取b时,那么b就会把读取的动作转发到a上,这样就读取到了文件a。所以,当删除文件a时,文件b并不会被删除,但是再读取b时,会提示无法打开文件。而当删除b时,a是不会有任何影响的。

命令: ln

语法 : ln [-s] [来源文件] [目的文件]

ln 常用的选项就一个 ‘-s’, 如果不加就是建立硬链接,加上就建立软链接。

[root@localhost ~]# mkdir 123
[root@localhost ~]# cd 123
[root@localhost 123]# cp /etc/passwd ./
[root@localhost 123]# ll
总用量 4
-rw-r--r-- 1 root root 1097 5月 10 17:08 passwd
[root@localhost 123]# du -sk
8 .
[root@localhost 123]# ln passwd passwd-hard
[root@localhost 123]# ll
总用量 8
-rw-r--r-- 2 root root 1097 5月 10 17:08 passwd
-rw-r--r-- 2 root root 1097 5月 10 17:08 passwd-hard
[root@localhost 123]# du -sk
8 .

PS:做了硬链接后,虽然两个文件大小都为 ‘1097’, 但是目录的大小并没有变化。
[root@localhost 123]# ll
总用量 4
-rw-r--r-- 1 root root 1097 5月 10 17:08 passwd-hard
[root@localhost 123]# rm -f passwd
[root@localhost 123]# du -sk
8 .

PS:删除源文件passwd, 空间依旧不变。这说明硬链接只是复制了一份inode信息。

[root@localhost ~]# ln 123 456
ln: "123": 不允许将硬链接指向目录

PS:硬链接不能用于目录。

[root@localhost ~]# mkdir 456
[root@localhost ~]# cd 456
[root@localhost 456]# cp /etc/passwd ./
[root@localhost 456]# ln -s passwd passwd-soft
[root@localhost 456]# ll
总用量 4
-rw-r--r-- 1 root root 1097 5月 10 17:18 passwd
lrwxrwxrwx 1 root root 6 5月 10 17:19 passwd-soft -> passwd
[root@localhost 456]# head -n1 passwd-soft
root:x:0:0:root:/root:/bin/bash
[root@localhost 456]# head -n1 passwd
root:x:0:0:root:/root:/bin/bash
[root@localhost 456]# rm -f passwd
[root@localhost 456]# head -n1 passwd-soft
head: 无法打开"passwd-soft" 读取数据: 没有那个文件或目录
[root@localhost 456]# ll
总用量 0
lrwxrwxrwx 1 root root 6 5月 10 17:19 passwd-soft -> passwd

PS:如果删除掉源文件,则软链接文件不能读取了,而且使用 ‘ll’ 查看发现颜色也变了。
[root@localhost ~]# ln -s 456 789
[root@localhost ~]# ls -ld 456 789
drwxr-xr-x 2 root root 4096 5月 10 17:22 456
lrwxrwxrwx 1 root root 3 5月 10 17:29 789 -> 456

PS:目录是可以软链接的。

PS:建议用绝对路径(源文件)创建软链接。

PS:硬链接使用的是同一个inode。

PS:同一个分区下,硬链接不会占空间,但是不同分区下,硬链接会占空间。

参考《跟阿铭学Linux》


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值