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

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

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 '/\.'

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$

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"

linux command to find files and replace with another file

I'm looking for a linux command to search a folder directory and it's subfolders for a file by name and replace all of them with a copy of another file. Any idea?
Assuming you have the replacement file in your home directory (~), you can use find to do the replacing. This will find all boom.txt files and replace them with the replace.txt file (keeping the boom.txt name).
find . -name "boom.txt" -exec cp ~/replace.txt {} \;
Adding to the answer by user1683793, the following command can also be an alternative for the copy and replace. This command is verified on Mintty(Windows 10).
find . -name "boom.txt" -print -exec cp ~/replace.txt {} \;

how to recursively list files from a folder?

'ls dir1/*/.ext' just lists all the files with just one level of nesting. What is the command to recursively list all the files with any level of nesting in linux?
ls -R dir1
Or:
find dir1 -name "*.ext"
The find command is one way to do this:
find dir1 -name .ext
The -name operator can take a wildcard to match with, but it's important to quote the wildcard expression so that it won't be expanded by your shell before calling into find:
find dir1 -name "*.ext"
The find command has many operators that can do various different tests on the files in the directory, of which -name is just one example. Consult the find manual page for more information.
To list folder recursively:
ls -R
You could use find:
find .
That command would list everything under the current folder

Resources