What is wrong with this script?
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix administration > What is wrong with this script?




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    What is wrong with this script?  
KAKA


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-09-07 12:51 AM

I created a script to remove whatever file which has last motified
date over certain days old.  The below script only works up to adding
list of files for removal in $FILELIST. However, when I tried to
insert the $FILELIST as a stdin and put in the xargs to rm each file,
this didn't seem to work.  What is wrong on this line in the script
below: "xargs -i rm {} < $FILELIST"

Thanks in advance!

 ########################################
################################

#!/bin/ksh

echo "usage: list_for_rm.sh "'$1 $2'" given "'$1'" is starting dir,
"'$2'" is file modified since"


STARTDIR=$1
MODTIME=$2
FILELIST=filelist.log
HISTORY=delete_history.log

echo "writing log to $FILELIST"

find $STARTDIR -type -f -mtime +$MODTIME -print | xargs -i ls -l {} >
$FILELIST

xargs -i rm {} < $FILELIST

cat $FILELIST >> $HISTORY






[ Post a follow-up to this message ]



    Re: What is wrong with this script?  
Barry Margolin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-09-07 06:31 AM

In article <1173396227.744871.126770@n33g2000cwc.googlegroups.com>,
"KAKA" <kaka.hui@gmail.com> wrote:

> I created a script to remove whatever file which has last motified
> date over certain days old.  The below script only works up to adding
> list of files for removal in $FILELIST. However, when I tried to
> insert the $FILELIST as a stdin and put in the xargs to rm each file,
> this didn't seem to work.  What is wrong on this line in the script
> below: "xargs -i rm {} < $FILELIST"

$FILELIST contains the output of "ls -l", which includes all the
attribute information, not just the names of files to remove.  You're
passing all those attributes to rm, so you get lots of errors about
files that don't exist.  And the permission modes look something like
"-rw-r--r--", which rm will interpret as options because it begins with
"-".

>
> Thanks in advance!
>
>  ########################################
################################
>
> #!/bin/ksh
>
> echo "usage: list_for_rm.sh "'$1 $2'" given "'$1'" is starting dir,
> "'$2'" is file modified since"
>
>
> STARTDIR=$1
> MODTIME=$2
> FILELIST=filelist.log
> HISTORY=delete_history.log
>
> echo "writing log to $FILELIST"
>
> find $STARTDIR -type -f -mtime +$MODTIME -print | xargs -i ls -l {} >
> $FILELIST
>
> xargs -i rm {} < $FILELIST
>
> cat $FILELIST >> $HISTORY

--
Barry Margolin, barmar@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 ***





[ Post a follow-up to this message ]



    Re: What is wrong with this script?  
KAKA


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-09-07 06:25 PM

If I am trying to pass the file name only to the rm {}.  Does it mean
I will have to write a seperate script to strip out everything else
except for the filename?  Anyways, even if I am doing such, I still
don't think the rm can be done precisely, b/c with the list of the
filenames only, it doesn't have the absolute path of the file.

If I am spliting this tasks into 2 scripts: (1) compile a list of
files for removal (2) remove the files according to filelist output.
How am I suppose to generate the output which I can pass to the rm{}
with absolute paths and without additional attribute of the files?

Please advise.  Thanks in advance.






[ Post a follow-up to this message ]



    Re: What is wrong with this script?  
Doug Freyburger


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-09-07 06:25 PM

"KAKA" <kaka....@gmail.com> wrote:
>
> find $STARTDIR -type -f -mtime +$MODTIME -print | xargs -i ls -l {} >
> $FILELIST
>
> xargs -i rm {} < $FILELIST
>
> cat $FILELIST >> $HISTORY

Better something like:

find $STARTDIR -type -f -mtime +$MODTIME -print | tee $FILELIST |
xargs ls -l >> $HISTORY

xargs rm < $FILELIST

The brackets for xargs are optional in the last position and they are
more work
than they are worth to get them right.






[ Post a follow-up to this message ]



    Re: What is wrong with this script?  
KAKA


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-10-07 12:24 AM

Thanks for the reply.  However, I would like to split the script into
2 pieces (one to list and one to remove) because I would let my user
of the directory to review what is going to be removed before actually
executing the rm.  However, I would not want to run the find again on
my remove script for 2 reasons. (1) find is resource intensive (2)
file system is active, if I run the find again in the 2nd script.  It
may not have the same result in the 1st script.

Any idea I can put the remove list with complete path and without
additional attribute?  Thx!







[ Post a follow-up to this message ]



    Re: What is wrong with this script?  
Doug Freyburger


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-10-07 12:24 AM

"KAKA" <kaka....@gmail.com> wrote:
>
> Thanks for the reply.

Please quote some amount of context.  On UseNet each post
needs to stand alone.

> However, I would like to split the script into
> 2 pieces (one to list and one to remove) because I would let my user
> of the directory to review what is going to be removed before actually
> executing the rm.  However, I would not want to run the find again on
> my remove script for 2 reasons. (1) find is resource intensive (2)
> file system is active, if I run the find again in the 2nd script.  It
> may not have the same result in the 1st script.

As I structured it you can run the find ... tee ... at one time
and xargs rm ... at another time.  I don't see the problem.

> Any idea I can put the remove list with complete path and without
> additional attribute?

cd $STARTDIR
REALDIR=` /bin/pwd `






[ Post a follow-up to this message ]



    Re: What is wrong with this script?  
KAKA


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-10-07 06:40 AM

Thanks Doug for your response.  I mean I need to have the output of
directory's full path, I mean from the $STARTDIR and all the directory
recursively under it.  Find can find files under the $STARTDIR
recursively, however, it doesn't print the full path.

Any solution?  (once again, I need to have a print out of full path of
the file (i.e /path/to/file/filename) without printing their file
attribute.

Thanks!






[ Post a follow-up to this message ]



    Re: What is wrong with this script?  
Barry Margolin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-11-07 12:16 AM

In article <1173505413.466850.289340@v33g2000cwv.googlegroups.com>,
"KAKA" <kaka.hui@gmail.com> wrote:

> Thanks Doug for your response.  I mean I need to have the output of
> directory's full path, I mean from the $STARTDIR and all the directory
> recursively under it.  Find can find files under the $STARTDIR
> recursively, however, it doesn't print the full path.
>
> Any solution?  (once again, I need to have a print out of full path of
> the file (i.e /path/to/file/filename) without printing their file
> attribute.
>
> Thanks!

If $STARTDIR is an absolute path then find will print absolute paths.

--
Barry Margolin, barmar@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 ***





[ Post a follow-up to this message ]



    Re: What is wrong with this script?  
KAKA


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-11-07 07:22 AM

Here is my script again.  I believe there is no where I can cd into
each directory and do a pwd here.  My question is, I would like the
$FILELIST to have full path of all files found.  I believe the
suggestion of cding into $STARTDIR and do a pwd would only work, if I
am looking for only one file and do find only once.   Is there any
other alternatives?  Thanks!


STARTDIR=$1
MODTIME=$2
FILELIST=filelist.log
echo "writing log to $FILELIST"
find $STARTDIR -type -f -mtime +$MODTIME -print | xargs -i ls -l {} >
$FILELIST








[ Post a follow-up to this message ]



    Re: What is wrong with this script?  
Barry Margolin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-11-07 06:19 PM

In article <1173577094.521818.315450@n33g2000cwc.googlegroups.com>,
"KAKA" <kaka.hui@gmail.com> wrote:

> Here is my script again.  I believe there is no where I can cd into
> each directory and do a pwd here.  My question is, I would like the
> $FILELIST to have full path of all files found.  I believe the
> suggestion of cding into $STARTDIR and do a pwd would only work, if I
> am looking for only one file and do find only once.   Is there any
> other alternatives?  Thanks!

cd "$1"
STARTDIR=`pwd`

> STARTDIR=$1
> MODTIME=$2
> FILELIST=filelist.log
> echo "writing log to $FILELIST"
> find $STARTDIR -type -f -mtime +$MODTIME -print | xargs -i ls -l {} >
> $FILELIST

BTW, there's no need to pipe find to ls, why not use find's -ls operator?

--
Barry Margolin, barmar@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 ***





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 05:17 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register