nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

Save every fifth file

This is a discussion on Save every fifth file within the Shell scripting forums, part of the Development/Scripting category; Hi ! Never done any scripting in Linux , so here is my problem. I need to have a script ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 06-05-2008, 07:30 PM
Junior Member
User
 
Join Date: Jun 2008
My distro: Debian
Posts: 3
Rep Power: 0
Mats is on a distinguished road
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
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-05-2008, 11:29 PM
rockdalinux's Avatar
Contributors
User
 
Join Date: May 2005
Location: Bangalore
My distro: RHEL, HP-UX, Solaris, FreeBSD, Ubuntu
Posts: 564
Rep Power: 7
rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough
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.
You may have my body & soul, but you will never touch my pride!

If you have knowledge, let others light their candles at it.

Certified to work on HP-UX / Sun Solaris / RedHat
Reply With Quote
  #3 (permalink)  
Old 06-09-2008, 06:42 PM
Junior Member
User
 
Join Date: Jun 2008
My distro: Debian
Posts: 3
Rep Power: 0
Mats is on a distinguished road
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
Reply With Quote
  #4 (permalink)  
Old 06-09-2008, 08:45 PM
Junior Member
User
 
Join Date: Jun 2008
My distro: Debian
Posts: 3
Rep Power: 0
Mats is on a distinguished road
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
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

Thread Thread Starter Forum Replies Last Post
shell script to search specific file from txt file inside zip file and extract it aasif.shaikh Shell scripting 2 05-31-2008 06:44 PM
Read arguments from a file and pass them to binary file AHJ Shell scripting 1 10-31-2007 06:04 PM
wget url marked 'save target-as' sasuhaib The Hangout 0 09-14-2007 11:17 AM
How to save history of a hung terminal ricc Linux software 2 06-20-2006 12:21 AM


All times are GMT +5.5. The time now is 04:33 AM.


Powered by vBulletin® Version 3.7.3 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36