0% found this document useful (0 votes)
84 views6 pages

Find CMD

The find command in UNIX is used to search for files and directories in a file hierarchy. It allows searching by various criteria like name, size, type and executing other commands on the results. The -exec option allows running other commands on each result by specifying a placeholder and delimiter. Using the plus sign (+) as a delimiter causes the command to run once with all results concatenated, while the semicolon (;) runs the command separately for each result.

Uploaded by

rahul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views6 pages

Find CMD

The find command in UNIX is used to search for files and directories in a file hierarchy. It allows searching by various criteria like name, size, type and executing other commands on the results. The -exec option allows running other commands on each result by specifying a placeholder and delimiter. Using the plus sign (+) as a delimiter causes the command to run once with all results concatenated, while the semicolon (;) runs the command separately for each result.

Uploaded by

rahul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Find Command

The find command in UNIX is a command line utility for walking a file hierarchy. It can be used to
find files and directories and perform subsequent operations on them.

By using the ‘-exec’ other UNIX commands can be executed on files or folders found.

Syntax:
$find [where to start searching from] [expression determines what to find] [-options] [what to find]

We can use find command to find files and directories present in our system. It provides more
search options when compared with locate command like:

1) Search only files

2) Search only directories

3) Search by name

4) Search by size

5) We can use search results automatically for some other commands

6) We can restrict maxdepth etc.

1. $find:
It will find all files and directories in current working directory and below in Linux file system.
This is the default behaviour.
We can find all files and directories in the specified directory and below.
$ find /dev
$ find /etc

2. maxdepth Option:
usually find command will search in all depth levels. But we can specify the required depth
level by using maxdepth option.

Observe the difference in results by executing the following commands:


Note:
1. For maxdepth option we should use single - but not double --

-maxdepth Valid

--maxdepth  Invalid

2. find command will find hidden files and directories also.

Find by Type:
We can find only files or only directories by using type option.

-type f  means only files

-type d  means only directories:

Note:
We can use these options simultaneously, but we should use first -maxdepth and then -type.

$find -type f -maxdepth 2 Generates warning

$find -maxdepth 2 -type f No warning

Find by Name:

We can find files and directories by name by using -name option.

If we want to ignore case then we should use -iname option.


Find Files by Size:
we can find files and folders bases on their size also, for that we need to use -size option

. + symbol means greater than (over)

- symbol means less than

1. To list out all file names where size is over 200kb

$ find / -type f -size +200k This command required root privileges

$ sudo find / -type f -size +200k | wc -l

2. To list out all file names where size is over 200kb but less than 4MB.

$ find / -type f -size +200k -size -4M | wc -l

3.To list out all file names where file size is less 200kb or more than 4MB.

$ find / -type f -size -200k -o -size +4M | wc -l


-o means or

4. To list out files whose size is exactly 10M

$find . -size 10M

Note: you can find empty files or folders by using -empty option.

some important options:


1. find a file which are accessed with in 1 hour.

$find . -amin -60

2. find a file which are accessed within 1 day.


$find . -atime -1

3.find a file which are accessed after the modification of a given file.

$find -anewer “abc.java”

4. find a file which are modified after the modification of a given file.
$find -newer “abc.java”

5. find the files which are modified 30 minutes back

$find . -not -mmin -30


6. find the files which are modified 1 day back.

$find . -not mtime -1

7. find the files which are changed after the modification of a given file.

$find . -cnewer “abc.java”

8. find the files which are changed within 3 days

$find . -ctime -2

9. find the files bases on the file permission.

$find . -perm 777

10. find the files with file owner’s name.


$find. -user name rnikrad

11. Find a file modified within a range of days

$ find ~ -mtime +1 -mtime -7

Find cmd with -exec option:

The find command is comprised of two main parts, the expression and the action. When we initially
use find, we usually start with the expression part. This is the part that allows us to specify a filter
that defines which files to select.

A classic example would be:

$ find Music/ -name *.mp3

Music/Gustav Mahler/01 - Das Trinklied vom Jammer der Erde.mp3

Music/Gustav Mahler/02 - Der Einsame im Herbst.mp3

Music/Gustav Mahler/03 - Von der Jugend.mp3

