如何在 Linux 中为每个输出行列出一个文件名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3886295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I list one filename per output line in Linux?
提问by fixxxer
I'm using ls -a
command to get the file names in a directory, but the output is in a single line.
我正在使用ls -a
命令获取目录中的文件名,但输出在一行中。
Like this:
像这样:
. .. .bash_history .ssh updater_error_log.txt
I need a built-in alternative to get filenames, each on a new line, like this:
我需要一个内置的替代方法来获取文件名,每个文件名都在一个新行上,如下所示:
.
..
.bash_history
.ssh
updater_error_log.txt
采纳答案by Bert F
Use the -1
option (note this is a "one" digit, not a lowercase letter "L"), like this:
使用该-1
选项(请注意,这是一个“一个”数字,而不是小写字母“L”),如下所示:
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.:
不过,首先,请确保您ls
支持-1
. GNU coreutils(安装在标准 Linux 系统上)和 Solaris 都可以;但如果有疑问,请使用man ls
或ls --help
或查看文档。例如:
$ man ls
...
-1 list one file per line. Avoid '\n' with -q or -b
回答by Peter G.
Yes, you can easily make ls
output one filename per line:
是的,您可以轻松地使ls
每行输出一个文件名:
ls -a | cat
Explanation: The command ls
senses if the output is to a terminal or to a file or pipe and adjusts accordingly.
说明:该命令ls
检测输出是到终端还是文件或管道,并相应地进行调整。
So, if you pipe ls -a
to python it should work without any special measures.
因此,如果您通过管道连接ls -a
到 python,它应该可以在没有任何特殊措施的情况下工作。
回答by Gilles 'SO- stop being evil'
Ls is designed for human consumption, and you should not parse its output.
ls 是为人类设计的,你不应该解析它的输出。
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
.
在 shell 脚本中,在少数情况下,解析 ls 的输出确实是实现预期效果的最简单方法。由于 ls 可能会破坏文件名中的非 ASCII 字符和控制字符,因此这些情况是不需要从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.
在 python 中,绝对没有理由调用ls
. Python 具有ls
内置的所有功能。使用os.listdir
列出目录的内容,os.stat
或者os
获取文件的元数据。os
模块中的其他功能也可能与您的问题相关。
If you're accessing remote files over ssh, a reasonably robust way of listing file names is through sftp:
如果您通过 ssh 访问远程文件,列出文件名的一种相当可靠的方法是通过 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).
这将每行打印一个文件名,并且与该ls
实用程序不同,sftp
它不会破坏不可打印的字符。您仍然无法可靠地列出文件名包含换行符的目录,但很少这样做(记住这是一个潜在的安全问题,而不是可用性问题)。
In python (beware that shell metacharacters must be escapes in remote_dir
):
在 python 中(注意 shell 元字符必须在 中转义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.
对于更复杂的交互,请在文档中查找 sftp 的批处理模式。
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.
在某些系统(Linux、Mac?OS?X,也许是其他一些 unices,但绝对不是 Windows)上,另一种方法是通过 ssh 和sshfs挂载远程文件系统,然后在本地工作。
回答by ILF
you can use ls -1
您可以使用 ls -1
ls -l
will also do the work
ls -l
也会做这项工作
回答by 8bittree
Easy, as long as your filenames don't include newlines:
很简单,只要您的文件名不包含换行符:
find . -maxdepth 1
If you're piping this into another command, you should probably prefer to separate your filenames by null bytes, rather than newlines, since null bytes cannot occur in a filename (but newlines may):
如果您将其传递到另一个命令中,您可能更喜欢用空字节而不是换行符分隔文件名,因为空字节不能出现在文件名中(但换行符可能):
find . -maxdepth 1 -print0
Printing that on a terminal will probably display as one line, because null bytes are not normally printed. Some programs may need a specific option to handle null-delimited input, such as sort
's -z
. Your own script similarly would need to account for this.
在终端上打印它可能会显示为一行,因为通常不会打印空字节。某些程序可能需要特定选项来处理以空分隔的输入,例如sort
's -z
。您自己的脚本同样需要考虑到这一点。