PDA

View Full Version : piping output from ls



test
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?

Thanks!

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

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

?

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

test
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


Thanks

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

head -1not
head -n1

jrib
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

jrib
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 '

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


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

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

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

munitras
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)

try

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

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

test
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
1

$ cat test2
2

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

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

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

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