Operating System - HP-UX
1754274 Members
3315 Online
108813 Solutions
New Discussion юеВ

Re: List only filenames in a directory

 
SOLVED
Go to solution
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.