locate command is useful and efficient if you need to locates a file based on its pathname. However, if you want more flexibilty and power in your search, you can use the
There are three main parts of the
Below is an example of a typical
Let's go through this piece-by-piece.
The first argument is which directories we want to look in. By default, the shell searches recursively through the directory specified.
$ find . . ./file1.txt ./file2.txt
This simple example will list out all file names and directories in the current folder (
.). You may place a relative or an absolute pathname here, or your home directory (
If you have a lot of files and want to see how many files are being listed, we can use the
$ find . | wc
3 3 32
This shows us that this particular find command returns 3 words, 3 lines and 32 characters. In other words, we have just three files (including folders) being outputted.
Now, it's probably not manageable to list out all contents, or as useful to just
wc results. Additionally, we wouldn't want to pipe our results as that can be too messy of a command.
Luckily we can use criteria to filter our results directly through the
A useful option we can use is
type, which is used to filter based on the type of file.
To specify a directory or file, we simply pass a
$ find ~ -type d # List all directories within our home $ find ~ -type f # List all files within our home
You can also use a
l type for symbolic links.
In addition to file type, we can specify name with the
-name option. We can add wildcards for more flexibility and power.
$ find ~ -type f -name "*.html" # All files in our home directory with a .html extension
For the case-insensitive option, we use
To specify a file size, we use the
-size option. Before typing out the size, place either a
+ symbol. This specifies if you want results that are smaller or larger than the given value.
$ find ~ -type f -name "*.html" -size -3M # Search for all files with an .html extension with a size < 3 megabytes.
There are plenty of other options that are available to suit your needs. Remember to check out the
man page for a list of all of them. Here are some of our favorites that you may find useful.
We can combine logical operators to further specify our search specifications; to specify precedence, surround the statements with
For example, if we wanted our search criteria to match two criteria, we can link a statement with an
-and. If we just want one of the criteria to be satisfied, use
$ find . -type f \( -name "*.html" \) -or \( -name "*.php" \) # Return all files with a .html extension and readable by others $ find . -type f \( -name "*.html" -and -perm -o=r \)
Lastly, we have our actions, where apply a command to the files that have been found.
ls -dilson matching files.
Command Line Kung Fu is packed with dozens of tips and practical real-world examples. You won't find theoretical examples in this book. The examples demonstrate how to solve actual problems. The tactics are easy to find, too. Each chapter covers a specific topic and groups related tips and examples together.$ Check price
Stretch out your back and relieve your back muscles with inversion therapy. This device counteracts the forces of gravity on the body by decompressing and elongating the spine. By using this product just ten minutes a day, you can be well on your way to improved circulation and posture while relieving muscle aches, back pain and stress.$$ Check price