Operating System - HP-UX
1753499 Members
4394 Online
108794 Solutions
New Discussion юеВ

List only filenames in a directory

 
SOLVED
Go to solution
Brecht De Baets
Frequent Advisor

List only filenames in a directory

Hi,

I want to list only the files in a directory (so no subdirectories and just the filenames). The result should be in a new file called files.txt.
So far I have used the following statement :
ls -l | grep ^- | awk '{print $9}' > files.txt

In certain directories I have filenames like :
1 3.doc

The statement above only gives the following result :
1

I have also tried the following :
ls -l | grep ^- | awk '{for (i=9; i<=NF; i++) {printf("%s ", $i)} printf("\n")}' > files.txt

But now the result is :
1 3.doc

Is there a way to become the full filename with all the spaces in it(so 1 3.doc) ?

Regards
22 REPLIES 22
Raj D.
Honored Contributor

Re: List only filenames in a directory

Brecht,
What about:
# find . -type f -exec ls -l {} \; | cut -c 59-

Cheers,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
Brecht De Baets
Frequent Advisor

Re: List only filenames in a directory

Thanks for the reply,

If I use the statement you posted, I also see the directory-structure, so :

/mnt/appserv/ontwikkel/1 3.doc

I only need the filename.

Regards,
Dennis Handly
Acclaimed Contributor

Re: List only filenames in a directory

>But now the result is: 1 3.doc

Isn't that what you want? (If you are complaining about the trailing space, you must explicitly mention it, since we can't see them.)

>Is there a way to become the full filename with all the spaces in it (so 1 3.doc)?

Well, you can smarten up your awk script:
ll | awk '
/^-/ {
for (i=9; i < NF; ++i)
printf("%s ", $i)
printf("%s\n", $NF)
}' > files.txt

Or you can use find(1) with -prune:
find . ! -name . -prune -o -type -f -print

The names will have leading "./".
Raj D.
Honored Contributor

Re: List only filenames in a directory

# ls -l | grep ^- |cut -c 59- #Cheers.
" If u think u can , If u think u cannot , - You are always Right . "
Brecht De Baets
Frequent Advisor

Re: List only filenames in a directory

My actual filename contains more than 1 space, so 1spacespacespacespace3.doc (the editor of the forum threw the extra spaces away).

The result with the awk command returns the filename with only 1 space.

The statement :
"find . ! -name . -prune -o -type -f -print"
returns an error

I also needs something to cut the directories away, so I only get the filename as a result.

Regards,
Brecht
Dennis Handly
Acclaimed Contributor

Re: List only filenames in a directory

>the editor of the forum threw the extra spaces away.

Right, you need to mention it or attach a file.

>The result with the awk command returns the filename with only 1 space.

You could smarten up awk by using index to find your $9 string and then use substr but $9 may not be unique.

>"find . ! -name . -prune -o -type -f -print" returns an error

Sorry, not -f:
find . ! -name . -prune -o -type f -print

>I also need something to cut the directories away

That's the easy part, for ".": :-)
find . ! -name . -prune -o -type f -print |
sed -e 's:^\./::'
Dennis Handly
Acclaimed Contributor

Re: List only filenames in a directory

>ls -l | grep ^- | cut -c 59- solved my problem.

Just wait until you have a file with more than the default field width for the filesize.
Brecht De Baets
Frequent Advisor

Re: List only filenames in a directory

Point taken, Dennis.

find . ! -name . -prune -o -type f -print |
sed -e 's:^\./::' is indeed a better statement.

I had to exclude the -print option. Otherwise it returned nothing ?

Also, when I replace the .(local dir) with the directory to search in, I don't get the filename but the directory itself, so :

find /mnt/appserv/ontwikkel ! -name . -prune -o -type f -print | sed -e 's:^\./::'

gives as a result :

/mnt/appserv/ontwikkel



Dennis Handly
Acclaimed Contributor

Re: List only filenames in a directory

>I had to exclude the -print option. Otherwise it returned nothing?

(Sorry, I can't get to my HP-UX system until tomorrow. From what I remember, you need that -print right there.
Which part returned nothing, the find or the whole pipeline?)

>when I replace the .(local dir) with the directory to search in, I don't get the filename but the directory itself, so:
find /mnt/appserv/ontwikkel ! -name . -prune -o -type f -print | sed -e 's:^\./::'

The monkey (cherchez le singe!) principle indicates you change all of the "." with /mnt/appserv/ontwikkel:
DIR=/mnt/appserv/ontwikkel
find $DIR ! -name $DIR -prune -o -type f -print | sed -e "s:^$DIR/::"

Or cd to that directory then use "find .".