How to Search Files and Folders via SSH

In this blog, I am going to list out some of the SSH commands that will help you to

  • Finding the location of the file.
  • Finding the files based on the size.
  • Search a certain text in all files under a specific directory.

Finding the location of the file:

To find a location of a file through SSH you can use the find command. Find is a very powerful tool and it accepts various arguments allowing you to specify the exact search term (i.e search by name, search by type or search by modified time).

Example 1:

If you want to find a file called myFile.txt under the current folder and all the subfolders, use the below command

find . -name myFile.txt

That finds all the files in the current directory with the exact name myFile.txt. Using the -iname flag instead uses a case-insensitive search:

find . -iname myFile.txt

Example 2:

If you want to find the files which contain the word apple in the part of the name (i.e, apple.php, apple.xml, green_apple.php), use the below command

Find a file which name start with index

find . -name "index*"

Find a file which name end with .php

find . -name "*.php"

Find a file which contain the word index in the mid of the name

find . -name "*index*"

Example 3:

If you want to find the files which has been modified in the last 3 days, use the below command

find . -mtime -3

Example 4:

Sometime you may want to find the specific folder, you can do this using the below command

find . -type d -name foo*

In the above command -type, which allows you to find files based on the Unix-based identifiers associated with all files.

  • -type d: for directories
  • -type f: for files
  • -type l: for symbolic links

Finding the files based on the size:

Sometimes we might require to get the files based on the size. This can be easily done using the find command.

Example 1:

If you want to find the files that larger than a certain size (i.e, 100k), use the below command

find . -size +100k

The above command will search all the files under the current directory.

If you want to find the files in the specific directory (i.e, etc/), use the below command

find /etc -size +100k

Example 2:

If you want to find the files within specified size limits (i.e, 100k and 150k), use the below command

find . -size +100k -size -150k

Search a certain text in all files under the specific directory:

To find a certain text in all files under a specific directory through SSH you can use the grep command.

Example 1:

If you want to find the string database in the file configuration.php, use the below command

grep "database" configuration.php

The above command instructs grep to look for the string database in the file configuration.php and display the containing line.

Example 2:

If you don’t know which file contains the text database, you can use the below command

grep -r -H "database" *

In the above command

  • -r (option): searching in all files recursively
  • -H (option): provide the result in a human-readable format
  • *: search in all files under current directory

Some other example:

Case insensitive search using grep

grep -i "database" configuration.php

Show line number while displaying the output using grep

grep -r "database" configuration.php

Highlighting the search result

grep -urn --color "database" app/

Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *