如何在Linux中将文件夹中的文件列表发送到txt文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4112151/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:49:19  来源:igfitidea点击:

How to send list of file in a folder to a txt file in Linux

linuxbashshellcentos

提问by alchemical

I'm fairly new to Linux (CentOS in this case). I have a folder with about 2000 files in it. I'd like to ideally execute a command at the command prompt that would write out the name of all the files into a single txt file.

我对 Linux(在这种情况下为 CentOS)相当陌生。我有一个文件夹,里面有大约 2000 个文件。我想理想地在命令提示符下执行一个命令,它将所有文件的名称写出到一个 txt 文件中。

If I have to, I could write an actual program to do it too, I was just thinking there might be a way to simply do it from the command prompt.

如果必须的话,我也可以编写一个实际的程序来执行此操作,我只是想可能有一种方法可以简单地从命令提示符执行此操作。

采纳答案by nonopolarity

you can just use

你可以使用

ls > filenames.txt

(usually, start a shell by using "Terminal", or "shell", or "Bash".) You may need to use cdto go to that folder first, or you can ls ~/docs > filenames.txt

(通常,使用“终端”、“shell”或“Bash”启动 shell。)您可能需要先使用cd转到该文件夹​​,或者您可以ls ~/docs > filenames.txt

回答by usta

If only names of regular files immediately contained within a directory (assume it's ~/dirs) are needed, you can do

如果只需要直接包含在目录中的常规文件的名称(假设它是~/dirs),则可以执行

find ~/docs -type f -maxdepth 1 > filenames.txt