LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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

Reply
 
Search this Thread
Old 03-09-2009, 10:37 AM   #1
neo2k
LQ Newbie
 
Registered: Aug 2008
Posts: 21

Rep: Reputation: 15
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

Last edited by neo2k; 03-09-2009 at 11:10 AM. Reason: Updated the description.
 
Old 03-09-2009, 12:58 PM   #2
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,041

Rep: Reputation: 630Reputation: 630Reputation: 630Reputation: 630Reputation: 630Reputation: 630
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
 
Old 03-09-2009, 01:19 PM   #3
neo2k
LQ Newbie
 
Registered: Aug 2008
Posts: 21

Original Poster
Rep: Reputation: 15
Thanks Schenidz..i am getting some error..i will try to debug it..if i don;t get an answer i will reply again.
 
Old 03-09-2009, 02:04 PM   #4
neo2k
LQ Newbie
 
Registered: Aug 2008
Posts: 21

Original Poster
Rep: Reputation: 15
Its not backing up the user given input, and its removing all the *.des files in the directory.

any other suggestions..
 
Old 03-09-2009, 02:39 PM   #5
sureshsujatha
Member
 
Registered: Mar 2009
Posts: 40

Rep: Reputation: 15
Quote:
Originally Posted by neo2k View Post
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?

Last edited by sureshsujatha; 03-14-2009 at 12:32 AM.
 
Old 03-09-2009, 02:42 PM   #6
neo2k
LQ Newbie
 
Registered: Aug 2008
Posts: 21

Original Poster
Rep: Reputation: 15
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.
 
Old 03-09-2009, 05:02 PM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,506

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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!
 
Old 03-10-2009, 08:18 AM   #8
neo2k
LQ Newbie
 
Registered: Aug 2008
Posts: 21

Original Poster
Rep: Reputation: 15
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
 
  


Reply

Tags
files, remove


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Opinion: GNU/Linux: Source Code and Human Rights LXer Syndicated Linux News 0 05-08-2008 07:51 PM
PF opinion needed/question lil_drummaboy *BSD 2 12-20-2004 04:39 PM
NIC Opinion needed. lil_drummaboy Linux - Networking 0 12-08-2004 07:25 PM
Opinion needed: FreeBSD vs Redhat Linux happyruyi Linux - Software 10 09-25-2003 07:44 PM
Opinion/Advice needed on a setup: tarballed Linux - General 3 11-20-2002 05:26 PM


All times are GMT -5. The time now is 07:37 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration