View Full Version : piping output from ls

October 9th, 2005, 09:51 PM
I'm trying to concatentate the, say, 2 most recent recent files in a directory and processes them using grep and whatnot. So I thought I'd try something like

ls -t | head -n 2 | cat | grep stuff

However, that really work. Can someone point me in the right direction?


October 9th, 2005, 11:46 PM
How about "ls -t | head -n 2 | xargs cat | grep stuff"?

October 10th, 2005, 04:52 AM
I tried that, but for some reason it doesn't work. I get:

ls -t | head -n1 | xargs cat
cat: test2: No such file or directory

where test2 is the correct file and, of course, it exists.


October 10th, 2005, 06:07 AM
You don't need cat in the pipeline:
ls -t | head -2 | xargs grep stuff

October 10th, 2005, 02:39 PM
I still get an error.

ls -t | head -n1 | xargs grep stuff
grep: test: No such file or directory

If it's of any use, the word 'test' is colour-coded as it would be displayed in the shell - it is not plain text. It's as if the output text of 'ls' is encoded and it's execution by xargs therefore fails.

even this doesn't work:
ls -t | head -n1 | xargs ls
ls: test: No such file or directory


October 10th, 2005, 03:01 PM
It should be

head -1not
head -n1

October 10th, 2005, 04:04 PM
"head -2" is equivalent to "head -n 2"

ls -t | head -2 | grep stuff

seems to work for me

jerome bettis
October 10th, 2005, 05:18 PM
what is ls -t ??

i type it and it does the exact same thing as ls without the -t

October 10th, 2005, 05:37 PM
what is ls -t ??

i type it and it does the exact same thing as ls without the -t

"-t sort by modification time"

you can check man pages for a quicker answer to questions like these :D

[edit]a really quick way to find out would be: $man ls | grep '\-t '

October 10th, 2005, 05:41 PM
How about:

ls -t | head -n2 | while read line; do cat $line | grep STUFF ;done

October 10th, 2005, 06:02 PM
Do you have spaces in your filenames? Thats the only reason I can see none of these examples working.

October 10th, 2005, 08:36 PM
There are no spaces in the file name. As you can see from the error message, the file name is 'test'. BTW, if there's some confusion, I'm trying to grep the contents of the files - not the output of ls.

October 11th, 2005, 12:08 AM
to find the two most recent files in a directory and then grep the contents of those file for a specified string ("stuff" in this example)


ls -t | cat | head -2 | xargs grep stuff

if you want more or less most recent files change the 'head -n' - as required
if you want to work with oldest files change the 'head -n' to 'tail -n'

an alternative structure would be to do this

grep stuff $(ls -t | cat | head -2)

mileage may vary depending on which shell you are running. I run bash.

hope it helps

October 11th, 2005, 12:18 AM
Given the error is returned by ls, I'm wondering if you have filesystem corruption. Post the exact output of 'ls', 'ls -t' and the output of your command.

October 11th, 2005, 04:19 AM
Thanks for your help everyone. This isn't working on two of my systems and is working on a third, so I don't think it's filesystem corruption

Here's a demo output

$ ls
test1 test2

$ls -t
test2 test1

$ cat test1

$ cat test2

ls -t | cat | head -n2 | xargs grep 1
grep: test2: No such file or directory
grep: test1: No such file or directory

jerome bettis
October 11th, 2005, 07:04 AM
for i in `ls -t | head -n2` ; do cat $i ; done | grep stuff

October 11th, 2005, 07:19 AM
Are you sure that you have not had ls aliased in ~/.alias or ~/.bashrc

October 11th, 2005, 03:00 PM
OK, munitras was on to something. I had aliased ls as

alias ls="ls --color"

and had thought nothing of it. However, --color encodes the output so it's in color. The correct thing to do is to use

alias ls="ls --color=auto"

To quote from 'man ls'

"auto Only use color if standard output is a terminal". That way when you pipe it the encoding is removed.

Thanks for your help and sorry for such a silly problem.

October 11th, 2005, 03:59 PM
glad it is working now.