Hot answers tagged

9

You can throw away error reporting from find with 2>/dev/null, or you can avoid running the command at all: test -d /my-directory && find /my-directory -type f -mtime +14 -print0 | xargs -r0 rm As a slight optimisation and clearer code, some versions of find - including yours - can perform the rm for you directly: test -d /my/directory &&...


7

If you want to exclude files by name, you can use this syntax: find . -type f ! -name '*.mp3' ! -name '*.mp4' -size +1M -delete or if your find does not support delete: find . -type f ! -name '*.mp3' ! -name '*.mp4' -size +1M -exec rm {} \;


7

find -type f \( -name "*zip" -o -name "*tar" -o -name "*gz" \) -size +1M -delete the \( \) construct allows to group different filename patterns by using -delete option, we can avoid piping and troubles with xargs See this, this and this ./ or . is optional when using find command for current directory Edit: As Eric Renouf notes, if your version of ...


6

One more way, assuming GNU find(1), just for fun: find $PWD -type f -name "file.txt" -printf '%p '


5

You can replace the LF character with a space using the 'tr' command tr '\012' ' ' < path.txt This can be part of the original command: find $PWD -type f -name "file.txt" | tr '\012' ' ' > paths.txt


5

You can use, paste too, find . -type f -name "path.txt" -exec paste -d' ' -s {} \; > path.txt


4

The -print action does have a value of true but only after it prints. Observe that this prints all the files twice: $ find . -print -print . . ./file1 ./file1 ./file2 ./file2 Despite having two print statements, this command only prints once: $ find . \( -not -print \) -print . ./file1 ./file2 Here, the first -print evaluates to true so -not -print ...


3

pdfunite $(sed 's/$/_*.pdf/' filenames.txt) output.pdf So if filenames.txt contains CSAI_isotig00407:342-556 CSAI_isotig00408:342-556 That command will effectively do pdfunite CSAI_isotig00407:342-556_*.pdf CSAI_isotig00408:342-556_*.pdf output.pdf


3

Use the exec command of find: find . -name someFile.java -exec javac {} \;


3

You seem to assume that --execdir invokes a (Bash) shell that then invokes ffmpeg. That is not the case: -exec command ; Execute command; true if 0 status is returned. All following arguments to find are taken to be arguments to the command until an argument consisting of `;' is encountered. The string `{}' ...


3

While doing: find /data/code/ -name "*.jar" -exec {} ls \; you are trying the execute the file found (e.g. /data/code/project/shared/build/thirdparty/log4j-1.2.8/commons-logging-1.0.4.jar) with ls as an argument to it, leading to the permission denied error. Just switch the order: find /data/code/ -name "*.jar" -exec ls {} \; GNU find has -ls option ...


3

