This guide illustrates how to search folders, files and text via SSH. A file location may need to be found or a certain text may need to be searched in all files under a directory. Two different commands can be used to accomplish this in SSH.
You can use the “find” command to locate a file. With Find, you can specify the exact search term from multiple arguments (i.e. by name, by type, or by modified time).
Follow the steps:
1) For instance, you would need to run the following command to search for a file named myfile.txt within the current folder (and all subfolders):
find . -name myFile.txt
2) You can use a wildcard pattern if you are unsure of the file name or want to match a specific part of the name:
find . -name “myFile*”
3) If you want to list just folders and exclude all files from the output:
find . -type d
4) Alternatively, you could use the following syntax to only include files modified during the last two days:
find . -mtime -2
5) Additionally, you can perform a text search within the files themselves. In this situation, the appropriate command to use is “grep”. Grep is a highly strong utility that takes several command-line options. It is advised to check the manual pages by entering man grep for a complete list.
Using the “grep” command to find a specific text is as given below:
grep “database” configuration.php
The above command directs grep to search the configuration.php file for the word “database” and print the line that it finds. If you’re unsure of which file the text is in, try using:
grep -r -H “database” *
This will instruct grep to search for the word “database” in all (*) files under the current working directory recursively (-r option) and to output the results in a human-readable format (-H option).
6) You can use the -l argument to only list the file names containing the string you are searching but not the line containing it:
grep -l “database” *
The filenames containing the word “database” will be displayed, but not the line containing it.
7) The grep command can also be used to filter the results of other commands. The following line will only output configuration.php results:
ls -la | grep configuration.php
8) Occasionally, find and grep may not be useful. To locate a specific file across the whole server, it would be wise to use an alternative command – whereis or which:
Upon execution of the above commands, the full path(s) to the Perl binary will be displayed.
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 WHUK site regularly for the latest updates.