What is the command to search all files in your current directory for the word?

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,

  • find . -type f -name style*
    8 is the path where file is expected to be found. This is the starting point to search files. The path can also be
    find . -type f -name style*
    9or
    find . -type f -name *.html
    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
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

  • find . -type f -name *.html
    7 is the name of the file type that you want to search.

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

What is the command to search all files in your current directory for the word?

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

What is the command to search all files in your current directory for the word?

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

What is the command to search all files in your current directory for the word?
List of hidden files in my home directory

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

What is the command to search all files in your current directory for the word?

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

What is the command to search all files in your current directory for the word?

💡 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.

What is the command to search all files in your current directory for the word?

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 250MB

Other units include:

  •  find . -type f -name "*.log"
    1: GigaBytes.
  •  find . -type f -name "*.log"
    2: MegaBytes.
  •  find . -type f -name "*.log"
    3: KiloBytes
  • find . -type f -name *.html
    6 : bytes.

Just replace with the relevant unit.

find . -type f -name style*
0

How 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:

What is the command to search all files in your current directory for the word?

Let's apply an example in my home directory.

find . -type f -name style*
2
What is the command to search all files in your current directory for the word?
Here we have files that were modified more than 10 days ago.

Practical examples of find . -type f -name style*4 with bash scripts

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*
3

Note 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


What is the command to search all files in your current directory for the word?
Zaira Hira

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

How would you search for a word in all files in current folder?

You can make grep search in all the files and all the subdirectories of the current directory using the -r recursive search option: grep -r search_term .

Which command is used to search a file or directory?

Both the find and locate commands are useful tools for finding files on your system.

What is the command to list all files in a directory?

To list all files in the current directory, type the following: ls -a This lists all files, including. dot (.) ... .
To display detailed information, type the following: ls -l chap1 .profile. ... .
To display detailed information about a directory, type the following: ls -d -l ..

What command is used for find the word in file?

The grep command searches through the file, looking for matches to the pattern specified. To use it type grep , then the pattern we're searching for and finally the name of the file (or files) we're searching in. The output is the three lines in the file that contain the letters 'not'.