Use find to locate dot files up to 7 characters long - shell

I tried this:
find . \( -name ".?" -o -name ".??" -o -name ".???" -o -name ".????" -o -name ".?????" -o -name ".??????" \)
But it didn't work

You can do it your way, by explicitly putting all the possible combinations. But that is cumbersome when you have many, or when you have to modify it. There are simpler, parameterized ways, to do that.
One way would be using regular expressions:
find . -regextype posix-egrep -regex ".*/\..{1,7}"
If your find does not support regular expressions, another easy way would be to filter the output of find with the help of awk:
find . -name ".*" | awk -F"/" 'length($NF)<8 {print}'

Your command works fine. But there are shorter alternatives. E.g.:
find . -name '.*' ! -name '.???????*'

Related

find and delete folder and/or zip file in a directory [duplicate]

I was trying to get a list of all python and html files in a directory with the command find Documents -name "*.{py,html}".
Then along came the man page:
Braces within the pattern (‘{}’) are not considered to be special (that is, find . -name 'foo{1,2}' matches a file named foo{1,2}, not the files foo1 and foo2.
As this is part of a pipe-chain, I'd like to be able to specify which extensions it matches at runtime (no hardcoding). If find just can't do it, a perl one-liner (or similar) would be fine.
Edit: The answer I eventually came up with include all sorts of crap, and is a bit long as well, so I posted it as an answer to the original itch I was trying to scratch. Feel free to hack that up if you have better solutions.
Use -o, which means "or":
find Documents \( -name "*.py" -o -name "*.html" \)
You'd need to build that command line programmatically, which isn't that easy.
Are you using bash (or Cygwin on Windows)? If you are, you should be able to do this:
ls **/*.py **/*.html
which might be easier to build programmatically.
Some editions of find, mostly on linux systems, possibly on others aswell support -regex and -regextype options, which finds files with names matching the regex.
for example
find . -regextype posix-egrep -regex ".*\.(py|html)$"
should do the trick in the above example.
However this is not a standard POSIX find function and is implementation dependent.
You could programmatically add more -name clauses, separated by -or:
find Documents \( -name "*.py" -or -name "*.html" \)
Or, go for a simple loop instead:
for F in Documents/*.{py,html}; do ...something with each '$F'... ; done
This will find all .c or .cpp files on linux
$ find . -name "*.c" -o -name "*.cpp"
You don't need the escaped parenthesis unless you are doing some additional mods. Here from the man page they are saying if the pattern matches, print it. Perhaps they are trying to control printing. In this case the -print acts as a conditional and becomes an "AND'd" conditional. It will prevent any .c files from being printed.
$ find . -name "*.c" -o -name "*.cpp" -print
But if you do like the original answer you can control the printing. This will find all .c files as well.
$ find . \( -name "*.c" -o -name "*.cpp" \) -print
One last example for all c/c++ source files
$ find . \( -name "*.c" -o -name "*.cpp" -o -name "*.h" -o -name "*.hpp" \) -print
I had a similar need. This worked for me:
find ../../ \( -iname 'tmp' -o -iname 'vendor' \) -prune -o \( -iname '*.*rb' -o -iname '*.rjs' \) -print
My default has been:
find -type f | egrep -i "*.java|*.css|*.cs|*.sql"
Like the less process intencive find execution by Brendan Long and Stephan202 et al.:
find Documents \( -name "*.py" -or -name "*.html" \)
Braces within the pattern \(\) is required for name pattern with or
find Documents -type f \( -name "*.py" -or -name "*.html" \)
While for the name pattern with and operator it is not required
find Documents -type f ! -name "*.py" -and ! -name "*.html"
#! /bin/bash
filetypes="*.py *.xml"
for type in $filetypes
do
find Documents -name "$type"
done
simple but works :)
I needed to remove all files in child dirs except for some files. The following worked for me (three patterns specified):
find . -depth -type f -not -name *.itp -and -not -name *ane.gro -and -not -name *.top -exec rm '{}' +
This works on AIX korn shell.
find *.cbl *.dms -prune -type f -mtime -1
This is looking for *.cbl or *.dms which are 1 day old, in current directory only, skipping the sub-directories.
find MyDir -iname "*.[j][p][g]"
+
find MyDir -iname "*.[b][m][p]"
=
find MyDir -iname "*.[jb][pm][gp]"
What about
ls {*.py,*.html}
It lists out all the files ending with .py or .html in their filenames

Using many -o in bash find

I am trying to make a small script to find all the video files on my computer.
#!/bin/bash
videos=("avi" "mp4" "mkv" "m4v" "wmv" "ogg" "mov") # etc...
for i in ${videos[#]}
do
find ~ -iname "*.$i"
done
This becomes prohibitively slow, as the program has to loop through the files many times. The only alternate solution I could find was
find ~ -iname "*.avi" -o -iname "*.mp4" -o -iname "*.mkv" # and on and on...
Is there a better, more idiomatic way of doing it?
You could use -regextype egrep -regex, example :
find -regextype egrep -regex '.*\.(avi|mp4|mkv|etc)'
You can use -iregex option:
find . -type f -iregex '.*\.\(avi\|mp4\|mkv\)'

Bash find exclude caseinsenstive

I am using the below to find files with matching extensions that I need. however, I can't seem to get it to exclude certain keywords I want and also make sure keywords case insenstive...
find . -type f \( -name \*.avi -o -name \*.mkv -o -name \*.mp4 -o -name \*.VOB \)
any kind of help I can get on this is greatly appreciated!
case insensitive: use -iname instead of -name.

Or condition in bash pattern

I'm searching some files with: find . -name "*.en.php" and find . -name "*.fr.php".
I want both commands in the same line, something like : find . -name "*.(en|fr).php" but it doesn't work.
Thanks in advance for your help.
EDIT
my command is like this : find . -not -path Config -name "*.fr.php", is there a solution do not repeat -not -path Config ?
Try:
find -name "*.en.php" -o -name "*.fr.php"
If you for example want to run command on each found file, than you need to additional ()
(this will count num of lines in all found files):
find \( -name "*.en.php" -o . -name "*.fr.php" \) -exec cat {} \; | wc -l
You should be able to combine expression with an or operator, thus:
find . -name '*.en.php' -o -name '*.fr.php' ...
You can see all the operators in the man page listed under OPERATORS (and, or, not, parentheses and so forth).
Use the find -o operator, eg.
find . -name "*.en.php" -o -name "*.fr.php"
Edit:
Like so:
find . -path './Config' -prune -o \( -name "*.en.php" -o -name "*.fr.php" \)
The default operator in find (if one is ommited) is and, the parentheses group the name expression. I've added -prune to prevent find from recursing into the Config directory.

bash: Filtering out directories and extensions from find?

I'm trying to find files modified recently with this
find . -mtime 0
Which gives me
en/content/file.xml
es/file.php
en/file.php.swp
css/main.css
js/main.js
But I'd like to filter out the en and es directories but would like to grab anything else. In addition, I'd like to filter out .swp files from the results of those.
So I want to get back:
css/main.css
js/main.js
xml/foo.xml
In addition to every other file not within es/en and not ending in .swp
properly, just in find:
find -mtime 0 -not \( -name '*.swp' -o -path './es*' -o -path './en*' \)
The -prune command prevents find form descending down the directories you wish to avoid:
find . \( -name en -o -name es \) -prune , -mtime 0 ! -name "*.swp"
Try this:
find . -mtime 0 | grep -v '^en' | grep -v '^es'
Adding the cap character at the beginning of the pattern given to grep ensures that it is a must to find the pattern at the start of the line.
Update: Following Chen Levy's comment(s), use the following instead of the above
find . -mtime 0 | grep -v '^\./en' | grep -v '^\./es'
find is great but the implementation in various UNIX versions differs, so I prefer solutions that are easier to memorize and using commands with more standard options
find . -mtime 0 | grep -v '^en' | grep -v '^es' | grep -v .swp
The -v flag for grep makes it return all lines that don't match the pattern.
The -regex option of find(1) (which can be combined with the -E option to enable extended regular expressions) matches the whole file path as well.
find . -mtime 0 -not \( -name '*.swp' -o -regex '\./es.*' -o -regex '\./en.*' \)
find "$(pwd -P)" -mtime 0 -not \( -name '*.swp' -o -regex '.*/es.*' -o -regex '.*/en.*' \)

Resources