What is wrong with this script? - Unix

This is a discussion on What is wrong with this script? - Unix ; 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 ...

+ Reply to Thread
Results 1 to 10 of 10

Thread: What is wrong with this script?

  1. What is wrong with this script?

    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


  2. Re: What is wrong with this script?

    In article <1173396227.744871.126770@n33g2000cwc.googlegroups. com>,
    "KAKA" 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 ***

  3. Re: What is wrong with this script?

    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.


  4. Re: What is wrong with this script?

    "KAKA" 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.


  5. Re: What is wrong with this script?

    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!



  6. Re: What is wrong with this script?

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


  7. Re: What is wrong with this script?

    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!


  8. Re: What is wrong with this script?

    In article <1173505413.466850.289340@v33g2000cwv.googlegroups. com>,
    "KAKA" 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 ***

  9. Re: What is wrong with this script?

    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




  10. Re: What is wrong with this script?

    In article <1173577094.521818.315450@n33g2000cwc.googlegroups. com>,
    "KAKA" 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 ***

+ Reply to Thread