0

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
  • 411,918
  • 54
  • 1,065
  • 1,164

3 Answers3

4

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
  • 411,918
  • 54
  • 1,065
  • 1,164
jesse_b
  • 35,934
  • 12
  • 91
  • 140
  • `ls -1` seems to me to be what I needed. – user542162 Sep 20 '22 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 – roaima Sep 20 '22 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 – roaima Sep 20 '22 at 17:29
  • 1
    @roaima I just didn't know about `-1` as an option... – user542162 Sep 20 '22 at 17:40
  • @jesse_b I don't know how to name this, "vertical list"? "Long list"? – user542162 Sep 20 '22 at 17:43
  • @user542162 I would just describe it as needing each file to be on a newline. "long list" just describes what `-l` does. – jesse_b Sep 20 '22 at 18:09
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,825
  • 2
  • 8
  • 29
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.

Silver Moon
  • 101
  • 2
  • 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