LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-04-2011, 09:35 AM   #1
123music
LQ Newbie
 
Registered: Jul 2011
Posts: 2

Rep: Reputation: Disabled
Linux command help


Hi,

I need help for one linux command. Its all about deleting files.

I have the directory structure as root/folder1/folder2/folder3.All these directories has subdirectories as well.
I want to delete all files except the files with extension .txt and .xml in folder2 and folder3. However folder1 should be untouched.

Please give me a linux command for this so that i can execute from root directory.

Thanks
 
Old 07-04-2011, 10:20 AM   #2
allez
Member
 
Registered: Jul 2008
Location: Russia/Siberia/Krasnoyarsk
Distribution: SuSE, CentOS, FreeBSD
Posts: 77

Rep: Reputation: 21
You may use find. This example deletes *.txt files from directory root/folder1/folder2/ and its subdirectories:
Code:
find root/folder1/folder2/ -type f -name "*.txt" -exec rm {} \;
You may also use this command in such a form:
Code:
find root/folder1/folder2/ -type f -name "*.txt" -delete
Any further information you can get from man find.

Last edited by allez; 07-04-2011 at 10:25 AM.
 
Old 07-04-2011, 10:50 AM   #3
123music
LQ Newbie
 
Registered: Jul 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
Linux command help

Quote:
Originally Posted by allez View Post
You may use find. This example deletes *.txt files from directory root/folder1/folder2/ and its subdirectories:
Code:
find root/folder1/folder2/ -type f -name "*.txt" -exec rm {} \;
You may also use this command in such a form:
Code:
find root/folder1/folder2/ -type f -name "*.txt" -delete
Any further information you can get from man find.
Hi,
Thanks for the command.

But what should i do if root has N no.of directories. I think its difficult to specify in find command. Is there a dynamic way where it searches all directories by itself?

Also i dont want *.txt files to be deleted. Actually in my directories i have files with several extensions like *.o, *.txt, *.exe, *.tmp, *.xml, *.rm, *.i etc..etc..
I want all files except *.txt and *.xml to be deleted.

Please help me.

Thanks
 
Old 07-04-2011, 11:30 AM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by 123music View Post
Is there a dynamic way where it searches all directories by itself?
Didn't you say that you want "folder1" untouched? allez's command will delete files in folder2 and any of its subdirectories.

Quote:
Originally Posted by 123music View Post
Also i dont want *.txt files to be deleted. Actually in my directories i have files with several extensions like *.o, *.txt, *.exe, *.tmp, *.xml, *.rm, *.i etc..etc..
I want all files except *.txt and *.xml to be deleted.
Code:
find root/folder1/folder2 -regextype posix-extended -type f -not -regex '.*\.(txt|xml)' -delete

Last edited by MTK358; 07-04-2011 at 11:32 AM.
 
Old 07-07-2011, 02:39 AM   #5
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
Hi,

I'm sorry for late post, but I thought that some explanation about excluding would be nice as of here.
 
Old 07-07-2011, 02:50 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
A little add-on to MTK358's suggestion to be sure you don't delete files inside folder1:
Code:
find root/folder1 -regextype posix-extended -type f -not -regex '.*\.(txt|xml)' -not -regex 'folder1/[^/]+$' -delete
Warning: test the command without -delete to check if the results are correct.

Last edited by colucix; 07-07-2011 at 02:52 AM.
 
Old 07-07-2011, 08:30 AM   #7
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
How can my command delete anything in folder1 in the first place?
 
Old 07-07-2011, 03:05 PM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by MTK358 View Post
How can my command delete anything in folder1 in the first place?
Indeed it can't. I just wonder about the presence of any other directory at the same level of folder2. Note that the search path in my command is one level up. This is because the OP provided a not-realistic example (and he stated "all these directories have subdirectories as well") but the actual situation could be more complex, so it's better to be cautious, especially when they want to remove files without knowing all the caveats implied in what they are doing.
 
Old 07-07-2011, 05:36 PM   #9
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,925
Blog Entries: 44

Rep: Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159
Hi,

Welcome to LQ!
Quote:
Originally Posted by 123music View Post
Hi,

I need help for one linux command. Its all about deleting files.

I have the directory structure as root/folder1/folder2/folder3.All these directories has subdirectories as well.
I want to delete all files except the files with extension .txt and .xml in folder2 and folder3. However folder1 should be untouched.

Please give me a linux command for this so that i can execute from root directory.

Thanks
Other members have provided you with good advice.
I do suggest that you look at links 2 & 3 below to hopefully expose you to some useful information.

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;



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
10 Linux Home Networking
11 Virtualization- Top 10

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


"Knowledge is of two kinds. We Know a subject ourselves, or we know where we can find information upon it."- Samuel Johnson
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How does Linux cp command determine the source and destination locations in command? linuxman2 Linux - General 4 04-14-2011 07:13 PM
URGENT! Is there any command to get a history command lines and time in SUSE Linux.? igsoper Linux - Software 5 06-25-2009 02:14 AM
LXer: The Linux Command Shell For Beginners: Fear Not The Command Line! LXer Syndicated Linux News 0 12-22-2008 06:30 PM
Translating windows pscp command to linux scp command help robward Linux - General 2 01-17-2008 06:02 AM
Require Linux/Perl equivalent command for windows Command alix123 Programming 7 08-19-2005 02:23 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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