Tag Info

Hot answers tagged

71

Why not simply use this: find -name "*.xls" -o -name "*.csv" You don't need regex for this. If you absolutely want to use regex simply use find -regex ".*\.\(xls\|csv\)"


61

I'd strongly suggest not to use find -L for the task (see below for explanation). Here are some other ways to do this: If you want to use a "pure find" method, it should rather look like this: find . -type l -xtype l (xtype is a test performed on a dereferenced link) This may not be available in all versions of find, though. But there are other options ...


56

Short answer (closest to your answer, but handles spaces) OIFS="$IFS" IFS=$'\n' for file in `find . -type f -name "*.csv"` do echo "file = $file" diff "$file" "/some/other/path/$file" read line done IFS="$OIFS" Better answer (also handles wildcards and newlines in file names) find . -type f -name "*.csv" -print0 | while IFS= read -r -d ...


49

locate(1) has only one big advantage over find(1): speed. find(1), though, has many advantages over locate(1): find(1) is primordial, going back to the very first version of AT&T Unix. You will even find it in cut-down embedded Linuxes via Busybox. It is all but universal. locate(1) is much younger and nonstandard. The earliest ancestor of ...


41

You need to quote your argument error* because the shell expands it. So what you're actually running now is find -name error_log, because that's what the shell can expand it to (there's a file named error_log in your current directory). find . -name 'error*' Is the correct invocation for your use case.


35

Recent versions of GNU find seem to have an -iname flag, for case-insensitive name search. find . -iname "WSFY321.c"


33

fdupes can do this. From man fdupes: Searches the given path for duplicate files. Such files are found by comparing file sizes and MD5 signatures, followed by a byte-by-byte comparison. In Debian or Ubuntu, you can install it with apt-get install fdupes. In Fedora/Red Hat/CentOS, you can install it with yum install fdupes. On Arch Linux you can use ...


31

You missed a ; or a + and a {}: find . -exec grep chrome {} \; or find . -exec grep chrome {} + find will execute grep and will substitute {} with the filename(s) found. The difference between ; and + is that with ; a single grep command for each file is executed whereas with + as many files as possible are given as parameters to grep at once.


26

yes, you can: find /media/d/ -type f -size +50M ! \( -name "*deb" -o -name "*vmdk" \) Explanation from the POSIX spec: ! expression : Negation of a primary; the unary NOT operator. ( expression ): True if expression is true. expression -o expression: Alternation of primaries; the OR operator. The second expression shall not be evaluated ...


26

With GNU find, you can use the -quit predicate: find . ... -print -quit If all you do is printing the name, and assuming the filenames don't contain newline characters, you could do: find . ... -print | head -n 1 That will not stop find after the first match, but possibly, depending on timing upon the second match or later. Basically, find will be ...


24

Well, the generic case that works with any command that writes to stdout is to use xargs, which will let you attach any number of command-line arguments to the end of a command: $ find … | xargs grep 'search' Or to embed the command in your grep line with backticks or $(), which will run the command and substitute its output: $ grep 'search' $(find …) ...


23

As for the find command, you can also just add more -exec commands in a row: find . -name "*" -exec chgrp -v new_group '{}' \; -exec chmod -v 770 '{}' \; Note that this command is, in its result, equivalent of using chgrp -v new_group file && chmod -v 770 file on each file. All the find's parameters such as -name, -exec, -size and so on, ...


20

Some versions* of find require that you provide a path argument which is a directory from which to start searching. Dot . simply represents the current directory is is usually where you want to search. You could replace this with any path that you want to be the base of the search. In some versions of find this can be left because the current directory is ...


20

In Unix, a filename beginning with a dot, like .erlang.cookie, is considered a hidden file and is not shown by bare ls. Type ls -a to also show hidden files. From man ls: -a, --all do not ignore entries starting with . However, you can show a hidden file with ls if you specify the name: $ ls .erlang.cookie .erlang.cookie


19

Using find: find /tmp/ -type f -exec md5sum {} + | grep '^file_md5sum_to_match' If you searching through / then you can exclude /proc and /sys see following find command example : Also I had done some testing, find take more time and less CPU and RAM where ruby script is taking less time but more CPU and RAM Test Result Find [root@dc1 ~]# time find ...


17

List the directories deeply-nested-first. find . -depth -type d -exec rmdir {} \; 2>/dev/null (Note that the redirection applies to the find command as a whole, not just to rmdir. Redirecting only for rmdir would cause a significant slowdown as you'd need to invoke an intermediate shell.) You can avoid running rmdir on non-empty directories by passing ...


17

You are telling grep to search 2 locations. grep only shows the full location if multiple locations are searched. For example touch /tmp/herp /tmp/derp cd /tmp echo "foo" > herp echo "foo" > derp Notice how if I search just 1 file, grep omits the file name grep -i "foo" /tmp/herp foo But if I specify multiple search locations, grep says where ...


16

The problem is, you didn't quote your -name parameter. Do this instead: find . -name '*.java' Explanation Without the quotes, the shell interprets *.java as a glob pattern and expands it to any file names matching the glob before passing it to find. This way, if you had, say, foo.java in the current directory, find's actual command line would be: find . ...


15

You can combine criteria with -o as suggested by Shadur. Note that -o has lower precedence than juxtaposition, so you may need parentheses. find . -name '*.jpg' -o -name '*.png' find . -mtime -7 \( '*.jpg' -o -name '*.png' \) # all .jpg or .png images modified in the past week On Linux, you can use -regex to combine extensions in a terser way. The ...


15

With GNU find, or other versions of find that have it: find . -iname 'WSFY321.c' With other versions: find . -name '[Ww][Ss][Ff][Yy]321.[Cc]' Or a compromise that's slower but easier to type: find . -name '????321.c' | grep -i '/WSFY[^/]*$' Or in zsh: print -rl -- **/(#i)WSFY321.c


14

$ find . -type f -print0 | xargs -0 grep foo $ grep -r foo . # GNU grep only and in zsh with setopt extendedglob, $ grep foo **/*(.)


14

This script fails if any file name contains spaces or shell globbing characters \[?*. The find command outputs one file name per line. Then the command substitution `find …` is evaluated by the shell as follows: Execute the find command, grab its output. Split the find output into separate words. Any whitespace character is a word separator. For each word, ...


14

If bash can't find a match, it passes the literal string to the application with *s unexpanded. For example: $ ls foo $ cat /tmp/test echo $1 $ /tmp/test *foo* foo $ /tmp/test *bar* *bar* bash expanded *foo* because it matched, but passed *bar* directly because it didn't. The nullglob option will tell bash to resolve non-matching patterns to the empty ...


14

Some versions of sort have a -z option, which allows for null-terminated records. find folder1 folder2 -name "*.txt" -print0 | sort -z | xargs -r0 myCommand Additionally, you could also write a high-level script to do it: find folder1 folder2 -name "*.txt" -print0 | python -c 'import sys; sys.stdout.write("\0".join(sorted(sys.stdin.read().split("\0"))))' ...


14

Find supports -o find . ! '(' -name '*.txt' -o -name '*.pdf' ')' You need the parenthesis to make the precedence right. Find does a lot of stuff; I suggest reading through its manpage. You can also do an or in grep (but really, you should not parse the output of ls) ls | grep -Ev '\.(txt|pdf)$' | column


14

You could pipe the output of find through head: find . -name '*.txt' | head -3


13

Use the -o flag between different parameters. find ./ -type f \( -iname \*.jpg -o -iname \*.png \) works like a charm.


13

find will look through a directory structure and return results based on a glob: find /your/dir -name "*abcde*" Adding the -type f switch will refine your search criteria to only return files. find /your/dir -type f -name "*abcde*" You could also include other switches like -maxdepth 2 to restrict the search to 2 levels of directories bellow the ...


13

Simply keep it within the realm of find: find . -type f -exec grep "something" {} \; -quit This is how it works: The -exec will work when the -type f will be true. And because grep returns 0 (success/true) when the -exec grep "something" has a match, the -quit will be triggered.


13

The shell is expanding the >> % part before xargs sees it. If you need to do shell redirections, you'll have to try something like this: find . -name "*.txt" -exec sh -c ' echo "hello world" >> "$0" ' {} \; How it works: find replaces {} with each file that it matches bash -c "some command" arg0... sets $0... inside the "some ...



Only top voted, non community-wiki answers of a minimum length are eligible