8

I'm trying to make a command which only displays the first 30 files when the ls command is invoked. I found this method...

ls | head -30

... but it ends up spitting out the files in one long list, not formatted in a row or colored as per the .bash_profile. So, is there any way to simply limit the output?

2
  • What do you mean by "spitting out the files in one long list, not formatted in a row"? In other words, formatted to your liking, what would it look like?
    – John1024
    Dec 20, 2014 at 1:51
  • @John1024 Sorry, I meant in columns like ls does. Basically I want the exact functionality of ls, just with trimmed results.
    – asteri
    Dec 20, 2014 at 1:56

2 Answers 2

9

Until asteri clarified the question, I thought John1024 had the answer. Now it seems that the following will work without the --color option:

ls -d $(ls | head -30)

Unfortunately this is too simplistic and will fail if there are blanks in the file names. To take account of that you need the more elaborate:

ls -b | head -30 | xargs ls -d

In both cases the principle is the same: ls | head gets the first 30 files, one per line, which are then presented as an argument list to another ls command, which needs the -d option in case any of the files are directories.

7
  • Wow! Your second one works flawlessly. Thank you!
    – asteri
    Dec 20, 2014 at 2:34
  • Upon further testing, it actually breaks when a directory has a space in its name. Still the best solution, though.
    – asteri
    Dec 21, 2014 at 5:42
  • I checked that case before I answered and it worked for me (just checked again and it still works!). The only time that I can think of when it would fail is if there is a new-line character in the name, not a normal occurrence.
    – AFH
    Dec 21, 2014 at 13:13
  • Are you on OSX or a normal Linux distro?
    – asteri
    Dec 21, 2014 at 16:37
  • 1
    In that case ls | sed 's/ /\\ /g' | tail -30 | xargs ls -d will work for literal spaces, but not other white spaces.
    – AFH
    Dec 21, 2014 at 20:55
4

To get coloring:

ls -l --color=always | head -30

Normally, ls produces color only when the output is going directly to a terminal. This is generally a good thing. To override that, use --color=always

The above produces output with one file per line. If you want, space allowing, more files per line, then try:

ls -l | head -30 | column

The column utility formats its input into multiple columns. The use of color may, however, confuse it.

5
  • I was in the middle of answering ls -ld $(ls|head -30), with elaborations for files with embedded spaces, but this is much more straightforward.
    – AFH
    Dec 20, 2014 at 1:57
  • Nice! This is almost perfect. Except that it's saying ls: illegal option --. Is that not available on OSX?
    – asteri
    Dec 20, 2014 at 2:01
  • (I just realized I had a wrong option in the example code, which is what lead to the confusion. Just ls | head -30 | column did the trick, minus the color.)
    – asteri
    Dec 20, 2014 at 2:04
  • @asteri I just checked the OSX man page for ls and it does not appear to support the --color option. The somewhat awkward work-around for color is to set the environment variables CLICOLORS=true and CLICOLORS_FORCE=true. If this works for you (I have no access to OSX to test it), I would suggest putting it all in a shell script so that those variable settings are deleted when the script ends.
    – John1024
    Dec 20, 2014 at 2:12
  • Using export CLICOLOR_FORCE=true did indeed work. You were completely right, though, that it messed up the formatting of the columns. This is perfect, though. Thank you. Wish I could give you more than one upvote.
    – asteri
    Dec 20, 2014 at 2:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .