How to list all files in a directory with absolute paths

You can use find. Assuming that you want only regular files, you can do:

find /path/to/dir -type f > listOfFiles.list

You can adjust the type parameter as appropriate if you want other types of files.


ls -d "$PWD"/* > listOfFiles.list

Note that in:

ls -d "$PWD"/* > listOfFiles.list

It's the shell that computes the list of (non-hidden) files in the directory and passes the list to ls. ls just prints that list here, so you could as well do:

printf '%s\n' "$PWD"/*

Note that it doesn't include hidden files, includes files of any type (including directories) and if there's no non-hidden file in the directory, in POSIX/csh/rc shells, you'd get /current/wd/* as output. Also, since the newline character is as valid as any in a file path, if you separate the file paths with newline characters, you won't be able to use that resulting file to get back to the list of file reliably.

With the zsh shell, you could do instead:

print -rNC1 $PWD/*(ND-.) > listOfFiles.list

Where:

  • -rC1 prints raw on 1 Column.
  • -N, output records are NUL-delimited instead of newline-delimited (lines) as NUL is the only character that can't be found in a file name.
  • N: expands to nothing if there's no matching file (nullglob)
  • D: include hidden files (dotglob).
  • -.: include only regular files (.) after symlink resolution (-).

Then, you'd be able to do something like:

xargs -0 rm -f -- < listOfFiles.list

To remove those files for instance.

You could also use the :P modifier in the glob qualifiers to get the equivalent of realpath() on the files expanded from the globs (gets a full path exempt of any symlink component):

print -rNC1 -- *(ND-.:P) > listOfFiles.list