1826216 Members
2870 Online
109691 Solutions
New Discussion

Unable to list file

 
SOLVED
Go to solution
hangyu
Regular Advisor

Unable to list file

When I try to list the file from a directory , as it is too many files , it pop the below message , can advise how can I list it ? thx

bash: /bin/ls: Argument list too long
10 REPLIES 10
Steven E. Protter
Exalted Contributor

Re: Unable to list file

Shalom,

Well you might try a different shell.

ls has a limit to the number of files it can display. Period.

Reorganize the files.

ls a*
ls b*

go for shorter lists.

# this will run VERY slowly.
ls -1 > list
vi list


SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
hangyu
Regular Advisor

Re: Unable to list file

thx reply ,

" ls has limit " , is it possible to increase the limit ? thx
RAC_1
Honored Contributor

Re: Unable to list file

that is MAX_ARG limit in ksh. should have somthing similar in bash. I don't think that can be increased.
There is no substitute to HARDWORK
Peter Nikitka
Honored Contributor
Solution

Re: Unable to list file

Hi,

the message 'arglist too long' tells not only a limitation in shells but a limit an exec() call can handle.
Changing to another shell to get a successful
ls -d *
wont help.
Rearanging the directory structure (yes, we have something like directories to put files into!) will also lead to a performace benefit when accessing files.

MfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Dennis Handly
Acclaimed Contributor

Re: Unable to list file

As Peter says, you can't get around this limitation if you have that many files. But you can list all files in the directory and then use grep to limit the ones you want to see. Assuming you wanted all files that start with a:
ls | grep "^a"
hangyu
Regular Advisor

Re: Unable to list file

thx reply ,

do you mean it is not possible to solve it ? thx
john korterman
Honored Contributor

Re: Unable to list file

Hi Hangyu,

if I remember correctly the message appears in connection with listing files when using wildcards, e.g. "ls a*", and it will expand to more than 200 file names.
You have to use the wildcards in such a way that they will expand to less than 200 files - or whatever the number is.

regards,
John K.
it would be nice if you always got a second chance
Frank de Vries
Respected Contributor

Re: Unable to list file

What I normally do in such cases is
to use find.

find /yourdir/* -prune -xdev -type f -exec ls -l {} \;

If subdir are included (not in your case I think, but since we are on the subject,
I may as well let it out:)
I make a loop.

for i in $(ls -lp | grep "/")
do
find /yourdir/* -prune -xdev -type f -exec ls -l {} \;
done


Look before you leap
Dennis Handly
Acclaimed Contributor

Re: Unable to list file

>Hangyu: do you mean it is not possible to solve it

You solve it by listing all of the files and doing grep. Or as John says, use wild cards that match fewer files, then do multiple ls(1). You may try what Steven said, use the posix shell, sh.

>john: than 200 files - or whatever the number is

I have a directory with > 5000 files, so it is greater than 200. It will depend on the length of each name. It think the total size is at least 1 Mb.
Peter Nikitka
Honored Contributor

Re: Unable to list file

Hi,

though Franks solution will work, it may take ages until the command finishes.

Look for this alternatives:

1) ls | fgrep 'string'
ls | grep 'regex-pattern'

2) First filter, then look for additional info:
find . -prune -type f -name 'filematch-pattern'
find . -prune -type f -name 'filematch-pattern' | xargs ls -ld
find . -prune -type f | xargs ls -ld

Bounds for the limiting values of arg_max are found in /usr/include/limits.h

I just don't have a HP at hand, but you can dive into SAM to check, if that value is a changeable kernel param - but I do not recommend this.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"