Linux文件和目录管理

Linux文件和目录的操作(一) pwd  cd

1、确定当前工作目录:pwd print work directory)

[root@localhost ~]# pwd

/root

 

2、切换工作目录:cd (change directory)

[root@localhost ~]# cd /tmp
[root@localhost tmp]# pwd
/tmp

 

特殊目录名

符号

含义

.

当前工作目录

..

父目录

~

用户主目录

-

上个工作目录

 

Linux文件和目录的操作(二) touch  mkdir

1、创建文件:touch
[user1@localhost ~]$ touch file1

#创建名称为file1的文件 

[user1@localhost ~]$ touch file2 file3

#一次性创建file2file3两个文件

 

2、创建目录:mkdir
[user1@localhost ~]$ mkdir folder1
#
创建名称为folder1的目录

[user1@localhost ~]$ mkdir folder2 folder3

#一次性创建folder2folder3两个文件

[user1@localhost ~]$ mkdir -p folder4/folder41

#创建名称为folder4folder41两个目录,folder41folder4的子目录

#-p选项(parents)表示父目录和子目录一起创建

 

Linux文件和目录的操作(三) ls

1、查看目录及文件:ls list

ls的命令选项

选项

含义

-a

列出所有文件,包括隐藏文件

-l

以详细格式列出(一般使用别名:ll

-d

列出目录信息

-F

列出文件类型

-h

以可读方式显示文件大小

-i

列出i-node节点索引号码

-n

uidgid表示用户名和密码

-r

倒序列出

-R

递归列出

-t

按修改时间排列

[user1@localhost ~]$ ls -a

.  ..  .bash_logout  .bash_profile  .bashrc  .emacs  file1  file2  file3  folder1  folder2  folder3  folder4  .lesshst  .mozilla  .viminfo

[user1@localhost ~]$ ls -l

total 16

-rw-rw-r-- 1 user1 user1    0 Feb  1 22:10 file1

-rw-rw-r-- 1 user1 user1    0 Feb  1 22:26 file2

-rw-rw-r-- 1 user1 user1    0 Feb  1 22:26 file3

drwxrwxr-x 2 user1 user1 4096 Feb  1 22:32 folder1

drwxrwxr-x 2 user1 user1 4096 Feb  1 22:32 folder2

drwxrwxr-x 2 user1 user1 4096 Feb  1 22:32 folder3

drwxrwxr-x 3 user1 user1 4096 Feb  1 22:33 folder4

[user1@localhost ~]$ ll

total 16

-rw-rw-r-- 1 user1 user1    0 Feb  1 22:10 file1

-rw-rw-r-- 1 user1 user1    0 Feb  1 22:26 file2

-rw-rw-r-- 1 user1 user1    0 Feb  1 22:26 file3

drwxrwxr-x 2 user1 user1 4096 Feb  1 22:32 folder1

drwxrwxr-x 2 user1 user1 4096 Feb  1 22:32 folder2

drwxrwxr-x 2 user1 user1 4096 Feb  1 22:32 folder3

drwxrwxr-x 3 user1 user1 4096 Feb  1 22:33 folder4

[user1@localhost ~]$ ls -d

.

[user1@localhost ~]$ ll -d

drwx------ 7 user1 user1 4096 Feb  1 22:33 .

[user1@localhost ~]$ ll -F

total 16

-rw-rw-r-- 1 user1 user1    0 Feb  1 22:10 file1

-rw-rw-r-- 1 user1 user1    0 Feb  1 22:26 file2

-rw-rw-r-- 1 user1 user1    0 Feb  1 22:26 file3

drwxrwxr-x 2 user1 user1 4096 Feb  1 22:32 folder1/

drwxrwxr-x 2 user1 user1 4096 Feb  1 22:32 folder2/

drwxrwxr-x 2 user1 user1 4096 Feb  1 22:32 folder3/

drwxrwxr-x 3 user1 user1 4096 Feb  1 22:33 folder4/

[user1@localhost ~]$ ll -h

total 16K

-rw-rw-r-- 1 user1 user1    0 Feb  1 22:10 file1

-rw-rw-r-- 1 user1 user1    0 Feb  1 22:26 file2

-rw-rw-r-- 1 user1 user1    0 Feb  1 22:26 file3

drwxrwxr-x 2 user1 user1 4.0K Feb  1 22:32 folder1

drwxrwxr-x 2 user1 user1 4.0K Feb  1 22:32 folder2

drwxrwxr-x 2 user1 user1 4.0K Feb  1 22:32 folder3

drwxrwxr-x 3 user1 user1 4.0K Feb  1 22:33 folder4

[user1@localhost ~]$ ll -i

total 16

393255 -rw-rw-r-- 1 user1 user1    0 Feb  1 22:10 file1

393260 -rw-rw-r-- 1 user1 user1    0 Feb  1 22:26 file2

393261 -rw-rw-r-- 1 user1 user1    0 Feb  1 22:26 file3

393256 drwxrwxr-x 2 user1 user1 4096 Feb  1 22:32 folder1

393258 drwxrwxr-x 2 user1 user1 4096 Feb  1 22:32 folder2

393259 drwxrwxr-x 2 user1 user1 4096 Feb  1 22:32 folder3

393262 drwxrwxr-x 3 user1 user1 4096 Feb  1 22:33 folder4

[user1@localhost ~]$ ll -n

total 16

-rw-rw-r-- 1 505 505    0 Feb  1 22:10 file1

-rw-rw-r-- 1 505 505    0 Feb  1 22:26 file2

-rw-rw-r-- 1 505 505    0 Feb  1 22:26 file3

drwxrwxr-x 2 505 505 4096 Feb  1 22:32 folder1

drwxrwxr-x 2 505 505 4096 Feb  1 22:32 folder2

drwxrwxr-x 2 505 505 4096 Feb  1 22:32 folder3

drwxrwxr-x 3 505 505 4096 Feb  1 22:33 folder4

[user1@localhost ~]$ ll -r

total 16

drwxrwxr-x 3 user1 user1 4096 Feb  1 22:33 folder4

drwxrwxr-x 2 user1 user1 4096 Feb  1 22:32 folder3

drwxrwxr-x 2 user1 user1 4096 Feb  1 22:32 folder2

drwxrwxr-x 2 user1 user1 4096 Feb  1 22:32 folder1

-rw-rw-r-- 1 user1 user1    0 Feb  1 22:26 file3

-rw-rw-r-- 1 user1 user1    0 Feb  1 22:26 file2

-rw-rw-r-- 1 user1 user1    0 Feb  1 22:10 file1

[user1@localhost ~]$ ll -R

.:

total 16

-rw-rw-r-- 1 user1 user1    0 Feb  1 22:10 file1

-rw-rw-r-- 1 user1 user1    0 Feb  1 22:26 file2

-rw-rw-r-- 1 user1 user1    0 Feb  1 22:26 file3

drwxrwxr-x 2 user1 user1 4096 Feb  1 22:32 folder1

drwxrwxr-x 2 user1 user1 4096 Feb  1 22:32 folder2

drwxrwxr-x 2 user1 user1 4096 Feb  1 22:32 folder3

drwxrwxr-x 3 user1 user1 4096 Feb  1 22:33 folder4

 

./folder1:

total 0

 

./folder2:

total 0

 

./folder3:

total 0

 

./folder4:

total 4

drwxrwxr-x 2 user1 user1 4096 Feb  1 22:33 folder41

 

./folder4/folder41:

total 0

[user1@localhost ~]$ ll -t

total 16

drwxrwxr-x 3 user1 user1 4096 Feb  1 22:33 folder4

drwxrwxr-x 2 user1 user1 4096 Feb  1 22:32 folder1

drwxrwxr-x 2 user1 user1 4096 Feb  1 22:32 folder2

drwxrwxr-x 2 user1 user1 4096 Feb  1 22:32 folder3

-rw-rw-r-- 1 user1 user1    0 Feb  1 22:26 file2

-rw-rw-r-- 1 user1 user1    0 Feb  1 22:26 file3

-rw-rw-r-- 1 user1 user1    0 Feb  1 22:10 file1

 

Linux文件和目录的操作(四) cp  mv

1、复制目录和文件:cp copy
[user1@localhost ~]$ ll

total 16

-rw-rw-r-- 1 user1 user1    0 Feb  1 22:10 file1

-rw-rw-r-- 1 user1 user1    0 Feb  1 22:26 file2

-rw-rw-r-- 1 user1 user1    0 Feb  1 22:26 file3

drwxrwxr-x 2 user1 user1 4096 Feb  1 22:32 folder1

drwxrwxr-x 2 user1 user1 4096 Feb  1 22:32 folder2

drwxrwxr-x 2 user1 user1 4096 Feb  1 22:32 folder3

drwxrwxr-x 3 user1 user1 4096 Feb  1 22:33 folder4

[user1@localhost ~]$ cp file1 file4

#复制文件

 [user1@localhost ~]$ cp -r folder4 folder5

#复制目录需要加上-r选项,递归复制目录及其目录下面的文件

[user1@localhost ~]$ ll

total 20

-rw-rw-r-- 1 user1 user1    0 Feb  1 22:10 file1

-rw-rw-r-- 1 user1 user1    0 Feb  1 22:26 file2

-rw-rw-r-- 1 user1 user1    0 Feb  1 22:26 file3

-rw-rw-r-- 1 user1 user1    0 Feb  2 00:44 file4

drwxrwxr-x 2 user1 user1 4096 Feb  1 22:32 folder1

drwxrwxr-x 2 user1 user1 4096 Feb  1 22:32 folder2

drwxrwxr-x 2 user1 user1 4096 Feb  1 22:32 folder3

drwxrwxr-x 3 user1 user1 4096 Feb  1 22:33 folder4

drwxrwxr-x 3 user1 user1 4096 Feb  2 00:45 folder5

[user1@localhost ~]$ cp file1 folder1

#复制文件到目录

[user1@localhost ~]$ ll folder1

total 0

-rw-rw-r-- 1 user1 user1 0 Feb  2 00:54 file1

 

2、移动和重命名目录和文件:mv move
[user1@localhost ~]$ ll

total 20

-rw-rw-r-- 1 user1 user1    0 Feb  1 22:10 file1

-rw-rw-r-- 1 user1 user1    0 Feb  1 22:26 file2

-rw-rw-r-- 1 user1 user1    0 Feb  1 22:26 file3

-rw-rw-r-- 1 user1 user1    0 Feb  2 00:44 file4

drwxrwxr-x 2 user1 user1 4096 Feb  2 00:54 folder1

drwxrwxr-x 2 user1 user1 4096 Feb  1 22:32 folder2

drwxrwxr-x 2 user1 user1 4096 Feb  1 22:32 folder3

drwxrwxr-x 3 user1 user1 4096 Feb  1 22:33 folder4

drwxrwxr-x 3 user1 user1 4096 Feb  2 00:45 folder5

[user1@localhost ~]$ mv file1 file5

#文件file1重命名为文件file5

[user1@localhost ~]$ mv folder1 folder6

#目录folder1重命名为目录folder6

 [user1@localhost ~]$ mv file2 folder2

#文件file2移动到目录folder2

[user1@localhost ~]$ ll

total 20

-rw-rw-r-- 1 user1 user1    0 Feb  1 22:26 file3

-rw-rw-r-- 1 user1 user1    0 Feb  2 00:44 file4

-rw-rw-r-- 1 user1 user1    0 Feb  1 22:10 file5

drwxrwxr-x 2 user1 user1 4096 Feb  2 01:06 folder2

drwxrwxr-x 2 user1 user1 4096 Feb  1 22:32 folder3

drwxrwxr-x 3 user1 user1 4096 Feb  1 22:33 folder4

drwxrwxr-x 3 user1 user1 4096 Feb  2 00:45 folder5

drwxrwxr-x 2 user1 user1 4096 Feb  2 00:54 folder6

[user1@localhost ~]$ ll folder2/

total 0

-rw-rw-r-- 1 user1 user1 0 Feb  1 22:26 file2

 

Linux文件和目录的操作(五) rm  rmdir

1、删除目录和文件:rm remove
[user1@localhost ~]$ ll

total 20

-rw-rw-r-- 1 user1 user1    0 Feb  1 22:26 file3

-rw-rw-r-- 1 user1 user1    0 Feb  2 00:44 file4

-rw-rw-r-- 1 user1 user1    0 Feb  1 22:10 file5

drwxrwxr-x 2 user1 user1 4096 Feb  2 01:06 folder2

drwxrwxr-x 2 user1 user1 4096 Feb  1 22:32 folder3

drwxrwxr-x 3 user1 user1 4096 Feb  1 22:33 folder4

drwxrwxr-x 3 user1 user1 4096 Feb  2 00:45 folder5

drwxrwxr-x 2 user1 user1 4096 Feb  2 00:54 folder6

[user1@localhost ~]$ rm file3

#删除文件file3

[user1@localhost ~]$ rm -r folder2

#删除目录需要加上-r选项,递归删除目录及其目录下面的文件

[user1@localhost ~]$ rm file4 file5

#一次删除file4file5两个文件

[user1@localhost ~]$ rm -r folder3 folder4

#一次删除folder3folder4两个目录

[user1@localhost ~]$ ll

total 8

drwxrwxr-x 3 user1 user1 4096 Feb  2 00:45 folder5

drwxrwxr-x 2 user1 user1 4096 Feb  2 00:54 folder6

 

2、删除空目录:rmdir remove directory
[user1@localhost ~]$ rmdir folder5

rmdir: folder5: Directory not empty
#非空目录使用mkdir无法删除

[user1@localhost ~]$ rm -r folder5/folder41/

#删除目录下面的文件

[user1@localhost ~]$ rmdir folder5

#成为空文件夹后可以使用rmdir删除

 

Linux文件和目录的操作(六) 文件类型 file

windows不同,linux不通过扩展名来确定文件类型,可通过文件名颜色或者文件属性来查看文件类型。

文件名颜色,属性对应位与类型对应表:

文件名颜色

文件属性对应位

文件类型

黑色

-

普通文件

蓝色

d

目录

浅蓝色

l

链接

绿色

-

脚本

红色

-

压缩文件

黄色(黑底)

c

字符设备文件

黄色(黑底)

b

块设备文件

浅黄色(黑底)

p

管道文件

粉色

s

套接字文件

clip_image002

#其中f1 f1_s hello man.ls.gz ogg这几个文件为自己建立的。

[root@localhost ~]# ll f1 f1_s hello man.ls.gz -d ogg

-rw-r--r-- 1 root root    4 Dec 12 14:19 f1

lrwxrwxrwx 1 root root    2 Dec 12 14:14 f1_s -> f1

-rwxr-xr-x 1 root root 4725 Dec 18 00:14 hello

-rw-r--r-- 1 root root 3589 Nov 29 19:59 man.ls.gz

drwxr-xr-x 2 root root 4096 Jan 22 01:18 ogg

[root@localhost ~]# cd /dev

[root@localhost dev]# ll zero hdc initctl gpmctl

srwxrwxrwx 1 root root     0 Feb  7 21:58 gpmctl

brw-rw---- 1 root disk 22, 0 Feb  7 21:57 hdc

prw------- 1 root root     0 Feb  7 21:58 initctl

crw-rw-rw- 1 root root  1, 5 Feb  7 21:57 zero

 

使用file命令查看文件类型

[root@localhost ~]# file f1 f1_s hello man.ls.gz ogg

f1:        ASCII text

f1_s:      symbolic link to `f1'

hello:     ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped

man.ls.gz: gzip compressed data, was "man.ls", from Unix, last modified: Thu Nov 29 19:59:36 2012

ogg:       directory

[root@localhost ~]# cd /dev

[root@localhost dev]# file gpmctl hdc initctl zero

gpmctl:  socket

hdc:     block special (22/0)

initctl: fifo (named pipe)

zero:    character special (1/5)

 

Linux文件和目录的操作(七) 目录树 tree

Linux的目录结构呈树状,使用tree命令可以直观显示出当前目录结构。

[stone@localhost ~]$ tree file1/

file1/

|-- f1

`-- f2

 

0 directories, 2 files

#file1目录下面有2个文件,没有目录。

 

Linux文件和目录的操作(八) 权限 chmod

Linux中的每一个文件(包括目录)都有r(读)、w(写)、x(执行)三种权限,对于文件和目录,这三种权限的意义不一样,具体区别如下表:

 

文件

目录

r

读取文件内容

只能读取目录中文件的文件名

w

修改文件内容

修改目录中的文件(添加,删除文件等)

x

执行文件(针对可执行文件)

进入目录

文件和目录的所有者可能是用户(user)、组(group)或者其他人(others),Linux针对不同的所有者可以设置不同的权限,故每一个文件或者目录都有三组权限,分别为用户权限,组权限和其他人权限。

[stone@localhost ~]$ ll

drwxrwxr-x 2 stone stone 4096 Feb 16 14:55 file1

如上,其中红色一组表示用户权限,蓝色一组表示组权限,黑色一组表示其他人权限。

权限可以采用三组八进制来表示,每一组内的字母对应的数值如下:

权限

数值

r

4

w

2

x

1

-(表示无权限)

0

故字母表示的权限就可以换算成数字表示的权限了,如rwxrwxr-x,就可以换算成7754+2+1 4+2+1 4+1)。

