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 ...
|
|||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
|
|||
|
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 |
| Sponsored Links | ||
|
|
|
||||
|
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
Code:
./script.sh 5 /tmp Code:
./script.sh 5 /tmp/d2 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 |
|
|||
|
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 |
|
|||
|
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 |
![]() |
| Bookmarks |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| 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 |