在linux中什么是一个文件的路径呢,说白了就是文件存在的地方。在linux的世界中,存在着绝对路径和相对路径。
绝对路径:路径的写法一定由根目录“/”写起。例如/usr/local/mysql,这就是绝对路径。
相对路径:路径的写法不是由根目录“/”写起。例如,首先用户进入到/,然后再进入到home,命令为cd /home然后cd test,此时用户所在路径为/home/test。第一个cd命令后跟/home,第二个cd命令后跟test,并没有斜杠,这个test是相对于/home目录来讲的,所以叫做相对路径。
pwd这个命令打印出当前所在目录
1
2
3
|
[root@localhost ~]# pwd /root [root@localhost ~]# |
cd进入到某个目录
1
2
3
4
|
[root@localhost ~]# cd /usr/local/ [root@localhost local]# pwd /usr/local [root@localhost local]# |
./指的是当前目录
../指的是当前目录的上级目录
1
2
3
4
5
6
7
8
9
10
|
[root@localhost local]# cd /usr/local/lib/ [root@localhost lib]# pwd /usr/local/lib [root@localhost lib]# cd ./ [root@localhost lib]# pwd /usr/local/lib [root@localhost lib]# cd ../ [root@localhost local]# pwd /usr/local [root@localhost local]# |
mkdir创建一个目录
mkdir其实就是make directory的缩写。其语法为mkdir [-mp] [目录名称],其中-m、-p为其选项。-m:这个参数用来指定要创建目录的权限,该参数不常用。-p:这个参数是很管用的。
1
2
3
4
5
|
[root@localhost ~]# mkdir /tmp/test/123 mkdir : cannot create directory 鈥tmp/test/123鈥 No such file or directory [root@localhost ~]# ls /tmp/test ls: cannot access /tmp/test: No such file or directory [root@localhost ~]# |
当我们想创建/tmp/test/123目录,可是提示不能创建,原因是/tmp/test目录不存在。
1
2
3
4
5
6
|
[root@localhost ~]# ls /tmp/test ls: cannot access /tmp/test: No such file or directory [root@localhost ~]# mkdir -p /tmp/test/123 [root@localhost ~]# ls /tmp/test 123 [root@localhost ~]# |
你看到这里,是不是明白-p参数的作用了?没错,它的作用就是递归创建目录,即使上级目录不存在,还有一种情况就是如果你想要创建的目录存在的话,会提示报错,然后你加上-p参数,就不会报错了。
1
2
3
4
5
6
|
[root@localhost ~]# ls /tmp/test 123 [root@localhost ~]# mkdir /tmp/test/123 mkdir : cannot create directory 鈥tmp/test/123鈥 File exists [root@localhost ~]# mkdir -p /tmp/test/123 [root@localhost ~]# |
rmdir删除一个目录
1
2
3
4
5
|
[root@localhost ~]# ls /tmp/test/ 123 [root@localhost ~]# rmdir /tmp/test/123/ [root@localhost ~]# ls /tmp/test/ [root@localhost ~]# |
rmdir其实是rmove directory缩写,其只有一个选项-p类似与mkdir命令,这个参数的作用是将上级目录一起删除。举个例子吧,新建目录mkdir -p d1/d2/d3,rmdir -p d1/d2/d3相当于是删除了d/,d1/d2,d1/d2/d3。如果一个目录目录中还有目录,那么当你直接rmdir该目录时,会提示该目录不为空。如果你非要删除不为空的目录,那你用rm指令吧。
rm删除目录或文件
rmdir只能删除目录但不能删除文件,想要删除一个文件,则要用rm命令了。rm同样也有很多选项。
-f:强制的意思,如果不加这个选项,当删除一个不存在的文件时会报错。
1
2
3
4
5
6
|
[root@localhost ~]# ls /tmp/111 ls: cannot access /tmp/111: No such file or directory [root@localhost ~]# rm /tmp/111 rm: cannot remove 鈥tmp/111鈥 No such file or directory [root@localhost ~]# rm -f /tmp/111 [root@localhost ~]# |
-i:这个选项的作用是,当用户删除一个文件时会提示用户是否真的删除。
1
2
3
4
5
6
|
[root@localhost ~]# ls /tmp/test/123/ 1.txt [root@localhost ~]# rm -i /tmp/test/123/1.txt rm: remove regular file 鈥tmp/test/123/1.txt鈥 y [root@localhost ~]# ls /tmp/test/123/ [root@localhost ~]# |
如果删除,输入y否则输入n
-r:当删除目录时,加该选项,如果不加这个选项会报错。rm是可以删除不为空的目录的。
1
2
3
4
5
6
7
8
|
[root@localhost ~]# ls /tmp/test 123 [root@localhost ~]# rm /tmp/test rm: cannot remove 鈥tmp/test鈥 Is a directory [root@localhost ~]# rm -rf /tmp/test [root@localhost ~]# ls /tmp/test ls: cannot access /tmp/test: No such file or directory [root@localhost ~]# |
ls在前面的命令中多次用到它。现在你已经明白它的含义了吧。没有错,就是查看某个目录或者文件,是list的简写。ls后可以跟一个目录,也可以跟一个文件。以下是ls的选项,在这里笔者并没有完全列出,只是列出了平时使用最多的选项。
-a:全部的文档都列出,包含隐藏的。linux文件系统中同样也有隐藏文件。这些隐藏文件的文件名是以.开头的。例如.test、/root/.123、/root/.ssh等等,隐藏文件可以是目录也可以是普通文件。
-l:详细列出文件的属性信息,包括大小、创建时间、所属主、所属组等等。
-d:后边跟目录,如果不加这个选项则列出目录下的文件,加上后只列出目录本身。
1
2
3
4
5
6
|
[root@localhost ~]# ls /root/ 1.txt case1.sh continue2.sh for3.sh fun.sh myfile test3.sh test.sh wc anaconda-ks.cfg case2.sh continue .sh for .sh if1.sh test test4.sh test.txt while2.sh break2.sh case .sh for1.sh function1.sh if2.sh test1.sh test5.sh until.sh while .sh break .sh code for2.sh function2.sh if3.sh test2.sh test6.sh users [root@localhost ~]# |
cp是copy的简写,即拷贝格式为cp [选项] [源文件] [目标文件],例如想把test1拷贝成test2,这样既可cp test1 test2,以下介绍几个常见的选项。
-d:这里涉及到一个“连接”的概念。连接分为软连接和硬链接。在以后的章节会详细解释,现在你只要明白这里的软连接跟windows中的款姐方式类似。如果不加这个-d则拷贝软连接时会把软连接的目标文件拷贝过去,而加上后,其实只是拷贝一个连接文件(即快捷方式)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[root@localhost cptest]# touch test [root@localhost cptest]# ln -s test test1 [root@localhost cptest]# ls -l test test1 -rw-r--r--. 1 root root 0 Sep 10 22:25 test lrwxrwxrwx. 1 root root 4 Sep 10 22:25 test1 -> test [root@localhost cptest]# cp test1 test2 [root@localhost cptest]# ls -l test test1 test2 -rw-r--r--. 1 root root 0 Sep 10 22:25 test lrwxrwxrwx. 1 root root 4 Sep 10 22:25 test1 -> test -rw-r--r--. 1 root root 0 Sep 10 22:25 test2 [root@localhost cptest]# cp -d test1 test3 [root@localhost cptest]# ls -l test test1 test2 test3 -rw-r--r--. 1 root root 0 Sep 10 22:25 test lrwxrwxrwx. 1 root root 4 Sep 10 22:25 test1 -> test -rw-r--r--. 1 root root 0 Sep 10 22:25 test2 lrwxrwxrwx. 1 root root 4 Sep 10 22:26 test3 -> test [root@localhost cptest]# |
-r:如果你要拷贝一个目录,必须要加-r选项,否则你是拷贝不了目录的。
1
2
3
4
5
6
7
8
9
10
|
[root@localhost cptest]# mkdir 123 [root@localhost cptest]# ls 123 test test1 test2 test3 [root@localhost cptest]# cp 123 456 cp: omitting directory 鈥23鈥 [root@localhost cptest]# [root@localhost cptest]# cp -r 123 456 [root@localhost cptest]# ls 123 456 test test1 test2 test3 [root@localhost cptest]# |
-i:如果遇到一个文件存在的文件,会问是否覆盖。
1
2
3
4
5
6
7
|
[root@localhost cptest]# ls 123 456 test test1 test2 test3 [root@localhost cptest]# touch 111 [root@localhost cptest]# touch 222 [root@localhost cptest]# cp -i 111 222 cp: overwrite 鈥22鈥 n [root@localhost cptest]# |
上例中,touch命令,看字面意思就是摸一下,没有,如果有这个文件,则会改变文件的访问时间,如果没有这个文件就会创建这个文件。
-u:该选项仅当目标文件存在时才生效,如果源文件比目标文件新才会拷贝,否则不做任何动作。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[root@localhost cptest]# rm -rf * [root@localhost cptest]# ls [root@localhost cptest]# echo 111 > 111 [root@localhost cptest]# cat 111 111 [root@localhost cptest]# echo 222 > 222 [root@localhost cptest]# cat 222 222 [root@localhost cptest]# cp -u 111 222 [root@localhost cptest]# cat 222 222 [root@localhost cptest]# echo aaa > 111 [root@localhost cptest]# cp -u 111 222 cp: overwrite 鈥22鈥 y [root@localhost cptest]# cat 222 aaa [root@localhost cptest]# |
mv是移动的意思,是move的简写。格式为:mv [选项] [源文件] [目标文件] ,下面介绍几个常用的选项。
-i:和cp的-i一样,当目标文件存在时询问用户是否要覆盖。
-u:和cp命令的-u选项一个作用,当目标文件存在时才生效,如果源文件比目标文件新才会移动,否则不做任何操作。
该命令集中情况;
1)、目标文件时目录,而且目标文件不存在;
2)、目标文件时目录,而且目标文件存在;
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@localhost mvtest]# mkdir aaa [root@localhost mvtest]# ls aaa [root@localhost mvtest]# mv aaa bbb [root@localhost mvtest]# ls bbb [root@localhost mvtest]# mkdir ccc [root@localhost mvtest]# ls bbb ccc [root@localhost mvtest]# mv bbb ccc [root@localhost mvtest]# ls ccc [root@localhost mvtest]# |
3)、目标文件不是目录,而且目标文件不存在;
4)、目标文件不是目录,而且目标文件存在;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@localhost mvtest]# touch aa [root@localhost mvtest]# ls aa [root@localhost mvtest]# mv aa bb [root@localhost mvtest]# ls bb [root@localhost mvtest]# touch cc [root@localhost mvtest]# ls bb cc [root@localhost mvtest]# mv bb cc mv: overwrite 鈥榗c鈥 y [root@localhost mvtest]# ls cc [root@localhost mvtest]# |
cat是比较常用的一个命令,即查看一个文件的内容并显示在屏幕上。
-n:查看文件时,把行号也显示到屏幕。
1
2
3
4
5
6
|
[root@localhost mvtest]# echo 1111 > 1.txt [root@localhost mvtest]# echo 2222 >> 1.txt [root@localhost mvtest]# cat -n 1.txt 1 1111 2 2222 [root@localhost mvtest]# |
上例中出现了一个 “>>”,这个符号跟前面介绍的“>”的作用都是重定向,即把前面输出的内容输出到后面的文件中。“>>”是追加的意思,而用“>”,如果文件中有内容则会删除文件中的内容,而“>>”则不会。
-A:显示所有东西,包括特殊字符。
1
2
3
4
5
6
7
8
9
10
11
12
|
[root@localhost mvtest]# echo 111 > 1.txt [root@localhost mvtest]# echo 222 >> 1.txt [root@localhost mvtest]# cat -n 1.txt 1 111 2 222 [root@localhost mvtest]# cat -A 1.txt 111$ 222$ [root@localhost mvtest]# cat -nA 1.txt 1 111$ 2 222$ [root@localhost mvtest]# |
tac其实是cat的反写,同样的功能也是反向打印内容到屏幕上。
1
2
3
4
5
6
7
8
9
|
[root@localhost mvtest]# echo 1234 > 1.txt [root@localhost mvtest]# echo 5678 >> 1.txt [root@localhost mvtest]# cat -nA 1.txt 1 1234$ 2 5678$ [root@localhost mvtest]# tac 1.txt 5678 1234 [root@localhost mvtest]# |
more也是用来查看一个文件内容。当文件内容太多,一屏幕不能显示全,而你用cat肯定是看不到前面的内容的,那么使用more就可以解决这个问题。当看完一屏后按空格键继续看下一屏。但看完所有内容后就会退出。如果你想要提前退出,只要按q键即可。
less作用跟more一样,但比more好在可以上翻下翻。空格键同样可以翻页,而按j键可以向下移动,按k键向上移动。在使用more和less查看某个文件时,你可以按“/”键,然后输入一个word回车,这样就可以找到word了。如果是多个word可以按“n”键显示下一个。另外你也可以不按“/”而是按“?”后面同样跟word来搜索这个word,唯一不同的是“/”是在当前行向下搜索,而“?”是在当前行向上搜索。
head后面直接跟文件名,则显示文件前10行。如果加-n选项则显示文件前n行。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[root@localhost mvtest]# head /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 [root@localhost mvtest]# head -n 5 /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 [root@localhost mvtest]# |
tail和head一样,后面直接跟文件名,则显示文件最后10行。如果加-n选项则显示文件最后n行。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[root@localhost mvtest]# tail /etc/passwd user_91:x:1095:100::/home/user_91:/bin/bash user_92:x:1096:100::/home/user_92:/bin/bash user_93:x:1097:100::/home/user_93:/bin/bash user_94:x:1098:100::/home/user_94:/bin/bash user_95:x:1099:100::/home/user_95:/bin/bash user_96:x:1100:100::/home/user_96:/bin/bash user_97:x:1101:100::/home/user_97:/bin/bash user_98:x:1102:100::/home/user_98:/bin/bash user_99:x:1103:100::/home/user_99:/bin/bash user_100:x:1104:100::/home/user_100:/bin/bash [root@localhost mvtest]# tail -n 5 /etc/passwd user_96:x:1100:100::/home/user_96:/bin/bash user_97:x:1101:100::/home/user_97:/bin/bash user_98:x:1102:100::/home/user_98:/bin/bash user_99:x:1103:100::/home/user_99:/bin/bash user_100:x:1104:100::/home/user_100:/bin/bash [root@localhost mvtest]# |
-f:动态显示文件的最后10行,如果文件时不断增加的,则用-f选项。如 tail -f /var/log/messages
一个linux目录或者文件,都会有一个所属主和所属组。所属主,即文件的拥有者,而所属组,即该文件所属主所在的一个组。linux这样设置文件属性的目的是为了文件的安全。例如,test文件的所属主是user0,而test1文件的所属主是user1,那么user1是不能查看test文件的,相应的user0也是不能查看test1文件的。然后又这样一个应用,我想要创建一个文件同时让user0和user1来查看怎么办呢?
这事“所属组”就派上用场了,即,创建一个群组users,让user0和user1同属于users组,然后创建一个文件test2,且其所属组为users,那么user0和user1都可以访问test2文件。
linux文件属性不仅规定了所属主和所属组,还规定了所属主(user)、所属组(group)以及其他用户(others)对文件的权限。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@localhost ~]# ls -l total 128 -rw-r--r--. 1 root root 0 Sep 5 22:51 1.txt -rw-------. 1 root root 1259 Jul 31 17:28 anaconda-ks.cfg -rw-r--r--. 1 root root 210 Sep 4 22:18 break2.sh -rw-r--r--. 1 root root 298 Sep 4 22:11 break .sh -rw-r--r--. 1 root root 304 Sep 4 18:32 case1.sh -rw-r--r--. 1 root root 261 Sep 4 18:39 case2.sh -rw-r--r--. 1 root root 146 Sep 6 02:08 case .sh drwxr-xr-x. 2 root root 21 Sep 4 00:46 code -rw-r--r--. 1 root root 178 Sep 4 23:08 continue2.sh -rw-r--r--. 1 root root 301 Sep 4 22:21 continue .sh drwxr-xr-x. 2 root root 28 Sep 10 22:42 cptest |
上例中,用ls -l查看当前目录下的文件,共显示了9列内容(用空格划分列),都代表了什么含义呢?
第一列,包含的东西有该文件类型、所属主、所属组已经其他用户对该文件的权限。第一列共10位。其中第一位用来描述该文件的类型。上例中,我们看到的类型有“d”和“-”,其实除了这两种外还有“l”,“b”、“c”、“s”等。
d:表示该文件是目录;
-:表示该文件是普通文件;
l:表示该文件是连接文件;
1
2
3
|
[root@localhost ~]# ls -l /etc/rc.local lrwxrwxrwx. 1 root root 13 Jul 31 17:23 /etc/rc.local -> rc.d/rc.local [root@localhost ~]# |
b:表示该文件是块设备文件,比如磁盘分区;
1
2
3
4
5
|
[root@localhost ~]# ls -l /dev/sda* brw-rw----. 1 root disk 8, 0 Sep 10 18:04 /dev/sda brw-rw----. 1 root disk 8, 1 Sep 10 18:04 /dev/sda1 brw-rw----. 1 root disk 8, 2 Sep 10 18:04 /dev/sda2 [root@localhost ~]# |
c:表示该文件是串行端口设备,例如键盘、鼠标;
s:表示该文件是套接字文件(socket),用于进程间通信;
后面的9位,每三个为一组。均为rwx三个参数的组合。其中r代表可读,w代表可写,x代表可执行。前三位为所属主(user)的权限,中间三位为所属组(group)的权限,最后三位为其他非本群组(others)的权限。
一个文件的属性为-rwxr-xr--,它代表的意思是,该文件时普通文件,文件的拥有者可读可写可执行,文件所属组对其有可读不可写可执行,其他用户对其只可读。
对于一个目录来讲,打开这个目录即为执行这个目录,所以任何一个目录必须要有x权限才能打开并查看该目录。例如一个目录的属性为drwxr--r--其所属主为root,那么除了root外的其他用户是不能打开这个目录的。
第二列,表示为链接占用的节点(inode),若为目录时,通常与该目录地下还有多少目录有关系。
第三列,表示该文件的所属主。
第四列,表示该文件的所属组。
第五列,表示该文件的大小。
第六列、第七列、第八列,表示该文件的创建日期或者最近的修改日期,分别为月份日期以及时间。
第九列,表示文件名。如果文件名前有个.,则表示该文件是隐藏文件。
更改文件的权限,也就是更改所属主、所属组以及他们对应的读写执行权限。
1)、更改所属组chgrp
语法:chgrp [组名] [文件名]
1
2
3
4
5
6
7
8
|
[root@localhost chtest]# groupadd testgroup [root@localhost chtest]# touch test1 [root@localhost chtest]# ls -l test1 -rw-r--r--. 1 root root 0 Sep 11 19:58 test1 [root@localhost chtest]# chgrp testgroup test1 [root@localhost chtest]# ls -l test1 -rw-r--r--. 1 root testgroup 0 Sep 11 19:58 test1 [root@localhost chtest]# |
2)、更改文件所属主chown
语法:chown [-R] [账号名] [文件名] 或 chown [-R] [账户名:组名] [文件名]
这里的-R选项只作用于目录,作用是级联更改,即不仅更改当前目录,连目录中的目录或者文件全部更改。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
[root@localhost chtest]# mkdir test [root@localhost chtest]# useradd user1 [root@localhost chtest]# ls -ld test drwxr-xr-x. 2 root root 6 Sep 11 21:54 test [root@localhost chtest]# touch test/test2 [root@localhost chtest]# ls -l test total 0 -rw-r--r--. 1 root root 0 Sep 11 21:54 test2 [root@localhost chtest]# chown user1 test [root@localhost chtest]# ls -ld test drwxr-xr-x. 2 user1 root 19 Sep 11 21:54 test [root@localhost chtest]# ls -l test total 0 -rw-r--r--. 1 root root 0 Sep 11 21:54 test2 [root@localhost chtest]# chown -R user1:testgroup test [root@localhost chtest]# ls -ld test drwxr-xr-x. 2 user1 testgroup 19 Sep 11 21:54 test [root@localhost chtest]# ls -l test total 0 -rw-r--r--. 1 user1 testgroup 0 Sep 11 21:54 test2 [root@localhost chtest]# |
上例中,首先建立一个目录test,然后在test目录下创建一个普通文件test2,因为是以root的身份创建的目录和文件,所以所属主以及所属组都是root。chown user1 test这使test的目录所属主由root变为user1,然后test目录下的test2文件所属主以及所属组还是root。接着chown -R user1:testgroup test这样把test连同test目录下的test2的所属主以及所属组都改变了。
3)、更改用户对文件的读写执行权限chmod
在linux中为了方便更改这些权限,linux使用数字代替rwx,具体规则为(r:4)、(w:2)、(x:1)、( -:0)。
举个例子,-rwxrwx---用数组表示就是770,具体是这样来的:
rwx=4+2+1=7;rwx=4+2+1=7;---=0+0+0=0
语法:chmod [-R] [xyz] [文件名](这里的xyz,表示数字)
-R:选项作用同chown,级联更改。
值得提一下的是,在linux系统中,默认一个目录的权限为755,而一个文件的默认权限是644。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[root@localhost chtest]# ls -ld test drwxr-xr-x. 2 user1 testgroup 19 Sep 11 21:54 test [root@localhost chtest]# ls -l test total 0 -rw-r--r--. 1 user1 testgroup 0 Sep 11 21:54 test2 [root@localhost chtest]# chmod 750 test [root@localhost chtest]# ls -ld test drwxr-x---. 2 user1 testgroup 19 Sep 11 21:54 test [root@localhost chtest]# ls -l test/test2 -rw-r--r--. 1 user1 testgroup 0 Sep 11 21:54 test/test2 [root@localhost chtest]# chmod -R 700 test [root@localhost chtest]# ls -ld test drwx------. 2 user1 testgroup 19 Sep 11 21:54 test [root@localhost chtest]# ls -l test total 0 -rwx------. 1 user1 testgroup 0 Sep 11 21:54 test2 [root@localhost chtest]# |
如果你创建一个目录,而该目录不想让其他人看到内容,则只需要设置成rwxr-----(740)即可。
chmod还支持使用rwx的方式来设置权限。从之前的介绍中我们可以发现,基本上这9个属性分别是(1)user(2)group(3)others三群。那么我们就可以使用u、g、o来代表三群的属性。此外,a则代表all亦即全部的三群。那么读写的属性就可以写成r、w、x。也就可以使用下面的方式:
chmod |
u g o a |
+(加入) -(除去) =(设定) |
r w x |
文档或目录 |
现在我想把一个文件设置成这样的权限rwxr-xr-x(755),使用这样的方式改变权限的命令为:
1
2
3
4
5
6
|
[root@localhost chtest]# ls -l test/test2 -rwx------. 1 user1 testgroup 0 Sep 11 21:54 test/test2 [root@localhost chtest]# chmod u=rwx,og=rx test/test2 [root@localhost chtest]# ls -l test/test2 -rwxr-xr-x. 1 user1 testgroup 0 Sep 11 21:54 test/test2 [root@localhost chtest]# |
另外还可以针对u、g、o、a增加或者减少某个权限(读、写、执行),例如:
1
2
3
4
5
6
7
8
9
10
11
12
|
[root@localhost chtest]# ls -l test/test2 -rwxr-xr-x. 1 user1 testgroup 0 Sep 11 21:54 test/test2 [root@localhost chtest]# chmod u-x test/test2 [root@localhost chtest]# ls -l test/test2 -rw-r-xr-x. 1 user1 testgroup 0 Sep 11 21:54 test/test2 [root@localhost chtest]# chmod a-x test/test2 [root@localhost chtest]# ls -l test/test2 -rw-r--r--. 1 user1 testgroup 0 Sep 11 21:54 test/test2 [root@localhost chtest]# chmod u+x test/test2 [root@localhost chtest]# ls -l test/test2 -rwxr--r--. 1 user1 testgroup 0 Sep 11 21:54 test/test2 [root@localhost chtest]# |
which命令用来查找可执行文件的绝对路径。which命令只能用来查找PATH环境变量中出现的路径下的可执行文件。
1
2
3
4
5
6
|
[root@localhost chtest]# which ls alias ls= ‘ls --color=auto‘ /usr/bin/ls [root@localhost chtest]# which cd /usr/bin/cd [root@localhost chtest]# |
whereis通过预先生成的一个文件列表库去查找跟给出的文件名相关的文件。
语法:whereis [-bmsu] [文件名称]
-b:只找binary文件;
-m:只找在说明文件manual路径下的文件;
-s:只找source来源文件;
-u:没有说明文档的文件;
1
2
3
4
5
6
7
|
[root@localhost chtest]# whereis passwd passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.gz [root@localhost chtest]# whereis -b passwd passwd: /usr/bin/passwd /etc/passwd [root@localhost chtest]# whereis -m passwd passwd: /usr/share/man/man1/passwd.1.gz [root@localhost chtest]# |
locate类似whereis,也是通过查找预先生成的文件列表库来告诉用户要查找的文件在哪里。后面直接跟文件名。如果你的linux没有这个命令,请安装软件包mlocate,这个软件包在你的系统安装盘里,后缀名是RPM,随后介绍find命令会告诉你如何查找这个包。如果你装的CentOS你可以使用这个安装米宁来安装yum install -y mlocate。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
[root@localhost chtest]# yum install -y mlocate Loaded plugins: fastestmirror Determining fastest mirrors * base: mirrors.shu.edu.cn * extras: mirrors.shu.edu.cn * updates: mirrors.shu.edu.cn base | 3.6 kB 00:00:00 extras | 3.4 kB 00:00:00 updates | 3.4 kB 00:00:00 (1/2): extras/7/x86_64/primary_db | 187 kB 00:00:00 (2/2): updates/7/x86_64/primary_db | 5.2 MB 00:00:09 Resolving Dependencies --> Running transaction check ---> Package mlocate.x86_64 0:0.26-8.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved =========================================================================================================== Package Arch Version Repository Size =========================================================================================================== Installing: mlocate x86_64 0.26-8.el7 base 113 k Transaction Summary =========================================================================================================== Install 1 Package Total download size: 113 k Installed size: 379 k Downloading packages: mlocate-0.26-8.el7.x86_64.rpm | 113 kB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : mlocate-0.26-8.el7.x86_64 1/1 Verifying : mlocate-0.26-8.el7.x86_64 1/1 Installed: mlocate.x86_64 0:0.26-8.el7 Complete! [root@localhost chtest]# locate passwd locate: can not stat () `/ var /lib/mlocate/mlocate.db‘: No such file or directory [root@localhost chtest]# |
这是因为系统没有生成那个文件列表库,你可以使用updatedb命令立即生成(更新)这个库。如果你的服务器上正跑着重要的业务,那么你最好不要去运行这个命令,因为一旦运行,服务器的压力会变大。这个数据库默认情况下每周更新一次。所以你用locate命令去搜索一个文件,正好是在两次更新时间段内,那你肯定是得不到结果的。你可以到/etc/updated.cnf去配置这个数据库生成(更新)规则。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
[root@localhost chtest]# updatedb [root@localhost chtest]# locate passwd /etc/passwd /etc/passwd- /etc/pam.d/passwd /etc/security/opasswd /usr/bin/gpasswd /usr/bin/grub2-mkpasswd-pbkdf2 /usr/bin/passwd /usr/ include /rpcsvc/yppasswd.h /usr/ include /rpcsvc/yppasswd.x /usr/lib/firewalld/services/kpasswd.xml /usr/lib64/security/pam_unix_passwd.so /usr/local/openssl-1.1.1-pre8/apps/passwd.c /usr/local/openssl-1.1.1-pre8/apps/passwd.d /usr/local/openssl-1.1.1-pre8/apps/passwd.o /usr/local/openssl-1.1.1-pre8/doc/man1/passwd.pod /usr/local/openssl-1.1.1-pre8/doc/man3/SSL_CTX_set_default_passwd_cb.pod /usr/local/openssl-1.1.1-pre8/test/recipes/20-test_passwd.t /usr/local/openssl-1.1.1-pre8/test/recipes/90-test_sslapi_data/passwd.txt /usr/local/share/doc/openssl/html/man1/openssl-passwd.html /usr/local/share/doc/openssl/html/man1/passwd.html /usr/local/share/doc/openssl/html/man3/SSL_CTX_get_default_passwd_cb.html /usr/local/share/doc/openssl/html/man3/SSL_CTX_get_default_passwd_cb_userdata.html /usr/local/share/doc/openssl/html/man3/SSL_CTX_set_default_passwd_cb.html /usr/local/share/doc/openssl/html/man3/SSL_CTX_set_default_passwd_cb_userdata.html /usr/local/share/doc/openssl/html/man3/SSL_get_default_passwd_cb.html |
find这个搜索工具是笔者用的最多的一个,所以请你务必熟悉它。
语法:find [路径] [参数]
-atime +n:访问或者执行时间大于n天的文件;
-ctime +n:写入、更改inode属性(例如更改所有者、权限或者连接)时间大于n天定的文件;
-mtime +n:写入时间大于n天的文件;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
[root@localhost ~]# find /root -atime 1 /root/cptest/111 /root/cptest/222 /root/mvtest/1.txt [root@localhost ~]# find /root -ctime 1 /root/cptest /root/cptest/111 /root/cptest/222 /root/.lesshst /root/test /root/mvtest /root/mvtest/1.txt [root@localhost ~]# find /root -mtime 1 /root/cptest /root/cptest/111 /root/cptest/222 /root/.lesshst /root/test /root/mvtest /root/mvtest/1.txt [root@localhost ~]# |
看到这里,你对这三个time是不是有些晕了,那么笔者就先给你介绍一下这三个time属性。
文件Access time,atime是在读取文件呢或者执行文件时更改的。
文件的Modified time,mtime是在写入文件时随文件内容的更改而更改的 。
文件Cteate time,ctime是在写入文件、更改所有者、权限或者链接设置时随inode的内容更改而更改的。
因此,更改文件的内容即会更改mtime和ctime,但是文件的ctime可能会在mtime未发生任何变化时更改,例如,更改了文件的权限,但文件呢内容并没有变化
ls -l命令可用来列出文件的atime、ctime和mtime。
ls -lc filename:列出文件的ctime
ls -lu filename:列出文件的atime
ls -l filename:列出文件的mtime
1
2
3
4
5
6
7
|
[root@localhost ~]# ls -ldc file1 drwxr-xr-x. 3 root root 32 Sep 12 18:11 file1 [root@localhost ~]# ls -ldu file1 drwxr-xr-x. 3 root root 32 Sep 12 18:12 file1 [root@localhost ~]# ls -ld file1 drwxr-xr-x. 3 root root 32 Sep 12 18:11 file1 [root@localhost ~]# |
-name filename:直接查找该文件名的文件,这个是使用最多的了。
1
2
3
4
|
[root@localhost ~]# find /root -name test3.sh /root/test3.sh /root/test/test3.sh [root@localhost ~]# |
-type type:通过文件的类型查找。type包含了f、b、c、d、l、s等等。
1
2
3
4
5
6
7
8
9
10
11
|
[root@localhost ~]# mkdir file1 [root@localhost ~]# mkdir file1/file2 [root@localhost ~]# touch file1/file3 [root@localhost ~]# touch file1/file2/file4 [root@localhost ~]# find ./file1 -type d ./file1 ./file1/file2 [root@localhost ~]# find ./file1 -type f ./file1/file2/file4 ./file1/file3 [root@localhost ~]# |
在前面的内容中年简单介绍了普通文件(-)、目录(d)等,在linux文件系统中,主要有以下几种类型的文件。
1)、正规文件(regular file):就是一般类型的文件,当用ls -l查看某个目录时,第一个属性为“-”的文件就是正规文件,或者叫普通文件。正规文件又可分成纯文字文件(ascii)或者二进制文件(binary)。纯文本文件时可以通过cat、more、less等工具直接查看内容的,而二进制文件并不能。例如我们用的命令/bin/ls这就是一个二进制文件。
2)、目录(directory):这个容易理解,就是目录,跟windows下的文件夹一个意思,只不过在linux中我们不叫文件夹,而是叫目录。ls -l查看第一个属性为“d”。
3)、连接档(link):ls -l查看第一个属性为“l”,类似windows下的快捷方式。这种文件在linux中很常见,而且笔者在日常的系统维护运维工作中用的很多。
4)、设备档(device):与系统周边相关的一些档案,通常都集中在/dev这个目录之下。通常又分为两种:
区块(block)设备档:就只一些存储数据,以提供系统存取的接口设备,简单的说就是硬盘啦。例如你的一号硬盘的代码是/dev/hda1等等的档案。第一个属性是“b”。
字符(character)设备档:即是一些串行端口的接口设备,例如键盘、鼠标等等。第一个属性是“c”。
原文:https://www.cnblogs.com/codebuglife/p/12550899.html