LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   shell script (http://www.linuxquestions.org/questions/programming-9/shell-script-872441/)

ajink 04-01-2011 02:35 PM

shell script
 
Hi,
I want to write a script that will detect all the unwanted files in a particular directory and delete them in one fly..
Can anyone guide me for this ???

szboardstretcher 04-01-2011 02:37 PM

Define "Unwanted"

Files older than a certain date? Files named "bad_file"? Files of a size less than 20k?

repo 04-01-2011 02:37 PM

What are unwanted files?
You can use find with the --delete option

Kind regards

SL00b 04-01-2011 02:38 PM

You'd have to tell us how you define "unwanted" first. What do you intend to key on?

onebuck 04-01-2011 03:27 PM

Hi,

Quote:

Originally Posted by ajink (Post 4311018)
Hi,
I want to write a script that will detect all the unwanted files in a particular directory and delete them in one fly..
Can anyone guide me for this ???

You should learn to use descriptive titles for your threads. That way you will receive targeted responses.



What have you done to find a solution to the problem? Other than to post here.

We will aid you when you help yourself to a solution. Provide us with what you have attempted and then maybe someone will be able to assist. 




You haven't provided enough information for us to aid so;
Quote:

FYI: I suggest that you look at 'How to Ask Questions the Smart Way' so in the future your queries provide information that will aid us in diagnosis of the problem or query.
Just a few links to aid you to gaining some understanding. I would start at 4,5 &6 since your query requirements are aligned to these. While the other links will enhance your Gnu/Linux experience;



1 Linux Documentation Project
2 Rute Tutorial & Exposition
3 Linux Command Guide
4 Bash Beginners Guide
5 Bash Reference Manual
6 Advanced Bash-Scripting Guide
7 Linux Newbie Admin Guide
8 LinuxSelfHelp
9 Utimate Linux Newbie Guide

The above links and others can be found at 'Slackware-Links'. More than just SlackwareŽ links!


---------- Post added 04-01-11 at 15:28 ----------

Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.

Cpare 04-01-2011 03:38 PM

Quote:

Originally Posted by ajink (Post 4311018)
Hi,
I want to write a script that will detect all the unwanted files in a particular directory and delete them in one fly..
Can anyone guide me for this ???

rm -rf unwanted

MTK358 04-03-2011 09:56 AM

Note the date posted. Maybe it was an April Fool's Day joke (I hope :)).

theNbomr 04-03-2011 11:13 AM

Quote:

Originally Posted by MTK358 (Post 4312369)
Note the date posted. Maybe it was an April Fool's Day joke (I hope :)).

Why? It does exactly what the OP specified, if you defined 'unwanted files' as the file named 'unwanted' or the set of all files in the directory tree named 'unwanted'. If no such file or directory exists, it does nothing.

--- rod.

MTK358 04-03-2011 11:43 AM

Quote:

Originally Posted by theNbomr (Post 4312441)
Why? It does exactly what the OP specified, if you defined 'unwanted files' as the file named 'unwanted' or the set of all files in the directory tree named 'unwanted'. If no such file or directory exists, it does nothing.

--- rod.

I was referring to the original post, not Cpare's reply.

ajink 04-05-2011 12:43 PM

I'll explain with an example...
I have a folder named TEMP.. This folder contains files a.c, b.c, b_bak.c
So in this I want to delete only b_bak.c.. So I tried :-

find ./TEMP ! -name a.c ! -name b.c | xargs -r rm

Now after executing this script it deletes the file but it says cannot delete ./TEMP as it is a directory...
So now I modified above since I do not want to delete the Folder itself:-

find ./TEMP ! -name a.c ! -name b.c ! -type d ./TEMP | xargs -r rm

but now it does nothing and says that Conjunction missing in find..
Plz can anyone help me with this ???

MTK358 04-05-2011 01:01 PM

Wouldn't it be a better idea to delete everything ending with _bak.c instead of entering the files that shouldn't be deleted? Seems like it would be both easier and less risky.

Code:

find TEMP -name '*_bak.c' -exec rm '{}' ';'
You should also use find's -exec oftion instead of xargs.

Nominal Animal 04-05-2011 05:05 PM

Quote:

Originally Posted by MTK358 (Post 4314822)
Code:

find TEMP -name '*_bak.c' -exec rm '{}' ';'
You should also use find's -exec oftion instead of xargs.

Or use
Code:

find TEMP -name '*_bak.c' -print0 | xargs -0 rm
which is faster (and will also work with all file names, even those with special characters in them).

If you use GNU find, then
Code:

find TEMP -name '*_bak.c' -delete
is likely even better.


All times are GMT -5. The time now is 05:21 PM.