LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-01-2011, 02:35 PM   #1
ajink
LQ Newbie
 
Registered: May 2010
Posts: 11

Rep: Reputation: 0
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 ???
 
Old 04-01-2011, 02:37 PM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,152
Blog Entries: 1

Rep: Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004
Define "Unwanted"

Files older than a certain date? Files named "bad_file"? Files of a size less than 20k?
 
Old 04-01-2011, 02:37 PM   #3
repo
LQ 5k Club
 
Registered: May 2001
Location: Belgium
Distribution: Linux Mint
Posts: 8,502

Rep: Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883
What are unwanted files?
You can use find with the --delete option

Kind regards
 
Old 04-01-2011, 02:38 PM   #4
SL00b
Member
 
Registered: Feb 2011
Location: LA, US
Distribution: SLES
Posts: 375

Rep: Reputation: 111Reputation: 111
You'd have to tell us how you define "unwanted" first. What do you intend to key on?
 
Old 04-01-2011, 03:27 PM   #5
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 11,204
Blog Entries: 3

Rep: Reputation: 1433Reputation: 1433Reputation: 1433Reputation: 1433Reputation: 1433Reputation: 1433Reputation: 1433Reputation: 1433Reputation: 1433Reputation: 1433
Hi,

Quote:
Originally Posted by ajink View Post
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.
 
Old 04-01-2011, 03:38 PM   #6
Cpare
Member
 
Registered: Aug 2001
Location: Magic City, USA
Distribution: Ubuntu
Posts: 58

Rep: Reputation: 15
Quote:
Originally Posted by ajink View Post
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
 
Old 04-03-2011, 09:56 AM   #7
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Note the date posted. Maybe it was an April Fool's Day joke (I hope ).
 
Old 04-03-2011, 11:13 AM   #8
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Quote:
Originally Posted by MTK358 View Post
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.
 
Old 04-03-2011, 11:43 AM   #9
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
Originally Posted by theNbomr View Post
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.
 
Old 04-05-2011, 12:43 PM   #10
ajink
LQ Newbie
 
Registered: May 2010
Posts: 11

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

Last edited by ajink; 04-05-2011 at 12:44 PM.
 
Old 04-05-2011, 01:01 PM   #11
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
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.
 
1 members found this post helpful.
Old 04-05-2011, 05:05 PM   #12
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
Quote:
Originally Posted by MTK358 View Post
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.
 
1 members found this post helpful.
  


Reply

Tags
delete, find, rm, xargs


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
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 07:58 PM
pass variable from one shell script into another shell script xskycamefalling Programming 9 10-03-2009 01:45 AM
help with execute mulitple shell script within shell script ufmale Programming 6 09-13-2008 12:21 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM


All times are GMT -5. The time now is 07:26 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