- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Script for removing all but 1 file
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2002 06:57 PM
07-21-2002 06:57 PM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2002 07:10 PM
07-21-2002 07:10 PM
Re: Script for removing all but 1 file
# 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2002 07:12 PM
07-21-2002 07:12 PM
Re: Script for removing all but 1 file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2002 07:14 PM
07-21-2002 07:14 PM
Re: Script for removing all but 1 file
<<<
ls -l $allowed |awk '{print $allowedDir/$9}'
>>>>
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2002 07:17 PM
07-21-2002 07:17 PM
Re: Script for removing all but 1 file
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2002 07:17 PM
07-21-2002 07:17 PM
Re: Script for removing all but 1 file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2002 07:28 PM
07-21-2002 07:28 PM
Re: Script for removing all but 1 file
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2002 07:32 PM
07-21-2002 07:32 PM
Re: Script for removing all but 1 file
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2002 07:43 PM
07-21-2002 07:43 PM
Re: Script for removing all but 1 file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2002 07:50 PM
07-21-2002 07:50 PM
Re: Script for removing all but 1 file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2002 10:37 PM
07-21-2002 10:37 PM
Re: Script for removing all but 1 file
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2002 10:55 PM
07-21-2002 10:55 PM
SolutionI 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2002 11:10 PM
07-21-2002 11:10 PM
Re: Script for removing all but 1 file
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2002 11:14 PM
07-21-2002 11:14 PM
Re: Script for removing all but 1 file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2002 12:30 AM
07-22-2002 12:30 AM
Re: Script for removing all but 1 file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2002 01:34 AM
07-22-2002 01:34 AM
Re: Script for removing all but 1 file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-23-2002 12:02 AM
07-23-2002 12:02 AM
Re: Script for removing all but 1 file
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