13

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
1
  • 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?
    – terdon
    Jan 17, 2014 at 4:45

3 Answers 3

11

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
10

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

1
  • 2
    globstar is far simpler in bash 4+, rm **/.*.swp (with shopt -s globstar in a source bash file or in your current shell instance)
    – user281208
    Jan 18, 2014 at 2:06
1

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.
2
  • -f is force, just use it if you mean it
    – Matteo
    Jan 17, 2014 at 6:29
  • rm -r does only make sense for directories, since you are deleting files -type f it does not make sense
    – Matteo
    Jan 17, 2014 at 6:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .