nixCraft Linux Forum

nixCraft

Linux / UNIX 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

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-06-2008, 07:30 PM
Junior Member
User
 
Join Date: Jun 2008
OS: Debian
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
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
  #2 (permalink)  
Old 05-06-2008, 11:29 PM
rockdalinux's Avatar
Is that all you got?
User
 
Join Date: May 2005
Location: Planet Vegeta
OS: Redhat
Posts: 703
Thanks: 15
Thanked 19 Times in 18 Posts
Rep Power: 10
rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light
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.
Reply With Quote
  #3 (permalink)  
Old 09-06-2008, 06:42 PM
Junior Member
User
 
Join Date: Jun 2008
OS: Debian
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
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 09-06-2008, 08:45 PM
Junior Member
User
 
Join Date: Jun 2008
OS: Debian
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
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


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 Off


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 31-05-2008 06:44 PM
Read arguments from a file and pass them to binary file AHJ Shell scripting 1 31-10-2007 06:04 PM
wget url marked 'save target-as' sasuhaib The Hangout 0 14-09-2007 11:17 AM
How to save history of a hung terminal ricc Linux software 2 20-06-2006 12:21 AM


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


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

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 37 38