How to delete all hidden .swp files from terminal

33,256

Solution 1

What you wanted to do is

rm .*swp

The * does not match files starting with a . unless you turn on dotglob (assuming you are using bash):

$ ls -la
-rw-r--r--   1 terdon terdon        0 Jan 17 05:50 .foo.swp
$ ls *swp  
ls: cannot access *swp: No such file or directory
$ shopt -s dotglob
$ ls *swp
.foo.swp

Solution 2

If you say: files are hidden, then they start with a dot (.), so try:

find . -type f -name ".*.swp" -exec rm -f {} \;

With this approach you're looking for all hidden files into the current directory and subdirectories. If you want delete the hidden files of just the current directory, a simple rm -f .*.swp works ok

Solution 3

Try using this

find . -type f -name "*.swp" -exec rm -f {} \;

-name "FILE-TO-FIND" : File pattern.
-exec rm -rf {} \; : Delete all files matched by file pattern.
-type f : Only match files and do not include directory names.
Share:
33,256

Related videos on Youtube

shinokada
Author by

shinokada

IB Diploma and MYP mathematics teacher who loves coding.

Updated on September 18, 2022

Comments

  • shinokada
    shinokada over 1 year

    How can I delete all .swp files? I tried rm *.swp but I got rm: *.swp: No such file or directory

    rwxr-xr-x  16 teacher  staff    544 Jan 17 13:19 .
    drwxr-xr-x  19 teacher  staff    646 Jan 16 12:48 ..
    -rw-r--r--   1 teacher  staff  20480 Jan 17 09:48 .6-1-period-2.txt.swp
    -rw-r--r--   1 teacher  staff  16384 Jan 17 09:05 .6-2-period-6.txt.swp
    -rw-r--r--@  1 teacher  staff   6148 Jan 15 16:16 .DS_Store
    -rw-r--r--   1 teacher  staff  12288 Jan 16 19:46 .grade8.txt.swp
    -rw-r--r--   1 teacher  staff  11070 Jan 17 09:48 6-1-period-2.txt
    
    • terdon
      terdon over 10 years
      Please always include your OS. Solutions very often depend on the Operating System being used. Are you using Unix, Linux, BSD, OSX, something else? Which version?
  • Matteo
    Matteo over 10 years
    -f is force, just use it if you mean it
  • Matteo
    Matteo over 10 years
    rm -r does only make sense for directories, since you are deleting files -type f it does not make sense
  • Admin
    Admin over 10 years
    globstar is far simpler in bash 4+, rm **/.*.swp (with shopt -s globstar in a source bash file or in your current shell instance)