linux文件与目录管理

基础知识

文件都有文件名数据,数据在Linux上被分成两个部分:用户数据(user data)与元数据(metadata)。用户数据,即文件数据块(data block),数据块是记录文件真实内容的地方;而元数据则是文件的附加属性,如文件大小、创建时间、所有者等信息。在Linux中,元数据中的inode号(inode 是文件元数据的一部分但其并不包含文件名,inode号即索引节点号)才是文件的唯一标识而非文件名。文件名仅是为了方便人们的记忆和使用,系统或程序通过文件名索引到inode号再去寻找正确的文件数据块。

为解决文件的共享使用,Linux系统引入了两种链接:硬链接(hard link)与软链接(又称符号链接,即soft link或symbolic link)。链接为Linux 系统解决了文件的共享使用,还带来了隐藏文件路径、增加权限安全及节省存储等好处。

  • 若一个inode号对应多个文件名,则称这些文件为硬链接。换言之,硬链接就是同一个文件使用了多个别名。创建一个硬链接之后,inode不变,但是硬连接总数(也称为引用计数)有增加,当删除文件时,只有硬连接总数为0时,系统才会真正删除文件。

  • 软链接与硬链接不同,若文件用户数据块中存放的内容是另一文件的路径名的指向,则该文件就是软连接。软链接就是一个普通文件,只是数据块内容有点特殊。软链接有着自己的inode号以及用户数据块,即有新文件生成,这个新文件(如上symbol.sh)指向旧文件(if.sh)。这个有点像Windows中的快捷方式。快捷方式指向的文件被删除了,则快捷方式无效;如果把快捷方式删除了,也不影响原来的文件。

