Tag Info

Hot answers tagged

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


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


6

grep searches for the first argument (the pattern) in the files passed on the command line or stdin if no files are passed. Without the quote your shell will expand lect* to all the files in the directory that begin with lect. Your command will then be: grep lect1.txt lect2.doc lect3.doc which means search for the text lect1.txt in both .doc files. ...


5

Use something like this perhaps (if gnu grep). grep -r 'content pattern' --include==*.cpp man grep --include=GLOB Search only files whose base name matches GLOB (using wildcard matching as described under --exclude) Also see the options for null delimiters. -Z, --null Output a zero byte (the ASCII NUL character) instead of the character that ...


5

Another valid method is: find /directory/containing/files -type f -print0 | xargs -0 grep "test to search" The print0 / option -0 is to allow for the case where filename strings contain special characters (like spaces).


4

Your aproach should be as follow. To stay on your way of thinking you'd do something like: myarray=() while IFS= read -rd '' files; do myarray+=("$files") done < <(find . -type f -name '*.php' -print0) printf '%s\n' "${myarray[@]}" As a side note. You don't need to define myarray=() the array variable at all. You can leave that out.


3

Perhaps simply piping the output of find into grep would do the trick: if find . -name test.txt -size 156c | grep -q .; then echo Found; fi The call to find will have no output unless a file matching the name and size conditions you set is found, and grep . will have exit status 0 ("true") only if its input is non-empty. Option -q asks to not print any ...


3