改变文件和目录的权限chmodchange mode

[stone@localhost ~]$ chmod 400 file1/

[stone@localhost ~]$ ll

dr-------- 2 stone stone 4096 Feb 16 14:55 file1

文件的权限比较好理解,目录的权限比较难理解,下面只针对目录权限举例:

[stone@localhost ~]$ mkdir file1

[stone@localhost ~]$ cd file1

[stone@localhost file1]$ touch f1 f2

[stone@localhost ~]$ tree file1/

file1/

|-- f1

`-- f2

 

0 directories, 2 files

[stone@localhost ~]$ ll -a file1/

total 8

drwxr-xr-x 2 stone stone 4096 Feb 16 15:57 .

drwx------ 4 stone stone 4096 Feb 16 14:55 ..

-rw-rw-r-- 1 stone stone    0 Feb 16 15:57 f1

-rw-rw-r-- 1 stone stone    0 Feb 16 14:55 f2

#新建一个目录file1,在file1下新建两个文件f1f2

[stone@localhost ~]$ chmod 400 file1/

[stone@localhost ~]$ ll -d file1/

dr-------- 2 stone stone 4096 Feb 16 15:57 file1/

[stone@localhost ~]$ ll file1/

total 0

?--------- ? ? ? ?            ? f1

?--------- ? ? ? ?            ? f2

[stone@localhost ~]$ rm file1/f1

rm: cannot remove `file1/f1': Permission denied

