2

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
Stephen Kitt
  • 434,908

3 Answers3

5

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?

Stephen Kitt
  • 434,908
jesse_b
  • 37,005
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.

telometto
  • 1,975
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.

  • 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. – Stéphane Chazelas Jun 23 '23 at 11:16