Linux Shell Scripting Cookbook(Third Edition)
上QQ阅读APP看书,第一时间看更新

How to do it...

To list all the files and folders descending from a given directory, use this syntax:

$ find base_path

The base_path can be any location from which find should start descending (for example, /home/slynux/).

Here's an example of this command:

$ find . -print
.history
Downloads
Downloads/tcl.fossil
Downloads/chapter2.doc

The . specifies the current directory and .. specifies the parent directory. This convention is followed throughout the Unix filesystem.

The print option separates each file or folder name with a \n (newline). The -print0 option separates each name with a null character '\0'. The main use for -print0 is to pass filenames containing newlines or whitespace characters to the xargs command. The xargs command will be discussed in more detail later:

$> echo "test" > "file name"
$> find . -type f -print | xargs ls -l
ls: cannot access ./file: No such file or directory
ls: cannot access name: No such file or directory
$> find . -type f -print0 | xargs -0 ls -l
-rw-rw-rw-. 1 user group 5  Aug 24 15:00 ./file name