You should see file with name ..PPCES20152015-02-02_flyer_ppces.pdf in the directory where the original file 2015-02-02_flyer_ppces.pdf was. Backslash char ('\') is escaping character which you use to escape characters with special meaning for bash like \, ", ', #, $, <space> and others. If you use it before regular character like digit or letter it ...


3

Ok, so you want to run a command in each directory in a directory tree — the current directory, its subdirectories, their subdirectories, etc. The first thing to do is enumerate the directories in question. With the find command, tell it to list only directories: find . -type d The command you want to run in each directory is gmic ./*jpg -gimp_montage ...


3

I would use find <PARENT_DIR> -type f -mtime 1 With 1 the time of last modification in days (you can prefix it with - or + to indicate "less than X days" or "more than X days") : so, if you want the the file modified in the last 3 days, you'll do -mtime -3


2

First, your snippet executes the command echo {} : ;if [ -f {} ]; then echo file; else echo directory;fi because it needs its output to evaluate the command substitution. Since there is no file named {}, this produces the output {} : directory Then the find command is executed with the arguments -exec, echo, {}, :, directory, so for every file, it ...


2

You're nearly there! This works: find . -mtime +30 -type f \( -name \*.xml -o -name \*.out \) Your / ( becomes \( (an escaped open parenthesis; as you discovered the shell treats ( specially so it needs to be escaped with \); likewise ) / should actually be \). The names need * to match anything ending with the given extension, and that also needs to be ...


2

Okay, i solved it myself. With help of find / -nouser -o -nogroup 2> /dev/null you see all unlinked/unowned files on your system and you can delete every single file left on your system. If you didn't use -r option with userdel command, you can do the following to get rid of all old user's files. Delete removed user's home directory. cd /home; rm -r ...


2

The modification time of a directory, like any other file (note how directories are called directories (a list of name/number mappings like a phone directory) and not folders) is updated whenever the content is modified. That is when a file is added (linked), removed (unlinked), or renamed in it. Beware that files can be linked to several directories. The ...


2

It's generally a bad idea to parse the output of ls or find, because you can't distinguish which characters are part of a file name and which characters are separators. If you don't control the file names, this can be a security hole, allowing an adversary to craft file names that cause your script to execute arbitrary code. See Why does my shell script ...


2

If you have to jump through alot of hoops, then the efficiency of xargs is lost anyway. Here is one crude work around: find . -iname "*.cpp" | grep "<pattern>" | while read -r x; do grep exa "$x"; done Every time I run into problems with spaces in file names, the answer is double quotes on a variable.


2

This should work even without GNU tools: #Find all C++ files that match a certain pattern and then search them find . -name "*.cpp" \ | grep "<name regex>" \ | perl -pne 's/\n/\0/' \ | xargs -0 grep "<content regex>" The perl call replaces line breaks with null characters, which will allow xargs -0 to interpret the input on a per-line ...


2

With GNU find (the implementation on non-embedded Linux and Cygwin): find /search/location -type l -xtype d With find implementations that lack the -xtype primary, you can use two invocations of find, one to filter symbolic links and one to filter the ones that point to directories: find /search/location -type l -exec sh -c 'find "$@" -L -type d -print' ...


2

And one more way, in just a single process (without find, -exec, xargs): grep -r "test to search" /directory/containing/files Well, with GNU grep, anyway. :) ETA: Since I've been asked to show the grep --include option, here's an analogue to heemayl's example ("you can also use wildcards e.g. *.txt to look for only files ending with .txt"): grep ...


2

This is about as inelegant as the other answers, but maybe less inefficient: locate --regex --basename "xfce4-keyboard-overlay$" | while IFS= read -r f; do [ -f "$f" ] && printf "%s\n" "$f"; done (broken into two lines for readability).  The above will handle names containing spaces.  The IFS= seems to be necessary to handle names with ...


2

With zsh: print -rl ${(0)^"$(locate -0 ...)"}(N.) (0) is a parameter expansion flag that splits on NUL characters (as we use locate -0), short for (ps:\0:). With ^, instead of adding (N.) at the end of the array, we add it to each element. (N.) is a glob qualifier, . to match only regular files, N to remove the element if it doesn't match (doesn't exist ...


2

Probably this would be better: combination of find and shell find / -type d -print0 | while read -r -d '' dir; do ls -ltr "$dir" | sed '$!d' done find will output each directory found, using the null byte instead of a newline to separate them. This stream is fed into a while loop, using read -d '' to extract each null-delimited directory name. Then, ...


2

I'd use this kind of construct as a starting point find / -type d -print0 | xargs -0 -I'{}' sh -c 'ls -ltr {} | tail -1' Caveat: it doesn't like empty directories (total 0 is output).


1

The limitation you mentioned is generally about the exec buffer used, not specific to individual commands. The purpose of xargs is exactly to address that problem; xargs will take as many arguments as possible to feed the command. This will get you the least command calls and thus good performance. Reducing the amount of arguments for the command by ...


1

Try: find /search/location -type l -exec test -e {} \; -print From man test: -e FILE FILE exists You might also benefit from this U&L answer to How can I find broken symlinks; be sure to read the comments too. Edit: test -d to check if "FILE exists and is a directory" find /search/location -type l -exec test -d {} \; -print


1

Use find to do all the filename filtering.  Rather than find . -name "*.cpp" | grep "foo" | xargs grep … do find . -name "*.cpp" -name "*foo*" -print0 | xargs -0 grep … If you want to do something slightly more complicated, like find . -name "*.cpp" | egrep "foo|bar" | xargs grep … you can do find . -name "*.cpp" "(" -name "*foo*" -o -name "*bar*" ...


1

May I suggest: find . -type f -name "CLEAN" -execdir ./CLEAN \; and in CLEAN, set your pwd rather than having it passed: #!/bin/sh MYDIR=`pwd` echo "${MYDIR}"


1

Assuming your find command in the Question is actually what you typed, you've mixed actions with the directory search roots, leading to the error message. I've based my suggestion on this man page for HP UX find, which tallies with your "no -maxdepth" statement. Try this variant instead: find /opt/projectname/bin -path '/opt/projectname/bin/*/*' -prune -o ...


1

Using "find" command: find ./*.xml -type f ! -name "deposit.xml" -exec mv {} ./Archive ';' Using "find" with "xargs" command: find ./*.xml -type f ! -name "deposit.xml" | xargs -I {} mv {} ./Archive Using "for" loop with "find" command. for f in $(find ./*.xml -type f ! -name "deposit.xml"); do mv ${f} ./Archive; done Also, according to the manpage for ...


1

Rather than mixing find and rsync, since all you appear to want to copy are the *.xq files, you can tell rsync to do this directly. I've included the --dry-run flag so that you can safely test it without any changes being effected; when you're ready simply remove it from the command line. rsync --dry-run -av --include '**/' --include '*.xq' --exclude '*' ...



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