Thursday, January 20, 2011

Make ls print it all on one line (like in terminal)

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
  • hi,

    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>
    
    From Christian
  • If your ls has this option, you can use a high value and it might do what you want:

    ls -w 10000 -C . | head
    
  • 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)
    
    From Justin
  • 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

    From ScottZ

0 comments:

Post a Comment