Find the answer to your Linux question:
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 ...
  1. #1
    Just 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?

  2. #2
    Linux 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]

  3. #3
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    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

  4. #4
    Just Joined!
    Join Date
    Dec 2009
    Posts
    28
    Thanks for your posts.
    These are treasure

  5. #5
    Just Joined! TheBoogyMaster's Avatar
    Join Date
    Apr 2009
    Posts
    45
    Quote Originally Posted by Utnubu View Post
    Thanks for your posts.
    These are treasure
    No These are juste Regular expressions
    Regular expression - Wikipedia, the free encyclopedia

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...