652

How can I 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.

2
  • 3
    See also: tree
    – dreftymac
    Feb 19, 2019 at 12:21
  • 3
    tree -aflix --noreport <path> but if you use tree and there are any symbolic links in the path you will have to deal with those or use an alternate solution from one of the suggested answers.
    – Vector
    Jul 29, 2020 at 15:37

26 Answers 26

748

Use find:

find .
find /home/dreftymac

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

find . -type f
find /home/dreftymac -type f
8
  • 2
    can ls parameters like --sort=extension "redeemed" by this solution?
    – n611x007
    Sep 28, 2012 at 19:19
  • 1
    You can even use printf output in order to display needed contextual info (e.g. find . -type f -printf '%p %u\n')
    – xsubira
    Apr 14, 2015 at 8:42
  • Can this be formatted with a falg? ie. python pprint.pprint(files) Apr 13, 2018 at 19:24
  • 1
    @Shayan find with the -printf predicate allows you to do everyting ls does, and then some. However, it is not standard. You can use find -exec stat {} \; but unfortunately the options to stat are not standardized, either.
    – tripleee
    Jul 15, 2021 at 7:48
  • 1
    ... In the end, unfortunately, the most portable solution might be a Perl or Python script.
    – tripleee
    Jul 15, 2021 at 8:07
439

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 }'
12
  • 11
    Can someone please explain the above awk expressions? Oct 10, 2014 at 17:08
  • 2
    This solution doesn't omit the directories (each directory gets its own line)
    – JayB
    Sep 27, 2016 at 18:40
  • 2
    Thanks. Btw, would be nice to have this in 1 line for quick copy & paste. Mar 9, 2018 at 16:29
  • 2
    The other answer should be accepted. This is a good second answer and does indeed answer the question as written, but the best solution is the one using find. The intention of the question is clear (which is not that it has to done using ls), and this answer is better only for those who can't use find. The claim this answer is more "right" because it answers the written question instead of the intended one is pretty ridiculous.
    – Jasper
    Jun 18, 2019 at 9:39
  • 2
    Do you mind explaining the awk code? It looks like you are using a regex to catch lines that end in ":" (the "headers" with parent directory paths), but I get lost after that and definitely don't understand the part where the last field NF is being evaluated as true/false. Thanks!
    – Josh
    Sep 11, 2020 at 23:17
92

ls -ld $(find .)

if you want to sort your output by modification time:

ls -ltd $(find .)

6
  • 16
    -bash: /bin/ls: Argument list too long
    – jperelli
    Mar 1, 2012 at 17:37
  • 1
    +1 worked for me with 12106 files, and I could use the --sort=extension parameter of ls
    – n611x007
    Sep 28, 2012 at 19:22
  • 6
    Thanks. I wouldn't have thought by myself of that (nice and short) syntax - i would have used find . -name "*" -exec ls -ld '{}' \; (that one works whatever the number of files is), but your command is way shorter to write ;)
    – SRG
    Feb 27, 2015 at 13:34
  • 2
    ls -ld $(find .) breaks for me if I'm listing a ntfs disk where files have spaces: ls: cannot access ./System: No such file or directory however find with quotes by @SRG works
    – kuz8
    Dec 1, 2016 at 2:49
  • 3
    A shorter alternative (depending on your needs) would be find . -ls.
    – IanS
    Jun 8, 2018 at 10:14
71

Best command is: tree -fi

-f print the full path prefix for each file
-i don't print indentations

e.g.

$ tree -fi
.
./README.md
./node_modules
./package.json
./src
./src/datasources
./src/datasources/bookmarks.js
./src/example.json
./src/index.js
./src/resolvers.js
./src/schema.js

In order to use the files but not the links, you have to remove > from your output:

tree -fi |grep -v \>

If you want to know the nature of each file, (to read only ASCII files for example) try a while loop:

tree -fi |
grep -v \> |
while read -r first ; do 
    file "${first}"
done |
grep ASCII
4
  • @Nakilon what's the closest thing? Does it display output similarly? How would you easily display similar output with a short command?
    – om01
    Sep 18, 2013 at 16:46
  • 7
    @om01: on osx it is a difficult as brew install tree, given you are using homebrew
    – ccpizza
    Jan 28, 2014 at 9:57
  • i couldn't list the files exclusivelty. it always lists the directories too. how can i do that? Jun 9, 2015 at 12:04
  • @kommrad You could adapt the file example and pipe to grep -v directory
    – tripleee
    Jul 15, 2021 at 7:45
66

Oh, really a long list of answers. It helped a lot and finally, I created my own which I was looking for :

To List All the Files in a directory and its sub-directories:

find "$PWD" -type f

To List All the Directories in a directory and its sub-directories:

find "$PWD" -type d

To List All the Directories and Files in a directory and its sub-directories:

find "$PWD"
4
  • 7
    And to filter by extension: find "$PWD" -type f | grep '\.json$'
    – Killroy
    Jan 15, 2019 at 14:55
  • 4
    No Need of post-processing with grep, use -name in find like: find "$PWD" -type f -name *.json and if you want to delete the files listed : find "$PWD" -type f -name *.json -exec rm {} \; similarly, if you want to copy it then replace rm with cp and destination: -exec cp {} destination Jun 18, 2019 at 8:41
  • must use "$PWD/" in my condition: find "$PWD/" -type f;
    – walknotes
    Aug 12, 2020 at 1:05
  • 1
    And to list a particular file with its full path find "$PWD/README.md" Nov 25, 2020 at 10:39
62

Try the following simpler way:

find "$PWD"
4
  • 11
    find "`pwd`" if the path contains spaces or some other special characters.
    – mustafa.0x
    Sep 30, 2013 at 11:30
  • 9
    How is this any different than find .? -.-
    – Salman
    Dec 12, 2014 at 6:13
  • 9
    @SalmanPK If you give find an absolute path like pwd to start with, it will print absolute paths. By the way, "How is this any different than find" ;-)
    – yckart
    Sep 3, 2015 at 13:46
  • find without an argument is a syntax error on some platforms. Where it isn't, just find is equivalent to find ..
    – tripleee
    Jul 15, 2021 at 8:09
23
du -a

Handy for some limited appliance shells where find/locate aren't available.

1
  • 1
    any idea how I can remove the filesize wihtout awk ?
    – Arnold Roa
    Dec 20, 2016 at 20:51
21

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 *.

18

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

