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
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 ***
>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
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/>
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
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
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
-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
~$ mkdir -p 1/2/3
~$ find 1 ! -name . -prune -print
1
~$ find 1/. ! -name . -prune -print
1/./2
--
Stᅵphane
Ah, yes, I missed the -prune, thanks
Bye, Jojo
Oh dang, I assumed it was standard since it was in both GNU and BSD,
thanks for clearing that up!