Tag Info

Hot answers tagged

50

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 ...


43

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.


29

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.


29

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 ...


24

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 ...


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, ...


22

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 …) ...


22

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 ...


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

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 ...


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 ...


17

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 ...


15

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 ...


15

My server had been infected and the attacker spread (...) malicious code (...). NUKE IT FROM ORBIT! That's the only way to be sure that everything is secure again. If you want you could make an image an analyze how the attacker got in the system but for that system the only salvation is reinstall from scratch. No removing only the "dot files" will make ...


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

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 . ...


13

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, ...


13

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 ...


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

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


13

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"))))' ...


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 ...


13

Don't try to parse find output except as a last resort. It is important to realize that on Unix file systems, file names are not strings (a common misconception) but rather binary blobs which can contain any character except / and the null character. Parsing file names safely and correctly is enough of a pain that 99% of the time you'll just want to avoid ...


12

The '+' makes one big command line out of all found files to minimize the number of commands to be run. Given the case that a find command finds four files. find . -type f -exec command '{}' \; would produce command file1 command file2 command file3 command file4 On the other hand find . -type f -exec command '{}' \+ produces command file1 file2 ...


12

Summary: If there ever was a shell that expanded {}, it's really old legacy stuff by now. In the Bourne shell and in POSIX-compliant shells, braces ({ and }) are ordinary characters (unlike ( and ) which are word delimiters like ; and &, and [ and ] which are globbing characters). The following strings are all supposed to be printed literally: $ echo { ...


12

find -iname '*.xml' Otherwise, your shell expands *.xml to XYZ.xml, and the command that actually gets executed is find -iname XYZ.xml The reason it works if there are no XML files in the current directory is that shells generally leave wildcards unexpanded if they don't match anything. In general, any time you want wildcards to be expanded by a ...


12

You can add to find the following expression: -printf '%Tc %p\n' to see something like Sun Aug 14 06:29:38 2011 ./.nx/config/host.nxs or -printf '%TD %TT %p\n' for 08/14/11 06:29:38.2184481010 ./.nx/config/host.nxs or -printf '%T+ %p\n' if you have GNU find, to see 2011-08-14+06:29:38.2184481010 ./.nx/config/host.nxs This last one, if available, ...



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