3

I need a long (vertical) list of all file names of all files in a directory.

How to get only file names from an ls -la (ll) listing?

Only the names, not:

drwxr-xr-x 9 USER GROUP  4096 Jul 20 10:30 filename1
drwxr-xr-x 9 USER GROUP  4096 Jul 20 10:30 filename2
drwxr-xr-x 9 USER GROUP  4096 Jul 20 10:30 filename3
drwxr-xr-x 9 USER GROUP  4096 Jul 20 10:30 filename4
drwxr-xr-x 9 USER GROUP  4096 Jul 20 10:30 filename5

Rather I need only:

filename1
filename2
filename3
filename4
filename5
1
  • ls -1 should be the right answer for this question.
    – igor
    Commented Nov 22, 2023 at 8:03

3 Answers 3

7

Printing only filenames is the default behavior of ls. When you add -l you are saying you want the "long format" output. If you just remove -l I think that will give you what you want. You can leave -a to include "hidden" files

Also as Stéphane mentioned you could add -1 if you still want each file on its own line, which will happen with or without -1 when redirected to a file or piped to another command.

If you want to save this to a file you can use the > or >> operators as with any command. See Redirections and What are the shell's control and redirection operators?

6
  • 1
    ls -1 seems to me to be what I needed.
    – user542162
    Commented Sep 20, 2022 at 17:06
  • 2
    "you could add -1 if you still want each file on its own line." - if you redirect the output of the command to a file or a pipe you'll get one item per line even without this flag Commented Sep 20, 2022 at 17:20
  • Ah. I wonder if @user542162 is mis-reading -l (letter "L") and -1 (number one) and assuming they're they same? That could explain part of the confusion I think Commented Sep 20, 2022 at 17:29
  • 1
    @roaima I just didn't know about -1 as an option...
    – user542162
    Commented Sep 20, 2022 at 17:40
  • @jesse_b I don't know how to name this, "vertical list"? "Long list"?
    – user542162
    Commented Sep 20, 2022 at 17:43
0

You can redirect the output directly to file by using > or >>.

E.g. ls -l > somefile.txt will create a file called somefile.txt containing the results of ls -l. Beware, though, as the single > will overwrite everything in the file you redirect to. Using >> will append the output to the file you choose to write to.

EDIT: ls -1 >> somefile.txt is probably the command you are looking for. For all files, ls -1a >> somefile.txt.

3
  • Sadly it also contains drwxr-xr-x 9 USER GROUP 4096 Jul 20 10:30 which I want not to take.
    – user542162
    Commented Sep 20, 2022 at 16:47
  • Please check my edit.
    – telometto
    Commented Sep 20, 2022 at 16:48
  • When redirecting the output of ls, the -1 option is not needed.
    – Kusalananda
    Commented Sep 21, 2022 at 10:44
0

Piping ls output to cat seems to work in my bash (Konsole) shell. Here is the output from my system.

$ ls | cat
bin
boot
cdrom
dev
etc
home
initrd.img
initrd.img.old
lib
lib32
lib64
libx32
lost+found
media
mnt
opt
proc
root
run
sbin
snap
srv
sys
tmp
usr
var
vmlinuz
vmlinuz.old
$ 

The ls -1 command preserves colorful text in output, whereas the above cat based command looses colors.

1
  • 2
    You're getting coloured output likely because you're actually running ls --color=auto instead of ls, possibly because you have a ls='ls --color=auto' alias or ls() { command ls --color=auto "$@"; } function in your shell customisation. Note that the fact that you get 1 column when the output goes to a file other than a terminal device (like a pipe) here is already noted in the accepted answer. Commented Jun 23, 2023 at 11:16

You must log in to answer this question.

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