How do I echo just 1 column of output from ls command?
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.
Related videos on Youtube
John
Updated on September 18, 2022Comments
-
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 over 10 yearsWhat is a situation in which you really want a column of
ls
output? -
terdon over 10 yearsAs a general rule, you should never parse ls. There are almost always better ways of getting the info you need.
-
YoMismo over 9 yearsI 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 over 2 yearsgrep -l "" * (small hack)
-
-
Anthon over 9 yearsBut it doesn't give you just the one column the OP is interested in.
-
MAQ over 8 yearsTo make it correct :
ls -1 /directory | head -2 | tail -1
orls -1 /directory | perl -ne 'print if $. == 2'
-
FelixJN over 8 years@KWubbufetowicz
ls | sed -n '1p;q'
seems simpler -
Stéphane Chazelas over 8 yearsNote that array indices start at
1
in sensible shells (zsh, yash, fish, csh, tcsh, rc, es...) and at0
in ksh and bash. For a solution portable to all Bourne-like shells, you could use:set -- *; printf '%s\n' "$2"
instead. -
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 about 8 yearsto clarify, using
54-
tells cut to every character starting at position 54; it doesn't stop at "human-recognized" column -
prusswan almost 8 yearsIt's not a bad idea..just needs to be refined.
-
Michael Lang over 7 yearsThe answer I was looking for, but not for the question of OP.
-
boctulus over 5 yearsls -C | awk '{ print $1 } looks give same output ... why $2 ?
-
Scott - Слава Україні about 5 yearsNot only is this answer wrong, but it's been given before.
-
AntumDeluge over 4 yearsAlthough I realize this is wrong for the original question, it is what I was looking for. So, thank you.
-
squarecandy almost 4 yearsIt'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 over 3 yearsI 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 over 3 yearsammended. Thanks
-
Kusalananda over 3 yearsThis may not do what you want it to if your filenames contain spaces or other whitespace characters.
-
Kusalananda over 3 yearsDoesn'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 over 3 yearsParsing
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 about 2 yearsworth pointing out that
-1
isminus digit-one
notminus lower-case-L
, as it can be a bit difficult to tell the difference when viewing it