Skip to content
Advertisement

Linux find xargs command grep showing path and filename

find /folder/202205??/ -type f | xargs head -50| grep '^Starting'

There are 20220501 20220502 20220503 and so on folders… This command searches all first 50 lines of all files in ‘/folder/202205??/’ and shows the lines beginning with text “Starting”

I haven’t the path and the filename of the files that are matched by the grep command. How can I get this information: path and filename and the matched line with a simple command?

Advertisement

Answer

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.

Advertisement