Easy way to search a files and folders via SSH

Search a Files and Folders via SSH

Here i will guide you how to search a files and folders via SSH.

In few cases you would need to find the location of a given file or to search for a certain text in all files under a directory. SSH provides various two different commands used to accomplish this.

To search a file location:

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

For example, to search for a file called myFile.txt under the current folder (and all subfolders), you would need to use the following command:

find . -name myFile.txt

If you are uncertain about the file name or would like to match a part of the name, you can use a wildcard pattern:

find . -name"myFile*"

If you would like to list only directories and leave all files out of the result:

find . -type d

Or if you want to filter only files modified in the last 2 days, you would need to use:

find . -mtime -2
search a file
search a file

You can also search for a given text in the files content as well. The command you should be using in this case is grep. Grep is a very powerful tool and accepts various command line arguments. For a full list it is recommended to check the manual pages by typing man grep.

An example of using grep to find a certain text can be found below:

grep "database" configuration.php

The above command instructs grep to look for the string “database” in the configuration.php file and display the containing line. If you don’t know which file contains the text, you can use:

grep -l "database" *

This will display the filenames containing the word “database”, but will not actually list the line containing it.

Grep can also be used to filter the results from other commands. For example, the line below will only output configuration.php result:

ls -la | grep configuration.php

In some rare cases, find and grep may prove not useful. For example, to find a certain file in the whole server, it would be best to use an alternative command – whereis or which:

whereis perl

or

which perl

The execution of the above commands will locate the perl binary and display the full path(s) to it.

It is possible to search folders, files, and text via SSH in this way. Our article is hoped to be helpful to you. Do visit our vpsjungle.com site regularly for the latest updates.

And to know the top 13 Basic Cat Commands in Linux click here to go.