Hot answers tagged

12

If you have GNU find, you can use -printf: find content/media/ -type f -printf '%p : %u : %g : %k'


11

Use stat on the -exec action of find: find .content/media/ -type f -exec stat -c '%n : %U : %G : %s' {} + Change the format sequences of stat to meet your need.


10

You can use fdupes. 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. You can call it like fdupes -r /path/to/dup/directory and it will print out a list of dupes. Update You can give it try to fslint also. After setting up fslint, go to ...


9

GNU find has an optimization which can be applied to find . but not to find . -type f: if it knows that none of the remaining entries in a directory are directories, then it doesn't bother to determine the file type (with the stat system call) unless one of the search criteria requires it. Calling stat can take measurable time since the information is ...


6

You can make it shorter: find . ! -empty -type f -exec md5sum {} + | sort | uniq -w32 -dD Do md5sum of found files on the -exec action of find and then sort and do uniq to get the files having same the md5sum separated by newline.


5

If you use ls -lR you have to then exclude content from the output, somehow, and then grep out what you want. find is probably quicker. Here's a very quick hack on my machine (the grep's are ugly, I take no pride in them). tony@trinity:~$ time ls -lR | grep "tony tony" | grep -vc "rwxr-xr-x" 115668 real 0m3.247s user 0m1.456s sys 0m1.808s ...


5

This "return" isn't the exit code of find, but the return value of the -execdir action for the purposes of chaining multiple find actions together. If you try: find . -execdir false '{}' \; -print then -print never executes (that is, there is no output), while both of: find . -execdir true '{}' \; -print find . -execdir false '{}' + -print print every ...


4

The commands came first, consistency was added later. The earliest manpage you're likely to find shows it as find pathname expression find dates back to the 1970s, and the assumptions of ordering and even syntax (whether a dash is needed for options) were added to various commands later (say during the later 1980s and early 1990s) to help users remember ...


4

That is because you are trying to use the GNU find, which is default in Linux, but Mac OS X comes with BSD find which has many differences. To install GNU find you will need Homebrew, pretty easy to install, just follow http://brew.sh/ After that you can install findutils: brew install findutils More info and other tools to mimic a Linux environment on ...


4

