find command with regex to find directories - linux

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

Related

Linux find catalogs and print only names

i wanna search for the catalogs which have the "program" in their names and echo these names in console. I have wrote this, but isn't working:
find usr -type d -name "program" -exec echo {}
The error is find: missing argument to `-exec'.
find usr -type d -name "program"
usr/lib64/libreofice/program
How to fix my command?
Some tiny example with the * wildcard.
find /my/path -name "*program*"
If you don' use wildcards, it will try to find exactly the files named program. Also, echoing is done automatically, you don't need the exec command.
Update
Answering to your comment. You can get the base name (name without the path) with:
find . -name "*program*" -exec basename {} \;

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"

Why does find . -name foo* fail?

OK, probably a stupid question, but why doesn't this work?
find . -name Orna* -ls
It seems that "find" should be able to handle this simple request... Am I missing something here? This should be a basic execution of the "find" command, but linux is being stupid, or maybe I am.
correct way of using Find Command are the following phrases
find . -type f -name "filename" # this command used to find files from the curent dir
find . -type d -name "dir name" # this command used to find dirs from the curent dir
find /. -type f -name "filename" # this command used to find files from the system
find /. -type d -name "dir name" # this command used to find dirs from the system
I wish it be a helpful for you
You need to quote the name parameter so the shell doesn't expand the wildcard, e.g.
find . -name "Orna*" -ls
To explain the "why" a little more than existing answers do -- wildcards are expanded by the shell before the command being invoked is run. Thus, let's say your current directory contains files Orna1 and Orna2.
In that case, when you run
find . -name Orna* -ls
...what's actually invoked by the shell is:
find . -name Orna1 Orna2 -ls
...thus, find never sees the wildcard expression at all!
Quoting the expansion, as in:
find . -name 'Orna*' -ls
...prevents the shell from trying to expand the wildcard before running your command, thus preventing this issue.

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 to exclude few folder levels in the FIND command results - unix

Following is the folder structure
- home/ABCD/test1/example1/sample1/textfile.txt
If I execute the find command like
find /home/ABCD/ -type f -print
I am getting the following output
/home/ABCD/test1/example1/sample1/textfile.txt
Note: I am executing the find command from the ABCD folder, In the results I want to exclude /home/ABCD/ folder I just want /test1/example1/sample1/testfile.txt as the result
How can I achieve this?
Since you are executing find from /home/ABCD/ do something like this:
find * -type f -print
Or if you are looking for files in test1 do this:
find test1 -type f -print
Also with -maxdepth N you can limit the recursion in find
If you only want to look for files named textfile.txt do
find test1 -type f -name 'textfile.txt' -print
If you want to print the leading slash do
find . -type f -printf '/%p\n'
For more info have a look here
Note: If have the above string in a variable, you can trim it like this:
string="/home/ABCD/test1/example1/sample1/textfile.txt"
echo "${string#/home/ABCD}"
Some more examples of string manipulation here
Just use . as the starting directory
find . -type f -print
gives
./test1/example1/sample1/textfile.txt
and if you really want a leading slash, use -printf for the output
find . -type f -printf '/%P\n'
You can use the mindepth parameter to start looking at one level below the current directory
find /home/ABCD/ -mindepth 1 -type f -print
This should substitute your current working directory name with a .
find . -type f | perl -pne "s#$PWD#.#"
So you would get results like:
./test1/example1/sample1/textfile.txt
If you do not want the preceeding ./, use this command instead:
find . -type f | perl -pne "s#$PWD/##"

Resources