2. Linux file and directory management

First, the directory path

1. relative path and absolute path

  • Absolute path: the path of writing [some of the root directory / write plays], such as: / usr / share / doc
  • Relative path: The path of writing [help / write plays], / usr / share / doc when you want to / usr / share / under man, can be written as: [cd ../man] This is the wording of the relative path of it! Means a relative path [path relative to the current working directory!]

2. The directory related operations

.         代表此层目录
..        代表上一层目录
-         代表前一个工作目录
~         代表『目前使用者身份』所在的家目录
~account  代表 account 这个使用者的家目录(account是个帐号名称)
例题:
请问在Linux底下,根目录下有没有上一级目录(..)存在?
答:
若使用『 ls -al / 』去查询,可以看到根目录下确实存在 . 与 .. 两个目录,再仔细的查阅, 可发现这两个目录的属性与权限完全一致,这代表根目录的上一层(..)与根目录自己(.)是同一个目录。
(1) cd (directory conversion - Change Directory)
[root@www ~]# cd [相对路径或绝对路径]
# 最重要的就是目录的绝对路径与相对路径,还有一些特殊目录的符号罗!
[root@www ~]# cd ~vbird
# 代表去到 vbird 这个使用者的家目录,亦即 /home/vbird
[root@www vbird]# cd ~
# 表示回到自己的家目录,亦即是 /root 这个目录
[root@www ~]# cd
# 没有加上任何路径,也还是代表回到自己家目录的意思喔!
[root@www ~]# cd ..
# 表示去到目前的上一级目录,亦即是 /root 的上一级目录的意思;
[root@www /]# cd -
# 表示回到刚刚的那个目录,也就是 /root 罗~
[root@www ~]# cd /var/spool/mail
# 这个就是绝对路径的写法!直接指定要去的完整路径名称!
[root@www mail]# cd ../mqueue
# 这个是相对路径的写法,我们由/var/spool/mail 去到/var/spool/mqueue 就这样写!
(2) pwd (display the current directory - Print Working Directory)
[root@www ~]# pwd [-P]
选项与参数:
    -P  :显示出确实的路径,而非使用连结 (link) 路径。

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

范例:显示出实际的工作目录,而非连结档本身的目录名而已
[root@www ~]# cd /var/mail   <==注意,/var/mail是一个连结档
[root@www mail]# pwd
/var/mail         <==列出目前的工作目录
[root@www mail]# pwd -P
/var/spool/mail   <==怎么回事?有没有加 -P 差很多~
[root@www mail]# ls -ld /var/mail
lrwxrwxrwx 1 root root 10 Sep  4 17:54 /var/mail -> spool/mail
# 看到这里应该知道为啥了吧?因为 /var/mail 是连结档,连结到 /var/spool/mail 
# 所以,加上 pwd -P 的选项后,会不以连结档的数据显示,而是显示正确的完整路径啊!
(3) mkdir (create - make directory)
[root@www ~]# mkdir [-mp] 目录名称
选项与参数:
-m :配置文件的权限喔!直接配置,不需要看默认权限 (umask) 的脸色~
-p :帮助你直接将所需要的目录(包含上一级目录)递回创建起来!

范例:请到/tmp底下尝试创建数个新目录看看:
[root@www ~]# cd /tmp
[root@www tmp]# mkdir test    <==创建一名为 test 的新目录
[root@www tmp]# mkdir test1/test2/test3/test4
mkdir: cannot create directory `test1/test2/test3/test4': 
No such file or directory       <== 没办法直接创建此目录啊!
[root@www tmp]# mkdir -p test1/test2/test3/test4
# 加了这个 -p 的选项,可以自行帮你创建多层目录!

范例:创建权限为rwx--x--x的目录
[root@www tmp]# mkdir -m 711 test2
[root@www tmp]# ls -l
drwxr-xr-x  3 root  root 4096 Jul 18 12:50 test
drwxr-xr-x  3 root  root 4096 Jul 18 12:53 test1
drwx--x--x  2 root  root 4096 Jul 18 12:54 test2
# 仔细看上面的权限部分,如果没有加上 -m 来强制配置属性,系统会使用默认属性。
# 那么你的默认属性为何?这要透过底下介绍的 umask 才能了解喔! ^_^
(4) rmdir (delete [empty] directory)
[root@www ~]# rmdir [-p] 目录名称
选项与参数:
-p :连同上一级『空的』目录也一起删除

范例:将於mkdir范例中创建的目录(/tmp底下)删除掉!
[root@www tmp]# ls -l   <==看看有多少目录存在?
drwxr-xr-x  3 root  root 4096 Jul 18 12:50 test
drwxr-xr-x  3 root  root 4096 Jul 18 12:53 test1
drwx--x--x  2 root  root 4096 Jul 18 12:54 test2
[root@www tmp]# rmdir test   <==可直接删除掉,没问题
[root@www tmp]# rmdir test1  <==因为尚有内容,所以无法删除!
rmdir: `test1': Directory not empty
[root@www tmp]# rmdir -p test1/test2/test3/test4
[root@www tmp]# ls -l        <==您看看,底下的输出中test与test1不见了!
drwx--x--x  2 root  root 4096 Jul 18 12:54 test2
# 瞧!利用 -p 这个选项,立刻就可以将 test1/test2/test3/test4 一次删除~
# 不过要注意的是,这个 rmdir 仅能『删除空的目录』喔!

