| « HOWTO:Install BeyondCompare on 64-bit Ubuntu | Bash History, setting it up » |
The find command can be really useful once you know how to use its more powerful options. It can be use in a simple form to find files from your current directory and all the subdirectories below.
or to find a specific file one way it to use grep,
A better way is to use find's own name search
If you want to play along with the examples, in a tmp directory create the following files in each of the following subdirectories.
Tip Use the touch command to create the files. When you are dome it should look like this
As mentioned above the simplest way to find file is to use the -name option. Using the example setup
we can search for MySource.c.
If you want to use the normal shell patterns such as '?', '*' or even '[asdf]' then you need to
put the pattern in single quotes to stop the shell from expanding them first. here is an example.
Here we use the -iname option so that find ignores the case of the name we enter.
Using the same find as above we add the -maxdepth option and limit it to 2 subdirectories
Find the file between sub-directory level 2 and 4.
# find -mindepth 2 -maxdepth 3 -name MySource.c
./MySource.c
./mysource.c
./backup/MySource.c
./backup/mysource.c
./backup1/MySource.c
./backup1/mysource.c
We have already seen we can find files with specific names or patterns and limit the
sub-directory level we not only start at but how deep we go. Now having found the file we
can do stuff with it or to it. Cool or what!
Say we want to find all the *.c files and then calculate their md5 checksums. This could
be some source code for a new tool you are compiling and need to verify that what you
downloaded is what the developer posted. We tell find to run or execute the command with
the -exec option and we also tell find that the command we want to run passing the
filename with the special token '{}' and end the command that is to be run by find
with '\;'.
Using the example above but this time we want all '.c' files but not those that are called
'Source.c'
See how easy it is to build up the command but adding options.
Say "someone" has been silly and created a file with a strange or awkward name. Such as
it has a space at the end of the filename of contains characters that are normally used
as patterns. renameing them or deleting them can be a problem. Find to the rescue. We can
use the files inode value to search with find and then use -exec to manipulate the
filename as required.
We need a few of example files, we shall use the following:
Note that there is a space at the end of the second filename, this means that when you
use 'ls' to see the files thay appear to have the same file name. You can use touch
to create the example files, put the strangely named files in quotes.
View the files with the ls command forcing the output into one column;
You should see two filenames that look the same, but you also know that cannot be true.
We can use the -name and the -not -name combinastion to find the right file and rename it.
Now have a look and it's sorted out with the filename that did have the space at the end
now renamed to old_testfile
To rename the remaining strangly name file we will use its inode to tell find which file
to rename. If we use the name itself 'MybashProgra?.sh' we get an error from ls saying
that the destination is not a directory. That is because there are two files that match
'MybashProgra?.sh'. To find which file we want to rename we use its inode. To find the
inode of a file we use ls again as below.
Now we can plug in the inode to finds -inum option and rename it as we like.
Find files based on their File type or permissions
To find files by thier type use the -type option and then the type required
Common search types will be
So to earch for the directories in our example use the following.
To search and select based on permissions use the -perm option. This one gets complex
rather quickly, so I'm only mentioning the bascis here.
If you want to search for files that have exactly the permissions wherethe owner and
the group have both read and write but the world only has read access use this.
It is more likely that you want to find files wherethe permissions are at least like
those above. But you also want those that have execute permission too. The different
is that there is a '-' in front of the permissions.
You can also use the symbolic options to represent the permissions so ther two
example above would become,
Finding Empty files
These files are usually lock-files and place holders created by other applications or
the test files from examples on blogs.
Finding the big files on your system
Find even has an option to let you find files bigger or smaller than some size that you enter.
The 'M' in the examples above means Megabytes the other type it understands are