Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

Thanks in advance.

share|improve this question
    
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 at 4:45

3 Answers 3

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
share|improve this answer

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|improve this answer
    
-f is force, just use it if you mean it –  Matteo Jan 17 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 at 6:30

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

share|improve this answer
    
globstar is far simpler in bash 4+, rm **/.*.swp (with shopt -s globstar in a source bash file or in your current shell instance) –  BroSlow Jan 18 at 2:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.