(以上关于软、硬链接的说明摘抄自IBM文档《理解 Linux 的硬链接与软链接》

ln命令说明

ln - make links between files
常用格式:ln [OPTION] TARGET LINK_NAME

  • ln target link_name等同于cp -l target link_name

  • ln -s target link_name等同于cp -s target link_name

  • -f,覆盖已有的link。

TIPs:

  • cp 命令也有创建链接的功能。使用-s表示创建符号链接,-l表示创建硬链接。

  • 注意:windows默认文件系统不支持软链接,如果将含软链接的压缩包在windows文件系统中,软链接将失效。

sunnogo@a3e420:~/test/script$ ln if.sh lnlink.sh
sunnogo@a3e420:~/test/script$ ls -li
total 16
7340612 -rwxrwxr-x 3 sunnogo sunnogo  94  6月 21  2013 if.sh
7340612 -rwxrwxr-x 3 sunnogo sunnogo  94  6月 21  2013 link.sh
7340612 -rwxrwxr-x 3 sunnogo sunnogo  94  6月 21  2013 lnlink.sh
7340899 -rw-rw-r-- 1 sunnogo sunnogo 519 11月 12 00:33 prof.c
7340881 lrwxrwxrwx 1 sunnogo sunnogo   5  2月 23 21:16 symbol.sh -> if.sh
sunnogo@a3e420:~/test/script$ ln -s if.sh lnsymbol.sh
sunnogo@a3e420:~/test/script$ ls -li
total 16
7340612 -rwxrwxr-x 3 sunnogo sunnogo  94  6月 21  2013 if.sh
7340612 -rwxrwxr-x 3 sunnogo sunnogo  94  6月 21  2013 link.sh
7340612 -rwxrwxr-x 3 sunnogo sunnogo  94  6月 21  2013 lnlink.sh
7340895 lrwxrwxrwx 1 sunnogo sunnogo   5  2月 23 21:26 lnsymbol.sh -> if.sh
7340899 -rw-rw-r-- 1 sunnogo sunnogo 519 11月 12 00:33 prof.c
7340881 lrwxrwxrwx 1 sunnogo sunnogo   5  2月 23 21:16 symbol.sh -> if.sh

touch

touch - change file timestamps
touch常用于Linux命令行创建空文件,而从man touch的结果看来,touch主要用于修改时间戳,创建文件只是兼职工作。
常用方式:

  • touch file,如果当前目录内无该文件,则以当前时间创建空文件;如果当前目录内有该文件,则以当前时间修改文件时间戳(访问时间和修改时间);

  • touch -t YearMonthDateHourMinute file,指定时间修改文件时间戳,时间方式如201402232200。

  • touch -a file,只修改访问时间(access timestamp)

  • touch -m file,只修改修改时间(modify timestamp)

sunnogo@a3e420:~/test/touch$ 
sunnogo@a3e420:~/test/touch$ touch first
sunnogo@a3e420:~/test/touch$ ls -l first 
-rw-r--r-- 1 sunnogo sunnogo 0  2月 23 21:56 first
sunnogo@a3e420:~/test/touch$ ls -lu first 
-rw-r--r-- 1 sunnogo sunnogo 0  2月 23 21:56 first
sunnogo@a3e420:~/test/touch$ 
sunnogo@a3e420:~/test/touch$ touch -at 201110101200 first 
sunnogo@a3e420:~/test/touch$ ls -l first 
-rw-r--r-- 1 sunnogo sunnogo 0  2月 23 21:56 first  <----修改时间戳不变
sunnogo@a3e420:~/test/touch$ ls -lu first 
-rw-r--r-- 1 sunnogo sunnogo 0 10月 10  2011 first  <----访问时间戳改变
sunnogo@a3e420:~/test/touch$ 
sunnogo@a3e420:~/test/touch$ touch -mt 201110101200 first 
sunnogo@a3e420:~/test/touch$ ls -l first 
-rw-r--r-- 1 sunnogo sunnogo 0 10月 10  2012 first  <----修改时间戳改变
sunnogo@a3e420:~/test/touch$ ls -lu first 
-rw-r--r-- 1 sunnogo sunnogo 0 10月 10  2011 first

vi 编辑保存文件

sunyongfeng@openswitch-OptiPlex-380:~/Documents$ vi test.txt

... 编辑文件
... wq 保存、退出编辑

sunyongfeng@openswitch-OptiPlex-380:~/Documents$ ls
test.txt

利用 cat 与 IO 重定向快速创建、编辑文件

命令:cat > filename << "EOF" 样例:

sunyongfeng@openswitch-OptiPlex-380:~/Documents$ cat > test.txt << "EOF"
> 这是一个使用 cat 与 IO 重定向的样例
> 快速创建文件 test.txt
> 并快速编辑
> EOF
sunyongfeng@openswitch-OptiPlex-380:~/Documents$ cat test.txt 
这是一个使用 cat 与 IO 重定向的样例
快速创建文件 test.txt
并快速编辑
sunyongfeng@openswitch-OptiPlex-380:~/Documents$ 

原理

The sole purpose of cat is (according to the man page): Quote:

cat - concatenate files and print on the standard output

So, as an experiment, try running “cat” alone on the command line. You'll see that it echoes everything you type as soon as you press enter. You repeat the process until you press Control-D (which sends an “end-of-file” character).

Just like any other command, if you use output redirection (the '>'), your shell will take the standard output of the command and write it to a file instead of displaying it on a screen. So, it's the shell (e.g. bash, ksh, tcsh, etc.) that creates the file–not the cat command.

Similarly, the input redirection basically says “pretend that the text after '«' indicates the end of the input”.

Putting them together, the cat command is behaving exactly as though it were given no arguments. Your shell is the one moving data around. As you type, the shell is looking for “EOF” to signal the end of the input. When that text is seen, it sends cat an end-of-file signal so that cat stops processing things. Also, cat is echoing everything you type as before, except that as it's echo'd, your shell is grabbing that text and writing it to a file–so you never see it displayed.

利用 echo 与 IO 重定向

同样地,可以使用 echo "string" > filename 快速创建文件。

mkdir - make directories
常用参数:

  • -p--parents,如果要创建的目录存在,也不会提示error,如果父目录不存在,则一并创建父目录。

样例:mkdir -p second_dir/21dir

sunnogo@a3e420:~/test/mkdir$ ls 
sunnogo@a3e420:~/test/mkdir$ 
sunnogo@a3e420:~/test/mkdir$ mkdir first_dir
sunnogo@a3e420:~/test/mkdir$ ls -al
total 12
drwxr-xr-x  3 sunnogo sunnogo 4096  2月 23 22:09 .
drwxrwxr-x 12 sunnogo sunnogo 4096  2月 23 22:09 ..
drwxr-xr-x  2 sunnogo sunnogo 4096  2月 23 22:09 first_dir
sunnogo@a3e420:~/test/mkdir$ 
sunnogo@a3e420:~/test/mkdir$ mkdir second_dir/21dir
mkdir: cannot create directory ‘second_dir/21dir’: No such file or directory
<----创建多级目录如果不加-p选项且有父目录不存在,就会提示错误
sunnogo@a3e420:~/test/mkdir$ 
sunnogo@a3e420:~/test/mkdir$ mkdir -p second_dir/21dir
sunnogo@a3e420:~/test/mkdir$ ls -l
total 8
drwxr-xr-x 2 sunnogo sunnogo 4096  2月 23 22:09 first_dir
drwxr-xr-x 3 sunnogo sunnogo 4096  2月 23 22:10 second_dir
sunnogo@a3e420:~/test/mkdir$ ls -l *
first_dir:
total 0

second_dir:
total 4
drwxr-xr-x 2 sunnogo sunnogo 4096  2月 23 22:10 21dir

文件与目录的删除命令:

  • rm,remove files or directories

  • rmdir

由于 rmdir 只能删除空目录,因此通常使用 rm 命令做文件与目录删除。

命令语法:rm [option] file
常见参数:

  • -f--force,不提示直接删除。有风险,须谨慎。

  • -i,删除每个文件前,先提示一把。如果批量删除多个文件的话,略显麻烦。

  • -I,避免删除多个文件时,像-i那么麻烦。如果删除三个或以上文件,只会提醒一次。

  • -r,删除目录以及递归删除目录下的文件和子目录。

本人较喜欢使用rm -rf dir的形式删除文件,也曾因此丢失重要数据,不过吃一堑长一智,也因此学会即时备份重要数据。

数据无价,强制删除需谨慎。请做好数据备份!

样例:

sunnogo@a3e420:~/test/rm$ ls
sunnogo@a3e420:~/test/rm$ touch a
sunnogo@a3e420:~/test/rm$ rm a
<----普通删除不提示,直接删除,对比下面的-i删除
sunnogo@a3e420:~/test/rm$ ls
sunnogo@a3e420:~/test/rm$ touch b
sunnogo@a3e420:~/test/rm$ rm -i b
rm: remove regular empty file ‘b’? y

sunnogo@a3e420:~/test/rm$ ls
sunnogo@a3e420:~/test/rm$ mkdir -p dir1/dir1_1/dir1_1_1
sunnogo@a3e420:~/test/rm$ touch dir1/aa dir1/bb dir1/cc dir1/dd
sunnogo@a3e420:~/test/rm$ rm -ri dir1/
rm: descend into directory ‘dir1/’? y
rm: remove regular empty file ‘dir1/dd’? y
rm: remove regular empty file ‘dir1/bb’? y
rm: remove regular empty file ‘dir1/cc’? y
rm: descend into directory ‘dir1/dir1_1’? y
rm: remove directory ‘dir1/dir1_1/dir1_1_1’? y
rm: remove directory ‘dir1/dir1_1’? y
rm: remove regular empty file ‘dir1/aa’? y
rm: remove directory ‘dir1/’? y
<----批量删除的-i选项太麻烦了。

sunnogo@a3e420:~/test/rm$ 
sunnogo@a3e420:~/test/rm$ mkdir -p dir1/dir1_1/dir1_1_1
sunnogo@a3e420:~/test/rm$ touch dir1/aa dir1/bb dir1/cc dir1/dd
sunnogo@a3e420:~/test/rm$ rm -rI dir1/
rm: remove all arguments recursively? y
<----大写的-I选项轻松多了

sunnogo@a3e420:~/test/rm$ 
sunnogo@a3e42
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值