Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


$ ls .
file1  file2
$ ls . | head

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
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
up vote 8 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('.'))
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

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

As simple as that, works for me!

share|improve this answer

Your Answer


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.