April 2024
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  

Categories

April 2024
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  

FIND Command

FIND Command

Find is a versatile tool which can be used to locate files and directories satisfying different user criteria. But the sheer number of options for this command line tool makes it at the same time both powerful and encumbering for the user. Here I will list a few combinations which one can use to get useful results using find command.

f – file
d – directory
l – symbolic link
c – character
p – named pipe (FIFO)
s – socket
b – block device

Find all HTML files starting with letter ‘a’ in your current directory (Case sensitive)
find . -name a\*.html

Same as above but case insensitive search.
find . -iname a\*.html

Find files which are larger than 5 MB in size.
find . -size +5000k -type f

Here the ‘+’ in ‘+5000k’ indicates greater than and k is kilobytes. And the dot ‘.’ indicates the current directory. The -type option can take any of the following values:

… Which is all files with 0 bytes size. The option -size can take the following:

c – bytes
w – 2 byte words
k – kilo bytes
b – 512 byte blocks

Note: The above command can also take the -empty parameter.

Find is very powerful in that you can combine it with other commands. For example, to find all empty files in the current directory and delete them, do the following:
find . -empty -maxdepth 1 -exec rm {} \;

To search for a html file having the text ‘Web sites’ in it, you can combine find with grep as follows:
find . -type f -iname \*.html -exec grep -s “Web sites” {} \;

… the -s option in grep suppresses errors about non-existent or unreadable files. And {} is a placeholder for the files found. The semicolon ‘;’ is escaped using backslash so as not to be interpreted by bash shell.

Note: You can use the -exec option to combine any command in Linux with the find command. Some of the useful things you can do with it are as follows:

Compress log files on an individual basis
find /var -iname \*.log -exec bzip {} \;

Find all files which belong to user lal and change its ownership to ravi
find / -user lal -exec chown ravi {} \;

Note: You can also use xargs command instead of the -exec option as follows:
find /var -iname \*.log | xargs bzip –

Find all files which do not belong to any user:
find . -nouser

Find files which have permissions rwx for user and rw for group and others :
find . -perm 766

… and then list them.

find . -perm 766 -exec ls -l {} \;

Find all directories with name music_files
find . -type d -iname \*music_files\*

Suppose you want to find files of size between 700k and 1000k, do the following:
find . \( -size +700k -and -size -1000k \)

And how about getting a formatted output of the above command with the size of each file listed ?
find . \( -size +700k -and -size -1000k \) -exec du -Hs {} \; 2>/dev/null

… here, the ‘2>/dev/null’ means all the error messages are discarded or suppressed.

You can also limit your search by file system type. For example, to restrict search to files residing only in the NTFS and VFAT filesystem, do the following:
find / -maxdepth 2 \( -fstype vfat -or -fstype ntfs \) 2> /dev/null

To View Or List Only Directories In Linux?
Do you like www.linuxnix.com ? Please consider supporting us by becoming a subscriber and get a Linux basics e-book for free.

How to view/list only directories in Linux?
Ans : This can be achieved in two ways
1. Through ls command
2. Through find command

With ls we have to use grep to get the directory listings.
Ls –l grep ^d

Example :
[root@test rmohan_a]# ls -l grep ^d
d——— 2 rmohan_a rmohan_a 4096 Sep 8 09:54 HTWFAIP
drwxrwxr-x 2 rmohan_a root 4096 Nov 27 12:30 LinuxCBT – RHEL5
drwxrwxr-x 2 rmohan_a root 4096 Oct 12 16:40 Software
[root@test rmohan_a]#

With find we can have more controle on how to display only directories.

A. To display all the directories and sub-directories in present directory
#find . -type d

B. Displaying only directories in present directory
#find /root/ -type d –maxdepth 1

C. Displaying just directories in present directry and its sub-directories
#find /root/ -type d –maxdepth 2

* find top 10 largest files in /var:

$ find /var -type f -ls | sort -k 7 -r -n | head -10

* find all files having size more than 5 GB in /var/log/:

$ find /var/log/ -type f -size +5120M -exec ls -lh {} \;

* find all today’s files and copy them to another directory:

$ find /home/me/files -ctime 0 -print -exec cp {} /mnt/backup/{} \;

* find all temp files older than a week and delete:

$ find /temp/ -mtime +7-type f | xargs /bin/rm -f

* find and rename all mp3 files by changing their uppercase names to lowercase:

$ find /home/me/music/ -type f -name *.mp3 -exec rename ‘y/[A-Z]/[a-z]/’ ‘{}’ \;

find mtime

find . -mtime 0 # find files modified between now and 1 day ago
# (i.e., within the past 24 hours)
find . -mtime -1 # find files modified less than 1 day ago
# (i.e., within the past 24 hours, as before)
find . -mtime 1 # find files modified between 24 and 48 hours ago
find . -mtime +1 # find files modified more than 48 hours ago

find . -mmin +5 -mmin -10 # find files modified between
# 6 and 9 minutes ago

Find Parameters

-daystart This flag starts at the beginning of the day.
-atime The time the file was last accessed — in number of days.
-ctime The time the file’s status last changed — in number of days.
-mtime The time the file was last modified — in number of days.
-amin The time the file was last accessed — in number of minutes. (It is not available on all implementations.)
-cmin The time the file’s status last changed — in number of minutes. (It is not available on all implementations.)
-mmin The time the file was last modified — in number of minutes. (It is not available on all implementations.)
-type This flag describes the type of file, such as d for directories.
-userX Files belonging to user X.
-groupX Files belonging to group X.
-newerX Files that are newer than file X.

Here’s how to list all the files in your home directory tree that were modified exactly one hour ago:$ find ~ -mmin 60 \! -type d

Giving a negative value for a flag means to match that number or sooner. For example, here’s how to list all the files in your
home directory tree that were modified exactly one hour ago or any time since

find ~ -mmin -60 \! -type d

$ date
Mon Oct 23 09:42:42 EDT 2006
$ touch -t 10230842 temp
$ ls -l temp
-rw-r–r– 1 joe joe 0 Oct 23 08:42 temp
$ find ~ -newer temp \! -type d

find / -user `whoami` -daystart -atime -1 \! -type d

Give different values for the various time flags to change the search times. You can also combine flags. For instance,
you can list all the files in your home directory tree that were both accessed and modified between now and seven days ago:
find ~ -daystart -atime -7 -mtime -7 \! -type d

find /home/$1/mail/*/mail/.spam/cur -type f -mtime +7 -exec rm {} \;
find /home/$1/mail/*/mail/.spam/new -type f -mtime +7 -exec rm {} \;
find . -type f -exec grep ‘NMX_FXNG_AND_CONTRACT_DBF’ {} \;

Delete Empty Directories
# find folder/ -type d -empty | xargs -i -t rm -rf {}
or
# find folder/ -type d -empty -delete

DISK SPACE COMMAND

Find files based and sorted on Size
# find / -type f -size +20000k -exec ls -lh {} \; 2> /dev/null | awk ‘{ print $NF “: ” $5 }’ | sort -nrk 2,2

2 comments to FIND Command

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>