Talk:Find

From Wikipedia, the free encyclopedia

Contents

[edit] Linux only example

I've taken this edited text from the article for comments, because it only works for Linux. I've tested it on AIX, Solaris, and HP-UX. This is what I got, instead of the whole structure.

0       ./outputDirectory/{}
0       ./outputDirectory

[edit] Recreate a directory structure (without files)

This command finds the directories (-type d) within the current directory (.) and makes the new directories in the '~/outputDirectory'. The tilde (~) represents your home directory (e.g. /home/joeBloggs).

find . -type d -exec mkdir -p ~/outputDirectory/{} \;

My question is, should this be redone to work in all Unix flavors, or just left out? Gbeeker 12:31, 17 February 2006 (UTC)

This appears to be related to the shell, rather than the OS. On bash, it works. On tcsh, it does not. This one works for tcsh:
find . -type d -exec mkdir -p ~/outputDirectory/'{}' \;
The man page actually warns that both {} and ; may need escaping from the shell. Maybe we can just leave this example out, as the article already contains examples about executing a command for all files in a directory. - Liberatore(T) 15:10, 17 February 2006 (UTC)
I thought that the ~user home directory was worth mentioning, so I worked it into another example. I also mentioned a bit about quoting, in case a space is a search term. The escaping ; and {} from the shell I did leave out. The Korn shell (ksh) allows escaping with a \ or single quotes. Unixguy 15:21, 20 March 2006 (UTC)

[edit] Tried to add

I tried to add

[edit] Search current directory for files modified less than 4 days ago and print long

find . -mtime +4 -exec ls -l '{}' ';'

[edit] Search for files modified more than 20 days ago and list long, oldest first

find . -mtime -20 -exec ls -lrt '{}' ';' but soon found to my dismay that my quest to deduce how you would do either of these in Linux still eludes me. They yield what seem to be files in the directory with random dates. --previous unsigned comments added by User:68.123.188.129.

Both searches includes the current directory (.) Therefore, "ls -l {} ;" actually includes a listing of the current directory. The second command do not work also because when "ls -ltr" is run, its argument is already a single file, so this is equivalent to "ls -l". - Liberatore(T) 11:50, 7 March 2006 (UTC)
You would have 2 options to get the files modified more than 20 days ago. First note that the find command scans the directory structure, and the ls command is run once for each file found, so the sort options for ls (-t and -r) have no effect, as Liberatore noted above.
Add -type f finds only types of 'file', and not directories.
find . -type f -mtime -20 -exec ls -l '{}' ';'
Or use the -d option to ls which will show the directory inode details, not the contents of the directory itself.
find . -mtime -20 -exec ls -ld '{}' ';'
So, it is actually difficult to get these files sorted by date, but there are ways to do it. You may want to search the newsgroup comp.unix.shell, and see the FAQ for examples. --Unixguy 12:00, 24 April 2006 (UTC)

[edit] Formated printing with Find

is it possible to format the output of the -print switch for the find command? Abountu 17:27, 18 September 2006 (UTC)