views:

1895

answers:

6

How can I get ls to spit out a flat list of recursive one-per-line paths?

For example, I just want a flat listing of files with their full paths:

/home/dreftymac/.
/home/dreftymac/foo.txt
/home/dreftymac/bar.txt
/home/dreftymac/stackoverflow
/home/dreftymac/stackoverflow/alpha.txt
/home/dreftymac/stackoverflow/bravo.txt
/home/dreftymac/stackoverflow/charlie.txt

ls -a1 almost does what I need, but I do not want path fragments, I want full paths.

Anyone?

A: 

I don't know about the full path, but you can use -R for recursion. Alternatively, if you're not bent on ls, you can just do find *.

Justin Johnson
A: 

find / will do the trick

Dmitry
+8  A: 

Use find:

find .
find /home/dreftymac

If you want files only (omit directories, devices, etc):

find . -type f
find /home/dreftymac -type f
stefanB
His example shows folders as well as files.
Justin Johnson
Mine shows folders as well ...
stefanB
A: 

If the directory is passed as a relative path and you will need to convert it to an absolute path before calling find. In the following example, the directory is passed as the first parameter to the script:

#!/bin/bash

# get absolute path
directory=`cd $1; pwd`
# print out list of files and directories
find $directory
John Keyes
If your system has `readlink` you can do `directory=$(readlink -e $1)`
Dennis Williamson
True, but cd/pwd combination will work on every system. `readlink` on OS X 10.5.8 does not support -e option.
John Keyes
+1  A: 

@OP, if you really want to use ls, then format its output using awk

ls -R /path | awk '
/:$/&&f{s=$0;f=0}
/:$/&&!f{sub(/:$/,"");s=$0;f=1;next}
NF&&f{ print s"/"$0 }'
ghostdog74
Not insisting on ls, but 1+ for sticking to the question against all odds.
dreftymac
+1  A: 

Using no external commands other than ls:

ls -R1 /path | 
  while read l; do case $l in *:) d=${l%:};; "") d=;; *) echo "$d/$l";; esac; done

Idelic