Find CMD
Find CMD
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:
3) Search by name
4) Search by size
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.
-maxdepth Valid
--maxdepth Invalid
Find by Type:
We can find only files or only directories by using type option.
Note:
We can use these options simultaneously, but we should use first -maxdepth and then -type.
Find by Name:
2. To list out all file names where size is over 200kb but less than 4MB.
3.To list out all file names where file size is less 200kb or more than 4MB.
Note: you can find empty files or folders by using -empty option.
3.find a file which are accessed after the modification of a given file.
4. find a file which are modified after the modification of a given file.
$find -newer “abc.java”
7. find the files which are changed after the modification of a given file.
$find . -ctime -2
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.
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:
Audio file with ID3 version 2.4.0, contains:MPEG ADTS, layer III, v1, 128 kbps, 44.1 kHz, Stereo
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: \;
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")
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
Note that because some of our file names hold spaces, we need to quote the results placeholder.
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:
Der Einsame im Herbst.mp3 ./Gustav Mahler/03 - Von der Jugend.mp3 ./Gustav Mahler/04 -
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:
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.