基础知识
文件都有文件名与数据,数据在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@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 -rf dir1/ <----数据无价,强制删除需谨慎。
使用 mv 命令实现类似 windows 的 ctrl + x 剪切功能。 mv 命令还可实现 windows 的重命名功能。
数据无价,本操作可能导致数据丢失。请做好数据备份!
mv - move (rename) files
常用方式:
-
重命名,
mv dir1/file1 dir2/file2
,如果已有dir2/file2的存在,则就像cp命令那样,有覆盖提示(-i)、更新(-u)、强制覆盖(-f)等一样的选项。 -
移动单个文件/目录(即剪切),
mv file1 dir1
-
移动多个文件/目录到同一个目录,
mv -t dest_dir file1 file2...
1. 创建三个文件 sunnogo@a3e420:~/test/mv$ echo "first file" > file1 <----创建文件的方式有很多种,touch只是其中一种。 sunnogo@a3e420:~/test/mv$ cat file1 first file sunnogo@a3e420:~/test/mv$ ls file1 sunnogo@a3e420:~/test/mv$ echo "second file" > file2 sunnogo@a3e420:~/test/mv$ echo "third file" > file3 sunnogo@a3e420:~/test/mv$ ls file1 file2 file3 2. 重命名文件 没有覆盖的情况: sunnogo@a3e420:~/test/mv$ mv file1 file4 sunnogo@a3e420:~/test/mv$ cat file4 first file sunnogo@a3e420:~/test/mv$ ls file2 file3 file4 有覆盖的情况: sunnogo@a3e420:~/test/mv$ mv file4 file1 sunnogo@a3e420:~/test/mv$ mv file1 file2 sunnogo@a3e420:~/test/mv$ ls file2 file3 sunnogo@a3e420:~/test/mv$ cat file2 first file 有覆盖提示的情况: sunnogo@a3e420:~/test/mv$ mv -i file2 file3 mv: overwrite ‘file3’? y sunnogo@a3e420:~/test/mv$ sunnogo@a3e420:~/test/mv$ ls file3 sunnogo@a3e420:~/test/mv$ cat file3 first file sunnogo@a3e420:~/test/mv$ 3. 移动多个文件到同一个目录 sunnogo@a3e420:~/test/mv$ mv file3 file1 sunnogo@a3e420:~/test/mv$ echo "second file" > file2 sunnogo@a3e420:~/test/mv$ echo "third file" > file3 sunnogo@a3e420:~/test/mv$ sunnogo@a3e420:~/test/mv$ ls file1 file2 file3 sunnogo@a3e420:~/test/mv$ mkdir dir sunnogo@a3e420:~/test/mv$ mv -t dir/ file1 file2 file3 sunnogo@a3e420:~/test/mv$ ls dir sunnogo@a3e420:~/test/mv$ ls dir/ file1 file2 file3 sunnogo@a3e420:~/test/mv$
tar 为 GNU 打包工具,目前在标准 Linux 发行版上已经整合进压缩/解压缩工具 gzip、bzip2 和 xz。
-
打包,后缀为
.tar
,称为 tarfile -
打包 + 压缩,后缀为
.tar.gz
、.tar.bz2
、.tar.xz
,称为 tarball。此为 linux 最常用的压缩、解压缩方式。
windows 中常见的压缩包有 .zip
和 .rar
,可通过 unzip
和 unrar
两个工具进行解压。通过 apt-get install 安装即可。
-
压缩
-
gzip,
tar -zcvf xxx.tar.gz ./*
-
bzip2,
tar -jcvf xxx.tar.bz2 ./*
-
xz,
tar -Jcvf xxx.tar.xz ./*
-
-
解压缩,
tar -xvf xxx.tar.gz
-
查看压缩包内容,
tar -tf xxx.tar.gz
以 p4factory 为例,log 如下。
压缩前大小:
sunyongfeng@openswitch-OptiPlex-380:~/workshop/p4factory$ du -hd 0 7.0M .
压缩:
sunyongfeng@openswitch-OptiPlex-380:~/workshop/p4factory$ sunyongfeng@openswitch-OptiPlex-380:~/workshop/p4factory$ tar -zcvf p4factory.tar.gz ./* ./apps/ ./apps/int/ ./apps/int/monitor/ ./apps/int/monitor/preprocessor.py ./apps/int/monitor/monitor.py ./apps/int/monitor/client_msg_handler.py ./apps/int/monitor/topology.json ./apps/int/monitor/client/ ./apps/int/monitor/client/index.html ./apps/int/monitor/client/styles/ ./apps/int/monitor/client/styles/main.css ./apps/int/monitor/client/lib/ ./apps/int/monitor/client/lib/cola.v1.min.js ...
压缩后大小:
sunyongfeng@openswitch-OptiPlex-380:~/workshop/p4factory$ ls -alh p4factory.tar.gz -rw-rw-r-- 1 sunyongfeng sunyongfeng 771K 4月 17 14:26 p4factory.tar.gz
解压过程:
sunyongfeng@openswitch-OptiPlex-380:~/workshop/test-tar$ tar xvf ../p4factory/p4factory.tar.gz ./apps/ ./apps/int/ ./apps/int/monitor/ ./apps/int/monitor/preprocessor.py ./apps/int/monitor/monitor.py ./apps/int/monitor/client_msg_handler.py ./apps/int/monitor/topology.json ./apps/int/monitor/client/ ./apps/int/monitor/client/index.html ./apps/int/monitor/client/styles/ ./apps/int/monitor/client/styles/main.css ./apps/int/monitor/client/lib/ ./apps/int/monitor/client/lib/cola.v1.min.js ...
ls命令主要用于查看文件的属性信息。Linux文件有以下几种主要属性。
$ ls -l drwxr-xr-x 4 cliff user 1024 Jun 18 09:40 WAITRON_EARNINGS -rw-r--r-- 1 cliff user 767392 Jun 6 14:28 scanlib.tar.gz ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ | | | | | | | | | | | | | | | | owner group size date time name | | | | number of links to file or directory contents | | | permissions for world | | permissions for members of group | permissions for owner of file: r = read, w = write, x = execute -=no permission type of file: - = normal file, d=directory, l = symbolic link, and others...
-
文件名
-
文件类型,如普通文件、目录文件、软链接文件、字符设备文件、设设备文件、socket文件等。
-
文件大小,单位可以是Byte、KByte、MByte、GByte或者block。
-
是否隐藏文件,linux的隐藏文件的定义较为简单,文件名以“.”的文件都叫隐藏文件。ls命令默认不会输出隐藏文件的信息。
-
文件权限,每三种用户类型:所属者(owner)、所属组(group)、其他人(other),每种用户有三种权限:可读、可写、可执行,可使用3 × 3 bits表示某种类型用户的某种权限。
-
owner信息,一般是名称或用户编号。
-
group信息,一般是名称或组编号。
-
时间属性,含Access time、modify time、change time,分别以atime、mtime、ctime表示。
-
access time,访问时间,读一次这个文件的内容,这个时间就会更新。比如对这个文件运用 more、cat等命令。ls、stat命令都不会修改文件的访问时间。
-
modify time,修改时间是文件内容最后一次被修改时间。比如:vi后保存文件。ls -l列出的时间就是这个时间。
-
change time,状态改动时间。是该文件的i节点最后一次被修改的时间,通过chmod、chown命令修改一次文件属性,这个时间就会更新。inode的概念将于《inode学习》一文详述。
-
注意:Linux中什么都是文件,目录也只是一种文件。这个观点始终贯穿Linux操作系统。
cp - copy files and directories
命令语法:cp [option] source destination
基本参数
cp的参数较多,一般使用
-a
参数即可完成大多数的拷贝操作需求。个人亦喜欢使用-aux
参数查看文件的更新情况。
-a
,等于-dpR,归档文件,并保留它们的属性。即整个目录保留属性复制
-u
,update,仅在源文件比目标文件新的情况下复制(相当于更新)
-v
,verbose,详细模式,解释到底发生了什么。这个参数在其他命令中也很常见。sunnogo@a3e420:~/test/script$ cp -auv ../prof.c . ‘../prof.c’ -> ‘./prof.c’ sunnogo@a3e420:~/test/script$【进阶】其他常见参数
-f
,force,强制覆盖已存在的目标文件,不提示。这个参数在其他命令中也很常见。
-l
,hard link,创建文件链接,而非复制。
-s
,symbolic link,创建一个符号链接而非复制文件。
-p
,preserve,保存属性信息,如owership、timestamp等
-R
,recursive,递归复制,复制目录需要加上这个选项。
-x
,仅限于当前文件系统的复制。sunnogo@a3e420:~/test/script$ ls -li total 4 7340612 -rwxrwxr-x 1 sunnogo sunnogo 94 6月 21 2013 if.sh sunnogo@a3e420:~/test/script$ sunnogo@a3e420:~/test/script$ cp -l if.sh link.sh sunnogo@a3e420:~/test/script$ ls -li total 8 7340612 -rwxrwxr-x 2 sunnogo sunnogo 94 6月 21 2013 if.sh 7340612 -rwxrwxr-x 2 sunnogo sunnogo 94 6月 21 2013 link.sh sunnogo@a3e420:~/test/script$ sunnogo@a3e420:~/test/script$ cp -s if.sh symbol.sh sunnogo@a3e420:~/test/script$ ls -li total 8 7340612 -rwxrwxr-x 2 sunnogo sunnogo 94 6月 21 2013 if.sh 7340612 -rwxrwxr-x 2 sunnogo sunnogo 94 6月 21 2013 link.sh 7340881 lrwxrwxrwx 1 sunnogo sunnogo 5 2月 23 21:16 symbol.sh -> if.sh注意cp -l和cp -s的差异,体现在ls -li中显示的inode是否一致和文件的硬链接总数。关于软、硬链接,在《Linux命令行创建“快捷方式” - ln》一文描述。
使用 find 命令进行文件或目录搜索。
基础用法
查找本目录下特定名称
find . -name filename
可用于查找本目录下一个特定名称的文件。.
表示待搜索目录路径(这里指当前目录),filename
表示待查找的文件。下文几乎所有的例子的path都使用当前路径。查找特定文件hello.c sunnogo@a3e420:~/test$ find . -name hello.c ./hello/hello.c查找特定后缀的文件
上述
filename
支持通配符,因此可简单实现按类型查找文件等功能。例如find . -name *.c
查找后缀为.c的文件 sunnogo@a3e420:~/test$ find . -name *.c ./script/prof.c ./hello/hello.c ./tdef/test.c ./cmd_line/config_file/parse_conf.c ./config_file/parse_conf.c查找模糊文件名的文件
在使用过程中,大家可能不记得某个文件的全名,但是大概记得文件名的某一段关键字符。这时通配符就又被派上用场了:
find . -name *parse*
查找名字带parse的文件 sunnogo@a3e420:~/test$ find . -name *parse* ./cmd_line/config_file/parse_conf ./cmd_line/config_file/parse_conf.c ./config_file/parse_conf ./config_file/parse_conf.c搜索文件与目录 一节描述文件与目录查找命令 find 的简单使用方法,find 命令的功能很强大,这里介绍其他进阶使用方法。
进阶用法
如果find命令的参数后接的数值,则有如下几种表示方式:
+n
,表示大于n
-n
,表示小于n
n
,表示等于n按大小查找文件
find . [-name filename] -size [+|-]n[b|c|w|k|M|G]
其中:
-size
,表示只输出文件大小大于/小于/等于特定值n的文件大小的单位如下,注意此处单位的大小写,k是小写的。
b表示512字节的块
c表示字节
w表示双字节
k表示1024字节
M表示1024kB
G表示1024MB
例子中用到的-ls参数是非posix标准参数,这里只是为了更好展示查找结果才使用。
查找当前目录及子目录中,所有文件大小大于4KB的文件 sunnogo@a3e420:~/test$ find . -size +4k -ls 8127133 8 -rwxrwxr-x 1 sunnogo sunnogo 7292 Dec 20 22:00 ./hello/hello 8130650 20 -rw-rw-r-- 1 sunnogo sunnogo 17967 Jul 26 2013 ./hello/hello.i 7219878 12 -rw-r--r-- 1 sunnogo sunnogo 12288 Jul 24 2013 ./.hello.c.swp 7209916 8 -rwxrwxr-x 1 sunnogo sunnogo 7122 Jan 17 2013 ./tdef/test 7340928 8 -rwxrwxr-x 1 sunnogo sunnogo 7702 Mar 19 2013 ./cmd_line/config_file/parse_conf 7996916 8 -rwxrwxr-x 1 sunnogo sunnogo 7702 Mar 19 2013 ./config_file/parse_conf 查找当前目录及子目录中,所有文件大小小于4KB的文件 sunnogo@a3e420:~/test$ find . -size -4k -ls 7340612 4 -rwxrwxr-x 3 sunnogo sunnogo 94 Jun 21 2013 ./script/link.sh 7340612 4 -rwxrwxr-x 3 sunnogo sunnogo 94 Jun 21 2013 ./script/if.sh 7340612 4 -rwxrwxr-x 3 sunnogo sunnogo 94 Jun 21 2013 ./script/lnlink.sh 7340881 0 lrwxrwxrwx 1 sunnogo sunnogo 5 Feb 23 2014 ./script/symbol.sh -> if.sh 7340895 0 lrwxrwxrwx 1 sunnogo sunnogo 5 Feb 23 2014 ./script/lnsymbol.sh -> if.sh 7340899 4 -rw-rw-r-- 1 sunnogo sunnogo 519 Nov 12 2013 ./script/prof.c ... 查找当前目录及子目录文件中,所有文件大小等于4KB的文件。 sunnogo@a3e420:~/test$ find . -size 4k -ls 7209663 4 drwxrwxr-x 11 sunnogo sunnogo 4096 Dec 26 00:22 . 7340537 4 drwxrwxr-x 2 sunnogo sunnogo 4096 Feb 23 2014 ./script 8130648 4 drwxrwxr-x 2 sunnogo sunnogo 4096 Dec 20 22:00 ./hello 7342618 4 drwxr-xr-x 3 sunnogo sunnogo 4096 Feb 23 2014 ./mv 7342622 4 drwxr-xr-x 2 sunnogo sunnogo 4096 Feb 23 2014 ./mv/dir 7341100 4 drwxr-xr-x 2 sunnogo sunnogo 4096 Feb 23 2014 ./touch ...按权限查找文件
Linux文件权限的格式有两种,这在[ls][1]一文的“查看文件权限信息”中有详细说明。每个文件供给三种用户使用(owner、group、other),每种用户有可读(readable)、可写(writable)、可执行(executable)三种权限。因此一个文件有九种基本权限。
数值格式。假设有个文件的权限是“751”,则“7”表示文件的owner可读、可写、可执行,“5”表示文件所属group可读、可执行,“1”表示other用户可执行。
符号格式,以r表示可读、w表示可写、x表示可执行。一般配以o表示owner、g表示group、o表示other。
find命令按文件权限查找文件有两种格式:
find -perm -mode
,All of the permission bits mode are set for the file,即目标文件的权限要匹配到mode中所有允许的权限,但未涉及的权限可有可无;
find -perm /mode
,Any of the permission bits mode are set for the file,即查找到的文件只要匹配到mode中允许的一种权限即可;如下例中的
find . -perm -700
表示查找权限至少是owner可读、可写、可执行的文件。如ls
显示的结果,只有文件hello满足此条件。注意文件hello还包含group可读、可写可执行权限以及other的可读、可执行权限。find . -perm /700
的结果与find . -perm -700
的结果就有明显差异,几乎把本目录中的所有文件都打出来了。sunnogo@a3e420:~/test/hello$ ls -al total 48 drwxrwxr-x 2 sunnogo sunnogo 4096 Dec 20 22:00 . drwxrwxr-x 11 sunnogo sunnogo 4096 Dec 26 00:22 .. -rwxrwxr-x 1 sunnogo sunnogo 7292 Dec 20 22:00 hello -rw-rw-r-- 1 sunnogo sunnogo 104 Jul 24 2013 hello.c -rw-rw-r-- 1 sunnogo sunnogo 0 Dec 20 22:00 .hello.hide -rw-rw-r-- 1 sunnogo sunnogo 17967 Jul 26 2013 hello.i -rw-rw-rw- 1 sunnogo sunnogo 1028 Jul 26 2013 hello.o -rw-rw-r-- 1 sunnogo sunnogo 491 Jul 26 2013 hello.s sunnogo@a3e420:~/test/hello$ find . -perm -700 -ls 8130648 4 drwxrwxr-x 2 sunnogo sunnogo 4096 Dec 20 22:00 . 8127133 8 -rwxrwxr-x 1 sunnogo sunnogo 7292 Dec 20 22:00 ./hello sunnogo@a3e420:~/test/hello$ find . -perm /700 -ls 8130648 4 drwxrwxr-x 2 sunnogo sunnogo 4096 Dec 20 22:00 . 8127133 8 -rwxrwxr-x 1 sunnogo sunnogo 7292 Dec 20 22:00 ./hello 8127134 0 -rw-rw-r-- 1 sunnogo sunnogo 0 Dec 20 22:00 ./.hello.hide 8131706 4 -rw-rw-rw- 1 sunnogo sunnogo 1028 Jul 26 2013 ./hello.o 8130651 4 -rw-rw-r-- 1 sunnogo sunnogo 491 Jul 26 2013 ./hello.s 7219321 4 -rw-rw-r-- 1 sunnogo sunnogo 104 Jul 24 2013 ./hello.c 8130650 20 -rw-rw-r-- 1 sunnogo sunnogo 17967 Jul 26 2013 ./hello.i sunnogo@a3e420:~/test/hello$ find . -perm /003 -ls 8130648 4 drwxrwxr-x 2 sunnogo sunnogo 4096 Dec 20 22:00 . 8127133 8 -rwxrwxr-x 1 sunnogo sunnogo 7292 Dec 20 22:00 ./hello 8131706 4 -rw-rw-rw- 1 sunnogo sunnogo 1028 Jul 26 2013 ./hello.omode也可以使用符号格式,如下例子使用符号格式实现
find . -perm -700与find . -perm /003
。等同“find . -perm -700” sunnogo@a3e420:~/test/hello$ find . -perm -u+r,u+w,u+x -ls 8130648 4 drwxrwxr-x 2 sunnogo sunnogo 4096 Dec 20 22:00 . 8127133 8 -rwxrwxr-x 1 sunnogo sunnogo 7292 Dec 20 22:00 ./hello 等同“find . -perm /003” sunnogo@a3e420:~/test/hello$ find . -perm /o+w,o+x -ls 8130648 4 drwxrwxr-x 2 sunnogo sunnogo 4096 Dec 20 22:00 . 8127133 8 -rwxrwxr-x 1 sunnogo sunnogo 7292 Dec 20 22:00 ./hello 8131706 4 -rw-rw-rw- 1 sunnogo sunnogo 1028 Jul 26 2013 ./hello.o按时间查找文件
Linux文件相关时间属性详见[ls][1]一文中“常见文件属性信息汇总”小节的描述。
按访问时间查找(access time,atime)
-amin n
,查找在此前第[n - 1, n]分钟的期间被访问的文件(当n不等于0时为如上结论,如果n等于0,默认无输出,该文件的access time是否在未来的时间)。
-anewer file
,查找access time比文件file晚的文件
-atime n
,查找在此前第[(n - 1)× 24, n × 24]小时的期间被访问的文件注意本章一开头对find的数值参数n的描述,+n表示大于n的情况,-n表示小于n的情况,同样适合于本小节所描述的参数。用于实现查找某个时间点被访问以前或之后的文件,这才是我们最想要的用途。注意此时的时间不再是个区间,而就是个时间点。具体用法如下例所述。
上述的时间期间可能不好理解,详见本文后面的小节“理解按时间查找文件中时间期间的含义”。
sunnogo@a3e420:~/test/hello$ touch -at 12262300 hello.i sunnogo@a3e420:~/test/hello$ stat hello.i | grep "Access: 2014" Access: 2014-12-26 23:00:00.000000000 +0800 sunnogo@a3e420:~/test/hello$ date Fri Dec 26 23:14:08 CST 2014 sunnogo@a3e420:~/test/hello$ find . -amin 15 ./hello.i sunnogo@a3e420:~/test/hello$ find . -amin -14 sunnogo@a3e420:~/test/hello$ find . -amin -15 ./hello.i
按状态变动时间查找(change time,ctime),命令与access time类似
-cmin n
-cnewer file
-ctime n
按修改时间查找(modify time,mtime)
-mmin n
-mnewer file
,-newer file
看似功能与本选项一致。
-mtime n
按文件类型查找文件
Linux文件相关时间属性详见[ls][1]一文中“常见文件属性信息汇总”小节的描述。
find PATH -type file_type
,文件类型file_type包含如下几种:
b,块设备文件
c,字符设备文件
d,目录文件
p,有名管道文件
f,普通文件
l,符号链接文件
s,socket文件
以下是样例:
sunnogo@a3e420:~/test$ find . -type d -ls 7209663 4 drwxrwxr-x 11 sunnogo sunnogo 4096 Dec 26 00:22 . 7340537 4 drwxrwxr-x 2 sunnogo sunnogo 4096 Feb 23 2014 ./script 8130648 4 drwxrwxr-x 2 sunnogo sunnogo 4096 Dec 20 22:00 ./hello ... sunnogo@a3e420:~/test$ find . -type l -ls 7340881 0 lrwxrwxrwx 1 sunnogo sunnogo 5 Feb 23 2014 ./script/symbol.sh -> if.sh 7340895 0 lrwxrwxrwx 1 sunnogo sunnogo 5 Feb 23 2014 ./script/lnsymbol.sh -> if.sh多条件查找
find
命令支持运算符,允许多个expression进行计算。主要运算符有:
()
,和四则运算一样,表示优先执行括号内的表达式;
! expr
,类似逻辑运算的not,表示对表达式expr的结果取反;
expr1 -a expr2
,类似逻辑运算的and,表示expr1和expr2进行与运算。类似C语言的短路求值(short-circuit evalution),若expr1为假,就不会计算expr2;
expr1 -o expr2
,类似逻辑运算的or,表示expr1和expr2进行或运行。同样使用短路求值,若expr1为真,则不会计算expr2事实上
find
命令也有-not
、-and
、-or
选项等同于上述三种运算符,但是这三种运算符是非POSIX兼容的,使用时需要注意。sunnogo@a3e420:~/test$ ls -al hello/ total 48 drwxrwxr-x 2 sunnogo sunnogo 4096 Dec 20 22:00 . drwxrwxr-x 12 sunnogo sunnogo 4096 Dec 26 23:27 .. -rwxrwxr-x 1 sunnogo sunnogo 7292 Dec 20 22:00 hello -rw-rw-r-- 1 sunnogo sunnogo 104 Jul 24 2013 hello.c -rw-rw-r-- 1 sunnogo sunnogo 0 Dec 20 22:00 .hello.hide -rw-rw-r-- 1 sunnogo sunnogo 17967 Dec 26 23:02 hello.i -rw-rw-rw- 1 sunnogo sunnogo 1028 Jul 26 2013 hello.o -rw-rw-r-- 1 sunnogo sunnogo 491 Jul 26 2013 hello.s 查找文件名为"hello"开头、且至少有一种用户有写权限的文件 sunnogo@a3e420:~/test$ find . -name "hello*" -a -perm /a+w -ls 8130648 4 drwxrwxr-x 2 sunnogo sunnogo 4096 Dec 20 22:00 ./hello 8127133 8 -rwxrwxr-x 1 sunnogo sunnogo 7292 Dec 20 22:00 ./hello/hello 8131706 4 -rw-rw-rw- 1 sunnogo sunnogo 1028 Jul 26 2013 ./hello/hello.o 8130651 4 -rw-rw-r-- 1 sunnogo sunnogo 491 Jul 26 2013 ./hello/hello.s 7219321 4 -rw-rw-r-- 1 sunnogo sunnogo 104 Jul 24 2013 ./hello/hello.c 8130650 20 -rw-rw-r-- 1 sunnogo sunnogo 17967 Dec 26 23:02 ./hello/hello.i 查找文件名为"hello"开头、且所有用户都有写权限的文件 sunnogo@a3e420:~/test$ find . -name "hello*" -a -perm -a+w -ls 8131706 4 -rw-rw-rw- 1 sunnogo sunnogo 1028 Jul 26 2013 ./hello/hello.o 查找文件名为"hello"开头、且不是所有用户都有写权限的文件 sunnogo@a3e420:~/test$ find . -name "hello*" -a ! -perm -a+w -ls 8130648 4 drwxrwxr-x 2 sunnogo sunnogo 4096 Dec 20 22:00 ./hello 8127133 8 -rwxrwxr-x 1 sunnogo sunnogo 7292 Dec 20 22:00 ./hello/hello 8130651 4 -rw-rw-r-- 1 sunnogo sunnogo 491 Jul 26 2013 ./hello/hello.s 7219321 4 -rw-rw-r-- 1 sunnogo sunnogo 104 Jul 24 2013 ./hello/hello.c 8130650 20 -rw-rw-r-- 1 sunnogo sunnogo 17967 Dec 26 23:02 ./hello/hello.i对查找结果进行处理
即对查找的结果作为参数输入给其他命令。
有以下四种形式的格式,\
只是个转义字符,为防止符号;
或+
在shell中有不同的含义。
-exec command "{}" \;
-exec command "{}" \+
-execdir command "{}" \;
-execdir command "{}" \+
目前尚不需理解命令参数结束符
;
与+
的差异以及exec
与execdir
的差异。sunnogo@a3e420:~/test$ mkdir exec sunnogo@a3e420:~/test$ cd exec sunnogo@a3e420:~/test/exec$ echo "abcdefg" > abc.txt sunnogo@a3e420:~/test/exec$ echo "xyzabc" > xyz.txt sunnogo@a3e420:~/test/exec$ echo "bcdefgh" > bcd.txt sunnogo@a3e420:~/test/exec$ mkdir exec_sub sunnogo@a3e420:~/test/exec$ cd exec_abc/ sunnogo@a3e420:~/test/exec/exec_abc$ echo "abcdefghijkl" > bck.txt sunnogo@a3e420:~/test/exec/exec_abc$ cd .. sunnogo@a3e420:~/test/exec$ find . -name "*bc*" ./bcd.txt ./exec_abc ./exec_abc/bck.txt ./abc.txt sunnogo@a3e420:~/test/exec$ find . -name "*bc*" -exec cat "{}" \+ bcdefgh cat: ./exec_abc: Is a directory abcdefghijkl abcdefg按格式输出查找结果
使用
fprint
参数实现,相对的内容较多但是不重要,在此不详细,需要的同学使用man find
看一下。使用正则表达式匹配文件名
-regex pattern
文件名可以用正则表达式匹配,这里不详述。其他参数
还可以根据硬连接数、inode信息、owner信息等查找文件
理解按时间查找文件中时间期间的含义
“查找在此前第[n-1, n]分钟的期间被访问的文件”,不好理解,先看下面的例子:
首先,查看文件hello.i的属性信息,发现access time是22:32:52
sunnogo@a3e420:~/test/hello$ stat hello.i File: ‘hello.i’ Size: 17967 Blocks: 40 IO Block: 4096 regular file Device: 805h/2053d Inode: 8130650 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1000/ sunnogo) Gid: ( 1000/ sunnogo) Access: 2014-12-26 22:32:52.248847993 +0800 Modify: 2014-12-26 22:32:50.072847903 +0800 Change: 2014-12-26 22:32:50.072847903 +0800 Birth: -其次,查看当前时间点,现在是22:57:38,距离hello.i的atime为24分钟46秒。
sunnogo@a3e420:~/test/hello$ date Fri Dec 26 22:57:38 CST 2014第三,看n值为24、25、26时
find . -amin n
的结果。sunnogo@a3e420:~/test/hello$ find . -amin 25 ./hello.i sunnogo@a3e420:~/test/hello$ find . -amin 24 sunnogo@a3e420:~/test/hello$ find . -amin 26接下来看看
find . -amin 0
会不会显示未来时间。实际上最科学的方法是去查找find命令的代码实现,这里仅仅是玩一玩,验证自己觉得可能比较有趣的情况,无需较真。sunnogo@a3e420:~/test/hello$ date Fri Dec 26 23:06:55 CST 2014 sunnogo@a3e420:~/test/hello$ touch -at 12300000 hello.i sunnogo@a3e420:~/test/hello$ stat hello.i | grep "Access: 2014" Access: 2014-12-30 00:00:00.000000000 +0800 sunnogo@a3e420:~/test/hello$ find . -amin 0 sunnogo@a3e420:~/test/hello$通过
chmod
和chown
命令改变权限与所有者。默认的权限为 755(默认 umask 为 022,权限为 777 - umask)。1 --x execute # Mode 764 = exec/read/write | read/write | read 2 -w- write # For: |-- Owner --| |- Group-| |Others| 4 r-- read ugo=a u=user, g=group, o=others, a=everyone样例:
# chmod [OPTION] MODE[,MODE] FILE # MODE is of the form [ugoa]*([-+=]([rwxXst])) # chmod 640 /var/log/maillog # Restrict the log -rw-r----- # chmod u=rw,g=r,o= /var/log/maillog # Same as above # chmod -R o-r /home/* # Recursive remove other readable for all users # chmod u+s /path/to/prog # Set SUID bit on executable (know what you do!) # find / -perm -u+s -print # Find all programs with the SUID bit # chown user:group /path/to/file # Change the user and group ownership of a file # chgrp group /path/to/file # Change the group ownership of a file # chmod 640 `find ./ -type f -print` # Change permissions to 640 for all files # chmod 751 `find ./ -type d -print` # Change permissions to 751 for all directories基础用法
ls -alh
命令查看目录下文件大小,其中目录文件不会递归计算整个目录占用的磁盘空间(此时显示的大小仅仅是目录文件的大小)。
du -hd 0
命令查看当前整个目录的大小。
du -hd 1
查看当前目录所有第一级子目录的大小。样例:
sunyongfeng@openswitch-OptiPlex-380:~/workshop/p4factory$ ls -alh total 120K drwxrwxr-x 14 sunyongfeng sunyongfeng 4.0K 3月 15 10:53 . drwxrwxr-x 42 sunyongfeng sunyongfeng 4.0K 3月 15 10:53 .. drwxrwxr-x 3 sunyongfeng sunyongfeng 4.0K 3月 15 10:53 apps -rwxrwxr-x 1 sunyongfeng sunyongfeng 184 3月 15 10:53 autogen.sh drwxrwxr-x 2 sunyongfeng sunyongfeng 4.0K 3月 15 10:53 cli -rwxrwxr-x 1 sunyongfeng sunyongfeng 213 3月 15 10:53 configure drwxrwxr-x 3 sunyongfeng sunyongfeng 4.0K 3月 15 10:53 docker drwxrwxr-x 8 sunyongfeng sunyongfeng 4.0K 3月 15 10:53 .git -rw-rw-r-- 1 sunyongfeng sunyongfeng 85 3月 15 10:53 .gitignore -rw-rw-r-- 1 sunyongfeng sunyongfeng 844 3月 15 10:53 .gitmodules -rw-rw-r-- 1 sunyongfeng sunyongfeng 4.0K 3月 15 10:53 init.mk -rwxrwxr-x 1 sunyongfeng sunyongfeng 3.7K 3月 15 10:53 install_deps.sh -rw-rw-r-- 1 sunyongfeng sunyongfeng 11K 3月 15 10:53 LICENSE drwxrwxr-x 2 sunyongfeng sunyongfeng 4.0K 3月 15 10:53 makefiles drwxrwxr-x 4 sunyongfeng sunyongfeng 4.0K 3月 15 10:53 mininet drwxrwxr-x 6 sunyongfeng sunyongfeng 4.0K 3月 15 10:53 modules -rw-rw-r-- 1 sunyongfeng sunyongfeng 4.7K 3月 15 10:53 README.md -rwxrwxr-x 1 sunyongfeng sunyongfeng 14K 3月 15 10:53 run_all_tests drwxrwxr-x 10 sunyongfeng sunyongfeng 4.0K 3月 15 10:53 submodules drwxrwxr-x 9 sunyongfeng sunyongfeng 4.0K 3月 22 10:31 targets drwxrwxr-x 2 sunyongfeng sunyongfeng 4.0K 3月 15 10:53 testutils drwxrwxr-x 4 sunyongfeng sunyongfeng 4.0K 3月 15 10:53 tools drwxrwxr-x 2 sunyongfeng sunyongfeng 4.0K 3月 15 10:53 travis -rw-rw-r-- 1 sunyongfeng sunyongfeng 3.5K 3月 15 10:53 .travis.ymlsunyongfeng@openswitch-OptiPlex-380:~/workshop/p4factory$ du -hd 0 7.0M .sunyongfeng@openswitch-OptiPlex-380:~/workshop/p4factory$ du -hd 1 1.8M ./apps 48K ./cli 32K ./docker 40K ./submodules 88K ./tools 2.8M ./.git 48K ./makefiles 20K ./travis 588K ./targets 20K ./testutils 408K ./mininet 1.2M ./modules 7.0M .du 命令详解
在ls命令中,可通过
ls -alh
查看文件的大小,但是此时看到的目录文件大小是Linux目录文件实际的大小,一般是4KB,而不是一般意义的目录总大小,命令du可用于查看目录的总大小。du - estimate file space usage Summarize disk usage of each FILE, recursively for directories.du命令默认递归把本目录以下的所有目录及文件的大小都输出到控制台。如果各目录层次下的文件太多,会导致控制台输出的内容太多。
一般情况下会使用
-d N
参数或者--max-depth=N
来控制du命令递归输出的目录层次数。N表示递归输出到第几级目录。-h
参数是让输出的文件大小值使用Human readable的形式表示,即以KB/MB/GB的形式出现,默认以KB为单位输出。查看本目录总大小
使用0级目录即可。注意与ls -alh的结果对比,ls的结果也有个total总计,但是这个total并不递归累加每个子目录下各个文件的大小,而是把本目录下的子目录当成4KB的普通文件。
sunnogo@a3e420:~/github/hexo$ du -d 0 -h 181M . sunnogo@a3e420:~/github/hexo$ ls -alh total 163M drwxrwxr-x 8 sunnogo sunnogo 4.0K Dec 24 23:09 . drwxrwxr-x 6 sunnogo sunnogo 4.0K Dec 12 22:41 .. -rw-rw-r-- 1 sunnogo sunnogo 2.0K Dec 20 23:13 _config.yml -rw-rw-r-- 1 sunnogo sunnogo 105K Dec 24 00:49 db.json -rw-rw-r-- 1 sunnogo sunnogo 7.3K Dec 13 19:38 debug.log drwxrwxr-x 13 sunnogo sunnogo 4.0K Dec 24 00:49 .deploy -rw-rw-r-- 1 sunnogo sunnogo 68 Dec 12 22:41 .gitignore drwxr-xr-x 5 sunnogo sunnogo 4.0K Dec 12 22:42 node_modules -rw-rw-r-- 1 sunnogo sunnogo 186 Dec 12 22:47 package.json drwxrwxr-x 12 sunnogo sunnogo 4.0K Dec 20 23:19 public drwxrwxr-x 2 sunnogo sunnogo 4.0K Dec 12 22:41 scaffolds drwxrwxr-x 5 sunnogo sunnogo 4.0K Dec 20 22:47 source drwxrwxr-x 4 sunnogo sunnogo 4.0K Dec 12 22:46 themes -rw-rw-r-- 1 sunnogo sunnogo 163M Dec 24 23:08 veryLargeFile.txt查看本目录下各个子目录的总大小
使用一级目录递归输出即可。注意,一级递归输出并没有输出本目录下的普通文件,但是大小已经统计在内。
sunnogo@a3e420:~/github/hexo$ du -d 1 -h 2.5M ./public 20K ./scaffolds 6.3M ./node_modules 4.5M ./themes 32K ./source 5.2M ./.deploy 181M .对输出的结果进行排序
简单地,du命令可与[sort][2]命令配合按大小或文件名输出结果。亦可再配合[head][3]或[tail][4]命令只显示排序前几或倒数前几的结果。
sunnogo@a3e420:~/github/hexo$ du -d 1 | sort -nr 185104 . 6376 ./node_modules 5324 ./.deploy 4592 ./themes 2560 ./public 32 ./source 20 ./scaffolds sunnogo@a3e420:~/github/hexo$ du -d 1 | sort -nr | head -n 3 185104 . 6376 ./node_modules 5324 ./.deploy
sort的
-n
选项表示按数值排序,-r
表示反向排序即从大到小排序,sort默认使用。注意符号
|
,在linux shell中这是一个很重要的概念,名为管道,即将前道工序输出的结果做为后一道工序的输入。head命令显示输入的前n行,tail命令则相反。
只输出大小大于某个特定值的文件
通过
-t SIZE
或--threshold=SIZE
实现本功能,注意 Size的单位是Byte。sunnogo@a3e420:~/github/hexo$ du -d 1 -h -t 4500000 6.3M ./node_modules 4.5M ./themes 5.2M ./.deploy 181M .忽略统计某些特定类型或匹配文件的大小
通过
-X PATTERN
或--exclude=PATTERN
实现本功能。注意PATTERN是支持通配符,并不支持正则表达式。一般用于过滤特定后缀的文件。如果想一次过滤多种PATTERN的文件,可通过多次使用
-X
或--exclude
实现。sunnogo@a3e420:~/github/hexo$ du -hd 1 --exclude '*.js' 2.3M ./public 20K ./scaffolds 2.4M ./node_modules 4.2M ./themes 32K ./source 5.0M ./.deploy 177M . sunnogo@a3e420:~/github/hexo$ du -hd 1 --exclude '*.js' --exclude '*.txt' 2.3M ./public 20K ./scaffolds 2.4M ./node_modules 4.2M ./themes 32K ./source 5.0M ./.deploy 14M .使用 file 命令确认文件类型。
以下样例查看:
普通文件文件
x86 可执行文件
mips 可执行
静态库
动态库
pdf 文件
目录文件
其中查看 elf 文件(可执行文件、动态链接库)需要注意,可通过 file 命令确认:
32 位还是 64 位
LSM 还是 MSB
arch,比如 x86-64 / MIPS32 rel2 等
是否裁剪符号表
sunyongfeng@openswitch-OptiPlex-380:~/workshop/test$ ls -al total 5704 drwxrwxr-x 5 sunyongfeng sunyongfeng 4096 1月 17 11:29 . drwxrwxr-x 42 sunyongfeng sunyongfeng 4096 3月 15 10:53 .. -rwxrwxr-x 1 sunyongfeng sunyongfeng 8600 1月 17 11:21 abc -rw-rw-r-- 1 sunyongfeng sunyongfeng 159 1月 4 13:41 abc.c -rwxrwxr-x 1 sunyongfeng sunyongfeng 7839 1月 17 11:22 abc.mips drwxrwxr-x 10 sunyongfeng sunyongfeng 4096 10月 20 10:41 bitbake -rwxr--r-- 1 sunyongfeng sunyongfeng 3303390 11月 24 14:31 dokuwiki-352d657ccca05d2bc7ec711601560eed.tgz -rwxr--r-- 1 sunyongfeng sunyongfeng 30881 11月 17 10:15 drivers.net.vrf.c -rw-rw-r-- 1 sunyongfeng sunyongfeng 198 11月 17 09:54 NCC.i drwxrwxr-x 7 sunyongfeng sunyongfeng 4096 11月 22 09:55 ops-build -rwxr--r-- 1 sunyongfeng sunyongfeng 1286670 10月 17 17:08 p4-specification.1.0.2.pdf -rwxr--r-- 1 sunyongfeng sunyongfeng 1160313 11月 24 10:42 p4-specification.1.0.3.pdf drwxrwxr-x 5 sunyongfeng sunyongfeng 4096 10月 20 13:41 tutorial sunyongfeng@openswitch-OptiPlex-380:~/workshop/test$ file abc abc: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=ef4d0d9412736424b6aeadf284d6b56102292e00, not stripped sunyongfeng@openswitch-OptiPlex-380:~/workshop/test$ file abc.c abc.c: C source, ASCII text sunyongfeng@openswitch-OptiPlex-380:~/workshop/test$ file abc.mips abc.mips: ELF 32-bit MSB executable, MIPS, MIPS32 rel2 version 1, dynamically linked, interpreter /lib/ld.so.1, for GNU/Linux 2.6.32, not stripped sunyongfeng@openswitch-OptiPlex-380:~$ file /usr/local/lib/libthrift.a /usr/local/lib/libthrift.a: current ar archive sunyongfeng@openswitch-OptiPlex-380:~$ file /usr/local/lib/libthrift-0.9.2.so /usr/local/lib/libthrift-0.9.2.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=d52175122ee7ad353edf11eb5c29305c2a1d926b, not stripped sunyongfeng@openswitch-OptiPlex-380:~/workshop/test$ file p4-specification.1.0.3.pdf p4-specification.1.0.3.pdf: PDF document, version 1.5 sunyongfeng@openswitch-OptiPlex-380:~/workshop/test$ file tutorial/ tutorial/: directory通过 tree 命令查看目录树。ubuntu 默认未带该 app,需要通过
sudo apt-get install tree
安装。用法:
tree -d dir
:只显示dir目录名称,而不显示内容
tree -a dir
:只显示dir目录名称和内容
tree -p dir
:只显示dir目录名称和内容,同时显示权限样例:
root@xie-vm:/home/xie# tree -d test test └── test1 1 directory root@xie-vm:/home/xie# tree -a test test ├── aaa.txt ├── ccc.log └── test1 ├── bbb.log └── ccc.log 1 directory, 4 files root@xie-vm:/home/xie# tree -p test test ├── [-rw-r--r--] aaa.txt ├── [-rw-r--r--] ccc.log └── [drwxr-xr-x] test1 ├── [-rw-r--r--] bbb.log └── [-rw-r--r--] ccc.log 1 directory, 4 files本文命令一览表
ls,list directory content,用来查看文件属性信息。
stat,display file or file system status,查看文件或文件系统状态信息,如文件创建、修改时间等。
du,estimate file space usage,计算文件 / 目录大小
查看文件名
直接敲一下ls命令就能看到当前目录下的所有非隐藏文件。
sunnogo@a3e420:~/test/hello$ ls hello hello.c hello.i hello.o hello.s查看隐藏文件
与 windows 不同,Linux 使用
.filename
表示隐藏文件 带-a
参数,可查看列出隐藏文件。如以下的.hello.hide文件。查看当前目录与上级目录
Linux中使用
.
表示当前目录,使用..
表示上一级目录。看看ls -a
、ls -a .
和ls -a ..
三者有什么差异。sunnogo@a3e420:~/test/hello$ ls -a . .. hello hello.c .hello.hide hello.i hello.o hello.s sunnogo@a3e420:~/test/hello$ ls -a . . .. hello hello.c .hello.hide hello.i hello.o hello.s sunnogo@a3e420:~/test/hello$ ls -a .. . .. cmd_line .config config_file .git .gitignore hello .hello.c.swp mkdir mv rm script tdef touch查看文件大小
ls命令使用
-s
参数查看文件大小,默认以block为单位。使用-h
参数将文件大小的单位转换为human readable的单位,即常见的KB、MB、GB等。ls的
-l
参数,即long list formating,把大多数的文件属性信息都显示出来,因此一般只记-l
参数,而无需去记类似-s
这样的参数。注意-l
参数默认不显示隐藏文件,默认的文件大小以Byte为单位。注意,目录文件的大小始终是4KB,不会去统计目录内部文件的总大小。
sunnogo@a3e420:~/test/hello$ ls -s total 40 8 hello 4 hello.c 20 hello.i 4 hello.o 4 hello.s sunnogo@a3e420:~/test/hello$ ls -sh total 40K 8.0K hello 4.0K hello.c 20K hello.i 4.0K hello.o 4.0K hello.s sunnogo@a3e420:~/test/hello$ ls -alh total 48K drwxrwxr-x 2 sunnogo sunnogo 4.0K Dec 20 22:00 . drwxrwxr-x 14 sunnogo sunnogo 4.0K Apr 20 2014 .. -rwxrwxr-x 1 sunnogo sunnogo 7.2K Dec 20 22:00 hello -rw-rw-r-- 1 sunnogo sunnogo 104 Jul 24 2013 hello.c -rw-rw-r-- 1 sunnogo sunnogo 0 Dec 20 22:00 .hello.hide -rw-rw-r-- 1 sunnogo sunnogo 18K Jul 26 2013 hello.i -rw-rw-r-- 1 sunnogo sunnogo 1.1K Jul 26 2013 hello.o -rw-rw-r-- 1 sunnogo sunnogo 491 Jul 26 2013 hello.s查看目录大小
详见 du 命令。
查看文件权限信息
如上所示,ls 命令
-l
参数的第一列显示文件权限信息,第一位表示文件类型,后九位表示文件权限。 以文件hello为例,其权限信息rwxrwxr-x
表示:
第1到3位
rwx
,对象是文件所属者,所属者对该文件有可读、可写、可执行三种权限;第4到6位
rwx
,对象是文件所属组,组是多用户组成的,所属组对该文件有可读、可写、可执行三种权限。第7到9位
r-x
,对象是其他用户,其他用户对该文件有可读、可执行两种权限,没有权限运行该文件。对文件的属性Linux系统上还有另一种用数值表示的方法,即通过3个bit来表示一种用户的权限,r是最高位,w是第二位,x是最低位,对应位置为1表示用户拥有对应的权限,0表示无此权限。例如,数字6(即110)表示可读、可写两种权限,数字5(即101)表示可读、可执行两种权限。这种表示方法在
chmod
命令中经常使用。查看文件类型
ls 命令
-l
参数即可查看文件类型,第一位的第一位表示文件类型,含目录(d),文件(-),字符型文件(c),块文件(b),软链接文件(l)等。查看文件时间属性
可用
stat
命令查看文的三种时间属性。ls命令也可查看时间属性,默认输出mtime,-c参数表示ctime,-u参数表示atime。
查看ctime,
ls -lc filename
查看atime,
ls -lu filename
查看mtime,
ls -l filename
【进阶】ls 排序相关参数
-t
,以文件的时间排序,默认是最后修改时间,如果有-u选项,则是以上次访问时间排序。
-S
,大写S,以文件大小排序输出。
-u
,小写u,输出文件的最后访问时间,而非最后修改时间。
-X
,大写X,按文件扩展名排序输出。TIPs:
命令行参数一般有单字母参数和全字参数两种形式,比如
ls -a
和ls --all
。单字母通常由英文破折号开始,全字参数则更容易看懂,通常以双英文破折号开始。许多参数都有单字母和全字两种版本,而有些则只有一种。ls命令支持标准通配符,问号代表一个字符,星号代表零或多个字符。
如果不知道某个命令怎么使用,要学会使用man手册,即Linux的帮助手册。通过命令
man your_command
即可查看对应命令的使用方法、参数说明等。