Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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
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
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.
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;
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.