LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-12-2011, 02:19 AM   #1
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 539

Rep: Reputation: 33
How to delete files under several sub-dirs in one command


Hi all,

Assumed I have logs as follow:

dir1/log/1.log
dir1/log/2.log
dir1/log/3.log

dir2/log/1.log
dir2/log/2.log
dir2/log/3.log


Now I do want go into each subdir to delete the logs but want to delete all the log files with a command

Possible?

Thomas
 
Old 09-12-2011, 02:28 AM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Hello,

Something like the following?
Code:
rm *.log
Also, this following link should definitely help you in the long run.... http://code.google.com/edu/tools101/linux/basics.html

Cheers,

Josh
 
Old 09-12-2011, 02:45 AM   #3
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 539

Original Poster
Rep: Reputation: 33
Your answer is wrong. I find out the correct one:

Code:
find -name "*.log" -type f -delete
 
Old 09-12-2011, 02:56 AM   #4
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Technically, it is correct. Your way is correct too. You could always go under the parent directory where the logs are stored, and issue the following command:
Code:
rm -r *.log
Of course, that would need to be modified. I'm not in linux right now, so I don't know the full arguments to make that work off the top of my head.
 
Old 09-12-2011, 10:52 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Bash v4.0+ has a new globstar shell option. When it's enabled you can use ** to match all subdirectories.

Code:
shopt -s globstar
rm **/*.log
 
1 members found this post helpful.
Old 09-13-2011, 10:15 AM   #6
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Quote:
Originally Posted by David the H. View Post
Bash v4.0+ has a new globstar shell option. When it's enabled you can use ** to match all subdirectories.

Code:
shopt -s globstar
rm **/*.log
Ooooo, thanks for that. Adding to my list of tricks....
 
Old 09-13-2011, 11:52 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Useful, isn't it?

I'd think I should clarify how it works a little, based on my understanding, as the behavior can be a bit confusing at first:

Code:
echo **			#expands to all files and directories, recursively.

echo **.txt		#identical to *.txt; does not recurse
			#when combined with other glob patterns.

echo **/		#expands to sub-directories only, recursively.

echo **/*.txt		#lists all .txt files in all directories recursively.
			#IOW, appended regular globs are applied to the top
			#level and all sub-directories.

echo **/*		#identical to **
 
Old 09-13-2011, 12:08 PM   #8
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
There's more than one way to skin a cat ... err ... log files.

It just depends on what one wants to specifically do. Deleting all the logs and the directories containing them is one option. But it might be desired to leave the directories in place. Are there other files in there that are not logs and want to be kept? Are the logs always at the same directory level? Is it desired to delete them from several directories but not from all directories? It's best to know exactly what the OQer wanted to precisely do. Any of the answers seen might well do, depending on the exact need.

The thing to do beyond this is to understand what each of the various commands actually do, and see if they fit the need. Maybe the command, or maybe the need, can be manipulated to find a simple solution. For example, I once wanted to remove every file but one the ones ending in .iso from a very large directory. The simple solution was to make a new directory, move the .iso files over to the new directory, completely remove the old directory full of junk (including the directory itself), and finally rename the temporary directory to the original name. That might not be a solution for your issue. But if you understand the commands, I think you will find that even in very complex situations, you can figure out a way to do what you want.
 
  


Reply



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
how to delete files using rm command asifbasha Linux - Newbie 4 06-08-2009 02:13 AM
bash shell command - delete all .svn dirs DamianS Programming 7 04-28-2007 01:33 PM
how to delete all files in one command for all user packets Linux - Newbie 4 04-16-2007 08:56 PM
Delete Files through Command Window ankz.upadhyay Linux - Newbie 5 03-27-2007 03:17 PM
command line: coloured files,dirs,etc kpachopoulos Linux - General 3 11-13-2005 04:24 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:51 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration