Results 1 to 4 of 4

Thread: Save every fifth file

  1. #1
    Junior Member
    Join Date
    Jun 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default Save every fifth file

    Hi !
    Never done any scripting in Linux , so here is my problem.

    I need to have a script that saves every fifth file in a directory, but deletes the other ones.

    A bonus would be if the calling of the script would 'set' the value of which files that I want to save (fifth , tenth , second ......)

    Sounds like an easy one for you here

  2. #2
    Is that all you got? rockdalinux's Avatar
    Join Date
    May 2005
    Location
    Planet Vegeta
    Posts
    987
    Thanks
    27
    Thanked 71 Times in 62 Posts
    Rep Power
    20

    Default

    I hope I'm not helping with school homework problem

    here is the script:
    Code:
    #!/bin/bash
    OUT="/tmp/list.$$"
    c=0
    pos=$1 # file position 
    dir=$2  # directory name
    [ $# -ne 2 ] && exit 1 || :
    find ${dir} -type f -print > $OUT
    while read line
    do
       (( c++ ))
       if [ $c -eq $pos ];
       then
    	echo "Deleting $line at $c pos..."
            echo "/bin/rm $line"
    	c=0 # reset
       fi
    done < $OUT
    
    /bin/rm $OUT
    Call script as follows for 5th position and /tmp dir:
    Code:
    ./script.sh 5 /tmp
    For 10th position and /tmp/d2 dir
    Code:
    ./script.sh 5 /tmp/d2
    Read scripting book if you need further information regarding all commands used in this script.

    Good luck!
    Rocky Jr.
    What's wrong? I hope I am not making you uncomfortable...

    Never send a boy to do a mans job.

  3. #3
    Junior Member
    Join Date
    Jun 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default Nice !

    Nice one !

    I had to change it a little bit so it saves every fifth , not deletes every fifth as it did in your script

    One problem that I still have is that when it creates the list it processes , the list is not sorted correct.

    Is there anyway to use the SORT command when using the FIND command in your script ?

    \Mats

  4. #4
    Junior Member
    Join Date
    Jun 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default Think I solved it...

    It works...
    I also added so that only files that starts with Sqr is processed.
    ThanX rockdalinux !!!

    here is the script as of now :

    #!/bin/bash
    OUT="/tmp/list.$$"
    c=0
    pos=$1 # file position
    dir=$2 # directory name
    [ $# -ne 2 ] && exit 1 || :
    find ${dir} -name Sqr\* -type f | sort -o $OUT
    while read line
    do
    (( c++ ))
    if [ $c -eq $pos ];
    then
    echo "Keeping $line at $c pos..."
    # echo "/bin/rm $line"
    c=0 # reset
    else
    echo "Deleting $line at $c pos..."
    echo "/bin/rm $line"
    /bin/rm $line
    fi
    done < $OUT
    /bin/rm $OUT

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 2
    Last Post: 31st May 2008, 07:44 PM
  2. Replies: 1
    Last Post: 31st October 2007, 07:04 PM
  3. wget url marked 'save target-as'
    By sasuhaib in forum The Hangout
    Replies: 0
    Last Post: 14th September 2007, 12:17 PM
  4. How to save history of a hung terminal
    By ricc in forum Linux software
    Replies: 2
    Last Post: 20th June 2006, 01:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •