Tag Info

Hot answers tagged

25

Why do so difficult with -regex? Just do it with -name find -type f -name "[[:upper:]]*"


25

From the findutils find manpage: If no expression is given, the expression -print is used (but you should probably consider using -print0 instead, anyway). (-print is a find expression.) The POSIX documentation confirms this: If no expression is present, -print shall be used as the expression. So find . is exactly equivalent to find . -print; ...


24

That's a really nice catch. From a quick look at the source code for GNU find, I would say this boils down to how fnmatch behaves on invalid byte sequences (pred_name_common in pred.c): b = fnmatch (str, base, flags) == 0; (...) return b; This code tests the return value of fnmatch for equality with 0, but does not check for errors; this results in any ...


20

The quotes protect the contents from shell wildcard expansion. Run that command (or even simpler just echo *test.txt in a directory with a footest.txt file and then one without any files that end in test.txt and you will see the difference. $ ls a b c d e $ echo *test.txt *test.txt $ touch footest.txt $ echo *test.txt footest.txt The same thing will ...


13

Never embed {} in the shell code! (by the way, some find implementations won't let you do that, and POSIX leaves the behaviour unspecified when {} is not on its own in an argument to find) find . -name accept_ra -exec sh -c 'echo 0 > "$1"' sh {} \; Or: find . -name accept_ra -exec sh -c 'for i do echo 0 > "$i"; done' sh {} + Note that the ...


13

find -name option uses shell pattern matching notation to perform matching filename. * is a pattern matching multiple characters, shall match a string of zero or more characters. find uses fnmatch to check pattern matching, so you can use ltrace to check the result: $ touch $'\U1212'aa $ touch D$'\351'sinstaller $ LC_ALL=en_US.utf8 ltrace -e fnmatch find ...


13

-print is the default action. Some find predicates are considered as actions as opposed to filters or conditions. For instance, -type f is not an action. -exec is an action even though it can also be used as a condition. Actions include -print, -exec and -ok. Some find implementations have other non-standard action predicates like the -print0, -printf, ...


12

That has nothing to do with bash or sudo. The find command is using the -exec action to run the given command on each file found. In this case, the command being run is touch -d @0 If you check man touch on a GNU system, you will find that -d, --date=STRING parse STRING and use it instead of current time So, the -d is a way of choosing ...


11

I'm not sure: grep -r -i 'the brown dog' /* is really what you meant. That would mean grep recursively in all the non-hidden files and dirs in / (but still look inside hidden files and dirs inside those). Assuming you meant: grep -r -i 'the brown dog' / A few things to note: Not all grep implementations support -r. And among those that do, the ...


11

A long time ago (in 7th edition, 32V, 4.2BSD, 4.3BSD), at the system-call level a zero-length pathname denoted the current working directory (when used for lookup; it was disallowed when trying to create or delete a file or directory). In System III, it was an error to use a zero-length pathname under all circumstances, and the POSIX standard has this to say ...


11

Here is a general pattern: find /directory/containing/files -type f -exec grep -H 'pattern_to_search' {} + Here at first find will search all files in the directory containing necessary files, you can also use wildcards e.g. *.txt to look for only files ending with .txt. In that case the command would be: find /directory/containing/files -type f -name ...


10

Try: find / -xdev -type f -size +100M It lists all files that has size bigger than 100M. If you want to know about directory, you can try ncdu. If you aren't running Linux, you may need to use -size +204800 or -size +104857600c, as the M suffix to mean megabytes isn't in POSIX. find / -xdev -type f -size +102400000c


9

Use the -perm test to find in combination with -not: find -type d -not -perm 775 -o -type f -not -perm 664 -perm 775 matches all files with permissions exactly equal to 775. -perm 664 does the same for 664. -not negates the test that follows, so it matches exactly the opposite of what it would have: in this case, all those files that don't have the ...


9

There are errors in your assumptions, but first some background: You should discern two uses of -exec: with \; the {} will be replaced by a single item found with + the {} will be replaced by many items (as many as the commandline can hold). Therefore your example of -exec use invokes as many cp command as items found by find. Using find ... -exec ...


9

I can think of two possible solutions: If you have installed mv from GNU coreutils (which probably is the case), then the following command... find / -name "HAHA" -type f -exec mv --backup=numbered "{}" /home \; ...will move all files called HAHA to /home. The --backup=numbered option of mv ensures that every time the mv command executes, it will check ...


8

The pattern given to -name has to match the entire base filename. The behaviour of the -name pattern is defined as: The primary shall evaluate as true if the basename of the current pathname matches pattern This means it's true when the whole of the basename matches the pattern you gave. You can think of a pattern as being basically like a shell glob: ...


8

The reason why the find command is slow That is a really interesting issue... or, honestly, mallicious: The command find . -mindepth 2 -mtime +5 -print -delete is very different from the usual tryout variant, leaving out the dangerous part, -delete: find . -mindepth 2 -mtime +5 -print The tricky part is that the action -delete implies the option ...


8

With GNU mv: find path_A -name '*AAA*' -exec mv -t path_B {} + That will use find's -exec option which replaces the {} with each find result in turn and runs the command you give it. As explained in man find: -exec command ; Execute command; true if 0 status is returned. All following arguments to find are taken to be arguments ...


8

You can just do the whole thing with (GNU) find and sort, no need for du: $ find . -iname '*png' -printf '%s %p\n' | sort -rn 68109 ./7.png 21751 ./2.png 21751 ./1.png 5393 ./6.png 2542 ./5.png 1717 ./4.png 1003 ./3.png 878 ./10.png 793 ./9.png 587 ./8.png


8

A -exec needs to be terminated with a ; or a +. The ; causes the command to execute once per found filename, whereas + causes the command to be executed once for all filenames. Here is a working example: find ! -iname "*.JPG" -exec dirname {} \; Some notable differences from your attempts: There must be a space between {} and \; The ; must be escaped, ...


8

If you want to crawl on dirs and subdirs: find /home/place/to/crawl -type f -exec file --mime-type {} \; | awk '{if ($NF == "image/jpeg") print $0 }' What it does? Search all inodes with the type file Execute the command file, to get a jpeg header of the file like: image/jpeg awk Edit: Added @Franklin tip, to use file with -i to use the mime string ...


7

I am a little confused by wording of the question. If what you want to do is delete everything in the ./Library/Caches directory apart from a single folder called Snapshots, there is no need to use find. In bash, shell globs are the simplest way: shopt -s extglob # probably already enabled echo Library/Caches/!(Snapshots) If this prints all the ...


7

In addition to @Gnouc answer , you can also add ls -la to get more details. You should have sudo privileges to do that . $ find / -xdev -type f -size +100M -exec ls -la {} \; | sort -nk 5


7

POSIX defined find -exec utility_name [argument ...] {} + as: The end of the primary expression shall be punctuated by a <semicolon> or by a <plus-sign>. Only a <plus-sign> that immediately follows an argument containing only the two characters "{}" shall punctuate the end of the primary expression. Other uses of the ...


7

As a start, you could print out files with a .jpg file extension with: sudo find / -name *.jpg -print See how that behaves, modify to suit, and you can then pipe the output into another function rather than just printout if you'd like. For example, if you want to allow .JPG as well: sudo find / -iname "*.jpg"


7

If you want to list files containing a JPEG image regardless of the extension in the filename, you could use find + file to list the files with mime type image/jpeg: find . -type f -exec sh -c ' file --mime-type "$0" | grep -q image/jpeg\$ && printf "$0\n" ' {} \; or find . -type f -exec sh -c ' mt=$(file --brief --mime-type "$0") [ ...


7

They are the same, they both write out the entire directory hierarchy from the current directory. From POSIX find documentation: The following commands are equivalent: find . find . -print


6

I don't know of any other way besides scanning the directory tree in question to collect the file sizes so that you can determine the largest file. If you know that there's a threshold of size you can instruct find to dismiss files that are below this threshold size. $ find . -type f -size +50M .... Would dismiss any files below the size of 50MB. If you ...


6

You want this: find / -type f -ctime +30 -mtime +30 -atime +30 -print0 | xargs -0 -P 4 md5sum You want the list of files to be fedinto the md5sum command. This is done with find / | xargs md5sum. Then you want to not have to worry about crazy characters (spaces, newlines, whatever) in filenames, so we use -print0 for find and -0 for xargs.



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