Print the Directory Where the 'Find' Linux Command Finds a Match

Print the directory where the 'find' linux command finds a match

To print the directory name only, use -printf '%h\n'. Also recommended to quote your variable with doublequotes.

find "$STORAGEFOLDER" -name .todo -printf '%h\n'

If you want to process the output:

find "$STORAGEFOLDER" -name .todo -printf '%h\n' | xargs ls -l

Or use a loop with process substitution to make use of a variable:

while read -r DIR; do
ls -l "$DIR"
done < <(exec find "$STORAGEFOLDER" -name .todo -printf '%h\n')

The loop would actually process one directory at a time whereas in xargs the directories are passed ls -l in one shot.

To make it sure that you only process one directory at a time, add uniq:

find "$STORAGEFOLDER" -name .todo -printf '%h\n' | uniq | xargs ls -l

Or

while read -r DIR; do
ls -l "$DIR"
done < <(exec find "$STORAGEFOLDER" -name .todo -printf '%h\n' | uniq)

If you don't have bash and that you don't mind about preserving changes to variables outside the loop you can just use a pipe:

find "$STORAGEFOLDER" -name .todo -printf '%h\n' | uniq | while read -r DIR; do
ls -l "$DIR"
done

How to print the line that matches my text using find in linux?

find ./ -type f -exec grep -Hn "Text To Find" {} \;

Use -A and -B flags to print lines before and after the match:

find ./ -type f -exec grep -Hn -A1 -B1 "Text To Find" {} \;

also you can just use grep:

grep -R -Hn -A1 -B1 "Text To Find" *

Using find to locate files that match one of multiple patterns

Use -o, which means "or":

find Documents \( -name "*.py" -o -name "*.html" \)

You'd need to build that command line programmatically, which isn't that easy.

Are you using bash (or Cygwin on Windows)? If you are, you should be able to do this:

ls **/*.py **/*.html

which might be easier to build programmatically.

How do I get the find command to print out the file size with the file name?

find . -name '*.ear' -exec ls -lh {} \;

just the h extra from jer.drab.org's reply. saves time converting to MB mentally ;)

How to grep the exact match and print only that match

This should be a job for awk, could you please try following, written and tested with shown samples in GNU awk. Please mention absolute path in place of . to get run it for any directory in find command.

The output should be filename : matched string(s) : line number for all files.

You could run following find command:

find . -type f -exec awk -f script.awk {} +

Where script.awk is as follows:

cat script.awk
BEGIN{ OFS=" : " }
NF{
val=""
for(i=1;i<=NF;i++){
if($i~/abcxyz/){
val=(val?val OFS:"")$i
}
}
if(val){
print FILENAME,val,FNR
}
}

For your shown samples(considering empty lines in it), sample output will be as follows.

Input_file  :  abcxyz.fgh     :  1
Input_file : gfhj.abcxyz : 3
Input_file : abcxyz.sh : 5
Input_file : abcxyz.fsdghj : 7

Explanation: Adding detailed explanation for above.

BEGIN{ OFS=" : " }              ##Setting OFS to space colon space in BEGIN section of this program.
NF{ ##Checking condition if line is NOT empty then do following.
val=""
for(i=1;i<=NF;i++){ ##Traversing through all field values here.
if($i~/abcxyz/){ ##checking condition if field is matching abcxyz then do following.
val=(val?val OFS:"")$i ##Creating val which has value of current field and keep adding it.
}
}
if(val){ ##Checking condition if val is NOT NULL then do following.
print FILENAME,val,FNR ##Printing FILENAME val and FNR here.
}
}
'

How to find all files containing specific text (string) on Linux?

Do the following:

grep -rnw '/path/to/somewhere/' -e 'pattern'
  • -r or -R is recursive,
  • -n is line number, and
  • -w stands for match the whole word.
  • -l (lower-case L) can be added to just give the file name of matching files.
  • -e is the pattern used during the search

Along with these, --exclude, --include, --exclude-dir flags could be used for efficient searching:

  • This will only search through those files which have .c or .h extensions:
grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
  • This will exclude searching all the files ending with .o extension:
grep --exclude=\*.o -rnw '/path/to/somewhere/' -e "pattern"
  • For directories it's possible to exclude one or more directories using the --exclude-dir parameter. For example, this will exclude the dirs dir1/, dir2/ and all of them matching *.dst/:
grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern"

This works very well for me, to achieve almost the same purpose like yours.

For more options, see man grep.

Linux find xargs command grep showing path and filename

The main problem here is that head doesn't pass on the info about what lines came from which file, so grep can pick out the matching lines but not show the file name or path. awk can do the matching and trimming to 50 lines, and you can control exactly what gets printed for each match. So something like this:

find /folder/202205??/ -type f -exec awk '/^Starting/ {print FILENAME ": " $0}; (FNR>=50) {nextfile}' {} +

Explanation: the first clause in the awk script prints matching lines (prefixed by the FILENAME, which'll actually include the path as well), and the second skips to the next file when it gets to line 50. Also, I used find's -exec ... + feature instead of xargs, just because it's a bit cleaner (and won't run into trouble with weird filenames). Terminating the -exec command with + instead of \; makes it run the files in batches (like xargs) rather than one at a time.



Related Topics



Leave a reply



Submit