linux档案和目录的管理

资料来自鸟哥的linux私房菜,记录下来供自己平常使用参考

一:目录和路径:

cd:change direcoty,变换目录的意思,就是从一个目录变到另一个目录,然后可以用绝对路径去变换目录,也可以用相对路径去变换目录,~ 这个符号表示回到自己的家目录,- 表示回到刚才的目录

[dmtsai@study ~]$ su -   #先切换身份成为root看看!
[root@study ~]# cd [相对路径或绝对路径] 
#最重要的就是目录的绝对路径与相对路径,还有一些特殊目录的符号啰!
[root@study ~]# cd ~dmtsai 
#代表去到dmtsai这个使用者的家目录,亦即/home/dmtsai 
[root@study dmtsai]# cd ~ 
#表示回到自己的家目录,亦即是/ root这个目录 
[root@study ~]# cd 
#没有加上任何路径,也还是代表回到自己家目录的意思喔!
[root@study ~]# cd .. 
#表示去到目前的上层目录,亦即是/root的上层目录的意思; 
[root@study /]# cd - 
#表示回到刚刚的那个目录,也就是/root啰~ 
[root@study ~]# cd /var/spool/mail 
#这个就是绝对路径的写法!直接指定要去的完整路径名称!
[root@study mail]# cd ../postfix
# 这个是相对路径的写法,我们由/var/spool/mail 去到/var/spool/postfix 就这样写!

pwd:显示目前所在的目录

[root@study ~]# pwd [-P] 
选项与参数:
-P :显示出确实的路径,而非使用连结(link) 路径。

范例:单纯显示出目前的工作目录: 
[root@study ~]# pwd 
/root    <==显示出目录啦~

范例:显示出实际的工作目录,而非连结档本身的目录名而已 
[root@study ~]# cd /var/mail    <==注意,/var/mail是一个连结档 
[root@study mail]# pwd 
/var/mail          <==列出目前的工作目录 
[root@study mail]# pwd -P 
/var/spool/mail    <==怎么回事?有没有加-P差很多~ 
[root@study mail]# ls -ld /var/mail 
lrwxrwxrwx. 1 root root 10 May 4 17:51 /var/mail -> spool/mail
 #看到这里应该知道为啥了吧?因为/var/mail是连结档,连结到/var/spool/mail
# 所以,加上pwd -P 的选项后,会不以连结档的资料显示,而是显示正确的完整路径啊!

mkdir:创建一个目录

[root@study ~]# mkdir [-mp]目录名称
选项与参数:
-m :设定档案的权限喔!直接设定,不需要看预设权限(umask) 的脸色~
-p :帮助你直接将所需要的目录(包含上层目录)递回建立起来!

范例:请到/tmp底下尝试建立数个新目录看看: 
[root@study ~]# cd /tmp 
[root@study tmp]# mkdir test     <==建立一名为test的新目录 
[root@study tmp]# mkdir test1/test2/test3/test4
mkdir: cannot create directory 'test1/test2/test3/test4': No such file or directory
#话说,系统告诉我们,没可能建立这个目录啊!就是没有目录才要建立的!见鬼嘛?
[root@study tmp]# mkdir -p test1/test2/test3/test4 
#原来是要建test4上层没先建test3之故!加了这个-p的选项,可以自行帮你建立多层目录!

范例:建立权限为rwx--x--x的目录 
[root@study tmp]# mkdir -m 711 test2 
[root@study tmp]# ls -ld test*
drwxr-xr-x. 2 root root 6 Jun 4 19:03 test
drwxr-xr-x. 3 root root 18 Jun 4 19:04 test1
drwx--x--x . 2 root root 6 Jun 4 19:05 test2
 #仔细看上面的权限部分,如果没有加上-m来强制设定属性,系统会使用预设属性。
#那么你的预设属性为何?这要透过底下介绍的umask才能了解喔!^_^