I am not sure what your intention is (you didn't make that clear), but if it's to chmod to 700 all the files that match the pattern, then, except for your typo (;\ instead of \;), your command seems to work as intended. However: when it finds a file containing that string grep -q gives me 0 so another exec executes but should not. Yes, it should do. 0 ...


4

You can do it with just shell wildcards. Build up a pattern with progressively more directory levels. pattern='*' set -- $pattern while [ $# -ne 1 ] || [ "$1" != "$pattern" ]; do for file; do … done pattern="$pattern/*" set -- $pattern done This misses dot files. Use FIGNORE='.?(.)' in ksh, shopt -s dotglob in bash, or setopt glob_dots in zsh ...


4

To run find on its own result, you can use the -c argument to sh (or bash) to prevent the outer find command from treating the inner {} specially. However, you then need to pass the result from the outer find as an argument to sh, which can be expanded with $0: find . -type d -name "*.data" \ -exec sh -c 'find "$0" -type f -name "*.important.txt" ...


4

The problem is that find finds the Webcam directory, too, and runs ls Webcam which lists all the files there. To only list files, not directories, tell find -type f


4

Your problem is that ls -lR will be executed for all files (which will display the files) and every directory (which will display the contents of the directory). If your directory-hierarchy would not be flat, but contain sub-directories, this would display the contents even more often, as -R tells ls to traverse subdirectories again. Instead you should ...


3

It is easiest to execute a small script for every file that checks the brief-mode output of file and prints the path if the output of file matches ELF or C source, the path is passed in as $0. find . -type f -exec sh -c \ 'file -b "$0" | grep -q "^ELF\|^C source" && printf %s\\n "$0"' {} \; This solution has the following advantages over the ...


3

In case you want to understand the original command, let's go though that step by step. find -not -empty -type f Find all non-empty files in the current directory or any of its subdirectories. -printf "%s\n" Print its size. If you drop these arguments, it will print paths instead, breaking subsequent steps. | sort -rn Sort numerically (-n), in ...


3

You should use find for this. You can safely test with the following command: find . \( -name frontend -prune \) -o -type f \( -name \*.o -o -name '*.cm[oixa]' -o -name \*.cmxa -o -name \*.annot \) -print Once you're happy with the list of files that gets printed, run the real command to delete the lot of them: find . \( -name frontend -prune \) -o ...


3

Throw xargs into the mix. E.g.: ls | xargs stat


3

What is wrong with a simple Bash for-loop? for f in ./* do stat "$f" done With the quotes and the ./ prefix, this is safe against the worst file names.


3

Defining aliases or functions to simplify commands you use often is the standard way. I don't know of any built-in function or command to do this same thing, provided as standard on most Linux and Unix-type systems. There are undoubtedly similar aliases or functions in pre-packaged collections; for example, Oh My Zsh's common-aliases plugin defines fd and ff ...


3

LC_ALL=C find . ! -name '*.gz' \ -type f \ -size +"$((500*1024*1024))c" \ -mtime +0 \ -exec gzip {} + Here using gzip for compression. See also xz, bzip2 or compress for other compression utilities with different compression formats. Note that some find implementations like GNU find also ...


3

If you're using -print0 you should use the -0 flag to xargs so it will read the names correctly. find ~/.jenkins/jobs/subco/workspace/myproject/ -name 'target' -print0 | xargs -0 rm -rf alternately, if you have GNU find you could use the -delete flag, though it won't work if the directories are not empty find ~/.jenkins/jobs/subco/workspace/myproject/ ...


2

If you don't want to descend into any of the directories named @eaDir then you should not use ! before -name: mkdir -p a/@eaDir mkdir -p b/c/@eaDir mkdir -p d/e/f touch a/@eaDir/xxx touch b/yyy touch b/c/@eaDir/xxx touch d/e/f/yyy find . -name '@eaDir' -prune -o -print will give you: . ./b ./b/yyy ./b/c ./a ./d ./d/e ./d/e/f ./d/e/f/yyy and ...


2

You seem to have confused a few things. Neither find nor cp are shell builtins. The only argument in the find command is the path, the rest are options and their values, but that's just semantics, the distinction here is not very important. More importantly, there are two classes of option flags. Those that take arguments and those that don't. For those that ...


2

find and cpio were created by Dick Haight and not by the people who wrote most of the orginal Unix utilities. There was no commandline argument parsing library at the time that you could link as a library, and that would enforce/stimulate some consistency (in the mid 80s I had source code for getopt on several systems ) Once people use commands and their ...


2

You can pipe your find into a sort that sorts primarily by the number of / characters in the pathname. For example, find alpha | awk '{n=gsub("/","/",$0);printf "%04d/%s\n",n,$0}' | sort -t/ | sed 's|[^/]*/||' This uses awk to prefix the pathname with the number of slashes, and sed to remove this prefix at the end. Actually, as you probably want the ...


2

Let's start with saving the file somewhere first. Take your command: find -type f -exec grep -il "xml" {} \; and read through: What are the shell's control and redirection operators? to where it says: > : Directs the output of a command into a file. to make your command something like: find -type f -exec grep -il "xml" {} \; > ...


2

As per the documentation, Please note that -a when specified implicitly (for example by two tests appearing without an explicit operator between them) or explicitly has higher precedence than -o. This means that find . -name afile -o -name bfile -print will never print afile. So basically you can imagine find surrounding any two ...


2

How about you just print all the files that don't match your extensions? find documents -type f ! \( -name \*.txt -o -name \*.doc -o -name \*.docx \) find media -type f ! -name \*.gif find pictures -type f ! \( -name \*.jpg -o -name \*.jpeg \) Why do you need to check other at all if anything is allowed in there? By the way, Unix convention is: ...


2

Your attempts aren't working because you're trying to use shell operators such as && and > in the command executed by find, but you're typing those operators directly in the command, so they're executed by the shell that's calling find. Your commands are parsed as find … > tmp.$$ && mv … e.g. the first find invocation is find ./ ...



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