I have a directory html. I would like to output the files of the directory, including the path, to a file.

You can do something like this with the command:

ls -R scripts/html > out.txt

But the results are like:


I would like the result as:


Use find instead of ls. It'll give you a relative path based on what you pass it. I'm using tail to snip off the first line which would normally show the base directory.

find scripts/html | tail -n+2 > out.txt
  • Thank you. That is perfect. But is there any way to exclude the directory from the first line? I'll try something, but am a novice. I believe sed could be used. Jan 24 '14 at 22:32
  • 1
    @KohjahBreese I've edited to do that. And it turns out you don't need stdbuf for find (I wonder what I was thinking of).
    – Oli
    Jan 25 '14 at 8:48
  • A simpler solution is simply: find dir/* > file.txt Jan 28 '14 at 16:08
  • 1
    @KohjahBreese The only problem I can see from that is won't (it might) list hidden directories in the first layer (but it will in subsequent).
    – Oli
    Jan 28 '14 at 16:25

in the directory you want to show the contents of:

for all (folders and files)

ls -d1 $PWD/**/*

for just folders

ls -d1 $PWD/**

for just files

ls -d1 $PWD/*.*

You should be able to use this to print files only:

find scripts/html '!' -type d

Anything that is not a directory will be printed.

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.