rmdir:删除一个目录,不过只能直接删除空的目录

[root@study ~]# rmdir [-p]目录名称
选项与参数:
-p :连同『上层』『空的』目录也一起删除

范例:将于mkdir范例中建立的目录(/tmp底下)删除掉!
[root@study tmp]# ls -ld test*    <==看看有多少目录存在?
drwxr-xr-x. 2 root root 6 Jun 4 19:03 test
drwxr-xr-x. 3 root root 18 Jun 4 19:04 test1
drwx--x--x. 2 root root 6 Jun 4 19:05 test2
[root@study tmp]# rmdir test   <==可直接删除掉,没问题 
[root@study tmp]# rmdir test1   <==因为尚有内容,所以无法删除!
rmdir: failed to remove 'test1': Directory not empty
[root@study tmp]# rmdir -p test1/test2/test3/test4 
[root@study tmp]# ls -ld test*     <==您看看,底下的输出中test与test1不见了!
drwx--x--x. 2 root root 6 Jun 4 19:05 test2
# 瞧!利用-p 这个选项,立刻就可以将test1/test2/test3/test4 一次删除~
# 不过要注意的是,这个rmdir 仅能『删除空的目录』喔!

$PATH:环境变量,用于执行档案的路径配置

范例:先用root的身份列出搜寻的路径为何?
[root@study ~]# echo $PATH 
/usr/local/sbin:/usr/local/bin:/sbin: /bin :/usr/sbin:/usr/bin:/root/bin

范例:用dmtsai的身份列出搜寻的路径为何?
[root@study ~]# exit     #由之前的su -离开,变回原本的帐号!或再取得一个终端机皆可!
[dmtsai@study ~]$ echo $PATH 
/usr/local/bin: /usr/bin :/usr/local/sbin:/usr/sbin:/home/dmtsai/.local/bin:/home/dmtsai/bin
 #记不记得我们前一章说过,目前/bin是连结到/usr/bin当中的喔!

[root@study ~]# PATH="${PATH}:/root"

二:档案和目录的管理

ls:用于查看系统中的档案和目录

[root@study ~]# ls [-aAdfFhilnrRSt]档名或目录名称.. 
[root@study ~]# ls [--color={never,auto,always}]档名或目录名称.. 
[root@ study ~]# ls [--full-time]档名或目录名称.. 
选项与参数:
 -a :全部的档案,连同隐藏档(开头为.的档案)一起列出来(常用)
-A :全部的档案,连同隐藏档,但不包括. 与.. 这两个目录
-d :仅列出目录本身,而不是列出目录内的档案资料(常用)
-f :直接列出结果,而不进行排序(ls 预设会以档名排序!)
-F :根据档案、目录等资讯,给予附加资料结构,例如:
      *:代表可执行档; /:代表目录; =:代表socket 档案; |:代表FIFO 档案;
-h :将档案容量以人类较易读的方式(例如GB, KB 等等)列出来;
-i :列出inode 号码,inode 的意义下一章将会介绍;
-l :长资料串列出,包含档案的属性与权限等等资料;(常用)
-n :列出UID 与GID 而非使用者与群组的名称(UID与GID会在帐号管理提到!)
-r :将排序结果反向输出,例如:原本档名由小到大,反向则为由大到小;
-R :连同子目录内容一起列出来,等于该目录下的所有档案都会显示出来;
-S :以档案容量大小排序,而不是用档名排序;
-t :依时间排序,而不是用档名。
--color=never :不要依据档案特性给予颜色显示;
--color=always :显示颜色
--color=auto :让系统自行依据设定来判断是否给予颜色
--full-time :以完整时间模式(包含年、月、日、时、分) 输出
--time={atime,ctime} :输出access 时间或改变权限属性时间(ctime) 
                       而非内容变更时间(modification time)

cp:用于复制档案或者目录资源到另一个路径

[root@study ~]# cp [-adfilprsu]来源档(source)目标档(destination) 
[root@study ~]# cp [options] source1 source2 source3 .... directory 
选项与参数:
 -a :相当于-dr --preserve=all的意思,至于dr请参考下列说明;(常用)
-d :若来源档为连结档的属性(link file),则复制连结档属性而非档案本身;
-f :为强制(force)的意思,若目标档案已经存在且无法开启,则移除后再尝试一次;
-i :若目标档(destination)已经存在时,在覆盖时会先询问动作的进行(常用)
-l :进行硬式连结(hard link)的连结档建立,而非复制档案本身;
-p :连同档案的属性(权限、用户、时间)一起复制过去,而非使用预设属性(备份常用);
-r :递回持续复制,用于目录的复制行为;(常用)
-s :复制成为符号连结档(symbolic link),亦即『捷径』档案;
-u :destination 比source 旧才更新destination,或destination 不存在的情况下才复制。
--preserve=all :除了-p 的权限相关参数外,还加入SELinux 的属性, links, xattr 等也复制了。
最后需要注意的,如果来源档有两个以上,则最后一个目的档一定要是『目录』才行!

范例一:用root身份,将家目录下的.bashrc复制到/tmp下,并更名为bashrc 
[root@study ~]# cp ~/.bashrc /tmp/bashrc 
[root@study ~]# cp -i ~/.bashrc /tmp/bashrc 
cp: overwrite `/tmp/bashrc'? n   <==n不覆盖,y为覆盖
#重复作两次动作,由于/tmp底下已经存在bashrc了,加上-i选项后,
# 则在覆盖前会询问使用者是否确定!可以按下n 或者y 来二次确认呢!

范例二:变换目录到/tmp,并将/var/log/wtmp复制到/tmp且观察属性: 
[root@study ~]# cd /tmp 
[root@study tmp]# cp /var/log/wtmp .  <==想要复制到目前的目录,最后的.不要忘 
[root@study tmp]# ls -l /var/log/wtmp wtmp 
-rw-rw-r-- . 1 root utmp 28416 Jun 11 18: 56 /var/log/wtmp
 -rw-r--r-- . 1 root root 28416 Jun 11 19:01 wtmp
 #注意上面的特殊字体,在不加任何选项的情况下,档案的某些属性/权限会改变;
# 这是个很重要的特性!要注意喔!还有,连档案建立的时间也不一样了!
# 那如果你想要将档案的所有特性都一起复制过来该怎办?可以加上-a 喔!如下所示:

[root@study tmp]# cp -a /var/log/wtmp wtmp_2 
[root@study tmp]# ls -l /var/log/wtmp wtmp_2
-rw-rw-r--. 1 root utmp 28416 Jun 11 18:56 /var/log/wtmp
-rw-rw-r--. 1 root utmp 28416 Jun 11 18:56 wtmp_2
# 了了吧!整个资料特性完全一模一样ㄟ!真是不赖~这就是-a 的特性!


范例三:复制/etc/这个目录下的所有内容到/tmp底下 
[root@study tmp]# cp /etc/ /tmp 
cp: omitting directory `/etc'    <==如果是目录则不能直接复制,要加上-r的选项 
[root@study tmp]# cp -r /etc/ /tmp 
#还是要再次的强调喔!-r是可以复制目录,但是,档案与目录的权限可能会被改变
# 所以,也可以利用『 cp -a /etc /tmp 』来下达指令喔!尤其是在备份的情况下!

范例四:将范例一复制的bashrc建立一个连结档(symbolic link) 
[root@study tmp]# ls -l bashrc 
-rw-r--r--. 1 root root 176 Jun 11 19:01 bashrc   <= =先观察一下档案情况 
[root@study tmp]# cp -s bashrc bashrc_slink 
[root@study tmp]# cp -l bashrc bashrc_hlink 
[root@study tmp]# ls -l bashrc* 
-rw-r--r- -. 2 root root 176 Jun 11 19:01 bashrc          <==与原始档案不太一样了!
-rw-r--r--. 2 root root 176 Jun 11 19:01 bashrc_hlink
lrwxrwxrwx. 1 root root 6 Jun 11 19:06 bashrc_slink -> bashrc
范例五:若~/.bashrc比/tmp/bashrc新才复制过来 
[root@study tmp]# cp -u ~/.bashrc /tmp/bashrc 
#这个-u的特性,是在目标档案与来源档案有差异时,才会复制的。
# 所以,比较常被用于『备份』的工作当中喔!^_^

范例六:将范例四造成的bashrc_slink复制成为bashrc_slink_1与bashrc_slink_2 
[root@study tmp]# cp bashrc_slink bashrc_slink_1 
[root@study tmp]# cp -d bashrc_slink bashrc_slink_2 
[root@study tmp]# ls -l bashrc bashrc_slink*
-rw-r--r--. 2 root root 176 Jun 11 19:01 bashrc
lrwxrwxrwx. 1 root root 6 Jun 11 19:06 bashrc_slink -> bashrc
-rw-r--r--. 1 root root 176 Jun 11 19:09 bashrc_slink_1             <==与原始档案相同 
lrwxrwxrwx. 1 root root 6 Jun 11 19:10 bashrc_slink_2 -> bashrc   <==是连结档!
#这个例子也是很有趣喔!原本复制的是连结档,但是却将连结档的实际档案复制过来了
# 也就是说,如果没有加上任何选项时,cp复制的是原始档案,而非连结档的属性!
# 若要复制连结档的属性,就得要使用-d 的选项了!如bashrc_slink_2 所示。

范例七:将家目录的.bashrc及.bash_history复制到/tmp底下 
[root@study tmp]# cp ~/.bashrc ~/.bash_history /tmp 
#可以将多个资料一次复制到同一个目录去!最后面一定是目录!

rm:remove,移除档案或目录的

[root@study ~]# rm [-fir]档案或目录
选项与参数:
-f :就是force 的意思,忽略不存在的档案,不会出现警告讯息;
-i :互动模式,在删除前会询问使用者是否动作
-r :递回删除啊!最常用在目录的删除了!这是非常危险的选项!

范例一:将刚刚在cp的范例中建立的bashrc删除掉!
[root@study ~]# cd /tmp 
[root@study tmp]# rm -i bashrc 
rm: remove regular file `bashrc'? y 
#如果加上-i的选项就会主动询问喔,避免你删除到错误的档名!

范例二:透过万用字元*的帮忙,将/tmp底下开头为bashrc的档名通通删除: 
[root@study tmp]# rm -i bashrc* 
#注意那个星号,代表的是0到无穷多个任意字元喔!很好用的东西!

范例三:将cp范例中所建立的/tmp/etc/这个目录删除掉!
[root@study tmp]# rmdir /tmp/etc 
rmdir: failed to remove '/tmp/etc': Directory not empty    <==删不掉啊!因为这不是空的目录!
[root@study tmp]# rm -r /tmp/etc 
rm: descend into directory `/tmp/etc'? y 
rm: remove regular file `/tmp/etc/fstab'? y 
rm: remove regular empty file `/ tmp/etc/crypttab'? ^C   <==按下[ctrl]+c中断
.....(中间省略).....
# 因为身份是root ,预设已经加入了-i 的选项,所以你要一直按y 才会删除!
# 如果不想要继续按y ,可以按下『 [ctrl]-c 』来结束rm 的工作。
#这是一种保护的动作,如果确定要删除掉此目录而不要询问,可以这样做: 
[root@study tmp]# \rm -r /tmp/etc 
#在指令前加上反斜线,可以忽略掉alias的指定选项喔!至于alias我们在bash再谈!
# 拜托!这个范例很可怕!你不要删错了!删除/etc 系统是会挂掉的!

范例四:删除一个带有-开头的档案 
[root@study tmp]# touch ./-aaa-   <== touch这个指令可以建立空档案!
[root@study tmp]# ls -l 
-rw-r--r--. 1 root root 0 Jun 11 19:22 -aaa-   <==档案大小为0,所以是空档案 
[root@study tmp] # rm -aaa- 
rm: invalid option -- 'a'                     <==因为"-"是选项嘛!所以系统误判了!
Try 'rm ./-aaa-' to remove the file `-aaa-'. <==新的bash有给建议的
Try 'rm --help' for more information.
[root@study tmp]# rm ./-aaa-

mv:移动档案或目录,可以实现改名的功能

[root@study ~]# mv [-fiu] source destination 
[root@study ~]# mv [options] source1 source2 source3 .... directory 
选项与参数:
-f :force 强制的意思,如果目标档案已经存在,不会询问而直接覆盖;
-i :若目标档案(destination) 已经存在时,就会询问是否覆盖!
-u :若目标档案已经存在,且source 比较新,才会更新(update)

范例一:复制一档案,建立一目录,将档案移动到目录中 
[root@study ~]# cd /tmp 
[root@study tmp]# cp ~/.bashrc bashrc 
[root@study tmp]# mkdir mvtest 
[ root@study tmp]# mv bashrc mvtest 
#将某个档案移动到某个目录去,就是这样做!

范例二:将刚刚的目录名称更名为mvtest2 
[root@study tmp]# mv mvtest mvtest2  <==这样就更名了!简单~
#其实在Linux底下还有个有趣的指令,名称为rename
# 该指令专职进行多个档名的同时更名,并非针对单一档名变更,与mv不同。请man rename。

范例三:再建立两个档案,再全部移动到/tmp/mvtest2当中 
[root@study tmp]# cp ~/.bashrc bashrc1 
[root@study tmp]# cp ~/.bashrc bashrc2 
[root@study tmp] # mv bashrc1 bashrc2 mvtest2 
#注意到这边,如果有多个来源档案或目录,则最后一个目标档一定是『目录!
# 意思是说,将所有的资料移动到该目录的意思!

------可以用于查看档案名称和目录名称
[root@study ~]#
basename /etc/sysconfig/network network <==很简单!就取得最后的档名~ [root@study ~]# dirname /etc/sysconfig/network /etc/sysconfig <==取得的变成目录名了!

三:档案内容查看

cat:直接打印全部文件内容

[root@study ~]# cat [-AbEnTv] 
选项与参数:
-A :相当于-vET 的整合选项,可列出一些特殊字符而不是空白而已;
-b :列出行号,仅针对非空白行做行号显示,空白行不标行号!
-E :将结尾的断行字元$ 显示出来;
-n :列印出行号,连同空白行也会有行号,与-b 的选项不同;
-T :将[tab] 按键以^I 显示出来;
-v :列出一些看不出来的特殊字符

范例一:检阅/etc/issue这个档案的内容 
[root@study ~]# cat /etc/issue
\S
Kernel \r on an \m

范例二:承上题,如果还要加印行号呢?
[root@study ~]# cat -n /etc/issue
     1 \S
     2 Kernel \r on an \m
     3
# 所以这个档案有三行!看到了吧!可以印出行号呢!这对于大档案要找某个特定的行时,有点用处!
# 如果不想要编排空白行的行号,可以使用『cat -b /etc/issue』,自己测试看看:

范例三:将/etc/man_db.conf的内容完整的显示出来(包含特殊字元) 
[root@study ~]# cat -A /etc/man_db.conf 
# $ 
....(中间省略).. .. 
MANPATH_MAP ^I /bin ^I^I^I /usr/share/man $ 
MANPATH_MAP ^I /usr/bin ^I^I /usr/share/man $ 
MANPATH_MAP ^I /sbin ^I^I^I / usr/share/man $ 
MANPATH_MAP ^I /usr/sbin ^I^I /usr/share/man $ 

tac:相对于cat来说倒着打印全部文件内容

[root@study ~]# tac /etc/issue

Kernel \r on an \m
\S
# 嘿嘿!与刚刚上面的范例一比较,是由最后一行先显示喔!

nl:添加行号打印全部内容

[root@study ~]# nl [-bnw]档案
选项与参数:
-b :指定行号指定的方式,主要有两种:
      -ba :表示不论是否为空行,也同样列出行号(类似cat -n);
      -bt :如果有空行,空的那一行不要列出行号(预设值);
-n :列出行号表示的方法,主要有三种:
      -n ln :行号在萤幕的最左方显示;
      -n rn :行号在自己栏位的最右方显示,且不加0 ;
      -n rz :行号在自己栏位的最右方显示,且加0 ;
-w :行号栏位的占用的字元数。

范例一:用nl列出/etc/issue的内容 
[root@study ~]# nl /etc/issue
     1 \S
     2 Kernel \r on an \m

# 注意看,这个档案其实有三行,第三行为空白(没有任何字元),
# 因为他是空白行,所以nl 不会加上行号喔!如果确定要加上行号,可以这样做:

[root@study ~]# nl -ba /etc/issue
     1 \S
     2 Kernel \r on an \m
     3
# 呵呵!行号加上来啰~那么如果要让行号前面自动补上0 呢?可这样

[root@study ~]# nl -ba -n rz /etc/issue
000001 \S
000002 Kernel \r on an \m
000003
# 嘿嘿!自动在自己栏位的地方补上0 了~预设栏位是六位数,如果想要改成3 位数?

[root@study ~]# nl -ba -n rz -w 3 /etc/issue
001 \S
002 Kernel \r on an \m
003
# 变成仅有3 位数啰~

more:一页一页地翻看档案内容

[root@study ~]# more /etc/man_db.conf
#
#
# This file is used by the man-db package to configure the man and cat paths.
# It is also used to provide a manpath for those without one by examining
# their PATH environment variable. For details see the manpath(5) man page.
#
.....(中间省略)..... 
--More--(28%)   <==重点在这一行喔!你的游标也会在这里等待你的指令
  • 空白键(space):代表向下翻一页;
  • Enter :代表向下翻『一行』;
  • /字串 :代表在这个显示的内容当中,向下搜寻『字串』这个关键字;
  • :f :立刻显示出档名以及目前显示的行数;
  • q :代表立刻离开more ,不再显示该档案内容。
  • b 或[ctrl]-b :代表往回翻页,不过这动作只对档案有用,对管线无用。

less:相比more的只能按顺序一页一页翻看可以往前翻看

[root@study ~]# less /etc/man_db.conf
#
#
# This file is used by the man-db package to configure the man and cat paths.
# It is also used to provide a manpath for those without one by examining
# their PATH environment variable. For details see the manpath(5) man page.
#
.....(中间省略)..... 
: <==这里可以等待你输入指令!
  • 空白键 :向下翻动一页;
  • [pagedown]:向下翻动一页;
  • [pageup] :向上翻动一页;
  • /字串 :向下搜寻『字串』的功能;
  • ?字串 :向上搜寻『字串』的功能;
  • n :重复前一个搜寻(与/ 或? 有关!)
  • N :反向的重复前一个搜寻(与/ 或? 有关!)
  • g :前进到这个资料的第一行去;
  • G :前进到这个资料的最后一行去(注意大小写);
  • q :离开less 这个程式;

head:按照给定的行号n,查看前n行的内容

[root@study ~]# head [-n number]档案
选项与参数:
-n :后面接数字,代表显示几行的意思

[root@study ~]# head /etc/man_db.conf 
#预设的情况中,显示前面十行!若要显示前20行,就得要这样: 
[root@study ~]# head -n 20 /etc/man_db.conf

范例:如果后面100行的资料都不列印,只列印/etc/man_db.conf的前面几行,该如何是好?
[root@study ~]# head -n -100 /etc/man_db.conf

tail:按照给定的行号n,查看后n行的内容

[root@study ~]# tail [-n number]档案
选项与参数:
-n :后面接数字,代表显示几行的意思
-f :表示持续侦测后面所接的档名,要等到按下[ctrl]-c才会结束tail的侦测

[root@study ~]# tail /etc/man_db.conf 
#预设的情况中,显示最后的十行!若要显示最后的20行,就得要这样: 
[root@study ~]# tail -n 20 /etc/man_db.conf

范例一:如果不知道/etc/man_db.conf有几行,却只想列出100行以后的资料时?
[root@study ~]# tail -n +100 /etc/man_db.conf

范例二:持续侦测/var/log/messages的内容 
[root@study ~]# tail -f /var/log/messages 
  <==要等到输入[ctrl]-c之后才会离开tail这个指令的侦测!

od:查看非文字档案内容

[root@study ~]# od [-t TYPE]档案
选项或参数:
-t :后面可以接各种『类型(TYPE)』的输出,例如:
      a :利用预设的字元来输出;
      c :使用ASCII 字元来输出
      d[size] :利用十进位(decimal)来输出资料,每个整数占用size bytes ;
      f[size] :利用浮点数值(floating)来输出资料,每个数占用size bytes ;
      o[size] :利用八进位(octal)来输出资料,每个整数占用size bytes ;
      x[size] :利用十六进位(hexadecimal)来输出资料,每个整数占用size bytes ;

范例一:请将/usr/bin/passwd的内容使用ASCII方式来展现!
[root@study ~]# od -tc /usr/bin/passwd
0000000 177 ELF 002 001 001 \0 \0 \0 \0 \0 \0 \0 \0 \0
0000020 003 \0 > \0 001 \0 \0 \0 364 3 \0 \0 \0 \0 \0 \0
0000040 @ \0 \0 \0 \0 \0 \0 \0 xe \0 \0 \0 \0 \0 \0
0000060 \0 \0 \0 \0 @ \0 8 \0 \t \0 @ \0 035 \0 034 \0
0000100 006 \0 \0 \0 005 \0 \0 \0 @ \0 \0 \0 \0 \0 \0 \0
.....(后面省略)....
# 最左边第一栏是以8 进位来表示bytes数。以上面范例来说,第二栏0000020代表开头是
# 第16 个byes (2x8) 的内容之意。

范例二:请将/etc/issue这个档案的内容以8进位列出储存值与ASCII的对照表 
[root@study ~]# od -t oCc /etc/issue
0000000 134 123 012 113 145 162 156 145 154 040 134 162 040 157 156 040
          \ S \n K ernel \ ron
0000020 141 156 040 134 155 012 012
          an \ m \n \n
0000027
# 如上所示,可以发现每个字元可以对应到的数值为何!要注意的是,该数值是8 进位喔!
# 例如S 对应的记录数值为123 ,转成十进位:1x8^2+2x8+3=83。

touch:修改档案的时间和新建档案

    • modification time (mtime)
      当该档案的『内容资料』变更时,就会更新这个时间!内容资料指的是档案的内容,而不是档案的属性或权限喔!

    • status time (ctime)
      当该档案的『状态(status)』改变时,就会更新这个时间,举例来说,像是权限与属性被更改了,都会更新这个时间啊。 

    • access time (atime)
      当『该档案的内容被取用』时,就会更新这个读取时间(access)。举例来说,我们使用cat去读取/etc/man_db.conf ,就会更新该档案的atime了
  • [root@study ~]# date; ls -l /etc/man_db.conf ; ls -l --time=atime /etc/man_db.conf ; \ 
    > ls -l --time=ctime /etc/man_db.conf  #这两行其实是同一行喔!用分号隔开 
    Tue Jun 16 00:43:17 CST 2015   #目前的时间啊!
    -rw-r--r--. 1 root root 5171 Jun 10 2014 /etc/man_db.conf   #在2014/06/10建立的内容(mtime) 
    -rw-r--r--. 1 root root 5171 Jun 15 23:46 /etc/man_db.conf   #在2015/06/15读取过内容(atime) 
    -rw-r--r--. 1 root root 5171 May 4 17:54 /etc/man_db.conf   #在2015/05/04更新过状态(ctime) 
    #为了要让资料输出比较好看,所以鸟哥将三个指令同时依序执行,三个指令中间用分号(;)隔开即可。

 

[root@study ~]# touch [-acdmt]档案
选项与参数:
-a :仅修订access time;
-c :仅修改档案的时间,若该档案不存在则不建立新档案;
-d :后面可以接欲修订的日期而不用目前的日期,也可以使用--date="日期或时间"
-m :仅修改mtime ;
-t :后面可以接欲修订的时间而不用目前的时间,格式为[YYYYMMDDhhmm]

范例一:新建一个空的档案并观察时间 
[dmtsai@study ~]# cd /tmp 
[dmtsai@study tmp]# touch testtouch 
[dmtsai@study tmp]# ls -l testtouch 
-rw-rw-r--. 1 dmtsai dmtsai 0 Jun 16 00:45 testtouch
 #注意到,这个档案的大小是0呢!在预设的状态下,如果touch后面有接档案,
# 则该档案的三个时间(atime/ctime/mtime) 都会更新为目前的时间。若该档案不存在,
# 则会主动的建立一个新的空的档案喔!例如上面这个例子!

范例二:将~/.bashrc复制成为bashrc,假设复制完全的属性,检查其日期 
[dmtsai@study tmp]# cp -a ~/.bashrc bashrc 
[dmtsai@study tmp]# date; ll bashrc; ll - -time=atime bashrc; ll --time=ctime bashrc 
Tue Jun 16 00:49:24 CST 2015                          <==这是目前的时间 
-rw-r--r--. 1 dmtsai dmtsai 231 Mar 6 06:06 bashrc   <==这是mtime 
-rw-r--r--. 1 dmtsai dmtsai 231 Jun 15 23:44 bashrc   <==这是atime 
-rw-r--r--. 1 dmtsai dmtsai 231 Jun 16 00 :47 bashrc   <==这是ctime
范例三:修改案例二的bashrc档案,将日期调整为两天前 
[dmtsai@study tmp]# touch -d "2 days ago" bashrc 
[dmtsai@study tmp]# date; ll bashrc; ll --time= atime bashrc; ll --time=ctime bashrc
Tue Jun 16 00:51:52 CST 2015
-rw-r--r--. 1 dmtsai dmtsai 231 Jun 14 00:51 bashrc
-rw-r--r--. 1 dmtsai dmtsai 231 Jun 14 00:51 bashrc
-rw-r--r--. 1 dmtsai dmtsai 231 Jun 16 00:51 bashrc
# 跟上个范例比较看看,本来是16 日变成14 日了(atime/mtime)~不过, ctime 并没有跟着改变喔!

范例四:将上个范例的bashrc日期改为2014/06/15 2:02 
[dmtsai@study tmp]# touch -t 201406150202 bashrc 
[dmtsai@study tmp]# date; ll bashrc; ll --time=atime bashrc; ll --time=ctime bashrc
Tue Jun 16 00:54:07 CST 2015
-rw-r--r--. 1 dmtsai dmtsai 231 Jun 15 2014 bashrc
-rw-r--r--. 1 dmtsai dmtsai 231 Jun 15 2014 bashrc
-rw-r--r--. 1 dmtsai dmtsai 231 Jun 16 00:54 bashrc
# 注意看看,日期在atime 与mtime 都改变了,但是ctime 则是记录目前的时间!

 

posted @ 2018-03-05 13:36  zzy0306  阅读(317)  评论(0编辑  收藏  举报