Directory listing

Hi,

I have a directory with a bunch of files say around 150K.

I want the directory's path and the filenames printed to a text file.

Example:
If I am in the directory /path/test and the files in this directory are

My output file should be like this

Thanks in advance

---------- Post updated at 03:59 PM ---------- Previous update was at 03:51 PM ----------

I found it on google.

Hope it helps someone.

ls -d $PWD/* 

Thanks

That will blow up if there are too many files in the directory. That's a useless use of ls, anyway, since * finds all the files.

ls | awk -v PWD="$PWD" '$0=PWD $0'

Try this (this does not have a limitation on the number of arguments, since printf is a shell built-in):

printf "%s\n" "$PWD"/*.txt

In this case the asterisk serves to get the pwd in, without it it will just list the files...

Hi jacobs.smith,

Using perl:

$ perl -MFile::Spec -e 'for ( glob "*" ) { printf qq[%s\n], File::Spec->rel2abs( $_ ) if -f }'

Modifying the solution posted in edited post #1 to allow for an empty directory, a directory name containing space characters, or a directory containing subdirectories or other non-files:

ls -1d "${PWD}"/* 2>/dev/null | while read filename
do
        if [ -f "${filename}" ]
        then
               echo "${filename}"
        fi
done

Ps. Storing 150k files on one directory is a design issue. You can get severe performance issues or even failures.
We can only issue a "ls" command in such a directory if there are enough resources to sort the list (the unix "ls" command always sorts the list) and the o/s will tolerate the command.
If "ls" stops working, use "find" and a custom "sort".

Folowing Scrutinizer's excellent post, I revise as follows (untested): O/P did not specify ".txt" in posted solution.

printf "%s\n" "$PWD"/* | while read filename
do
        if [ -f "${filename}" ]
        then
               echo "${filename}"
        fi
done