New answers tagged find
3
find (both the GNU and BSD variants) do not support lookahead/lookbehind. GNU grep only supports the POSIX basic and extended regular expression variants as well as a couple of others.
Lookahead and lookbehind are Perl-style regular expression elements, so you'd have to use Perl directly, or GNU grep with the -P option, which then interprets Perl regex.
1
Your syntax is correct; the gotcha here is a bit of weirdness with Cygwin's view of the filesystem.
In Cygwin, the root directory / points to what, in the Windows filesystem, is the root of your Cygwin install, which is usually c:\cygwin. To access anything outside that, you canonically have to use /cygdrive/[drive letter]; for example, the ...
3
OSX find has no -printf action. The +: command not found error is because your command is enclosed in back ticks (`), so the shell is treating the results of the find command as a command and attempting to execute them, specifically it is trying to execute + which is the first thing printed by the command you ran. You will get the same error if your run
...
0
find doesn't support extended pattern matching. Assuming you have a find with -iname support (like GNU find), I'd use something like:
find . \( -iname '*.ppt*' -o -iname '*.pps*' \) -exec unoconv -v -f pdf '{}' \;
If you're really worried about that matching files incorrectly, you can use more -o clauses with more restrictive patterns.
6
The easiest way would be to just add -mindepth 1, which will skip the first depth hierarchy and thus leave out your parent directory.
Also, you don't need an extra -exec call to rm, you can just delete the folders directly if they're empty.
find /var/www/html/content/processing -mindepth 1 -type d -mtime +1 -delete
If they're not empty:
find ...
2
The problem is that find returns the current directory (.) along with the other directories, so it deletes the processing folder as well as the subdirectories. A quick way to get around that would be to append the option
-not -name .
which stops find from outputting the current directory, and in turn stops it from being deleted.
That would work if you ...
3
From Deleting Files of the find manual:
10.1.6 Using the -delete action
The most efficient and secure method of solving this problem is to use the ‘-delete’ action:
find /var/tmp/stuff -mtime +90 -delete
So, just call find -name 'pattern%' -delete.
2
Assuming that the linked question would run on your system, you can invert the match with !:
find ! -perm -g=w
Also, you can read the documentation for find by looking into the manpages:
man find
0
The answer is "no". There is no shorthand for "find file by filename with substring in current directory or below" in GNU find. The aliases I use to work around this limitation are as follows (for zsh):
# find file in cwd by [S]ubstring, case [I]nsensitive, change to first sub[D]irectory
finds() { find . -name "*$**"; }
findsi() { find . -iname "*$**"; ...
0
I'd say the second one is slower as it involves two processes tree and grep while the first one has only one process find.
Additionally, tree writes the names of all files it finds to its output stream which is then consumed by grep. At the same time, find prints only the names of matching files.
When using find, it tries to match only the file name to ...
1
Unless you have requirements not mentioned in your question you are needlessly complicating matters. Just try something like this:
find . -maxdepth 1 -type f | grep -v *\.svg
find will print the file names it finds by default; you do not need to echo {}. You also don't need the regex at all; you could simply run:
find . -maxdepth 1 ! -name "*.svg"
In ...
1
I'd do what Adrian says,
But I'd add word boundries arond the foo so as to not accidentaly delete files containing "food" (unless it is deleting everything containing food is what you want.)
$ find -type f -exec grep -q "\<foo\>" {} \; -delete
acutally, I'd redirect the output before deleteing so I could review before deleting:
$ find -type f ...
11
Here is a safe way:
grep -lrIZ foo . | xargs -0 rm -f --
-l prints file names of files matching the search pattern.
-r performs a recursive search for the pattern foo in the given directory .
-I (capital i) causes binary files like PDFs to be skipped.
-Z ensures that file names are zero-terminated so that a name containing white space do not get ...
1
find . -type d -name "*-old" -prune -o -name "*.cpp" -print
1
$ find . -type f -name '*.cpp' ! -path '*-old*'
8
$ find -type f -exec grep -q "foo" {} \; -exec echo rm -- {} \;
This recursively searches for files containing foo. The second -exec is only run if the first exec exits successfully, i.e. if grep matches. Dryrun and remove the echo if the output seems correct.
Or alternatively
$ find -type f -exec grep -q "foo" {} \; -print
and
$ find -type f -exec ...
Top 50 recent answers are included