linuxdot.org Linux Expo Birmingham 2002
Linux news | Newbie's Linux manual | Linux links | Link us
The Linux columns | Book reviews
  DistroWatch + TuxReports October 28, 2002

Contents | Previous | Next | Download

Finding files

$ find

The find command finds files and directories, matching a specified criteria. Many things can be done with find, making it appear intimidating. But in practise, the first two examples given, will be what you use most.


Finding files by name

find . -name '*.txt'
Find all files ending with .txt in the current directory.


find ~ -iname '*dex*'
Find all files in your home directory (~), ignoring case (-iname), with dex anywhere in the filename ('*dex*').


Finding files by size

find ~/work -empty
Find all empty files in the ~/work directory.


find /usr/bin -size +1024k
Find files in the /usr/bin directory, over one megabyte in size (-size +1024k).


find /usr/bin -size +1024k -size -2048k
Find files in the /usr/bin directory, over one megabyte (-size +1024k), and under two megabytes (-size -2048k).


Running commands on files found

With the -exec option, you can run a command on each file found. '{}' indicates where you want the result to go (i.e. anywhere a filename would go as part of a command), and ';' indicates the end of the command to be run.

For confirmation before running a command on each file found, use -ok instead of -exec.


find /usr/bin -size +1024k -size -2048k -exec ls -lh '{}' ';'
Same as last example, but each result ('{}') is to be listed (ls) in long (-l) human-readable (-h) form.


find ~ -name '*temp*' -ok rm '{}' ';'
Find files in your home directory (~), with 'temp' somewhere in the filename ('*temp*'), and confirm removal (-ok rm '{}' ';').


Finding files by owner

find ~ -user root
Find files in your home (~) directory, belonging to root (-user root).


find /site -group columnists
Find files in the /site directory, belonging to the columnists group (-group columnists).


Finding files by modification time

find ~/work -mtime -1
Find files in the ~/work directory, modified less than 24 hours ago (-mtime -1).


find ~/work -mtime +7
Find files in the ~/work directory, modified over a week ago (-mtime +7).


find reports -mmin -5
Find files in reports directory, modified less than 5 minutes ago.


find reports -mmin +60 -mmin -180
Find files in reports directory, modified over an hour ago, and under 3 hours ago.


find reports -newer reports/sales.doc
Find files in the reports directory, modified later than the file, reports/sales.doc.


find ~/mywork -used +200
Find files that haven't been accessed for over 200 days since last being modified.

Contents | Previous | Next | Download

About us  
Latest stable kernel: 2.4.19 | Latest development kernel: 2.5.44
Copyright © 1998-2002 Linuxdot.org.
Linux ® is a registered trademark of Linus Torvalds.