[stone@localhost ~]$ cd file1/

-bash: cd: file1/: Permission denied

#仅有r权限,只能查看目录内的文件,不能删除目录内的文件,不可以进入目录。

[stone@localhost ~]$ chmod 200 file1/

[stone@localhost ~]$ ll -d file1/

d-w------- 2 stone stone 4096 Feb 16 16:14 file1/

[stone@localhost ~]$ ll file1/

ls: file1/: Permission denied

[stone@localhost ~]$ rm file1/f2

rm: cannot remove `file1/f2': Permission denied

[stone@localhost ~]$ cd file1/

-bash: cd: file1/: Permission denied

#只有w权限,不能查看目录内的文件,不能删除目录内的文件,不可以进入目录。

[stone@localhost ~]$ chmod 100 file1

[stone@localhost ~]$ ll -d file1/

d--x------ 2 stone stone 4096 Feb 16 15:57 file1/

[stone@localhost ~]$ ll file1/

ls: file1/: Permission denied

[stone@localhost ~]$ rm file1/f1

rm: cannot remove `file1/f1': Permission denied

[stone@localhost ~]$ cd file1/

[stone@localhost file1]$

#仅有x权限,不能查看目录内的文件,不能删除目录内的文件,但可以进入目录。

[stone@localhost ~]$ chmod 300 file1/

