Operating System - HP-UX
1827854 Members
1630 Online
109969 Solutions
New Discussion

Script for removing all but 1 file

 
SOLVED
Go to solution
Chern Jian Leaw
Regular Advisor

Script for removing all but 1 file

Hi,

I have a script below for removing all other files except for a file hard-coded in the script:

#cat rmOtherFiles.sh
#!/bin/sh
allowedDir=/tmp/restore/testDir
cd ${allowedDir}

validDir=/tmp/restore/testDir/subTest1

if [ "$?" != "0" ]
then
echo cd to $allowedDir failed
exit 1
fi

ls -l $allowed |awk '{print $allowedDir/$9}' > /tmp/restore/toRemove.txt

for i in `cat /tmp/restore/toRemove.txt`
do
if [ $i != $validDir ]
then
rm -rf $i
fi
done

The index i contains the directory entries as in /tmp/restore/testDir/subTest1, /tmp/restore/testDir/subTest2 and /tmp/restore/testDir/subTest3.


#./rmOtherFiles.sh
awk:06020562 Field $() is not correct.
The input line number is 1
The source line number is 1

Could someone explain what this error is all about? How do I fix this error?

Also does anyone have any other better suggestions on removing all other directories, except for a particular directory specified ?

Thanks.
16 REPLIES 16
Steven Sim Kok Leong
Honored Contributor

Re: Script for removing all but 1 file

Hi,

# cat rmOtherFiles.sh
#!/bin/sh

allowedDir=/tmp/restore/testDir
toRemove=/tmp/restore/toRemove.txt
validDir=/tmp/restore/testDir/subTest1

if [ ! -d $allowedDIR ]
then
echo $allowedDIR does not exist
exit 1
elif [ ! -x $allowedDIR ]
echo $allowedDIR cannot be entered
exit 2
else
find $allowedDIR -type d > $toRemove
for i in `cat $toRemove | grep -v $validDIR`
do
echo Removing directory $i...
rm -fr $i
done
fi

Hope this helps. Regards.

Steven Sim Kok Leong
Steven Sim Kok Leong
Honored Contributor

Re: Script for removing all but 1 file

Hi,

Missed out a "then":

# cat rmOtherFiles.sh
#!/bin/sh

allowedDir=/tmp/restore/testDir
toRemove=/tmp/restore/toRemove.txt
validDir=/tmp/restore/testDir/subTest1

if [ ! -d $allowedDIR ]
then
echo $allowedDIR does not exist
exit 1
elif [ ! -x $allowedDIR ]
then
echo $allowedDIR cannot be entered
exit 2
else
find $allowedDIR -type d -print > $toRemove
for i in `cat $toRemove | grep -v $validDIR`
do
echo Removing directory $i...
rm -fr $i
done
fi

Hope this helps. Regards.

Steven Sim Kok Leong


T G Manikandan
Honored Contributor

Re: Script for removing all but 1 file

Where are you defining $allowed.
<<<
ls -l $allowed |awk '{print $allowedDir/$9}'

>>>>
Chern Jian Leaw
Regular Advisor

Re: Script for removing all but 1 file

TG,

I've defined allowedDir at the very beginning of the script, just after #!/bin/sh.

Any suggestions?
Error seems to be at the awk area.

Thanks.
Steven Sim Kok Leong
Honored Contributor

Re: Script for removing all but 1 file

Hi,

I forgot to mention that the reason why your awk fails in your script is because the / in $allowedDIR/$9 becomes a arithmetic operator for division when unquoted.

Thus you should change it to

awk '{print $allowedDir"/"$9}'

instead of

awk '{print $allowedDir/$9}'

in your script.

Hope this helps. Regards.

Steven Sim Kok Leong
Chern Jian Leaw
Regular Advisor

Re: Script for removing all but 1 file

Steven,

I tried having awk '{print $allowedDir"/"$9}' > /tmp/restore/toRemove.txt

and the same error mentioned previously is reproduced. Could you point out where the error is ?

Thanks.
T G Manikandan
Honored Contributor

Re: Script for removing all but 1 file

ls -l $allowedDir|awk -F " " '{print $9}'

Thanks
Steven Sim Kok Leong
Honored Contributor

Re: Script for removing all but 1 file

Hi,

The other thing is that I don't think you can pass the variable $allowedDir into the awk statement.

It will default to the same variable as $9. You can do a test:

# a=test
# ls -l | awk '{print $a"/"$9}'

Hope this helps. Regards.

Steven Sim Kok Leong
Steven Sim Kok Leong
Honored Contributor

Re: Script for removing all but 1 file

Hi,

Once you fixed the typo error highlighted by TG (i.e. it should be $allowedDIR instead of $allowed, I thought it was a typo in your query, you will need to fix at least two issues:
1) / should be "/" because it should be a character not an arithmetic division operator
2) passing of variables into awk

My very first two responses is a suggestion of an alternative script to achieve your objectives, assuming that you are only looking at directories, hence the -type d filter in the find command.

Using find will get rid of the issues you will have with awk:

find $allowDIR -type d -print

Hope this helps. Regards.

Steven Sim Kok Leong
Chern Jian Leaw
Regular Advisor

Re: Script for removing all but 1 file

Steven,
I tried working on your script but it produced an error: /tmp/restore/testDir does not exist.

I do have /tmp/restore/testDir directory on my disk. I invoked the script from /tmp/restore. I'm not sure about the cause of this error.

Could you help me out?

TG,
I've also tried your method of :
ls -l $allowedDir |awk -F""'{print $9}'>/tmp/restore/toRemove.txt

But the entire contents of the directory testDir gets removed!

Is the syntax of my if statement correct? i.e:
validDir=/tmp/restore/testDir/subTest1
for i in 'cat /tmp/restore/toRemove.txt'
do
if [$i != $validDir ]; then
rm -rf $i
fi
done

Please help.

Thanks.
Steven Sim Kok Leong
Honored Contributor
Solution

Re: Script for removing all but 1 file

Hi,

I made a typo i.e. in my script, I used allowedDIR instead of allowedDir and validDIR instead of validDir.
I have fixed the typos and tested the below amended script to work:

#!/bin/sh

allowedDir=/tmp/restore/testDir
toRemove=/tmp/restore/toRemove.txt
validDir=/tmp/restore/testDir/subTest1

if [ ! -d $allowedDir ]
then
echo $allowedDir does not exist
exit 1
elif [ ! -x $allowedDir ]
then
echo $allowedDir cannot be entered
exit 2
else
find $allowedDir/* -type d -print > $toRemove
for i in `cat $toRemove | grep -v $validDir`
do
echo Removing directory $i...
rm -fr $i
done
fi

Hope this helps. Regards.

Steven Sim Kok Leong
Chern Jian Leaw
Regular Advisor

Re: Script for removing all but 1 file

Steven,

I've made the changes as what've adviced. Unfortunately, it's still producing the error:
/tmp/restore/testDir does not exist.

This script is residing on /tmp/restore, and testDir does exist.

Hope you don't mind having a look at my attached script, and perhaps point out my error.

Thanks.

Steven Sim Kok Leong
Honored Contributor

Re: Script for removing all but 1 file

Hi,

You missed out the exclamation mark before -d:

It should be

[ ! -d $allowedDir ]

instead of:

[ -d $allowedDir ]

Hope this helps. Regards.

Steven Sim Kok Leong
oiram
Regular Advisor

Re: Script for removing all but 1 file

Hi,
this script work for me:
#!/bin/sh
allowedDir=/tmp/restore/testDir/
cd ${allowedDir}

validDir=/tmp/restore/testDir/subTest1

if [ "$?" != "0" ]
then
echo cd to $allowedDir failed
exit 1
fi

ls -l $allowed |awk '{ print $9 }' > /tmp/restore/toRemove.txt

for i in `cat /tmp/restore/toRemove.txt`
do
if [ $allowedDir$i != $validDir ]
then
rm -rf $i
fi
done
Peter Kloetgen
Esteemed Contributor

Re: Script for removing all but 1 file

Hi,

a few comments to your script:

if you redirect the output of awk, the output file should allways be quoted:

awk '{print something}' inputfile > "output_file"

if you want to work with variables in awk, you should call your variable the following way:

awk -v var=/destination/path '{action}' input_file

If you use the variable inside awk, don't use the syntax: $var, but instead only "var"!

Allways stay on the bright side of life!

Peter


I'm learning here as well as helping
Reinhard Burger
Frequent Advisor

Re: Script for removing all but 1 file

Hi

Very interesting discussion
But one simple question :

Isn't it the easiest to just copy the file or directory you want to prevent from deleting to a save place ?
sequence of actions

1.) cp /remove/filetoSave /keepalive/
2.) rm /remove/*
3.) mv /keepalive/filetosave /remove/


Yes really silly example but in principal it would work and maybe an easier solution
keep it simple