How do I list one filename per output line in Linux?
Yes, you can easily make ls
output one filename per line:
ls -a | cat
Explanation: The command ls
senses if the output is to a terminal or to a file or pipe and adjusts accordingly.
So, if you pipe ls -a
to python it should work without any special measures.
you can use ls -1
ls -l
will also do the work
Use the -1
option (note this is a "one" digit, not a lowercase letter "L"), like this:
ls -1a
First, though, make sure your ls
supports -1
. GNU coreutils (installed on standard Linux systems) and Solaris do; but if in doubt, use man ls
or ls --help
or check the documentation. E.g.:
$ man ls
...
-1 list one file per line. Avoid '\n' with -q or -b
Ls is designed for human consumption, and you should not parse its output.
In shell scripts, there are a few cases where parsing the output of ls does work is the simplest way of achieving the desired effect. Since ls might mangle non-ASCII and control characters in file names, these cases are a subset of those that do not require obtaining a file name from ls
.
In python, there is absolutely no reason to invoke ls
. Python has all of ls
's functionality built-in. Use os.listdir
to list the contents of a directory and os.stat
or os
to obtain file metadata. Other functions in the os
modules are likely to be relevant to your problem as well.
If you're accessing remote files over ssh, a reasonably robust way of listing file names is through sftp:
echo ls -1 | sftp remote-site:dir
This prints one file name per line, and unlike the ls
utility, sftp
does not mangle nonprintable characters. You will still not be able to reliably list directories where a file name contains a newline, but that's rarely done (remember this as a potential security issue, not a usability issue).
In python (beware that shell metacharacters must be escapes in remote_dir
):
command_line = "echo ls -1 | sftp " + remote_site + ":" + remote_dir
remote_files = os.popen(command_line).read().split("\n")
For more complex interactions, look up sftp's batch mode in the documentation.
On some systems (Linux, Mac OS X, perhaps some other unices, but definitely not Windows), a different approach is to mount a remote filesystem through ssh with sshfs, and then work locally.