This command will result in a list of mp3 files in the Music directory and all its subdirectories

The action part in this example is the default action, -print. This action prints the resulting paths with
newline characters in between. It’ll run if no other action is specified.

In contrast, the -exec action allows us to execute commands on the resulting paths.

Let’s say we want to run the file command on the list of mp3 files we just found to determine their
filetype. We can achieve this by running the following command:

$ find Music/ -name *.mp3 -exec file {} \;

Music/Gustav Mahler/01 - Das Trinklied vom Jammer der Erde.mp3:

Audio file with ID3 version 2.4.0, contains:MPEG ADTS, layer III, v1, 128 kbps, 44.1 kHz, Stereo

Music/Gustav Mahler/02 - Der Einsame im Herbst.mp3:

Audio file with ID3 version 2.4.0, contains: MPEG ADTS, layer III, v1, 128 kbps, 44.1 kHz, Stereo

Let’s disscuss the arguments passed to the -exec flag, which include:

1) A command: file
2) A placeholder: {}
3) A command delimiter: \;

Now we’ll walk through each of these three parts in-depth.

Any command that can be executed by our shell is acceptable here.

We should note that this isn’t our shell executing the command, rather we’re using Linux’s exec
directly to execute the command. This means that any shell expansion won’t work here, as we don’t
have a shell. Another effect is the unavailability of shell functions or aliases.

As a workaround for our missing shell functions, we can export them and call bash -c with our
requested function on our file.

To see this in action, we’ll continue with our directory of Mahler’s mp3 files. Let’s create a shell
function that shows the track name and some details about the quality:
function mp3info () {

TRACK_NAME=$(basename "$1")

FILE_DATA=$(file "$1" | awk -F, '{$1=$2=$3=$4=""; print $0}')

echo "${TRACK_NAME%.mp3}: $FILE_DATA"

If we try to run the mp3info command on all of our files, -exec will complain that it doesn’t know
about mp3info:
find. -name "*.mp3" -exec mp3info {} \;
find: ‘mp3info’: No such file or directory

As mentioned earlier, to fix this, we’ll need to export our shell function and run it as part of a
sawned shell:

$ export -f mp3info

$ find . -name "*.mp3" -exec bash -c "mp3info \"{}\"" \;


01 - Das Trinklied vom Jammer der Erde : 128 kbps 44.1 kHz Stereo

02 - Der Einsame im Herbst : 128 kbps 44.1 kHz Stereo

03 - Von der Jugend : 128 kbps 44.1 kHz Stereo

Note that because some of our file names hold spaces, we need to quote the results placeholder.

4. The Results Placeholder

The results placeholder is denoted by two curly braces {}.

We can use the placeholder multiple times if necessary.

5. The Delimiter

We need to provide the find command with a delimiter so it’ll know where our -exec arguments
stop.
Two types of delimiters can be provided to the -exec argument:
the semi-colon (;) or the plus sign (+).

The delimiter determines the way find handles the expression results. If we use the semi-colon (;),
the -exec command will be repeated for each result separately. On the other hand, if we use the plus
sign (+), all of the expressions’ results will be concatenated and passed as a whole to the -exec
command, which will run only once.

Let’s see the use of the plus sign with another example:

$ find . -name "*.mp3" -exec echo {} +


./Gustav Mahler/01 - Das Trinklied vom Jammer der Erde.mp3 ./Gustav Mahler/02 -

Der Einsame im Herbst.mp3 ./Gustav Mahler/03 - Von der Jugend.mp3 ./Gustav Mahler/04 -

Von der Schönheit.mp3 ./Gustav Mahler/05 - Der Trunkene im Frühling.mp3

./Gustav Mahler/06 - Der Abschied.mp3

From a performance point of view, we usually prefer to use the plus-sign delimiter, as running
separate processes for each file can incur a serious penalty in both RAM and processing time.

However, we may prefer using the semi-colon delimiter in one of the following cases:

The tool run by -exec doesn’t accept multiple files as an argument.

Running the tool on so many files at once might use up too much memory.
We want to start getting some results as soon as possible, even though it’ll take more time to get all
the results.

Difference between find and locate Commands:

You might also like