how to search for a directory from the terminal in ubuntu - linux

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$

Related

find command with regex to find directories

I have the following linux find command that gives a base path then also searches if there are any directories that contain the name 3.7.1
In my base path /some/folder/path/folder1 i would like to change folder1 into a regular expression something like folder[1-3] how can I do this?
# working command
find /some/folder/path/folder1/another_folder -type d -name "3.7.1*" -ls
# would like to use some regular expresion on folder1-3
find /some/folder/path/folder[1-3]/another_folder -type d -name "3.7.1*" -ls
error I am seeing: /folder[1-3]/another_folder/': No such file or directory
That's the correct syntax already:
find /some/folder/path/folder[1-3] -type d -name "3.7.1*" -ls

How can I find a file within a specific directory name?

So I need to find all files in /home/ with a file name of "options.php".
find . -name "options.php"
When 'in home', that will find all options.php files, however, I want to only find all options.php files when they are in /public_html/.
So in other words, it should ignore all other 'options.php' files found.
eg, positive/show results:
/home/usr1/public_html/options.php
/home/usr2/public_html/options.php
eg, shouldnt show me:
/home/usr1/public_html/wp-admin/options.php
/home/usr2/public_html/wp-content/plugins/whatever/options.php
You can pass a pattern via -path option as follows:
find /home/ -path '*/public_html/options.php'
For a more flexible pattern use -regex which accepts a regular expression applied on the whole path. But in this particular case -regex has no advantage over -path:
find /home/ -regex '.*/public_html/options.php'
Filter the desired results from the found results with grep.
find . -name "options.php" | grep 'public_html/options.php'
You can limit the depth of find:
find . -maxdepth N, this way It should only find options.php in your desired folder.
The ls utility is much better suited for this task:
ls -1 /home/*/public_html/options.php
If you want to process the result list and do not want to have an error message or warning in case no such files are found, then simply redirect the error output of the command:
ls -1 /home/*/public_html/options.php 2>/dev/null
An alternative using the find utility would be:
find /home -path "*/public_html/options.php"
Or, if you want to prevent matches in folders called "public_html" further down in the hierarchy:
find /home -path "/home/*/public_html/options.php"
find /home -maxdepth 3 -path "*/public_html/options.php"

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

How can I find a directory and a subdirectory and a subdirectory in linux?

I am new to linux. I am trying to find a directory structure that looks like this: /org/voltdb/client.
I can find lots of tutorials showing how to find a directory but none showing how to find an entire directory path.
How can I search for a particular directory hierarchy?
This does not work: find / -type d -name "/org/voltdb/client" -ls
Jon Lin's solution does not work, because the '/org/voltdb/client' will not appear in the output of find. You should use this instead:
find / -type d -name 'client' | grep /org/voltdb/client
You can use the find command. It's very versatile for searching with patterns. Try:
find / -type d -path '/org/voltdb/client'
You could just grep the output:
find / -type d -name 'org' | grep /org/voltdb/client

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

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

Resources