Unix administration - What is wrong with this script?

This is Interesting: Free IT Magazines  
Home > Archive > Unix administration > March 2007 > What is wrong with this script?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author What is wrong with this script?
KAKA

2007-03-08, 7:51 pm

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

Barry Margolin

2007-03-09, 1: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 ***
KAKA

2007-03-09, 1: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.

Doug Freyburger

2007-03-09, 1: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.

KAKA

2007-03-09, 7:24 pm

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!


Doug Freyburger

2007-03-09, 7:24 pm

"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 `

KAKA

2007-03-10, 1: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!

Barry Margolin

2007-03-10, 7:16 pm

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 ***
KAKA

2007-03-11, 1: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



Barry Margolin

2007-03-11, 1: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 ***
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com