-1

Simplified situation: I'm located in ~. There is a folder ~/folder and inside, there are many, many, many .txt files named 1.txt, 2.txt, etc.

I want to generate a file_list.txt inside of ~ whose contents are:

folder/1.txt
folder/2.txt
etc...

If I do

ls folder>file_list.txt

I get

1.txt
2.txt
etc.

How do I get the folder to be listed as well? I've just been doing a workaround loop in python but that's a bit slow when the number of files I have is this large.

2

You don’t need ls for this, the shell’s own globbing will do what you’re after:

printf "%s\n" folder/* > file_list.txt
0

Another approach is to use find looking for all the "regular" (not directories, devices, etc)

find folder -type f > file_list.txt
1
  • 1
    With just ls you can use ls -R or ls -r if you don't want all the sub directories. – Jason Croyle Feb 24 at 0:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.