范例:将所有目录下的东西删掉
[root@www tmp]# rmdir -r test

3. $ PATH (environment variable)

范例:先用root的身份列出搜寻的路径为何?
[root@www ~]# echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin
:/bin:/usr/sbin:/usr/bin:/root/bin  <==这是同一行!

范例:用vbird的身份列出搜寻的路径为何?
[root@www ~]# su - vbird
[vbird@www ~]# echo $PATH
/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/vbird/bin
# 仔细看,一般用户vbird的PATH中,并不包含任何『sbin』的目录存在喔!
- 不同身份使用者默认的PATH不同,默认能够随意运行的命令也不同(如root与vbird);
- PATH是可以修改的,所以一般使用者还是可以透过修改PATH来运行某些位於/sbin或/usr/sbin下的命令来查询;
- 使用绝对路径或相对路径直接指定某个命令的档名来运行,会比搜寻PATH来的正确;
- 命令应该要放置到正确的目录下,运行才会比较方便;
- 本目录(.)最好不要放到PATH当中。

Second, file and directory management

1. ls (file and directory view)

[root@www ~]# ls [-aAdfFhilnrRSt] 目录名称
[root@www ~]# ls [--color={never,auto,always}] 目录名称
[root@www ~]# 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)

2. cp / rm / mv (copy, delete, move)

① cp (copy a file or directory)
[root@www ~]# cp [-adfilprsu] 来源档(source) 目标档(destination)
[root@www ~]# cp [options] source1 source2 source3 .... directory
选项与参数:
-a  :相当於 -pdr 的意思,至於 pdr 请参考下列说明;(常用)
-d  :若来源档为连结档的属性(link file),则复制连结档属性而非文件本身;
-f  :为强制(force)的意思,若目标文件已经存在且无法开启,则移除后再尝试一次;
-i  :若目标档(destination)已经存在时,在覆盖时会先询问动作的进行(常用)
-l  :进行硬式连结(hard link)的连结档创建,而非复制文件本身;
-p  :连同文件的属性一起复制过去,而非使用默认属性(备份常用);
-r  :递回持续复制,用於目录的复制行为;(常用)
-s  :复制成为符号连结档 (symbolic link),亦即『捷径』文件;
-u  :若 destination 比 source 旧才升级 destination !
最后需要注意的,如果来源档有两个以上,则最后一个目的档一定要是『目录』才行!
范例一:用root身份,将家目录下的 .bashrc 复制到 /tmp 下,并更名为 bashrc
[root@www ~]# cp ~/.bashrc /tmp/bashrc
[root@www ~]# cp -i ~/.bashrc /tmp/bashrc
cp: overwrite `/tmp/bashrc'? n  <==n不覆盖,y为覆盖
# 重复作两次动作,由於 /tmp 底下已经存在 bashrc 了,加上 -i 选项后,
# 则在覆盖前会询问使用者是否确定!可以按下 n 或者 y 来二次确认呢!

范例二:变换目录到/tmp,并将/var/log/wtmp复制到/tmp且观察属性:
[root@www ~]# cd /tmp
[root@www tmp]# cp /var/log/wtmp . <==想要复制到目前的目录,最后的 . 不要忘
[root@www tmp]# ls -l /var/log/wtmp wtmp
-rw-rw-r-- 1 root utmp 96384 Sep 24 11:54 /var/log/wtmp
-rw-r--r-- 1 root root 96384 Sep 24 14:06 wtmp
# 注意上面的特殊字体,在不加任何选项的情况下,文件的某些属性/权限会改变;
# 这是个很重要的特性!要注意喔!还有,连文件创建的时间也不一样了!
# 在默认的条件中, cp 的来源档与目的档的权限是不同的,目的档的拥有者通常会是命令操作者本身。
# 那如果你想要将文件的所有特性都一起复制过来该怎办?可以加上 -a 喔!如下所示:

[root@www tmp]# cp -a /var/log/wtmp wtmp_2
[root@www tmp]# ls -l /var/log/wtmp wtmp_2
-rw-rw-r-- 1 root utmp 96384 Sep 24 11:54 /var/log/wtmp
-rw-rw-r-- 1 root utmp 96384 Sep 24 11:54 wtmp_2
# 了了吧!整个数据特性完全一模一样ㄟ!真是不赖~这就是 -a 的特性!
范例三:复制 /etc/ 这个目录下的所有内容到 /tmp 底下
[root@www tmp]# cp /etc/ /tmp
cp: omitting directory `/etc'   <== 如果是目录则不能直接复制,要加上 -r 的选项
[root@www tmp]# cp -r /etc/ /tmp
# 还是要再次的强调喔! -r 是可以复制目录,但是,文件与目录的权限可能会被改变
# 所以,也可以利用『 cp -a /etc /tmp 』来下达命令喔!尤其是在备份的情况下!

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

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

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

