简体   繁体   中英

UNIX Shell: List all files in directory excluding directory names

I want to list all the files in a directory recursively. I am storing this output in a file that I will later iterate through and use each line as an argument in another command.

However all the commands I have tried have listed the directory name as one of the output lines followed by the files and directories contained in the directory.

I have tried the following:

tree -if --noreport . > files_names.txt

This has given me some success, but it still prints the directories. An example output is as follows:

/testdir
/testdir/rightfolder/
/testdir/rightfolder/file2.txt
/testdir/rightfolder/file3.txt
/testdir/wrongfolder/
/testdir/wrongfolder/file.txt

I have checked the man pages for tree and ls.

Is there a flag or another command that will give me the correct output. I have considered using a flag for tree to list the directories and then removing all those entries from the original list but this is not elegant at all.

您可以使用find(1)并按类型进行过滤:

find ./ -type f

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM