Coder Perfect

In Linux, how can I list one filename per output line?

Problem

I’m using the ls -a command to get the file names in a directory, but the result is only one line long.

Like this:

.  ..  .bash_history  .ssh  updater_error_log.txt

I’m looking for a built-in way to get filenames on a new line, like this:

.  
..  
.bash_history  
.ssh  
updater_error_log.txt

Asked by fixxxer

Solution #1

Use the -1 option, as seen below (notice that this is a “one” digit, not a lowercase letter “L”).

ls -1a

Make that your ls supports -1 first; GNU coreutils (included on standard Linux systems) and Solaris do; if in doubt, try man ls or ls —help or consult the documentation. E.g.:

$ man ls
...
       -1     list one file per line.  Avoid '\n' with -q or -b

Answered by Bert F

Solution #2

Yes, you can easily have ls output one filename per line with the following command:

ls -a | cat

Explanation: The command ls detects if the output is to a terminal, a file, or a pipe and alters the output accordingly.

So, piping ls -a to python should work without any modifications.

Answered by Peter G.

Solution #3

You should not parse the output of Ls because it is intended for human consumption.

There are a few occasions in shell scripts when parsing the output of ls is the simplest technique to achieve the desired effect. These scenarios are a subset of those that do not require acquiring a file name from ls since ls may mangle non-ASCII and control characters in file names.

In Python, there is no purpose to use the ls command. Python includes all of the features of ls. To list the contents of a directory, use os.listdir, and to get file metadata, use os.stat or os. Other os modules functionalities are likely to be applicable to your issue as well.

If you’re using ssh to access distant files, sftp is a good technique to get a list of file names:

echo ls -1 | sftp remote-site:dir

Sftp, unlike the ls tool, prints one file name per line and does not mangle nonprintable characters. You won’t be able to accurately show folders if a file name contains a newline, but that’s not something you’ll do very often (remember this as a potential security issue, not a usability issue).

In python (note that shell metacharacters in remote dir must be escapes):

command_line = "echo ls -1 | sftp " + remote_site + ":" + remote_dir
remote_files = os.popen(command_line).read().split("\n")

Look up sftp’s batch mode in the docs for more complicated interactions.

A alternate technique is to mount a remote filesystem through ssh with sshfs and then work locally on some systems (Linux, Mac OS X, possibly some other unices, but definitely not Windows).

Answered by Gilles ‘SO- stop being evil’

Solution #4

ls -1 is a command that can be used.

ls -l will also complete the task.

Answered by ILF

Solution #5

ls -w1 is another option.

This allows you to specify the number of columns to display. ls’s manpage says:

   -w, --width=COLS
          set output width to COLS.  0 means no limit

Answered by smihael

Post is based on https://stackoverflow.com/questions/3886295/how-do-i-list-one-filename-per-output-line-in-linux