Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ksh: /usr/bin/find: arg list too long

1,315 views
Skip to first unread message

Luke

unread,
Oct 14, 2009, 12:23:21 PM10/14/09
to

Unix version is: SunOS 5.10 Generic_141414-02 sun4v sparc SUNW,T5240

I am encountering the following error:

'ksh: /usr/bin/find: arg list too long'

for this script:

#!/bin/ksh
FILE_AGE_CRITERION_IN_DAYS=$1
FILE_NAME_PATTERN_TO_MATCH=*
DIR_TO_CLEAN=$3
LOG_DIR=$4

FILES_DELETED_COUNT=0
for FILE in `find $DIR_TO_CLEAN/* -prune -type f -name
"$FILE_NAME_PATTERN_TO_MATCH" -mtime +$FILE_AGE_CRITERION_IN_DAYS -
print`
do
rm -f $FILE
let "FILES_DELETED_COUNT+=1"
done
echo "- Deleted $FILES_DELETED_COUNT file(s) from $DIR_TO_CLEAN" | tee
-a $LOG_DIR/$LOG_FILE


The directory that I am trying to clean up is huge and I would prefer
to not have to provide multiple file name patterns to match.

Can someone provide an alternative code snippet that will:

1) Avoid the 'ksh: /usr/bin/find: arg list too long' issue
2) Delete all files in a directory that meet an age criterion
3) Not delete any files in any sub-directories (prune functionality)
4) Not delete any sub-directories in the directory


TIA

Luke

Barry Margolin

unread,
Oct 14, 2009, 12:38:16 PM10/14/09
to
In article
<4b12bc4e-d879-437e...@j39g2000yqh.googlegroups.com>,
Luke <luke_...@hotmail.com> wrote:

If your find has the -maxdepth option, you can use it to solve this
problem:

find $DIR_TO_CLEAN -maxdepth 1 -type f ...

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***

Eddie Corns

unread,
Oct 14, 2009, 1:00:48 PM10/14/09
to
Luke <luke_...@hotmail.com> writes:

>for this script:


>TIA

>Luke

The first argument to find is a path not a file so you don't use the "/*" ie

find $DIR_TO_CLEAN -prune etc.

Not sure if that would be enough, if the output from find was still too large
it might cause the for statement to blow up. You could do something like:

$ find $DIR blah blah | while read file
do
rm -f $file
blah
done

Or

$ echo Before `ls $DIR | wc -l`
$ find $DIR blah -exec rm -f {} \;
$ echo After `ls $DIR | wc -l`

Eddie

Dagon

unread,
Oct 14, 2009, 2:13:01 PM10/14/09
to
Luke <luke_...@hotmail.com> wrote:
>I am encountering the following error:
>'ksh: /usr/bin/find: arg list too long'
>
...

>for FILE in `find $DIR_TO_CLEAN/* -prune -type f -name
...

You're letting the shell expand the * under $DIR_TO_CLEAN, which means it's
trying to pass thousands of names to the find command. Instead, pass only the
one directory to find and let it figure out all the children, that's what it
does. If you want to exclude the parent directory itself, include -mindepth
1.

--
Mark Rafn da...@dagon.net <http://www.dagon.net/>

Stephane CHAZELAS

unread,
Oct 14, 2009, 3:13:58 PM10/14/09
to
2009-10-14, 09:23(-07), Luke:

>
> Unix version is: SunOS 5.10 Generic_141414-02 sun4v sparc SUNW,T5240
>
> I am encountering the following error:
>
> 'ksh: /usr/bin/find: arg list too long'
>
> for this script:
>
> #!/bin/ksh
> file_age_criterion_in_days=$1
> file_name_pattern_to_match=*
> dir_to_clean=$3
> log_dir=$4
>
> files_deleted_count=0
> for file in `find $dir_to_clean/* -prune -type f -name
> "$file_name_pattern_to_match" -mtime +$file_age_criterion_in_days -
> print`
[...]

files_deleted_count=$(
find "$dir_to_clean/." ! -name . -prune \
! -type d \
-name "$file_name_pattern_to_match" \
-mtime "+$file_age_criterion_in_days" \
-exec rm -f {} \; -exec echo . \;
)

--
Stᅵphane

Joachim Schmitz

unread,
Oct 15, 2009, 3:32:38 AM10/15/09
to

Why not just
files_deleted_count=$(
find "$dir_to_clean" ! -name . -prune \


! -type d \
-name "$file_name_pattern_to_match" \
-mtime "+$file_age_criterion_in_days" \
-exec rm -f {} \; -exec echo . \;
)

I.e. without the /. ?

Bye, Jojo

Bjarni Juliusson

unread,
Oct 15, 2009, 4:34:36 AM10/15/09
to
> The first argument to find is a path not a file so you don't use the "/*" ie
>
> find $DIR_TO_CLEAN -prune etc.
>
> Not sure if that would be enough,

Make that "-maxdepth 1" to never descend into any subdirectories.

find $DIR_TO_CLEAN -maxdepth 1 -type f
-name "$FILE_NAME_PATTERN_TO_MATCH"
-mtime +$FILE_AGE_CRITERION_IN_DAYS

The -print isn't needed.


Bjarni
--

INFORMATION WANTS TO BE FREE

Stephane CHAZELAS

unread,
Oct 15, 2009, 1:34:37 PM10/15/09
to
2009-10-15, 10:34(+02), Bjarni Juliusson:

>> The first argument to find is a path not a file so you don't use the "/*" ie
>>
>> find $DIR_TO_CLEAN -prune etc.
>>
>> Not sure if that would be enough,
>
> Make that "-maxdepth 1" to never descend into any subdirectories.
[...]

-maxdepth is a GNU extension (also recognised by some BSDs), it
won't work on Solaris. See the \( -name . -o -prune \) standard
equivalent (or ! -name . -prune for the standard equivalent of
-mindepth 1 -maxdepth 1).

--
Stᅵphane

Stephane CHAZELAS

unread,
Oct 15, 2009, 1:36:18 PM10/15/09
to
2009-10-15, 09:32(+02), Joachim Schmitz:

[...]
>> files_deleted_count=$(
>> find "$dir_to_clean/." ! -name . -prune \
>> ! -type d \
>> -name "$file_name_pattern_to_match" \
>> -mtime "+$file_age_criterion_in_days" \
>> -exec rm -f {} \; -exec echo . \;
>> )
>
> Why not just
> files_deleted_count=$(
> find "$dir_to_clean" ! -name . -prune \
> ! -type d \
> -name "$file_name_pattern_to_match" \
> -mtime "+$file_age_criterion_in_days" \
> -exec rm -f {} \; -exec echo . \;
> )
>
> I.e. without the /. ?
[...]

~$ mkdir -p 1/2/3
~$ find 1 ! -name . -prune -print
1
~$ find 1/. ! -name . -prune -print
1/./2

--
Stᅵphane

Joachim Schmitz

unread,
Oct 15, 2009, 3:10:18 PM10/15/09
to

Ah, yes, I missed the -prune, thanks

Bye, Jojo

Bjarni Juliusson

unread,
Oct 15, 2009, 4:14:33 PM10/15/09
to

Oh dang, I assumed it was standard since it was in both GNU and BSD,
thanks for clearing that up!

0 new messages