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.

有帮助吗?

解决方案

You could use find(1) and filter by type:

find ./ -type f
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top