[stone@localhost ~]$ ll -d file1/

d-wx------ 2 stone stone 4096 Feb 16 15:57 file1/

[stone@localhost ~]$ ll file1/

ls: file1/: Permission denied

[stone@localhost ~]$ rm file1/f1

[stone@localhost ~]$ cd file1/

[stone@localhost file1]$

#wx权限,不能查看目录内的文件,可以删除目录内的文件,可以进入目录。

[stone@localhost ~]$ chmod 500 file1/

[stone@localhost ~]$ ll -d file1/

dr-x------ 2 stone stone 4096 Feb 16 16:14 file1/

[stone@localhost ~]$ ll file1/

total 0

-rw-rw-r-- 1 stone stone 0 Feb 16 14:55 f2

[stone@localhost ~]$ rm file1/f2

rm: cannot remove `file1/f2': Permission denied

[stone@localhost ~]$ cd file1/

[stone@localhost file1]$

#rx权限,可以查看目录内的文件,不能删除目录内的文件,可以进入目录。

[stone@localhost ~]$ chmod 600 file1/

[stone@localhost ~]$ ll -d file1/

drw------- 2 stone stone 4096 Feb 16 16:14 file1/

[stone@localhost ~]$ ll file1/

total 0

?--------- ? ? ? ?            ? f2

[stone@localhost ~]$ rm file1/f2

rm: cannot remove `file1/f2': Permission denied

