I need to look in a directory for sub-directories that all start by "course" but they have version next. For example
course1.1.0.0
course1.2.0.0
course1.3.0.0
So how should I modify my command to make it give me the right list of directories?
find test -regex "[course*]" -type d
You can do:
find test -type d -regex '.*/course[0-9.]*'
it will match files whose name is course plus an amount of numbers and dots.
For example:
$ ls course*
course1.23.0 course1.33.534.1 course1.a course1.a.2
$ find test -type d -regex '.*course[0-9.]*'
test/course1.33.534.1
test/course1.23.0
You need to remove the brackets, and use the proper wildcard syntax for regexes (.*):
find test -regex "course.*" -type d
You can also use the more familiar shell wildcard syntax, by using the -name option instead of -regex:
find test -name 'course*' -type d
I suggest using a regex for precise matching of version number sub directories:
find . -type d -iregex '^\./course\([0-9]\.\)*[0-9]$'
TESTING:
ls -d course*
course1.1.0.0 course1.1.0.5 course1.2.0.0 course1.txt
find . -type d -iregex '^\./course\([0-9]\.\)*[0-9]$'
./course1.1.0.0
./course1.1.0.5
./course1.2.0.0
UPDATE: To match [0-9]. exactly 3 times use this find command:
find test -type d -regex '.*/course[0-9]\.[0-9]\.[0-9]\.[0-9]$'
Related
I have a folder with these files:
sample.jpg
sample.ods
sample.txt
sample.xlsx
Now, I need to find and remove files that end with either .ods or .xlsx.
To fish them out I initially use:
ls | grep -E "*.ods|*.xlsx"
This gives me:
sample.ods
sample.xlsx
Now, I don't want to parse ls so I use find:
find . -type f -regextype grep -regex '.*/*.ods\|*.xlsx' | wc -l
But that gives me the output of 1 while I expect to have 2 files before I extend the command to:
find . -type f -regextype grep -regex '.*/*.ods\|*.xlsx' | xargs -d"\n" rm
Which works but removes only the .ods file but not the .xlsx one.
What am I missing here?
I'm on ubuntu 18.04 and my find version is find (GNU findutils) 4.7.0-git.
You don't need to use regex here, just use -name and -or and so:
find . -type f -name "*.ods" -or -name "*.xlsx" -delete
Find files ending with either ods or xlsx and delete
If you really wanted to use regex, you could use the following:
find . -maxdepth 1 -regextype posix-extended -regex "(.*\.ods)|(.*\.xlsx)" -delete
Make sure that the expressions are in between brackets
Have the following regex to detect a file start with 3 consecutive capital letters:
(?=(.*[A-Z]){3})(?=(.*[a-z]){3})(?=.*(_|[^\w]))
When I search a file I found it:
root#node01:~# find . -type f -name DASD-680.mp4.part
./DASD-680.mp4.part
But when I search didn't found any results:
root#RPI01:~# find . -type f -regextype egrep -regex "(?=(.*[A-Z]){3})(?=(.*[a-z]){3})(?=.*(_|[^\w]))"
What's the exactly problem???
Update:
file exists:
TAD-007.mp4
but no result with this:
/usr/bin/find . -type f -regextype posix-extended -regex "[A-Z]{3}.*"
thanks!
find gives list of files prefixed with ./ like ./DASD-680.mp4.part
the regex motif must match the whole filename, included ./
the regex flavor doesn't support quantifiers
Here is a way to do what you want:
find . -type f -regex '..[A-Z][A-Z][A-Z].*'
where the first 2 dots matches ./
You cal also use:
find . -type f -regex '\./[A-Z][A-Z][A-Z].*'
I'm trying to write a regex to find files recursively with Mac Terminal (bash, not zsh even though Catalina wants me to switch over for whatever reason) using the find command. I'm looking for files that are:
Exactly 8 hexadecimal digits (0-9 and A-F)
But NOT only decimal digits (0-9)
In other words, it would match A1234567, ABC12DEF, 12345ABC, and ABCDABCD, but not 12345678 or 09876543.
To find files that are exactly 8 hex digits, I've used this:
find -E . -type f -regex '.*/[A-F0-9]{8}'
The .*/ is necessary to allow the full path name to precede the filename. This is eventually going to get fed to rm, so I have to keep the path.
It SEEMS like this should work to fulfill both of my requirements:
find -E . -type f -regex '.*/(?![0-9]{8})[A-F0-9]{8}'
But that returns an error:
find: -regex: .*/(?![0-9]{8})[A-F0-9]{8}: repetition-operator operand invalid
It seems like the find command doesn't support lookaheads. How can I do this without one?
With any POSIX-compliant find
find . -type f \
-name '????????' \
! -name '*[![:xdigit:]]*' \
-name '*[![:digit:]]*'
And if you insist on using regexps for this, here you go
find -E . -type f \
-regex '.*/[[:xdigit:]]{8}' \
! -regex '.*/[[:digit:]]*'
Those who use GNU find should drop -E and insert -regextype posix-extended after paths to make this work.
It's probably easiest to just filter out the results you don't like:
find -E . -type f -regex '.*/[A-F0-9]{8}' -print | egrep -v '.*/[0-9]{8}$'
$ find -E . -type f -regex '.*/[A-F0-9]{8}' -print
./01234567
./ABCDEFAF
./ABCDEF01
./ABCDEF2A
./ABCDEFA2
./x/01234567
./x/ABCDEFAF
./x/ABCDEF01
./x/ABCDEF2A
./x/ABCDEFA2
$ find -E . -type f -regex '.*/[A-F0-9]{8}' -print | egrep -v '.*/[0-9]{8}$'
./01234567
./ABCDEFAF
./ABCDEF01
./ABCDEF2A
./ABCDEFA2
./x/01234567
./x/ABCDEFAF
./x/ABCDEF01
./x/ABCDEF2A
./x/ABCDEFA2
My find didn't understand -E and was inexplicably grumpy about -regex in general, but this still worked:
find . -type f -name '[A-F0-9][A-F0-9][A-F0-9][A-F0-9][A-F0-9][A-F0-9][A-F0-9][A-F0-9]' -a -name '*[A-F]*'
Not as elegant as oguz ismail's, but easier to read for my clogged brain, lol
How to find multiple filenames with the bash find command?
$ find /path/* -type f -name pattern
The pattern should match a list of file names:
fname1.jpg
fname2.png
myfile.css
example.gif
I tryed with
https://alvinalexander.com/linux-unix/linux-find-multiple-filenames-patterns-command-example
find multiple filenames command: finding three filename extensions
find . -type f \( -name "*cache" -o -name "*xml" -o -name "*html" \)
and it works.
Anyway I think it would be cleaner with a -name pattern, rather than with a list of -names.
from
$ man find
-name pattern
I m searching for something like: -name '[fname2.png|myfile.css|example.gif ]'
-regex alternative would look as follows:
find . -type f -regextype posix-egrep -regex ".+\.(jpg|png|css)$"
As for -name option:
-name pattern - Base of file name (the path with the leading
directories removed) matches shell pattern.
Shell pattern is not a full-fledged regex pattern.
Just mix them:
find -name "aoc*" -regextype awk -regex ".*[0-9].(class|scala)"
This searches for files, matching shell-pattern aoc* and end in number, with ending .class or .scala.
For your example:
find -name "fname*" -regextype awk -regex ".*[0-9].(png|jpg|css)"
Available types are listet with:
find -regextype -help
However, I first tried "-regextype sed" which is available, but sed itself has options, changing the styles of regexes. And patterns I used to use with sed didn't work, but since the pattern works with awk, it's sufficient for me.
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]+.*'