Custom Search

Ubuntu search files using find command basic

Basic find command




There are several ways to search files in Ubuntu. Probably you may already come across my other post about searching file in Ubuntu using locate command. Here is another way of searching files in Ubuntu. This time we are using the GNU find command. The find command comes with many options. That makes find a very powerful command. However, new users may not be very comfortable with find. I mean most users prefer a simple command, a command with less options to remember.




Don't ever think like that if you want to be a true Linux user. You must take advantage of powerful Linux command otherwise you would never see Linux full potential.




The manual page is a great help. Always make yourself a habit to read manual page for every new command. You don't have to read it all the first time you open it. My tip is, read the synopsis so you know the command's syntax. Then scan through some options. Some manual do have examples. Here are some informations about find in manual page:





NAME
find - search for files in a directory hierarchy


SYNOPSIS
find [-H] [-L] [-P] [path...] [expression]


DESCRIPTION
This manual page documents the GNU version of find. GNU find searches
the directory tree rooted at each given file name by evaluating the
given expression from left to right, according to the rules of precedence
(see section OPERATORS), until the outcome is known (the left
hand side is false for and operations, true for or), at which point
find moves on to the next file name.


If you are using find in an environment where security is important
(for example if you are using it to search directories that are writable
by other users), you should read the "Security Considerations" chapter
of the findutils documentation, which is called Finding Files and comes
with findutils. That document also includes a lot more detail and
discussion than this manual page, so you may find it a more useful
source of information.


EXPRESSIONS
The expression is made up of options (which affect overall operation
rather than the processing of a specific file, and always return true),
tests (which return a true or false value), and actions (which have side
effects and return a true or false value), all separated by operators.
-and is assumed where the operator is omitted.
If the expression contains no actions other than -prune, -print
is performed on all files for which the expression is true.



The meaning of synopsis options:





  • -H means do not follow symbolic links, except while processing the command line arguments.

  • -L means follow symbolic links.

  • -P means never follow symbolic links.




Let's try some examples to search file in Ubuntu using find command.




Example 1:




luzar@ubuntu:~$ find -P /home -iname File.txt
/home/luzar/File.txt
luzar@ubuntu:~$



In the example above, I used find command to search in /home directory for a file named file.txt. See that I used -iname instead of -name because -iname is case insensitive.




This concludes the basic find command guide. We'll continue advanced find command in another post.

No comments:

Post a Comment

Please keep comment relevant and strictly no spam will be tolerated. Thank you.