Tag Info

Hot answers tagged

24

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


20

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


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


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

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

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


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

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


6

Have you tried ? fc-list | grep -i "media" Also give a try to fc-scan, fc-match


6

Use find with an absolute path. find /path/ -size +20M It will print the whole path. If you do not know the working directory then use command substitution for pwd like this: find "`pwd`" -size +20M #or like this: find "$(pwd)" -size +20M To get your working directory Anyway, it seems that Bash man now advise to use $() over `` so you should ...


6

You can use the pwd command or print out the file realpath: $ find "$(pwd)" -size +20M $ find . -size +20M -exec realpath {} + Both commands real give you the absolute path to the files.


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.


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

It can be partially accomplished by piping the grep command to lsattr command. lsattr -R | grep +i However, I believe when you mention the entire ext3 file system the search might involve /proc , /dev and some other directories which if reports some error you just want to ignore. You can probably run the command as, lsattr -R 2>/dev/null | grep -- ...


6

find . -depth -print0 | perl -0lne ' if ("$_/" =~ m{/Caches(/.*)}s && $1 !~ m{/Snapshots/}) {rmdir $_ or unlink $_}' If your find doesn't support -print0, you can replace it with -exec printf '%s\0' {} +. The idea is to print the list of files NUL-terminated (as 0 is the only byte that can't occur in a file path) and use perl's -n with -0 ...



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