General
cancel
Showing results for 
Search instead for 
Did you mean: 

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
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 .".
Brecht De Baets
Frequent Advisor

Re: List only filenames in a directory

Hi,

The following statement gives the expected result :

find $DIR -type f | sed -e "s:^$DIR/::"

Actually, I don't seem to need the -prune and the -name options (if I include them I only get to see the directory without the filename).

Regards,
Brecht
Dennis Handly
Acclaimed Contributor

Re: List only filenames in a directory

>The following statement gives the expected result

The -prune option was in case you had subdirectories.

>if I include them I only get to see the directory without the filename.

I'll do some testing when I get back to make sure it isn't something obvious:
Ah, if you don't have ".", you have to use:
find $DIR ! -path $DIR -prune -o -type f -print | sed -e "s:^$DIR/::"

Or: -name $(basename $DIR)
Brecht De Baets
Frequent Advisor

Re: List only filenames in a directory

Thanks Dennis,

Actually I can have subdirectories, but I don't need to see them.

The statement

find $DIR ! -path $DIR -prune -o -type f | sed -e "s:^$DIR/::"

gives a nice result, but if there are some subdirectories onder the $DIR directory, they are also listed.
How can they be left away ?

For example : under $DIR, I have :
1 file (test.dat) and one not-empty directory (testdir). If I issue the above statement, I get :

test.dat
testdir

However I only want test.dat to be displayed.
Dennis Handly
Acclaimed Contributor

Re: List only filenames in a directory

>I can have subdirectories, but I don't need to see them.

That -prune should skip those.

>find $DIR ! -path $DIR -prune -o -type f | sed -e "s:^$DIR/::"
>gives a nice result, but if there are some subdirectories under the $DIR directory, they are also listed. How can they be left away?
>I only want test.dat to be displayed.

That's what the "-print" on the end should do.

Brecht De Baets
Frequent Advisor

Re: List only filenames in a directory

Hi Dennis,

If I add the -print option again, I don't get any results ?

Regards,
Brecht
Elmar P. Kolkman
Honored Contributor
Solution

Re: List only filenames in a directory

A solution might also be:

ls -aF | grep -v /$ | sed 's|[*/=>@|]$||' >files.txt

Or a variant. (Some tools might combine the grep and sed, for instance...)
Every problem has at least one solution. Only some solutions are harder to find.
Dennis Handly
Acclaimed Contributor

Re: List only filenames in a directory

>If I add the -print option again, I don't get any results?

What is the exact command you are using so I can try to duplicate it?
Brecht De Baets
Frequent Advisor

Re: List only filenames in a directory

Dennis,

find $DIR ! -path $DIR -prune -o -type f -print | sed -e "s:^$DIR::"

Regards,
Brecht
Raj D.
Honored Contributor

Re: List only filenames in a directory

Brecht, # ls -l|grep ^- | awk '{for(i=1;i<9;i++) {$i=""};print}' |sed 's/^[ ]*//g' #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

Thanks for the replies :

Elmar, your statement seems to work fine !

Raj, If I try your statement, I lose some spaces in filenames with more than 1 space.
For example filename 1spacespacespace2.dat
return 1space2.dat.
Brecht De Baets
Frequent Advisor

Re: List only filenames in a directory

Elmar,

Can you please explain the [*/=>@|]$ part in your sed statement ?

Regards,
Brecht
Dennis Handly
Acclaimed Contributor

Re: List only filenames in a directory

find $DIR ! -path $DIR -prune -o -type f -print | sed -e "s:^$DIR::"

(You are missing a "/" in sed.)

Ok, I found my problem. "-o" does shortcut evaluation and so the files show up on the left of the OR and never get to the -print. So we either need to swap the two predicates or select only directories on the left:
find $DIR -type f -print -o ! -path $DIR -prune | sed -e "s:^$DIR/::"
find $DIR ! -path $DIR -prune -type d -o -type f -print | sed -e "s:^$DIR/::"

>Can you please explain the [*/=>@|]$ part in your sed statement?

When you use ls -F, you get the files "formatted" by a trailing special char. If "/", directories are excluded.
The sed part just strips off the other special trailing formatting chars as not needed:
* (executable); | (pipe); / (directory, already gone); @ (symlink)
(Not sure about "=" or ">" for HP-UX.)
Brecht De Baets
Frequent Advisor

Re: List only filenames in a directory

Thanks to you all, for the provided solutions.