how do you search for a particular keyword within a php file? - linux

I'm trying to search for a word/phrase within all the php files and all sub-directories where the command is run from. I'm using ssh with root access.

find . -type f -name \*php|xargs grep -i yourword

Related

How to search for all the hidden files in my computer?

I want to find all the hidden files inside a directory in linux terminal.
I have found out that we have a grep command to search for the file but I need to search for hidden files.
grep -r search *
Any help would be appreciated. Thanks.
try this on your terminal too show all the hidden files on your system:
find / -name ".*" 2> /dev/null
or you can use other way like in this web https://devconnected.com/how-to-show-hidden-files-on-linux/
Simply use (with GNU grep)
grep -r search .
if you want to search contents of files in the current directory and its subdirectories recursively.
Note: It isn't clear if you want to search filenames or contents of files.
The proper solution:
find /dir -name '.*' -type f
If by "hidden file" you mean Linux file names that begin with . that are often hidden by default, (and directories starting with . whose contents might also be considered "hidden") then try this command:
find . -print | grep '/\.'

Linux - Print only filenames for the directory and sub-directories

I'm very new at Linux world.
I've the following directories on my linux (centos rhel fedora"):
Folder_Root:
/root/main
/root/main/files
/root/main/files/file_1.txt
/root/main/files/file_2.ssh
/root/main/files/file_2.txt
/root/main/file_3.txt
I'm trying to make a print of all the files in all the directories. Basically I am trying to get the following list:
file_1.txt
file_2.ssh
file_2.txt
file_3.txt
I already try 'ls' command and 'ls -al': but it prints also the direcotry name.
I also try to use 'ls -lR | more': but it prints a lot of details that I don't want to use.
Do you recommend any command?
How about using:
find . -type f -exec basename {} \;
or even:
find . -type f -printf "%f\n"
There is a similar question asked here and it has many answers, hope this helps:
List only file names in directories and subdirectories in bash
How about using find:
find /root/main -type f

how to search for a directory from the terminal in ubuntu

I am using Ubuntu and I want to search for a specific Directory called "sdk".
All that I know is, that "sdk" Directory is located somewhere under /user Directory
how can I search for "sdk" Directory from the terminal?
I tried the following find / -name example docs -type d
but it says no such file or Directory.
you can search for directory by using find with flag -name
you should use
find /user -name "sdk" -type d
meaning find directories named sdk in or below the directory /user
or if you want to be case-insensitive
find /user -iname "sdk" -type d
Please try the below command.
locate foldername | grep /foldername$

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).

Resources