1833883 Members
1728 Online
110063 Solutions
New Discussion

How to list the files?

 
SOLVED
Go to solution
N Gopal
Frequent Advisor

How to list the files?

Hi,

I have one dir which contains all txt files.

In one script i am using statement to list the files.
dir name is say dir.

ls $dir_path > /abc/def/listfile

it will list all files names in listfile.

Now this dir contains xml files as well. I want to list only txt file. I updated code like..

ls $dir_path/*.txt > /abc/def/listfile

It is listing the files in listfile along with full path. But in previous case only file names are coming. I want only name.

Can any please help me what needs to be changed?

Thanks
10 REPLIES 10
Laurent Menase
Honored Contributor

Re: How to list the files?

(cd $dir_path ; ls *.txt) >abc/def/listfile
or
find $dir_path -name \*.txt -exec basename {} \; >abc/def/listfile

.....
Dennis Handly
Acclaimed Contributor
Solution

Re: How to list the files?

>I want to list only txt file.

You can use fgrep to select them:
ls $dir_path | fgrep .txt > /abc/def/listfile
Shrikant Lavhate
Esteemed Contributor

Re: How to list the files?

Hi,

try

#ls $dir_path/*.txt |sed -e "s/$dir_path/ /"|cut -c 3-

Gives out only txt file names.
Will it remain a personal, if I broadcast it here!
Ralph Grothe
Honored Contributor

Re: How to list the files?

How about

ls /path/to/files/*txt | rev | cut -d/ -f1 | rev
Madness, thy name is system administration
N Gopal
Frequent Advisor

Re: How to list the files?

Many thanks to all,

Out of all Dennis's reply is most useful in my case.

Thanks for your concerns
Dennis Handly
Acclaimed Contributor

Re: How to list the files?

Oops, if you don't want to find foo.txtxxx you need to anchor it:
ls $dir_path | grep '\.txt$' > listfile
Arturo Galbiati
Esteemed Contributor

Re: How to list the files?

Hi,
ll $Dir_path|awk '/txt$/'
Rgds,
Art
Weertman
Frequent Advisor

Re: How to list the files?

(cd $dir_path;ls *.txt)
N Gopal
Frequent Advisor

Re: How to list the files?

Hi All,

Laurent and Weertman actually i do not want to use cd.

Thanks Dennis again this is much better for me.

Thanks

N Gopal
Frequent Advisor

Re: How to list the files?

Hi,

Thanks a lot i got solution of my problem.