Help with pipe command

Posted on 2013-12-17
Last Modified: 2013-12-17
I can use this command to find a list of files (out of hundreds in this directory):
grep "Konto-Segment" /usr/tmp/*.tmp

That produces a list like this:
/usr/tmp/l1647444.tmp:Es konnte kein Konto-Segment mit Hilfe des angegebenen Qualifiers ermittelt werden.
/usr/tmp/l1647445.tmp:Es konnte kein Konto-Segment mit Hilfe des angegebenen Qualifiers ermittelt werden.
/usr/tmp/l1669057.tmp:Es konnte kein Konto-Segment mit Hilfe des angegebenen Qualifiers ermittelt werden.

I would like a list of filenames with timestamps like this instead:

Dec 16 10:50 l1647444.tmp
Dec 16 10:52 l1647445.tmp
Dec 16 11:40 l1649057.tmp

Can I pipe the output of the "cat" command or use "xargs"  to "ls -l" or something to get just the file names and the timestamp of each file?

I have Oracle Enterprise Linux 5 in case that makes a difference.
Question by:Mark Geerlings
  • 3
  • 2
LVL 84

Expert Comment

Comment Utility
grep -l "Konto-Segment" /usr/tmp/*.tmp | xargs stat -t"%b %d %T" -f"%Sm %N"
LVL 68

Accepted Solution

woolmilkporc earned 500 total points
Comment Utility
1) you can use "grep -l". This will display just filenames, not matching lines.

2) Use this output to run "stat" against these files. "ls -l" will not work in cases where the file is older than 6 months. Use "date" to convert the output of stat to the desired format:

grep -l "Konto-Segment" /usr/tmp/*.tmp | while read file
    echo $(date -d "01/01/1970 + $(stat -c %Y $file) seconds" "+%b %d %H:%M") $file

Too late, ozo was faster. But does it really work? My "stat" doesn't do it the suggested way.
LVL 34

Author Comment

by:Mark Geerlings
Comment Utility
No, the suggestion from ozo doesn't work for me.  That returns:
stat: invalid option -- %

The suggestion from wollmilkporc gives me exactly what I asked for.  Here is a sample of the output:

Dec 16 15:50 /usr/tmp/l1647444.tmp
Dec 17 09:13 /usr/tmp/l1647445.tmp
Nov 16 21:05 /usr/tmp/l1669057.tmp

I don't understand shell scripting well enough to understand exactly how this command gets the job done, but I'll plan to accept that suggestion as the answer, since it gives me what I asked for.

If someone is willing to explain what the syntax on the "echo..." line does I would appreciate that, so I may then be able to tweak this a bit if I want something slightly different someday.  I understand the "| while read file" and the "do ... done" loop syntax.  I've used "%H" and "%M" with "date" before, but most of the rest of that line is unintelligible to me.
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

LVL 68

Expert Comment

Comment Utility
Glad I could help.

The basic idea is using date's capability to not only display the current date but also to accept a different date specification by means of the "-d" option, even using kind of "natural speech" like "1 day ago", "yesterday + 1000 seconds" etc.

"stat", on the other hand, can display a file's modification date in seconds since epoch (01/01/1970 00:00:00).

Combining these two capabilities yields the desired result.
Using the command substitution "$(stat -c %Y $file)" ($file is the name read from grep's output) we get the file's modification date in seconds since epoch.
We now feed this value to "date -d" and instruct "date" to add it to 01/01/1970 (the start of epoch) and then reformat the result according to the desired appearance (%b %d %H:%M means "abbreviated month, space, decimal day, space, hour, colon, minute).

We put the whole construct into another command substitution "$(date -d ... ...) and echo out the result, followed by the filename read from grep's output.

Have fun with shell scripting und schöne Feiertage!

LVL 34

Author Comment

by:Mark Geerlings
Comment Utility
Viele dank!  Mein Deutsch ist nicht so gut, aber ich kan "schöne Feiertage" verstehen.  (I think that's "Happy Holidays" for English speakers, or as we prefer to say here: "Merry Christmas".
LVL 68

Expert Comment

Comment Utility
Thanks for the points!

I saw that you posted German text samples so I assumed you were from Austria/Germany/Switzerland, whatever (I'm from Germany, by the way).
Sorry for the misinterpretation!


Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Recently, an awarded photographer, Selina De Maeyer (, completed a photo shoot of a beautiful event ( in An…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now