[stone@localhost ~]$ cd file1/

-bash: cd: file1/: Permission denied

#rw权限,可以查看目录内的文件,不能删除目录内的文件,不可以进入目录。

综上,对于目录,w一定要配合x使用,单独使用没有意义。

 

R

W

X

查看文件

 

 

删除文件

 

进入目录

 

 

 

Linux文件和目录的操作(九) 控制默认权限 umask

Linux默认的文件权限为666,目录权限为777。可通过umask(掩码)设置不允许的权限。具体如下表:

 

文件

目录

默认权限

666

777

umask

0002

0002

最终权限

664

775

[stone@localhost ~]$ umask

0002

[stone@localhost ~]$ mkdir file2

[stone@localhost ~]$ touch f2

[stone@localhost ~]$ ll f2 -d file2

-rw-rw-r-- 1 stone stone    0 Feb 16 17:44 f2

drwxrwxr-x 2 stone stone 4096 Feb 16 17:44 file2

[stone@localhost ~]$ rm -r f2 file2

[stone@localhost ~]$ umask 0022

#设置umask

[stone@localhost ~]$ mkdir file2

[stone@localhost ~]$ touch f2

[stone@localhost ~]$ ll f2 -d file2

-rw-r--r-- 1 stone stone    0 Feb 16 17:55 f2

