Results 1 to 5 of 5
Hello friends,
i have a question about deleting files except from .txt files in a directory
how can i do it?
with rm -rf <directory_name> I can delete all file ...
- 01-18-2010 #1Just Joined!
- Join Date
- Dec 2009
- Posts
- 28
how to delete
Hello friends,
i have a question about deleting files except from .txt files in a directory
how can i do it?
with rm -rf <directory_name> I can delete all file but
how can I customize it?
- 01-18-2010 #2Linux Guru
- Join Date
- Nov 2004
- Posts
- 6,110
As well as wildcards such as asterisk (*) which matches all and question mark (?) which matches one character there are other types of wildcard. For example, in this case you can use the caret to reverse the match. If you want to operate on any file that doesn't end in .txt you can use
Code:ls *[^.txt] rm *[^.txt]
- 01-18-2010 #3
Not quite. bigtomrodney's code would delete every file in the directory whose last character is not '.', 'x', or 't'. It is tangentially true that it will not delete a file ending in .txt, but it will also not delete a file ending in .docx, .odt, etc.
However, the basic theory is correct. * matches any number of characters, ? matches a single character. Therefore, rm fil? would delete files called "file", "fill", etc.
You can use [...] syntax to match specific characters. Therefore, while rm ?e would delete files "me", "he", and "we", rm [mh]e would only delete "me" and "he".
To fine tune the files that you're matching, you can also use the find command, which has a lot of ways to specify files that you're looking for. You can search by name, file type, time of last modification, etc. You can then ask find to delete those files for you.
For more information on wildcards, like we saw above, check the bash man page ("man bash" in a terminal), particularly the sections on brace expansion and pathname expansion. For more information on find, check its man page ("man find").
Hope this helps!DISTRO=Arch
Registered Linux User #388732
- 01-18-2010 #4Just Joined!
- Join Date
- Dec 2009
- Posts
- 28
Thanks for your posts.
These are treasure
- 01-19-2010 #5
No These are juste Regular expressions

Regular expression - Wikipedia, the free encyclopedia


Reply With Quote
