Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

share|improve this question
2… - Several good solutions on that question / answer – bodhi.zazen Jan 24 '14 at 22:10
up vote 2 down vote accepted

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
share|improve this answer
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. – Kohjah Breese Jan 24 '14 at 22:32
@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 – Kohjah Breese Jan 28 '14 at 16:08
@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/*.*
share|improve this answer

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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