drwxr-xr-x 2 stone stone 4096 Feb 16 17:55 file2

 

Linux文件和目录的操作(十) 查看属性 stat

windows下可以通过右键点击文件或者文件夹选择属性来查看相关信息,那么在linux下就要使用stat命令来查看文件或者目录的属性(当然也可以使用ls命令加上相关的命令行选项列出属性)。

[root@localhost ~]# stat file1

  File: `file1'

  Size: 4096            Blocks: 8          IO Block: 4096   directory

Device: 802h/2050d      Inode: 2584587     Links: 2

Access: (0400/dr--------)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2013-02-17 17:31:19.000000000 +0800

Modify: 2013-02-16 14:41:09.000000000 +0800

Change: 2013-02-16 14:41:09.000000000 +0800

[root@localhost ~]# stat file1/f1

  File: `file1/f1'

  Size: 0               Blocks: 0          IO Block: 4096   regular empty file

Device: 802h/2050d      Inode: 2585358     Links: 1

Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2013-02-16 14:38:19.000000000 +0800

Modify: 2013-02-16 14:38:19.000000000 +0800

Change: 2013-02-16 14:38:19.000000000 +0800

 

Linux文件和目录的操作(十一) 索引节点 inode

通过stat命令可以查看文件的属性(元信息),那么这些属性保存在哪儿呢,嘿嘿,就是下面要讲到的inode了。每一个文件(包括目录)对应一个inode,通过stat命令可以看到inode包含的内容如下:

[root@localhost ~]# echo aaa > file1/f1