cp points to note:

  • The need to retain the information complete source file?
  • Whether the source file for the linked files (symbolic link file)?
  • Whether the source files for a particular file, such as FIFO, socket, etc.?
  • Whether the source file is a directory?
② rm (remove file or directory)
[root@www ~]# rm [-fir] 文件或目录
选项与参数:
-f  :就是 force 的意思,忽略不存在的文件,不会出现警告信息;
-i  :互动模式,在删除前会询问使用者是否动作
-r  :递回删除啊!最常用在目录的删除了!这是非常危险的选项!!!

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

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

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

范例四:删除一个带有 - 开头的文件
[root@www tmp]# touch ./-aaa-  <==touch这个命令可以创建空文件!
[root@www tmp]# ls -l 
-rw-r--r-- 1 root  root      0 Sep 24 15:03 -aaa-  <==文件大小为0,所以是空文件
[root@www tmp]# rm -aaa-
Try `rm --help' for more information.  <== 因为 "-" 是选项嘛!所以系统误判了!
[root@www tmp]# rm ./-aaa-
③ mv (move files and directories, rename)
[root@www ~]# mv [-fiu] source destination
[root@www ~]# mv [options] source1 source2 source3 .... directory
选项与参数:
-f  :force 强制的意思,如果目标文件已经存在,不会询问而直接覆盖;
-i  :若目标文件 (destination) 已经存在时,就会询问是否覆盖!
-u  :若目标文件已经存在,且 source 比较新,才会升级 (update)

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

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

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

3. To obtain the file name and path of the directory name

[root@www ~]# basename /etc/sysconfig/network
network         <== 很简单!就取得最后的档名~
[root@www ~]# dirname /etc/sysconfig/network
/etc/sysconfig  <== 取得的变成目录名了!

Third, the content of the document review

1. Direct view file contents

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

范例一:检阅 /etc/issue 这个文件的内容
[root@www ~]# cat /etc/issue
CentOS release 5.3 (Final)
Kernel \r on an \m

范例二:承上题,如果还要加印行号呢?
[root@www ~]# cat -n /etc/issue
     1  CentOS release 5.3 (Final)
     2  Kernel \r on an \m
     3
# 如果不想要编排空白行的行号,可以使用『cat -b /etc/issue』
(2) tac (reverse listed)
[root@www ~]# tac /etc/issue

Kernel \r on an \m
CentOS release 5.3 (Final)
# 嘿嘿!与刚刚上面的范例一比较,是由最后一行先显示喔!
(3) nl (add line numbers to print)
[root@www ~]# nl [-bnw] 文件
选项与参数:
-b  :指定行号指定的方式,主要有两种:
      -b a :表示不论是否为空行,也同样列出行号(类似 cat -n);
      -b t :如果有空行,空的那一行不要列出行号(默认值);
-n  :列出行号表示的方法,主要有三种:
      -n ln :行号在萤幕的最左方显示;
      -n rn :行号在自己栏位的最右方显示,且不加 0 ;
      -n rz :行号在自己栏位的最右方显示,且加 0 ;
-w  :行号栏位的占用的位数。

范例一:用 nl 列出 /etc/issue 的内容
[root@www ~]# nl /etc/issue
     1  CentOS release 5.3 (Final)
     2  Kernel \r on an \m

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

[root@www ~]# nl -b a /etc/issue
     1  CentOS release 5.3 (Final)
     2  Kernel \r on an \m
     3
# 呵呵!行号加上来罗~那么如果要让行号前面自动补上 0 呢?可这样

[root@www ~]# nl -b a -n rz /etc/issue
000001  CentOS release 5.3 (Final)
000002  Kernel \r on an \m
000003
# 嘿嘿!自动在自己栏位的地方补上 0 了~默认栏位是六位数,如果想要改成 3 位数?

[root@www ~]# nl -b a -n rz -w 3 /etc/issue
001     CentOS release 5.3 (Final)
002     Kernel \r on an \m
003
# 变成仅有 3 位数罗~

2. Turn Pages View

(1) more (a one turn)
[root@www ~]# more /etc/man.config
#
# Generated automatically from man.conf.in by the
# configure script.
#
# man.conf from man-1.6d
....(中间省略)....
--More--(28%)  <== 重点在这一行喔!你的光标也会在这里等待你的命令
  • Key blank (space): the representative of a folded downwards;
  • Enter: turned down on behalf of "line";
  • / String: this represents the content displayed them down search for "string" as a keyword;
  • : F: immediately shows the file name and line number currently displayed;
  • q: representatives immediately leave more, no longer displays the contents of the file.
  • b or [ctrl] -b: representatives back flip, but this action only useful for files on the pipeline useless.
(2) less (a one turn)
[root@www ~]# less /etc/man.config
#
# Generated automatically from man.conf.in by the
# configure script.
#
# man.conf from man-1.6d
....(中间省略)....
:   <== 这里可以等待你输入命令!
  • Spacebar: turning a downward;
  • [Pagedown]: Scroll down one;
  • [Pageup]: a turning upward;
  • / String: down search "string" of;
  • ? String: up search 'string' of;
  • n: repetition before a search (and / or related?!)
  • N: a reverse search before repeating (and / or related?!)
  • q: less leave this program

3. Interception

(1) head (the first few lines removed)
[root@www ~]# head [-n number] 文件 
选项与参数:
-n  :后面接数字,代表显示几行的意思

[root@www ~]# head /etc/man.config
# 默认的情况中,显示前面十行!若要显示前 20 行,就得要这样:
[root@www ~]# head -n 20 /etc/man.config

范例:如果后面100行的数据都不列印,只列印/etc/man.config的前面几行,该如何是好?
[root@www ~]# head -n -100 /etc/man.config
(2) tail (lines later withdrawn)
[root@www ~]# tail [-n number] 文件 
选项与参数:
-n  :后面接数字,代表显示几行的意思
-f  :表示持续侦测后面所接的档名,要等到按下[ctrl]-c才会结束tail的侦测

[root@www ~]# tail /etc/man.config
# 默认的情况中,显示最后的十行!若要显示最后的 20 行,就得要这样:
[root@www ~]# tail -n 20 /etc/man.config

范例一:如果不知道/etc/man.config有几行,却只想列出100行以后的数据时?
[root@www ~]# tail -n +100 /etc/man.config

范例二:持续侦测/var/log/messages的内容
[root@www ~]# tail -f /var/log/messages
  <==要等到输入[crtl]-c之后才会离开tail这个命令的侦测!
例题:
假如我想要显示 /etc/man.config 的第 11 到第 20 行呢?
答:
这个应该不算难,想一想,在第 11 到第 20 行,那么我取前 20 行,再取后十行,所以结果就是:『 head -n 20 /etc/man.config | tail -n 10 』,这样就可以得到第 11 到第 20 行之间的内容了! 但是里面涉及到管线命令,需要在第三篇的时候才讲的到!
(3) od (non-text)
[root@www ~]# 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@www ~]# od -t c /usr/bin/passwd
0000000 177   E   L   F 001 001 001  \0  \0  \0  \0  \0  \0  \0  \0  \0
0000020 002  \0 003  \0 001  \0  \0  \0 260 225 004  \b   4  \0  \0  \0
0000040 020   E  \0  \0  \0  \0  \0  \0   4  \0      \0  \a  \0   (  \0
0000060 035  \0 034  \0 006  \0  \0  \0   4  \0  \0  \0   4 200 004  \b
0000100   4 200 004  \b 340  \0  \0  \0 340  \0  \0  \0 005  \0  \0  \0
.....(后面省略)....
# 最左边第一栏是以 8 进位来表示bytes数。以上面范例来说,第二栏0000020代表开头是
# 第 16 个 byes (2x8) 的内容之意。

范例二:请将/etc/issue这个文件的内容以8进位列出储存值与ASCII的对照表
[root@www ~]# od -t oCc /etc/issue
0000000 103 145 156 164 117 123 040 162 145 154 145 141 163 145 040 065
          C   e   n   t   O   S       r   e   l   e   a   s   e       5
0000020 056 062 040 050 106 151 156 141 154 051 012 113 145 162 156 145
          .   2       (   F   i   n   a   l   )  \n   K   e   r   n   e
0000040 154 040 134 162 040 157 156 040 141 156 040 134 155 012 012
          l       \   r       o   n       a   n       \   m  \n  \n
0000057
# 如上所示,可以发现每个字节可以对应到的数值为何!
# 例如e对应的记录数值为145,转成十进位:1x8^2+4x8+5=101。
(4) touch (time to modify the file or create a new file)
  • Time modification (mtime) :
    When "data content" of the document changes will be upgraded this time! Content data refers to the contents of the file, rather than the property or rights to the file Oh!
  • Time Status (ctime) :
    When "state (status)" of the document changes will be upgraded this time, for example, such rights and property is changed, will be upgraded this time ah.
  • Time Access (atime) :
    When the "content of the file is accessible," will upgrade the read time (access). For example, we use cat to read /etc/man.config, will upgrade the atime of the file.
[root@www ~]# touch [-acdmt] 文件
选项与参数:
-a  :仅修订 access time;
-c  :仅修改文件的时间,若该文件不存在则不创建新文件;
-d  :后面可以接欲修订的日期而不用目前的日期,也可以使用 --date="日期或时间"
-m  :仅修改 mtime ;
-t  :后面可以接欲修订的时间而不用目前的时间,格式为[YYMMDDhhmm]

范例一:新建一个空的文件并观察时间
[root@www ~]# cd /tmp
[root@www tmp]# touch testtouch
[root@www tmp]# ls -l testtouch
-rw-r--r-- 1 root root 0 Sep 25 21:09 testtouch
# 注意到,这个文件的大小是 0 呢!在默认的状态下,如果 touch 后面有接文件,
# 则该文件的三个时间 (atime/ctime/mtime) 都会升级为目前的时间。若该文件不存在,
# 则会主动的创建一个新的空的文件喔!例如上面这个例子!

范例二:将 ~/.bashrc 复制成为 bashrc,假设复制完全的属性,检查其日期
[root@www tmp]# cp -a ~/.bashrc bashrc
[root@www tmp]# ll bashrc; ll --time=atime bashrc; ll --time=ctime bashrc
-rw-r--r-- 1 root root 176 Jan  6  2007 bashrc  <==这是 mtime
-rw-r--r-- 1 root root 176 Sep 25 21:11 bashrc  <==这是 atime
-rw-r--r-- 1 root root 176 Sep 25 21:12 bashrc  <==这是 ctime

Fourth, file / directory permissions default permissions and hide

1. Default Permissions

(1) umask
[root@www ~]# umask 002
[root@www ~]# touch test3
[root@www ~]# mkdir test4
[root@www ~]# ll 
-rw-rw-r-- 1 root root     0 Sep 27 00:36 test3
drwxrwxr-x 2 root root  4096 Sep 27 00:36 test4

2. Hide property

(1) chattr (profile hidden attribute)
[root@www ~]# chattr [+-=][ASacdistu] 文件或目录名称
选项与参数:
+   :添加某一个特殊参数,其他原本存在参数则不动。
-   :移除某一个特殊参数,其他原本存在参数则不动。
=   :配置一定,且仅有后面接的参数

A  :当配置了 A 这个属性时,若你有存取此文件(或目录)时,他的存取时间 atime
     将不会被修改,可避免I/O较慢的机器过度的存取磁碟。这对速度较慢的计算机有帮助
S  :一般文件是非同步写入磁碟的(原理请参考第五章sync的说明),如果加上 S 这个
     属性时,当你进行任何文件的修改,该更动会『同步』写入磁碟中。
a  :当配置 a 之后,这个文件将只能添加数据,而不能删除也不能修改数据,只有root 
     才能配置这个属性。 
c  :这个属性配置之后,将会自动的将此文件『压缩』,在读取的时候将会自动解压缩,
     但是在储存的时候,将会先进行压缩后再储存(看来对於大文件似乎蛮有用的!)
d  :当 dump 程序被运行的时候,配置 d 属性将可使该文件(或目录)不会被 dump 备份
i  :这个 i 可就很厉害了!他可以让一个文件『不能被删除、改名、配置连结也无法
     写入或新增数据!』对於系统安全性有相当大的助益!只有 root 能配置此属性
s  :当文件配置了 s 属性时,如果这个文件被删除,他将会被完全的移除出这个硬盘
     空间,所以如果误删了,完全无法救回来了喔!
u  :与 s 相反的,当使用 u 来配置文件时,如果该文件被删除了,则数据内容其实还
     存在磁碟中,可以使用来救援该文件喔!
注意:属性配置常见的是 a 与 i 的配置值,而且很多配置值必须要身为 root 才能配置

范例:请尝试到/tmp底下创建文件,并加入 i 的参数,尝试删除看看。
[root@www ~]# cd /tmp
[root@www tmp]# touch attrtest     <==创建一个空文件
[root@www tmp]# chattr +i attrtest <==给予 i 的属性
[root@www tmp]# rm attrtest        <==尝试删除看看
rm: remove write-protected regular empty file `attrtest'? y
rm: cannot remove `attrtest': Operation not permitted  <==操作不许可
# 看到了吗?呼呼!连 root 也没有办法将这个文件删除呢!赶紧解除配置!

范例:请将该文件的 i 属性取消!
[root@www tmp]# chattr -i attrtest
(2) lsattr (show files hidden attributes)
[root@www ~]# lsattr [-adR] 文件或目录
选项与参数:
-a :将隐藏档的属性也秀出来;
-d :如果接的是目录,仅列出目录本身的属性而非目录内的档名;
-R :连同子目录的数据也一并列出来! 

[root@www tmp]# chattr +aij attrtest
[root@www tmp]# lsattr attrtest
----ia---j--- attrtest

3. Observe the File Types

(1) file

ASCII file or data, or binary

[root@www ~]# file ~/.bashrc
/root/.bashrc: ASCII text  <==告诉我们是 ASCII 的纯文字档啊!
[root@www ~]# file /usr/bin/passwd
/usr/bin/passwd: setuid 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, stripped
# 运行档的数据可就多的不得了!包括这个文件的 suid 权限、兼容於 Intel 386
# 等级的硬件平台、使用的是 Linux 核心 2.6.9 的动态函式库连结等等。
[root@www ~]# file /var/lib/mlocate/mlocate.db
/var/lib/mlocate/mlocate.db: data  <== 这是 data 文件!

Fifth, the search command file

1. which

This command is based on the " PATH " environment variable to this path specification, to search for the file name "Running gear" in ~ So, the focus is to find the "run profile" it!

[root@www ~]# which [-a] command
选项或参数:
-a :将所有由 PATH 目录中可以找到的命令均列出,而不止第一个被找到的命令名称

范例一:分别用root与一般帐号搜寻 ifconfig 这个命令的完整档名
[root@www ~]# which ifconfig
/sbin/ifconfig            <==用 root 可以找到正确的运行档名喔!
[root@www ~]# su - vbird <==切换身份成为 vbird 去!
[vbird@www ~]$ which ifconfig
/usr/bin/which: no ifconfig in (/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin
:/home/vbird/bin)         <==见鬼了!竟然一般身份帐号找不到!
# 因为 which 是根据使用者所配置的 PATH 变量内的目录去搜寻可运行档的!所以,
# 不同的 PATH 配置内容所找到的命令当然不一样啦!因为 /sbin 不在 vbird 的 
# PATH 中,找不到也是理所当然的啊!了乎?
[vbird@www ~]$ exit      <==记得将身份切换回原本的 root

范例二:用 which 去找出 which 的档名为何?
[root@www ~]# which which
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot '
        /usr/bin/which
# 竟然会有两个 which ,其中一个是 alias 这玩意儿呢!那是啥?
# 那就是所谓的『命令别名』,意思是输入 which 会等於后面接的那串命令啦!
# 更多的数据我们会在 bash 章节中再来谈的!

范例三:请找出 cd 这个命令的完整档名
[root@www ~]# which cd
/usr/bin/which: no cd in (/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin
:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
# 瞎密?怎么可能没有 cd ,我明明就能够用 root 运行 cd 的啊!

2. whereis (look for a specific file)

[root@www ~]# whereis [-bmsu] 文件或目录名
选项与参数:
-b    :只找 binary 格式的文件
-m    :只找在说明档 manual 路径下的文件
-s    :只找 source 来源文件
-u    :搜寻不在上述三个项目当中的其他特殊文件

范例一:请用不同的身份找出 ifconfig 这个档名
[root@www ~]# whereis ifconfig 
ifconfig: /sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz
[root@www ~]# su - vbird        <==切换身份成为 vbird
[vbird@www ~]$ whereis ifconfig <==找到同样的结果喔!
ifconfig: /sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz
[vbird@www ~]$ exit              <==回归身份成为 root 去!
# 注意看,明明 which 一般使用者找不到的 ifconfig 却可以让 whereis 找到!
# 这是因为系统真的有 ifconfig 这个『文件』,但是使用者的 PATH 并没有加入 /sbin
# 所以,未来你找不到某些命令时,先用文件搜寻命令找找看再说!

范例二:只找出跟 passwd 有关的『说明文件』档名(man page)
[root@www ~]# whereis -m passwd
passwd: /usr/share/man/man1/passwd.1.gz /usr/share/man/man5/passwd.5.gz

3. locate

[root@www ~]# locate [-ir] keyword
选项与参数:
-i  :忽略大小写的差异;
-r  :后面可接正规表示法的显示方式

范例一:找出系统中所有与 passwd 相关的档名
[root@www ~]# locate passwd
/etc/passwd
/etc/passwd-
/etc/news/passwd.nntp
/etc/pam.d/passwd
....(底下省略)....

Use locate to find data when special fast, because the data is to find locate "database has been created / var / lib / mlocate /" from the inside to search for data, so do not store them directly on to the hard disk take data.

Locate database upgrade method is very simple, direct input "updatedb" on it! updatedb command to read the configuration /etc/updatedb.conf this configuration file, and then go inside to search the hard disk file name of action, and finally to upgrade the entire database file Lo! Because updatedb will go to search the hard disk, so when you run updatedb, may wait a few minutes Oh!

  • updatedb: According /etc/updatedb.conf configuration file name to search the hard disk in the system, and upgrade the database file in / var / lib / mlocate;
  • locate: based on database records in / var / lib / mlocate, identify key file name entered by the user.

4. find

[root@www ~]# find [PATH] [option] [action]
选项与参数:
1. 与时间有关的选项:共有 -atime, -ctime 与 -mtime ,以 -mtime 说明
   -mtime  n :n 为数字,意义为在 n 天之前的『一天之内』被更动过内容的文件;
   -mtime +n :列出在 n 天之前(不含 n 天本身)被更动过内容的文件档名;
   -mtime -n :列出在 n 天之内(含 n 天本身)被更动过内容的文件档名。
   -newer file :file 为一个存在的文件,列出比 file 还要新的文件档名

范例一:将过去系统上面 24 小时内有更动过内容 (mtime) 的文件列出
[root@www ~]# find / -mtime 0
# 那个 0 是重点!0 代表目前的时间,所以,从现在开始到 24 小时前,
# 有变动过内容的文件都会被列出来!那如果是三天前的 24 小时内?
# find / -mtime 3 有变动过的文件都被列出的意思!

范例二:寻找 /etc 底下的文件,如果文件日期比 /etc/passwd 新就列出
[root@www ~]# find /etc -newer /etc/passwd
# -newer 用在分辨两个文件之间的新旧关系是很有用的!

If I wanted to find "four days had been modifiers file filename" mean? It can use the "find / var -mtime -4." If that is "four days before that day," to use "find / var -mtime 4"

  • Representative +4 greater than or equal to five days before the filename: ex> find / var -mtime +4
  • -4 or less representative of file filename 4 days: ex> find / var -mtime -4
  • 4 represents the 4-5 day file filename: ex> find / var -mtime 4
选项与参数:
2. 与使用者或群组名称有关的参数:
   -uid n :n 为数字,这个数字是使用者的帐号 ID,亦即 UID ,这个 UID 是记录在
            /etc/passwd 里面与帐号名称对应的数字。这方面我们会在第四篇介绍。
   -gid n :n 为数字,这个数字是群组名称的 ID,亦即 GID,这个 GID 记录在
            /etc/group,相关的介绍我们会第四篇说明~
   -user name :name 为使用者帐号名称喔!例如 dmtsai 
   -group name:name 为群组名称喔,例如 users ;
   -nouser    :寻找文件的拥有者不存在 /etc/passwd 的人!
   -nogroup   :寻找文件的拥有群组不存在於 /etc/group 的文件!
                当你自行安装软件时,很可能该软件的属性当中并没有文件拥有者,
                这是可能的!在这个时候,就可以使用 -nouser 与 -nogroup 搜寻。

范例三:搜寻 /home 底下属於 vbird 的文件
[root@www ~]# find /home -user vbird
# 这个东西也很有用的~当我们要找出任何一个使用者在系统当中的所有文件时,
# 就可以利用这个命令将属於某个使用者的所有文件都找出来喔!

范例四:搜寻系统中不属於任何人的文件
[root@www ~]# find / -nouser
# 透过这个命令,可以轻易的就找出那些不太正常的文件。
# 如果有找到不属於系统任何人的文件时,不要太紧张,
# 那有时候是正常的~尤其是你曾经以原始码自行编译软件时。
选项与参数:
3. 与文件权限及名称有关的参数:
   -name filename:搜寻文件名称为 filename 的文件;
   -size [+-]SIZE:搜寻比 SIZE 还要大(+)或小(-)的文件。这个 SIZE 的规格有:
                   c: 代表 byte, k: 代表 1024bytes。所以,要找比 50KB
                   还要大的文件,就是『 -size +50k 』
   -type TYPE    :搜寻文件的类型为 TYPE 的,类型主要有:一般正规文件 (f),
                   装置文件 (b, c), 目录 (d), 连结档 (l), socket (s), 
                   及 FIFO (p) 等属性。
   -perm mode  :搜寻文件权限『刚好等於』 mode 的文件,这个 mode 为类似 chmod
                 的属性值,举例来说, -rwsr-xr-x 的属性为 4755 !
   -perm -mode :搜寻文件权限『必须要全部囊括 mode 的权限』的文件,举例来说,
                 我们要搜寻 -rwxr--r-- ,亦即 0744 的文件,使用 -perm -0744,
                 当一个文件的权限为 -rwsr-xr-x ,亦即 4755 时,也会被列出来,
                 因为 -rwsr-xr-x 的属性已经囊括了 -rwxr--r-- 的属性了。
   -perm +mode :搜寻文件权限『包含任一 mode 的权限』的文件,举例来说,我们搜寻
                 -rwxr-xr-x ,亦即 -perm +755 时,但一个文件属性为 -rw-------
                 也会被列出来,因为他有 -rw.... 的属性存在!

范例五:找出档名为 passwd 这个文件
[root@www ~]# find / -name passwd
# 利用这个 -name 可以搜寻档名啊!

范例六:找出 /var 目录下,文件类型为 Socket 的档名有哪些?
[root@www ~]# find /var -type s
# 这个 -type 的属性也很有帮助喔!尤其是要找出那些怪异的文件,
# 例如 socket 与 FIFO 文件,可以用 find /var -type p 或 -type s 来找!

范例七:搜寻文件当中含有 SGID 或 SUID 或 SBIT 的属性
[root@www ~]# find / -perm +7000 
# 所谓的 7000 就是 ---s--s--t ,那么只要含有 s 或 t 的就列出,
# 所以当然要使用 +7000 ,使用 -7000 表示要含有 ---s--s--t 的所有三个权限,
# 因此,就是 +7000 ~了乎?
(1) find additional action

  • {} Represents the "find found by the content" as shown above, find the result is placed in position {};
  • -exec until; keyword, find additional action on behalf of start (-exec) to the end (;), additional action in the middle is the find command. In this case, is the "ls -l {}" Lo!
  • Because ";" at the bash environment is of special significance, so use the backslash to escape.

Guess you like

Origin www.cnblogs.com/hq82/p/11539967.html