How do I echo just 1 column of output from ls command?

202,248

Solution 1

The following command will format the ls output into one column:

ls -1 /directory

Solution 2

The most reliable way to do this is to put the files into an array, and get the second one, which avoids having to do any parsing at all:

files=(*)
printf '%s\n' "${files[1]}"

The order in which you get back the files depends on the value of LC_COLLATE. As such, you might want to set LC_COLLATE=C first, if you want a "standard" sorting in all corner cases.

Solution 3

You would need to add -C as ls uses single-column mode when the output is not a terminal. awk then prints the second column:

ls -C | awk '{print $2}'

Solution 4

I think you are looking for

ls -1

It won't show permission, owner, group, date, size... but simple file name in single column.

Solution 5

As already has been mentioned - using columns for parsing ls output is not very robust because ls breaks the lines according to your file name lengths and terminal width. It often is interesting though to display a single column of a list of elements. You can do this using the cut command:

echo file1 file2 file3 file4 | cut -d" "  -f2

will display

file2

Note that -d allows you to select the column delimiter, so for example with

cut -d, -f2

you can display the second column of a comma separated list.

Share:
202,248

Related videos on Youtube

John
Author by

John

Updated on September 18, 2022

Comments

  • John
    John almost 2 years

    Lets say when I do ls command the output is:

    file1 file2 file3 file4
    

    Is it possible to display only a certain column of output, in this case file2? I have tried the following with no success:

    echo ls | $2
    

    Basically all I want to do is echo only the second column, in this case, I want to echo:

    file2
    
    • michas
      michas over 10 years
      What is a situation in which you really want a column of ls output?
    • terdon
      terdon over 10 years
      As a general rule, you should never parse ls. There are almost always better ways of getting the info you need.
    • YoMismo
      YoMismo over 9 years
      I wouldn't do that stranger :P (sorry I allways liked that phrase) the number of columns depend on the names files and folders have, so 2nd column may be the one you want, or may not, or may be in one ls and after creating some files/folders it may no longer be what you want. As terdon pointed out there sure are better ways to get what you want
    • Olav
      Olav over 2 years
      grep -l "" * (small hack)
  • Anthon
    Anthon over 9 years
    But it doesn't give you just the one column the OP is interested in.
  • MAQ
    MAQ over 8 years
    To make it correct : ls -1 /directory | head -2 | tail -1 or ls -1 /directory | perl -ne 'print if $. == 2'
  • FelixJN
    FelixJN over 8 years
    @KWubbufetowicz ls | sed -n '1p;q' seems simpler
  • Stéphane Chazelas
    Stéphane Chazelas over 8 years
    Note that array indices start at 1 in sensible shells (zsh, yash, fish, csh, tcsh, rc, es...) and at 0 in ksh and bash. For a solution portable to all Bourne-like shells, you could use: set -- *; printf '%s\n' "$2" instead.
  • clerksx
    clerksx over 8 years
    @StéphaneChazelas At least for this question, the tags include "bash", so this answer is specific to that shell.
  • Jeff Schaller
    Jeff Schaller about 8 years
    to clarify, using 54- tells cut to every character starting at position 54; it doesn't stop at "human-recognized" column
  • prusswan
    prusswan almost 8 years
    It's not a bad idea..just needs to be refined.
  • Michael Lang
    Michael Lang over 7 years
    The answer I was looking for, but not for the question of OP.
  • boctulus
    boctulus over 5 years
    ls -C | awk '{ print $1 } looks give same output ... why $2 ?
  • Scott - Слава Україні
    Scott - Слава Україні about 5 years
    Not only is this answer wrong, but it's been given before.
  • AntumDeluge
    AntumDeluge over 4 years
    Although I realize this is wrong for the original question, it is what I was looking for. So, thank you.
  • squarecandy
    squarecandy almost 4 years
    It's the answer to what you would assume the overview question in the title means, and it has lots of votes and good answers, therefore is the top google hit, which keeps reinforcing that feedback loop.
  • Patrick
    Patrick over 3 years
    I don't think there's an -all flag. I think that should just be -al which is combining -a for including files and folders that start with . (normally hidden) and -l which is "Long Format" and puts one entry per line, including file size and permissions.
  • Diego Andrés Díaz Espinoza
    Diego Andrés Díaz Espinoza over 3 years
    ammended. Thanks
  • Kusalananda
    Kusalananda over 3 years
    This may not do what you want it to if your filenames contain spaces or other whitespace characters.
  • Kusalananda
    Kusalananda over 3 years
    Doesn't the -l option override -C? Then what is the question you're answering? The question here is how to get the second filename.
  • tkrennwa
    tkrennwa over 3 years
    Parsing ls is anyway not they way to go to fetch a filename for exactly this reason, but the question was about the 2nd column of that output. In general, find to the rescue!
  • Admin
    Admin about 2 years
    worth pointing out that -1 is minus digit-one not minus lower-case-L, as it can be a bit difficult to tell the difference when viewing it