Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

ls prints differently depending on whether the output is to a terminal or to something else.

e.g.:

$ ls .
file1  file2
$ ls . | head
file1
file2

Is there some way to make ls print out on one line as if it's to a terminal when it's not. There's a -C argument that sorta does that, but it will split it into several lines.

$ ls
file1  file10  file11  file12  file13  file14  file15  file16  file17  file18  file19 file2  file3  file4  file5  file6  file7  file8  file9
$ ls -C . | head
file1 file11 file13 file15 file17 file19 file3  file5  file7  file9
file10 file12 file14 file16 file18 file2 file4  file6  file8

The reason I want to do this is that I want to monitor the files in a directory that were changing quickly. I had constructed this simple command line:

while [[ true ]] ; do ls ; done | uniq

The uniq prevents it from spamming my terminal and only showing changes. However it was printing it all on differnet lines, which was making the uniq useless, and increasing the amount of noise. In theory one could use watch for this, but I wanted to see a file as soon as it appeared/disappeared.

This is the final solution:

while [[ true ]] ; do ls | tr '\n' ' ' ; done | uniq
share|improve this question
4  
if you are piping ls to another command, you are doing something wrong. What are you actually trying to accomplish? –  Justin Jan 24 '10 at 16:01
    
Justin, I have updated the question with an explaination. –  Rory Jan 25 '10 at 10:30

5 Answers 5

up vote 4 down vote accepted

i don't know of a switch which could do that, but you can pipe your output through tr to do it:

ls | tr "\n" " " | <whatever you like>
share|improve this answer

If your ls has this option, you can use a high value and it might do what you want:

ls -w 10000 -C . | head
share|improve this answer

ah, now that you've updated the question....

while true ; do echo * ; done | uniq

will do what you posted, just simpler.

however, you are better off using something that uses inotify to do this.. like

inotifywait -m . -e create,delete

if you don't have inotify, then something like this works well too:

import os
import time

last = set()
while True:
    cur = set(os.listdir('.'))
    added = cur-last
    removed = last-cur
    if added: print 'added', added
    if removed: print 'removed', removed
    last = set(os.listdir('.'))
    time.sleep(0.1)
share|improve this answer

This may be what you are looking for but I may have misunderstood your question but here goes :)

A few commands worth looking into:

watch - execute a program periodically, showing output fullscreen xargs - build and execute command lines from standard input (-0 if you want to handle files with spaces etc)

You can do something like the following to show the last 10 files/dirs that changed with a 2 second interval (default for watch)

watch "ls -lart | tail -10"

the options -lart tell ls to be verbose and sort based on modification time.

If you really just want the files I would just do something like:

watch "ls -lart | awk '{print \$8}' | tail -10 | xargs"

or just to display them:

ls -lart | awk '{print $8}' | tail -10 | xargs

share|improve this answer
ls | xargs

It works for me, it's the simplest way I've found ever. Hope this helps you as well.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.