3
  • Unknown option '-1'. Aborting.
    – ilw
    Jul 7, 2017 at 13:18
  • @ilw That's weird; I'd think ls -1 is fairly standard; but try just leaving it out if it's unsupported. The purpose of that option is to force ls to print one line per file but that's usually its behavior out of the box anyway. (But then of course, don't use ls in scripts.) (Looking at the POSIX doco, this option was traditionally BSD only, but was introduced in POSIX in 2017.)
    – tripleee
    Apr 12, 2021 at 4:50
  • All the subfolders will be in the list, not just files. Jun 3, 2022 at 21:14
14

find / will do the trick

0
14

Run a bash command with the following format:

find /path -type f -exec ls -l \{\} \;

Likewise, to trim away -l details and return only the absolute paths:

find /path -type f -exec ls \{\} \;
2
  • find -ls avoids running an external process for each file and is a lot easier to type.
    – tripleee
    Apr 12, 2021 at 4:47
  • 1
    You don´t need the -exec ls \{\} \; part, since the default behavior of find is to print the full path. That is, find /path -type f does the job if you don´t need the file attributes from ls -l. Jul 14, 2021 at 21:23
10

The easiest way for all you future people is simply:

du

This however, also shows the size of whats contained in each folder You can use awk to output only the folder name:

du | awk '{print $2}'

Edit- Sorry sorry, my bad. I thought it was only folders that were needed. Ill leave this here in case anyone in the future needs it anyways...

1
  • Interesting, because it shows me stuff I didn't know I wanted to know -- kind of like Google suggest. It turns out, I like knowing how much space each file takes. Feb 2, 2015 at 18:04
9

With having the freedom of using all possible ls options:

find -type f | xargs ls -1

8

I think for a flat list the best way is:

find -D tree /fullpath/to-dir/ 

(or in order to save it in a txt file)

find -D tree /fullpath/to-dir/ > file.txt
1
  • 5
    Tried on Mac OS 10.13.3. find: illegal option -- D Aug 14, 2018 at 9:53
7

Adding a wildcard to the end of an ls directory forces full paths. Right now you have this:

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

You could do this instead:

$ ls /home/dreftymac/*
/home/dreftymac/.
/home/dreftymac/foo.txt
/home/dreftymac/bar.txt
/home/dreftymac/stackoverflow:
alpha.txt
bravo.txt
charlie.txt

Unfortunately this does not print the full path for directories recursed into, so it may not be the full solution you're looking for.

2
  • Also unfortunately you can't sudo ls with a wildcard (because the wildcard is expanded as the normal user). Mar 9, 2017 at 0:14
  • Also unfortunately, ls has a lot of pesky corner cases; see parsing ls
    – tripleee
    Jul 15, 2021 at 8:12
7

Don't make it complicated. I just used this and got a beautiful output:

ls -lR /path/I/need
3
  • 7
    OP wants just the full path and nothing else. ls -lR wouldn't meet that goal. Feb 8, 2017 at 17:41
  • sorry, it doesn't do it May 13, 2018 at 9:53
  • Sorry man, doesn't fit the bill. If you need a list of full paths, you won't get it this way. At least not with bash or zsh on BSD or MacOS Dec 15, 2020 at 11:06
6

Here is a partial answer that shows the directory names.

ls -mR * | sed -n 's/://p'

Explanation:

ls -mR * lists the full directory names ending in a ':', then lists the files in that directory separately

sed -n 's/://p' finds lines that end in a colon, strip off the colon and print the line

By iterating over the list of directories, we should be able to find the directories as well. Still workin on it. It is a challenge to get the wildcards through xargs.

0
5

The realpath command prints the resolved path:

realpath *

To include dot files, pipe the output of ls -a to realpath:

ls -a | xargs realpath

To list subdirectories recursively:

ls -aR | xargs realpath

In case you have spaces in file names, man xargs recommends using the -o option to prevent file names from being processed incorrectly, this works best with the output of find -print0 and it starts to look a lot more complex than other answers:

find -print0 |xargs -0 realpath

See also Unix and Linux stackexchange question on how to list all files in a directory with absolute path.

0
4
tar cf - $PWD|tar tvf -             

This is slow but works recursively and prints both directories and files. You can pipe it with awk/grep if you just want the file names without all the other info/directories:

tar cf - $PWD|tar tvf -|awk '{print $6}'|grep -v "/$"          
1
  • 2
    you can also use simply : tar cvf /dev/null $PWD
    – RuleB
    Sep 14, 2013 at 18:49
4

A lot of answers I see. This is mine, and I think quite useful if you are working on Mac.

I'm sure you know there are some "bundle" files (.app, .rtfd, .workflow, and so on). And looking at Finder's window they seem single files. But they are not. And $ ls or $ find see them as directories... So, unless you need list their contents as well, this works for me:

find . -not -name ".*" -not -name "." | egrep -v "\.rtfd/|\.app/|\.lpdf/|\.workflow/"

Of course this is for the working dir, and you could add other bundles' extensions (but always with a / after them). Or any other extensions if not bundle's without the /.

Rather interesting the ".lpdf/" (multilingual pdf). It has normal ".pdf" extension (!!) or none in Finder. This way you get (or it just counts 1 file) for this pdf and not a bunch of stuff…

4

ls -lR is what you were looking for, or atleast I was. cheers

1
  • OP wants just the full path and nothing else. ls -lR wouldn't meet that goal. May 14, 2020 at 9:13
4

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"
2
  • If your system has readlink you can do directory=$(readlink -e $1) Nov 20, 2009 at 0:37
  • True, but cd/pwd combination will work on every system. readlink on OS X 10.5.8 does not support -e option.
    – John Keyes
    Nov 20, 2009 at 1:06
3

Recursive list of all files from current location:

ls -l $(find . -type f)

1
  • Better yet find . -type f -ls which won't choke if the output from find is too long for ls
    – tripleee
    Apr 12, 2021 at 4:45
3

If you have to search on a large disk like 100 Gb or more, I suggest to do the command tree that @kerkael posted and not the find or ls.

I suggest that when you do the command tree you write the output in the file, different from the @kerkael's post.

Example:

tree -fi > result.txt

Afterwards, do a grep command in file using a pattern like grep -i "*.docx" > result.txt so you do not lose time. This way is faster for searching file(s) on big disks.

I did these commands on a 270GB disk that I get a file txt taken 100MB.

The time that taken for command tree was 14 minutes. enter image description here

2

I knew the file name but wanted the directory as well.

find $PWD | fgrep filename

worked perfectly in Mac OS 10.12.1

2
  • Works like a charm, also on MacOS 10.15.7, in Terminal, using zsh as default shell. Wonderful, and thanks! Dec 15, 2020 at 11:09
  • 1
    This is rather imprecise and inefficient. A better attempt is find . -name filename
    – tripleee
    Apr 12, 2021 at 4:44
2

@ghostdog74: Little tweak with your solution.
Following code can be used to search file with its full absolute path.

sudo ls -R / | awk '
/:$/&&f{s=$0;f=0}
/:$/&&!f{sub(/:$/,"");s=$0;f=1;next}
NF&&f{ print s"/"$0 }' | grep [file_to_search]
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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