How to delete the contents of a USB Stick in Linux

5

1

I'm not looking to format the stick at all, but rather to iterate through the files deleting them all (though after typing 'iterate through' I can sense a Python script coming along.

So far I've been doing rm filename1 rm filename2 rm filename3 etc, is there a way to just 'rm -all' while on in the root of the USB drive?

Dave Melia

Posted 2013-09-06T17:47:53.610

Reputation: 121

2man is your friend. – Kale Muscarella – 2013-09-06T17:53:02.213

I tried this before posting -- couldn't figure it out. – Dave Melia – 2013-09-06T17:54:43.000

rm filename* or rm | grep filename – Everett – 2013-09-20T21:17:21.993

Answers

17

Yes:

rm -rf *

Be carefull where you are when you run this it will delete everything from the current directory and all subdirectories.

If you only want to delete files, and no directories use:

rm *

As @DanielAndersson very correctly pointed out in the comments, this will not delete hidden files and directories (those beginning with a .). To delete those as well do

rm -rf * .*

This will give an error about not being able to delete . and .. (the current and parent directories respectively). You can safely ignore that, rm will never delete these since they are protected by the POSIX standard (see here and here). If you don't want to see the error message you can specify that you only want to delete those dotfiles and folders whose . is followed by a non . character:

rm -rf * .[^.]*

Finally, if you want to delete all files in the current directory and all subdirectories but keep the directories, do this:

find . -type f -delete

terdon

Posted 2013-09-06T17:47:53.610

Reputation: 45 216

You're the man! Thanks @terdon -- I would upvote but you need 15 rep :( – Dave Melia – 2013-09-06T17:53:51.200

9I'll remember September sixth as the first time in the history of the internet someone legitimately recommended executing rm -rf *. – Marcks Thomas – 2013-09-06T18:03:07.740

@DaveMelia if you can't upvote, please at least accept the answer (the tickmark on the left) so the question can be marked as answered. – terdon – 2013-09-06T18:22:44.077

@MarcksThomas heh, true, true, might be the first time I've seen a legitimate use myself :). – terdon – 2013-09-06T18:23:12.813

2One could note that hidden files and directories in the current directory won't be erased by rm -rf * (unless dotglob happens to be set). – Daniel Andersson – 2013-09-06T18:57:22.800

@MarcksThomas, yeah, they defanged rm -rf a few years ago. it will prompt you if it detects you running 'rm -rf /' and ask if you are really really sure. – Frank Thomas – 2013-09-06T19:17:27.197

Oi, anyway this should cover most hidden files and directories: rm -rf /path/to/directory/{*,.[A-Za-z0-9]*} – justbrowsing – 2013-09-07T01:50:12.897

@terdon, do not advocate .* it contains the parent directory via .. – justbrowsing – 2013-09-07T23:44:56.883

1@justbrowsing true but that will just give an error message about not being able to delete . and ... I don't know if that is a GNU thing though so, I'll make that clear in the answer. – terdon – 2013-09-08T03:49:56.500

@justbrowsing I now know that rm will never delete . and .. so there is reason to worry about it.

– terdon – 2013-09-10T15:05:10.313