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.


11

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


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


7

You can do it with just find and awk: find . -type f -name '*.php' -size +1000c -exec awk ' FNR > 1 {nextfile} length >= 1000 {print FILENAME}' {} + The awk script skips to next file after the first line of every file. It prints the filename of the current file if the current line is >= 1000 characters long.


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

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

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

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

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


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


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

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

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

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

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

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

If you want something more general than matching a specific character, you would have to use regular expressions. Since the question is not tagged "linux", the proper answer would use POSIX: find . | grep '[*~]' If you want to make it Linux-specific, you can use the GNU find option -regex (also supported by FreeBSD). If the pathname has an embedded ...


2

Use the -prune option to skip that directory find ~/.jenkins/jobs/subco -path ~/.jenkins/jobs/subco/myapp -prune -o -name '*.jar' -exec rm -r {} +


2

You can try this one using find and grep : find . -type f -name '*.php' -exec grep '.\{1000\}' {} +


2

Given a path to the file ./some/where/thatcertainfile, stripping off the final /thatcertainfile gives you a path to the directory. Launch a shell to be able to use string manipulation on the path. find . -name thatcertainfile -exec sh -c 'rm -r "${0%/*}"' {} \; Alternatively, use zsh. To transform a path into the name of the containing directory, use 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" {} \; > ...



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