How to combine command substitution and regex? - regex

Is there a possibility to do command substitution in a regex?
I want to find files in Linux with specific names. The name may include fix strings, but it may also only include the hostname.
So what i want to do is something like:
find /home/ -type f -regextype posix-extended -regex '.*(string1|string2|`hostname`).*'
I'm not sure whether it's possible to somehow concat the output of the hostname command with the regex?
Thanks in advance!

Try this :
find /home/ -type f -regextype posix-extended -regex ".*(string1|string2|$HOSTNAME).*"
if you need to use a command instead :
find /home/ -type f -regextype posix-extended -regex ".*(string1|string2|$(hostname)).*"

Related

using shell variable in find regex

i would like to delete files with some pattern(like start with 'test')that older than 1 day, i have:
mydir=/find/my/path
find $mydir -type f -mtime -1\
-regextype egrep -regex '$mydir\/test.*'\
-delete
but it did not delete files for me, i tried
mydir=/find/my/path
find $mydir -type f -mtime -1\
-regextype egrep -regex '.*\/test.*'\
-delete
this one works. Why did the first not work? according to
'find' using regex with variables
i can use variable in find regex, what's wrong?
It's because in the 1st one, you tried to evaluate a variable $mydir inside a single inverted comma with '$mydir\/test.*'. This won't evaluate the $mydir's value and will be taken literally as $mydir.
Use double inverted comma here and try again with
mydir=/find/my/path
find $mydir -type f -mtime -1\
-regextype egrep -regex "$mydir\/test.*"\
-delete
Gaganshera's answer already explains the the problems in your script and shows how to fix them. This is a different but equivalent solution combining find and globs:
mydir=/find/my/path
find "$mydir"/test* -type f -mtime -1 -delete

RegEx for finding all files that start with any letter followed by a number

I am trying to make my bash script smart. I have some code that is doing a clean up of 10000's of files. What I am trying to work out is how can I find all files that have a bunch of letters a-z or A-Z in front of 0901*.*
find . -maxdepth 1 -type f -regextype posix-extended -regex '[a-z]\(0901).*'
find . -maxdepth 1 -type f -regextype posix-extended -regex '[a-z]*.*'
returns everything
while
find . -maxdepth 1 -type f -regextype posix-extended -regex '[a-z]\(0901).*'
returns nothing.
How do I solve this problem?
The following option should work with sed as the regex engine:
find . -maxdepth 1 -type f -regextype sed -regex "[A-Za-z]\+0901.*"
I am interpreting a bunch of letters as one or more letter, hence I used [A-Za-z]+ in front of the digits. You should not need parentheses here, but if you wanted to use them, you would have to escape those via backslash:
find . -maxdepth 1 -type f -regextype sed -regex "[A-Za-z]\+\(0901\).*"
With -regex returning the entire path (relatively as the case may be), need to take ./ into account. So, borrowing heavily from Tim's answer.
find . -maxdepth 1 -type f -regextype sed -regex '.*[A-Za-z]\+\(0901\).*'
I think you have to use this regex:
"[A-Za-z]0901"
Good Luck!

globular vs regex in find

I am trying to find file names similar to this: fsimage_0000000000501205926
This is what I tried :
works: find . -name 'fsimage_???????????????????' -mtime -1
Following another SO post I tried this and it doesn't work:
find . -regextype posix-extended -regex '^fsimage_[0-9]{19}' -mtime -1
*** EDIT:
As suggested escaping the curly braces doesn't work either.
What I am doing wrong with the regex command ? I am using 4.4.2 GNU findutils.
You can use this regex in find:
find . -regextype posix-extended -regex '.*/fsimage_[0-9]{19}'
PS: If you're on OSX then use:
find -E . -regex '.*/fsimage_[0-9]{19}'

How to find files with regex and list them?

I am new to the whole command-line thing and trying to figure out how to search the current directory and its sub directories for files with a specific filename via regex. Then I want to have the files listed in my command-line.
The regex should match files like:
B2ctes_UCUAAwF-K-large-123x322-132x423.jpg
this_is-a-123-file_name-3124x2445-4235x32.jpeg
file-32x32-64x64.png
The important part is the -[number]x[number]-[number]x[number]
My attempt looks like this:
find . -type f -regex ".+?-\d+x\d+-\d+x\d+\.\w{3,4}" -ls;
There are two problems with this:
-ls puts shows a lot of information. I just want the filenames.
The regex doesn’t work. I have tried to use .+, but even that does not return anything.
You can use this find with regex:
find . -regextype posix-extended -type f -regex ".*-[[:digit:]]+x[[:digit:]]+-[[:digit:]]+x[[:digit:]]+\.[[:alnum:]]{3,4}"
Or on OSX:
find -E . -type f -regex ".*-[[:digit:]]+x[[:digit:]]+-[[:digit:]]+x[[:digit:]]+\.[[:alnum:]]{3,4}"
And without regex:
find . -type f -name "*-[[:digit:]]*x[[:digit:]]*-[[:digit:]]*x[[:digit:]]*.[[:alnum:]]*"
What about simply :
find . -type f -name '-[0-9]*x[0-9]*-[0-9]*x-[0-9]*'
or
find . -type f -regextype posix-egrep -regex '.*-[0-9]+x[0-9]+-[0-9]+x-[0-9]+.*'

Failing to match directories using find -regex

I have some directories under /var/log in the form of IP addresses i.e. 192.168.10.3
However the regex I have written is not matching them. Any clues?
find /var/log/ -type d -regex '/var/log/([0-9]+\.){3}[0-9]+'
Cheers.
Change your regex to,
find /var/log/ -type d -regextype sed -regex ".*/\([0-9]\{1,3\}\.\)\{3\}[0-9]\+"
OR
find /var/log/ -type d -regextype posix-egrep -regex ".*/([0-9]+.){3}[0-9]+"