Results 1 to 5 of 5
Hi All,
I am planning to delete few files from a subdirectory, my aim is to locate all *.des files, remove them all except the one which the user specifies.here ...
- 03-09-2009 #1Just Joined!
- Join Date
- Mar 2009
- Posts
- 8
help needed in BASH scripting
Hi All,
I am planning to delete few files from a subdirectory, my aim is to locate all *.des files, remove them all except the one which the user specifies.here is the code i wrote which is not coming out right. des1 is already existing in the folder, and i am copying that to a new file with a different name and deleting all the *.des files in that folder.
one more thing is, that there are also other type of files in the directory. so i just want to remove only the *.des files keeping the only one which the user specifies.
#!/bin/bash
echo "Enter Des File name"
read des1
echo " Enter the 5 digit name"
read des2
cp $des1 $des2
touch $des2
for i in `ls .`
rm -f $i/*.des
i know that above code removes all *.des files, wat command do i add to exclude that particular user given file.
if you point me to the command name , i will figure out the code...
Thanks
- 03-09-2009 #2
You can just use a simple if statement to check that i isn't equal to the given file.
On another note you could also make your program cleaner and easier to understand by searching for .des files like so:
Which would mean you can delete the file by:Code:for i in `ls *.des`
This will also make your if statement cleaner and easier to code. Good luck!Code:rm -f $i
Linux User #453176
- 03-10-2009 #3Just Joined!
- Join Date
- Mar 2009
- Posts
- 8
Thanks a lot..will do that
- 03-10-2009 #4
You could try something like this:
Try the script with echo/printf instead of rm to verify the correct globbing.Code:#!/bin/bash read -p'Enter des file name to preserve: ' shopt -s extglob rm -- */!("${REPLY%.des}").des
- 03-10-2009 #5Just Joined!
- Join Date
- Mar 2009
- Posts
- 8
Thanks guys..i did try this one and it worked..
find .not -name $des1 -iname "*.des" -exec rm -f {} \;


Reply With Quote