Search all .htaccess files in a Linux server webroot for a word and return file paths to a text file - linux

I have a Linux server and want to search all the .htaccess files in all the folders (public_html webroot and subfolders) that have a certain word (eg ldap) in it. I also want the file paths returned to these .htaccess files with the word in it and saved to a text file.
Can I do this with grep or find and what syntax is optimal.
I tried find . -type f -printf '"%p"\n' | xargs grep ldap > /tmp/results.txt but want to only search .htaccess files exclusively.
Thanks

Following your example, try using:
find . \( -type f -name .htaccess \) -print0 | xargs -0 grep -H ldap > /tmp/results.txt
this find will list null-terminated files .htaccess in . directory, and xargs -0 pass them to the grep. grep -H ldap will list files containing ldap string with filenames.

Related

Search and replace string within all files ending in wp-config.php only

I am trying to search for the string localhost within all wp-config.php files in the home directory of my server and replacing it with myhostname.com without affecting any other files.
here is the seach command i have so far
find /home/ -type f -exec sed -i 's/localhost/myhostname.com/g' {} ;
You may try the below.
sed -i 's/string/myhostname.com/g' *wp-config.php

Mass Find/Replace within files having specific filename under command line

I am looking for a quick command to search all .htaccess files for a specific IP address and change it to another IP address from the command line
something like
grep -rl '255.255.254.254' ./ | xargs sed -i 's/254/253/g'
I know the above example is a bad way to do it, just an example (and showing I did some searching to find a solution
Search: files with filename .htaccess (within 2 levels deep of current path?)
Find: 255.255.254.254
Replace with: 255.255.253.253
or, is this too much to ask of my server and I would be better off replacing them as I find them?
Try:
find . -type f -name '.htaccess' -execdir sed -i 's/255\.255\.254\.254/255.255.253.253/g' {} +
How it works:
find .
Start looking for files in the current directory.
-type f
Look only for regular files.
-name '.htaccess'
Look only for files named .htaccess.
-execdir sed -i 's/255\.255\.254\.254/255.255.253.253/g' {} +
For any such files found, run this sed command on them.
Because . is a wildcard and you likely want to match only literal periods, we escape them: \.
We use -execdir rather than the older -exec because it is more secure against race conditions.

check if a file is in a folder or its subfolder using linux terminal

I want to check if the particular file is in a folder or its sub folder or not using Linux terminal.
Which should I use for this? I use find and grep command but it travels only one folder.
In order to search from your current directory, use
find . -name filename
In order to search from root directory use
find / -name filename
If you don't know the file extension try
find . -name filename.*
Also note that find command only displays the files in the path which you have permission to view. If you don't have permission for a/b/c path then it will just display a message mentioning that path can't be searched
If you want to search for by filename, use find:
find /path -name "filename"
example:
find . -name myfile.txt
If need to find all files containing a specific string, use grep:
grep -r "string" /path
example:
grep -r foobar .
By default, find will traverse all subdirectories, for example:
mkdir level1
mkdir level1/level2
touch level1/level2/file
find . -name "file"
Output:
./level1/level2/file
locate file name
This is the simple command
I also prefer using a combination of tree and grep. Something like
tree | grep filename
Try
find . -name "filename" -type f
-type f restricts to only files in the current directory (replace . with your path).

Find/Replace through all files on the root directory through SSH

I just noticed that my website got hacked and all the .php files have a base64 encode string on line 1. The string is the same across every file so...
How can I search my entire root directory and remove this?
try:
find /path/to/dir -type f -name '*.php' -exec sed -i '/string-to-remove/{1d;}' '{}' \;
edit:
added -name '*.php' to restrict this to .php files.
added sed matching directive

List all files in a directory with abosolute paths (excluding directories)

I've currently got:
ls -1 $(pwd)/*
Gives me all the files in a directory with absolute paths - but formats it with the directory at the start of each list of files.
Is there a way just to get a list of files in a directory recursively (absolute paths) - excluding the directory/sub-directories themselves?
find $(pwd) -type f -print
or
find $(pwd) -type f -ls
If you are feeding it into something else, you might want -print0 (to handle filenames with spaces).
E.g.: find . -type f -print0 | xargs --null --no-run-if-empty grep

Resources