Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
Last edited by neo2k; 03-09-2009 at 11:10 AM.
Reason: Updated the description.
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.
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!
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.