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"/* > listOfFiles.list
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 Bourne/POSIX/rc-like 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 files reliably.
With the zsh
shell, you could do instead:
print -rNC1 $PWD/*(ND-.) > listOfFiles.list
Where:
-rC1
prints r
aw on 1
C
olumn.
-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