Here is some useful commands you should have around you when you’re looking for something on your hard drive.
Find in content files
This linux command can be used if you want to find something into content files. Be sure to be in the desired directory where you want to start the search. It can take a while because of the (-r) recursion. It will display number lines with the -n parameter. grep –help to find more parameters.
1 |
grep -nr "string to find" * |
Find in content of filtered extension files
This is the same but using find command with more possibilities likes file filtering. This example will search in .php files:
1 |
find . -name "*.php" -print | grep "string to find" |
Find in file names
Find your myfile.php file in the current directory and subdirectories
1 |
find . -name "myfile.php" -print |
Find in directory names
If you want to find all directories by their names:
1 |
find . -name 'directory to find' -type d |
Find in Linux indexed db
You could use the locate command to get files and/or directories containing your search keywords. This command uses a Linux indexed database which won’t be updated by the system automatically. Then you should use the updatedb command to refresh it before making your search:
1 2 |
updatedb locate string to find |
Find and replace in all files
The sed command let you do something very useful: to search and replace a string in all files easily. For example if you want the rain gives way to the sun, you could replace all rainy occurrences by sunny in all your files like this :
1 |
find . -type f -exec sed -i 's/rainy/sunny/g' {} \; |
Recent Comments