LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   needed opinion on the below code (https://www.linuxquestions.org/questions/linux-newbie-8/needed-opinion-on-the-below-code-710267/)

neo2k 03-09-2009 10:37 AM

needed opinion on the below code
 
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

schneidz 03-09-2009 12:58 PM

hi, not quite sure from your description but here is some untested code to fool around with:
Code:

#!/bin/bash
echo "Enter Des File name"
read des1
mv $des1 $des1-bak
rm -f `find . -name "*.des"`
mv $des1-bak $des1


neo2k 03-09-2009 01:19 PM

Thanks Schenidz..i am getting some error..i will try to debug it..if i don;t get an answer i will reply again.

neo2k 03-09-2009 02:04 PM

Its not backing up the user given input, and its removing all the *.des files in the directory.

any other suggestions..

sureshsujatha 03-09-2009 02:39 PM

Quote:

Originally Posted by neo2k (Post 3469910)
Thanks Schenidz..i am getting some error..i will try to debug it..if i don;t get an answer i will reply again.

What is this "error" you are getting neo2k?

neo2k 03-09-2009 02:42 PM

suresh,

I solved the error, it was a space issue in my script. The part is i am not able to write a effective code to remove the user input file from the directory.

for e.g.

A(main dir)--> b, c

b and c are subdirectories, basically i want the code to look under b and c remove all the *.des files , except the user input.

colucix 03-09-2009 05:02 PM

I did not really understand the two input names in the code of post #1 (please, clarify). However, following your description, you can exclude a file name from a list by means of grep -v:
Code:

#!/bin/bash
read -p "Enter the name of the file to preserve: " des
find A -mindepth 2 -name \*.des | grep -v ${des}$ | xargs echo rm

test the code with the echo command. This does not actually remove the files: it just display the resulting command. If your test is successfull, remove the echo part and the trick is done! :)

neo2k 03-10-2009 08:18 AM

Thanks colucix..i tried this one and it worked for me

find . -not -name $des1 -iname "*.des" -exec rm -f {} \;

i figured out this part, but the question..if i want to loop this command through the first subdirectory or the user specified directory..how would i do that?

if i figured it out i will post it here


All times are GMT -5. The time now is 06:33 AM.