Searching for files is relatively easy when you are using a GUI. But in certain environments like GUI-less servers, you need to search for files using the command line.
There is a powerful command in Linux that helps you search for files and folders called
find . -type f -name style*
4. In this article, we will discuss the find . -type f -name style*
4 command with some examples.What is the find command in Linux?
The
find . -type f -name style*
4 command lets you efficiently search for files, folders, and character and block devices.Below is the basic syntax of the
find . -type f -name style*
4 command:find /path/ -type f -name file-to-search
Where,
8 is the path where file is expected to be found. This is the starting point to search files. The path can also befind . -type f -name style*
9orfind . -type f -name style*
0 which represent root and current directory, respectively.find . -type f -name *.html
1 represents the file descriptors. They can be any of the below:find . -type f -name *.html
find . -type f -name *.html
2 – Regular file such as text files, images and hidden files.find . -type f -name *.html
3 – Directory. These are the folders under consideration.find . -type f -name *.html
4 – Symbolic link. Symbolic links point to files and are similar to shortcuts.find . -type f -name *.html
5 – Character devices. Files that are used to access character devices are called character device files. Drivers communicate with character devices by sending and receiving single characters [bytes, octets]. Examples include keyboards, sound cards and mouse.find . -type f -name *.html
6 – Block devices. Files that are used to access block devices are called block device files. Drivers communicate with block devices by sending and receiving entire blocks of data. Examples include USB, CD-ROM
7 is the name of the file type that you want to search.find . -type f -name *.html
Examples of the find command
Now we know the syntax of the
find . -type f -name style*
4 command, let's look at some examples.How to search files by name or extension
Suppose we need to find files that contain "style" in their name. We'll use this command:
find . -type f -name style*
Output
Now let's say we want to find files with a particular extension like
find . -type f -name *.html
9. We'll modify the command like this:find . -type f -name *.html
Output
How to search hidden files
Hidden files are represented by a dot in the beginning of the filename. They are normally hidden, but can be viewed with
find . -type f -name ".*"
0 in the current directory.We can modify the
find . -type f -name style*
4 command as shown below to search for hidden files.find . -type f -name ".*"
Output
How to search log files and configuration files
Log files usually have the extension
find . -type f -name ".*"
2, and we can find them like this: find . -type f -name "*.log"
Output
Similarly, we can search for configuration files like this:
find . -type f -name "*.conf"
How to search other files by type
We can search for character block files by providing
find . -type f -name *.html
5 to find . -type f -name *.html
1:find / -type c
Similarly, device block files can be found by using
find . -type f -name *.html
6:find / -type b
How to search directories
In the example below, we are finding the folders named
find . -type f -name ".*"
6. Note that we are using find . -type f -name ".*"
7.find . -type d -name "lib*"
Output
💡 Tip: we can identify directories by looking at the
find . -type f -name *.html
3 flag in the output of find . -type f -name ".*"
9.How to search files by size
An incredibly helpful use of the
find . -type f -name style*
4 command is to list files based on a particular size.find / -size +250MB
Here, we are listing files whose size exceeds 250MBOther units include:
1: GigaBytes.find . -type f -name "*.log"
2: MegaBytes.find . -type f -name "*.log"
3: KiloBytesfind . -type f -name "*.log"
6 : bytes.find . -type f -name *.html
Just replace with the relevant unit.
find . -type f -name style*
0How to search files by modification time
find . -type f -name style*
1- -mtime +10 means you are looking for a file modified 10 days ago.
- -mtime -10 means less than 10 days.
- -mtime 10 If you skip + or – it means exactly 10 days.
Below are the contents of my home directory:
Let's apply an example in my home directory.
find . -type f -name style*
2Practical examples of find . -type f -name style*
4 with bash scripts
find . -type f -name style*
We can combine
find . -type f -name style*
4 with find . -type f -name "*.log"
7 or find . -type f -name "*.log"
8 to create meaningful bash scripts that can be automated.Let's say we want to create a script that moves log files older than 7 days to a backup path. From there, it deletes log files older that older than 30 days. We can create a script and schedule it with
find . -type f -name "*.log"
9. You can learn more about find . -type f -name "*.log"
9 jobs here.Let's view the script:
find . -type f -name style*
3Note that we are using
find . -type f -name "*.conf"
1 with find . -type f -name style*
4. Basically, find . -type f -name "*.conf"
1 executes the command provided [ find . -type f -name "*.log"
8 and find . -type f -name "*.log"
7 in our case]. find . -type f -name "*.conf"
6 is the placeholder which holds the results of the command. Lastly, we provide the delimiter find . -type f -name "*.conf"
7. As we do not want the shell to interpret the semicolon, we escape it with find . -type f -name "*.conf"
8.The shared script is very useful in archiving and removing logs.
Wrapping up
In this article, we have studied the
find . -type f -name style*
4 command in detail and learned how to search files by name, type, size and modification time.I hope you found this tutorial helpful.
Share your thoughts on Twitter!
You can read my other posts here.
Resources: Banner images from Office illustrations by Storyset and Canva.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
I am a DevOps Consultant and writer at FreeCodeCamp. I aim to provide easy and to-the-point content for Techies!
If you read this far, tweet to the author to show them you care. Tweet a thanks
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started