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

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

Related

Locate files older than 7 days and contain the word "t" in the third char of the file name

I am trying to figure out how to find all the files that are older than 7 days and contain the letter "t" as the third character (of the filename).
I only figure out how to find the files that are older that 7 days:
find /home -mtime +7 -print
To restrict to filenames having a "t" in the third position, like "25t.txt" or "data-19.doc", add this clause:
-name "??t*"
to the command. -name looks only the base name, i.e. with the path removed.
You need to specialize your find with a regex in this way:
find /home -mtime +7 -regextype posix-extended -regex '^.*\/.{2}T.*' -print
Explanation of the command:
You add a regular expression that filter all the result of the find for the first N character before the "/" character and after the "/" character have at third position the character "T". You need the first part of the regular expression ( ^.*\/ ) because the find return the result with fullpath so in the form "./dir/dir1/filename.extension". The last part of regular espression is to filter all the file with extension.
Annotation: you can substitute "T" with character you want.

List files with names that contain alphabetic characters and any other symbols (i.e numbers, punctuation, etc.) and sort them by size

I need help modifying command i have already written.
That's what i was able to achieve:
find -type f -name '*[:alpha:]*' -exec ls -ltu {} \; | sort -k 5 -n -r
However, this command also finds filenames that cosist solely of alphabetic characters, so i need to get rid of them too. I have tried doing something like this to the code:
find -type f -name '*[:alpha:]*' -and ! -name '[:alpha:]' -exec ls -ltu {} \; | sort -k 5 -n -r
But it does nothing. I understand that something is wrong with my name formatting but i have no idea how to fix it.
Character classes like [:alpha:] may only be used within character range [..] expressions, e.g. [0-9_[:alpha:]]. They may not be used alone.
[:alpha:] by itself a character range expression equivalent to [ahlp:] and matches any of the characters "ahlp" or colons. It does not match alphabetical characters.
To find files that contains both at least one alphabetic and at least one non-alphabetic characters:
find dir -type f -name '*[[:alpha:]]*' -name '*[^[:alpha:]]*'

One-liner to move files that match a pattern to a new directory

I need to move files in my home directory into a new directory. I need to select files with names that begin with a random set of 20 capital letters. The random set will consist of only 4 letters: T, A, G, or C. Because of the variability of names searched for, and the presence of names that would consist of 20 consecutive capital letters but without meeting the T, A, G, or C only requirement, a regex seems like the best option here.
I have tried to use a regular expression, but it doesn't work with this one-liner.
find source -name "([TAGC]{20})" -exec mv -i {} -t ~/dst \;
Is there a way of making this, or a similar, one-liner work for this situation, or do I need to write a script?
You can try with -regex option:
find . -regex ".*/[TAGC]{20}[^/]*$" -exec mv -i {} -t ~/dst \;
The regex must match the entire filename which begin with ./
find . -regextype posix-egrep -regex '\./[TAGC]{20}' -print

How to find all file has 4 characters in their name

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.

How to find files which contains preferred character in Linux

How can I find files and directories which contains one character for example "a"
I know that there is command ls a* but this find all files which starts with character
Try the find command to copy files which begin with the "a" character.
find /your/source/path -name 'a*' -exec cp {} /your/target/path \;
To find files which contain the "a" character use the following command.
grep -r 'a' /your/source/path/* | xargs cp /your/target/path
As the others have mentioned,
find . -name '*a*'
should do what you are looking for.
However, note that this only looks for lowercase 'a'. If you want it to be case insensitive you can use
find . -iname '*a*'
The '*' is a wildcard and means that it matches any random selection of text. If for example you had instead written
find . -name 'a'
'find' will only find files with the name 'a', without any extensions.
Thus,
find . -name '*a'
will find all files ending with the letter 'a', while
find . -name 'a*'
will find all files starting with the letter 'a'.
find [root_of_the_search] -name [pattern]
for example
find . -name "*a*"
you can copy them like this
cp `find . -name "a"` /destination/path

Resources