[root@localhost ~]# stat file1/f1

  File: `file1/f1'

  Size: 4               Blocks: 8          IO Block: 4096   regular file

Device: 802h/2050d      Inode: 2585358     Links: 1

Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2013-02-16 14:38:19.000000000 +0800

Modify: 2013-02-18 17:39:17.000000000 +0800

Change: 2013-02-18 17:39:17.000000000 +0800

名称

含义

Size

文件大小

Inide

Inode编号

Links

文件链接数

Access: (0644/-rw-r--r--)

文件权限

Uid

用户id

Gid

id

Access: 2013-02-16 14:38:19.000000000 +0800

访问时间

Modify: 2013-02-18 17:39:17.000000000 +0800

修改时间

Change: 2013-02-18 17:39:17.000000000 +0800

改变时间

注意:inode包含的信息中没有文件名。

  

Linux文件和目录的操作(十二) 三个时间 atime mtime ctime

Inode中有三个时间:atimemtimectime,分别为访问时间(access time),修改时间(modify time),改变时间(change time)。

1、查看三个时间

通过stat命令可以查看这三个时间,还可以通过ls命令来查看。

[root@localhost ~]# ll -u file1/f1

-rw-r--r-- 1 root root 4 Feb 19 17:07 file1/f1

#查看atime

[root@localhost ~]# ll file1/f1

-rw-r--r-- 1 root root 4 Feb 19 17:07 file1/f1

#查看mtime,故ll命令默认查看的是mtime

[root@localhost ~]# ll -c file1/f1

-rw-r--r-- 1 root root 4 Feb 19 17:07 file1/f1

#查看ctime

2、文件的三个时间的含义

名称

含义

atime

最近一次访问文件的时间

mtime

最近一次修改文件内容的时间

ctime

最近一次改变文件属性的时间

3、目录的三个时间的含义

名称

含义

atime

最近一次访问目录的时间

mtime

最近一次修改目录内容的时间

ctime

最近一次改变目录属性的时间

 

Linux文件和目录的操作(十三) 硬软链接 lnlink

学习了inode,硬链接和软链接就好理解了。

1、硬链接

特点:

-只能用于文件,不能用于目录,适用于不同用户对同一文件的访问。

-创建硬链接后文件链接数都增加1

-两个文件名对应一个i-node节点和文件内容。

-删除一个文件名只是脱链一个文件,另一个还有效。

[root@localhost ~]# ll file1/

total 4

-r--r--r-- 1 root root 5 Feb 20 10:59 f1

-rw-r--r-- 1 root root 0 Feb 19 17:51 f2

[root@localhost ~]# cd file1/

[root@localhost file1]# ln f1 f1_h

#创建f1的硬链接f1_h

[root@localhost file1]# ll

total 8

-r--r--r-- 2 root root 5 Feb 20 10:59 f1

-r--r--r-- 2 root root 5 Feb 20 10:59 f1_h

-rw-r--r-- 1 root root 0 Feb 19 17:51 f2

#f1f1_h的链接数增加1

[root@localhost file1]# ll -i

total 8

2585429 -r--r--r-- 2 root root 5 Feb 20 10:59 f1

2585429 -r--r--r-- 2 root root 5 Feb 20 10:59 f1_h

2585359 -rw-r--r-- 1 root root 0 Feb 19 17:51 f2

#f1f1_hinode编号一样

 

2、软链接

特点:

-可以用于文件和目录,相当于windows中的快捷方式。

-创建软链接后文件链接数保持不变

-两个文件名对应2i-node节点和文件内容。

-删除原始文件会导致空链接。

[root@localhost file1]# ln -s f2 f2_s

#创建f2软链接f2_s

[root@localhost file1]# ll

total 8

-r--r--r-- 2 root root 5 Feb 20 10:59 f1

-r--r--r-- 2 root root 5 Feb 20 10:59 f1_h

-rw-r--r-- 1 root root 0 Feb 19 17:51 f2

lrwxrwxrwx 1 root root 2 Feb 20 17:44 f2_s -> f2

#f2f2_s的链接数都为1

[root@localhost file1]# ll -i

total 8

2585429 -r--r--r-- 2 root root 5 Feb 20 10:59 f1

2585429 -r--r--r-- 2 root root 5 Feb 20 10:59 f1_h

2585359 -rw-r--r-- 1 root root 0 Feb 19 17:51 f2

2585608 lrwxrwxrwx 1 root root 2 Feb 20 17:44 f2_s -> f2

#f2f2_s的两个inode节点编号不同

请使用浏览器的分享功能分享到微信等