How to find all file has 4 characters in their name - linux

I want to find all files that have 4 characters in their name I try this command
ls [0-9A-Za-z]{4}
and
ls *????
they don't work any help

Try this :
ls [0-9A-Za-z][0-9A-Za-z][0-9A-Za-z][0-9A-Za-z]
or simpler as proposed in the comments by #Paul R
ls ????
You can't mix regex notation with glob notation.
If you want to use regex, you can give a try to find :
find . -type f -regextype posix-egrep -regex './\w{4}'
Note:
\w
is the same as
[0-9A-Za-z_]
in regex, and
-typef is for filtering files only.

There are two ways:
ls ????
or
echo ????
If you want to omit directories it's another story.

Related

how to delete files have specific pattern in linux?

I have a set of images like these
12345-image-1-medium.jpg 12345-image-2-medium.png 12345-image-3-large.jpg
what pattern should I write to select these images and delete them
I also have these images that don't want to select
12345-image-profile-small.jpg 12345-image-profile-medium.jpg 12345-image-profile-large.png
I have tried this regex but not worked
1234-image-[0-9]+-small.*
I think bash not support regex as in Javascript, Go, Python or Java
for pic in 12345*.{jpg,png};do rm $pic;done
for more information on wildcards take a look here
So long as you do NOT have filenames with embedded '\n' character, then the following find and grep will do:
find . -type f | grep '^.*/[[:digit:]]\{1,5\}-image-[[:digit:]]\{1,5\}'
It will find all files below the current directory and match (1 to 5 digits) followed by "-image-" followed by another (1 to 5 digits). In your case with the following files:
$ ls -1
123-image-99999-small.jpg
12345-image-1-medium.jpg
12345-image-2-medium.png
12345-image-3-large.jpg
12345-image-profile-large.png
12345-image-profile-medium.jpg
12345-image-profile-small.jpg
The files you request are matched in addition to 123-image-99999-small.jpg, e.g.
$ find . -type f | grep '^.*/[[:digit:]]\{1,5\}-image-[[:digit:]]\{1,5\}'
./123-image-99999-small.jpg
./12345-image-3-large.jpg
./12345-image-2-medium.png
./12345-image-1-medium.jpg
You can use the above in a command substitution to remove the files, e.g.
$ rm $(find . -type f | grep '^.*/[[:digit:]]\{1,5\}-image-[[:digit:]]\{1,5\}')
The remaining files are:
$ l1
12345-image-profile-large.png
12345-image-profile-medium.jpg
12345-image-profile-small.jpg
If Your find Supports -regextype
If your find supports the regextype allowing you to specify which set of regular expression syntax to use, you can use -regextype grep for grep syntax and use something similar to the above to remove the files with the -execdir option, e.g.
$ find . -type f -regextype grep -regex '^.*/[[:digit:]]\+-image-[[:digit:]]\+.*$' -execdir rm '{}' +
I do not know whether this is supported by BSD or Solaris, etc.., so check before turning it loose in a script. Also note, [[:digit:]]\+ tests for (1 or more) digits and is not limited to 5-digits as shown in your question.
Ok I solve it with this pattern
12345-image-*[0-9]-*
eg:
rm -rf 12345-image-*[0-9]-*
it matches all the file names start with 12345-image- then a number then - symbol and any thing after that
as I found it's globbing in bash not regex
and I found this app really use full

regex for find cmd to output all files with an 'e' in them but not at start/end of name

I need a find command to output all filenames in a certain directory that contain an 'e' but the 'e' must not be at the start/end of the file name.
$ ls .
emil eva extreme let yes
The find command I am looking for should only output let and yes, not the other names.
Things I have tried so far:
find dir -name "*e*
find dir -name "^e$"
find dir -name "[a-z]e[a-z]"
Similar questions I cannot figure out:
list all files that:
begin with a,z or y
do not begin with x,z or y
consist of only one char
consist of only two chars
consist of only two OR three chars
Thanks in advance :)
if you want to understand, enter the command
man find
I think you can easily solve the others and if not, ask them in another question.
solution for case with letters "e":
find . -maxdepth 1 -type f -regextype sed -regex '^\./[^e].*e.*[^e]$'
explanation:
find. looks in the current directory
-maxdepth 1 does not go into subdirectories
-type f only displays files
-regextype sed sets the type of regular expressions to those for sed
-regex '^\./[^e].*e.*[^e]$' the regular expression
^ the beginning of the sequence
\. literal dot
/ literal '/'
[^e] character not being the letter e
.* any number of any characters
e literal sign e
$ end of the sequence

find command regex with optional subexpression

I have the following file list
file <-
file.2019041543764832 <-
file.2019041643764832 <-
file.2019041243764832
file.2019041143764832
I want to find all the marked files which are prefixed with file and optionally suffixed by the dates 20190415xxxxx or 20190416xxxxx
I have tried the following but it does not yield any output.
find . -regex 'file(\.2019041(5|6)[0-9].*)?' -regextype egrep
I need some help with the correct regex type and the correct synatx to achieve this.
find . -regex './file\(.2019041\(5\|6\)[0-9]*\)?' -regextype egrep
or just
find . -regex './file\(.2019041[56][0-9]*\)?'
(When using find ., my version of find prefixes the matches with ./, so I added that to the regexp.)

Unix search ignore caps

I want to search for a specific file by name in Solaris.
But I don't know whether the file has caps in the name or not, so I want to ignore the caps.
If i use:
find . -name 'word'
it won't find me file that is named WoRd.
I know I have to use -i somehow, but I just can't manage to find the correct syntax.
Thanks.
Please try this one. It will be solved your problem
$find . -iname 'word'
Where i for ignore case sensitive
Try this, -type f matches files only, you were correct to assume using the -i flag
find . -type f -print | grep -i "word"
Also check out this answer that goes deeper: https://unix.stackexchange.com/questions/40766/help-understanding-find-syntax-on-solaris

Finding files of fixed length

I am trying to find file names that start with the letter 'a' and are of length 6. I have tried many variations, the latest one being:
find /usr/bin -type f -regex "^[a]" > grep {6}
However I get an error message of:
find: paths must precede expression: {6}
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
What am I doing wrong?
Without any regexes, just globbing:
find /usr/bin -type f -name 'a?????'
References:
Findutils manual: Shell pattern matching
Bash manual, Filename expansion and pattern matching
I would use the following command which is using extended posix regexes:
find /usr/bin -type f -regextype posix-extended -regex '.*/a.{5}'
Let me explain the pattern from the end:
.{5} matches five arbitrary characters
a matches a literal a
the / matches the path delimiter right before the filename
.* is the path, in this case /usr/bin
Btw, a simple command which does not even require a special regex engine would be:
find /usr/bin -type f -regex '.*/a.....'
$ is the end of the filename
..... are five arbitrary characters
a is a literal a
.*/ is the preceding path
Another thing. While your regex is wrong and grep is not required at all, why do you get this strange error message?
You are using find ... > grep where I think you wanted to use find ... | grep. Note that > will redirect the output of the find command to a file. In this case a file named grep. If you want to redirect the output of the find command into the input of a grep command you need to use the pipe symbol find ... | grep.
A > filename redirection can appear anywhere in a command line, it does not necessarily have to be at the end. That' why {6} is interpreted as the last argument to find. Since this argument is not expected, find supposed that you accidentally passed a search path at the end, which is a common mistake. That's why the message.

Resources