Your parenthesis attempt lacked spaces around them so find saw an option called (-name and didn't know what to do with it. Instead, add spaces around the parens like: find . \( -name "*.xml" -o -name "*.ipm" \) -type f -exec rm -iv {} \; Also, if you have GNU find you can use -delete instead of the -exec rm -iv {} \; As Wildcard noted, since we're doing ...


3

This is a case where you should stuff the code execution into a spawned shell. find . -name '*~' -exec sh -c 'cp "$0" "${0%~}"' {} \; This way you don't need to write a script. You just do it in a one-liner. Note: You don't need the -- "end of options" specifier for cp here, because It doesn't seem to be specified in the POSIX specs for cp, and find ...


3

The answer to the question you asked has to do with boolean algebra and operator precedence. Your find command reads: If it's a regular file, and it's name matches *.tex, then do nothing. Otherwise, if it isn't readable, skip it without recursing. Otherwise run grep. Put the readability test first. This will cause the tests matching the files you're ...


3

One simple way would be to pipe the find output through xargs (whose default action when no explicit command is given is to echo its arguments) find $PWD -type f -name "file.txt" | xargs > paths.txt Unlike simply replacing all the newlines with spaces, this preserves the final newline.


2

I'd do something like (assuming GNU tools): find /mnt/SAN/documents -type f -print0 | awk -F / ' NR == FNR{check[$0]; next} $NF in check {print "found:", $0; delete check[$NF]} END { for (i in check) print "Not found:", i }' filename.list RS='\0' - Which would find one occurrence for each filename in filename.list. Or to report all ...


2

less $(find . -name myfile.txt) less `find . -name myfile.txt` The first is, I believe, both POSIX-compliant and nest-able. The second, I believe, is more portable.


2

Similar to @coffeeMug, this is the more up-to-date way to doing this as it is apparently faster: find . -name "*.log" -exec ls -l '{}' + I'll also point you to CommandLineFu, which is always helpful with these things.


2

You can achieve this using find's -exec flag: find . -name "*.log" -exec ls -l '{}' \; In this example find searches for all log files in current directory and then list them using ls -l. In your case you should replace ls with less. See the ACTION part of find man page here find(1) man page.


2

Not really. Consider that find doesn't know what is in the directory tree, but finds out by reading recursively through all directory listing. (sorry for the pun.) For example there could be two first-level directories, one of which has 1 file, and another that has 1000000 files. Something like copying the tree would be different, since a program could ...


2

If you mean that you want to create symbolic links from $HOME/this, $HOME/there/that, $HOME/Data/ddd, etc. but not $HOME/Data or $HOME/somewhere/Labs, then the criteria for matching should be find "$HOME" ! \( -name Data -o -name Labs \) -exec … You were missing parentheses. Juxtaposition binds tighter than -o so -name Data -o name Labs -exec … executes ...


2

When you invoke python3.5 -m site then python searches for a file site.py in your sys.path. This sys.path is a list determined where your python is installed and by environment variables, and various other mechanisms including the ones to recognise packages installed under site-packages. Now your matplotlib might be in one of those paths. But if you e.g. ...


2

generate a sed script from your index file (File2) instead of a loop then run that script against your File1.. It will be MUCH faster :). awk '{ print "s/sp_"$1"/"$2"/g"}' File2.txt > tranform.sed then do: sed -i -f transform.sed File1.txt so your entire script could be: awk '{ print "s/sp_"$1"/"$2"/g"}' File2.txt > transform.sed sed -f ...


2

Using parenthesis works, but you need to escape them so the shell does not interpret them as subshell invocations. You can use backslash, \( and \) or quotes. If none of your files have names that contain newlines or other control characters, which should be the case unless someone tries to be nasty, then you can use the pipe to xargs approach, which is the ...


2

/sbin is a symlink to /usr/sbin and /bin is a symlink to /usr/bin. ls -ld /bin /sbin will show you this.


2

I recently wrote an example of stuffing a shell one-liner into a find command, and this is another use case for the same. Instead of: find samples \( -iname '*.wav' -o -iname '*.mp3' \) -execdir ffmpeg -i "$(basename "{}")" -qscale:a 6 "${$(basename "{}")%.*}.ogg" \; Try: find samples \( -iname '*.wav' -o -iname '*.mp3' \) -exec sh -c 'ffmpeg -i "$1" -...


2

Two possible solutions that spring to mind. 1. Iterate across all the directories in LOREM and symlink them to $HOME cd "$HOME/LOREM" for item in * do test -d "$item" || continue mv -f "$HOME/$item" "$HOME/$item.DELETE_ME_LATER" 2>/dev/null ln -s "$HOME/LOREM/$item" "$HOME/$item" done # Once you are happy that only the correct files have ...


2

You can do this with find, but to do it robustly you will need to embed a shell one-liner as well. The proper way to do this is one of the following: Stuff the looping into the spawned shell: find . -type f -name '._*' -exec sh -c 'for a in "$@"; do f="${a%/*}/${a##*/._}"; [ -e "$f" ] && printf %s\\n "$f"; done' find-sh {} + Or, spawn a separate ...


2

You want to use the $() syntax. eg javac $(find ./someDir/anotherDir/ -name someFile.java)


2

You should be able to use -execdir to make a copy relative to the directory of the found file(s) e.g. find Italy -name '*.front.*' -execdir cp -- {} 'fanart.jpg' \; Example: given $ tree Italy/ Italy/ ├── Florence │   ├── photo.back.001.jpg │   ├── photo.back.002.jpg │   ├── photo.back.003.jpg │   ├── photo.front.001.jpg │   ├── photo.front.002.jpg │   └─...



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