Archive for 三月, 2008
在Shell里列出文件名和路径的几种方法
关于如何列出指定路径里的文件和相对路径,这个问题看上去很简单。其实还是有一些技巧的。简单的用ls命令得出的输出格式,有的时候不是我们想要的。
用”ls -R”显示出来的结果格式是这样:
$ ls -R sources
sources:
1.2 1.txt 2.txt one temp tmp
sources/1.2:
sources/one:
two
sources/one/two:
x.txt y.txt z.txt
sources/temp:
虽然这样的格式,看起来比较清晰,不过要是进一步做处理的话就不太方便了。比如,我们要把一个目录的文件和它所在的路径都找出来,并且不显示空目录的话,用”ls -R”就不行了。
这是我们就可以用find命令。
$ find ./sources ! -type d -print
./sources/1.txt
./sources/2.txt
./sources/one/two/x.txt
./sources/one/two/y.txt
./sources/one/two/z.txt
./sources/tmp
这种格式,处理起来就方便多了。
下面是用find来显示文件路径和文件名的几种常用组合:
find / -type d -print (只显示路径)
find / -type l -print (只显示soft link)
find / -type b -print (只显示block special files)
find / -type f -print (只显示普通的文件)
另外,也可以用!来加以修饰,
find / ! -type d -print (列出目录以外的所有东西)
参考:
http://www.unix.com/unix-advanced-expert-users/35349-list-all-files-full-path-file.html

