HowTo: Unix rm All Files In a Directory

by on April 24, 2013 · 3 comments· LAST UPDATED April 26, 2013

in ,

I am a new Unix user. How do I rm (remove) all files in a directory called personal-files under Unix like operating systems?

You need to use the rm command to delete the specified files and directories. The syntax for rm is:

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
Requirementsrm
Estimated completion timeN/A
rm file
rm file1 file2
rm wildcard-based-file-name-matching
rm [options] filenames

In this example, rm would delete the files named file.txt, foo.txt, and bar.txt assuming that all files are located in the current directory:

 
rm file.txt foo.txt bar.txt
 
WARNING! These examples may crash your computer or may result into data loss if executed. YHBW.

Say hello to * wildcard

The star (*) wildcard (also known as asterisk) can represent all file names. So, to delete all files use * wildcard. In this example, delete all files in the current directory (warning files can not be undeleted so be careful with the following command):

 
rm *
 

To delete all files from $HOME/personl-files, enter:

 
rm $HOME/personl-files/*
 

Pass the -i (i.e., interactive) option to rm command to prompt the user for confirmation before removing each file and directory.

 
rm -i *
 

To delete all *.doc file (files ending with .doc extensions, enter:

 
rm *.doc
 

The following would tell the rm command to delete all files in the current directory that have the string movie in their name:

 
rm *movie*
 

The following would tell the rm command to delete all files that begin with a lower case letter z.

 
rm z*
 

Question mark wildcard

The ? (question mark) is used to match exactly one character. For example three question marks (???) would represent any file name onsisting of three characters. In this example, the rm command would delete all files in the current directory whose names, inclusive of any extensions, are exactly three characters in length:

 
rm ???
 

The following would tell the rm command to delete all files in the current directory that begin with the letter z and are six characters in length:

 
rm z?????
 

The following would tell the rm command to delete all files in the current directory that have a two-character filename extension:

 
rm *.??
 

Square brackets wildcard

The square brackets ([]) can match any of the characters enclosed in the brackets. For example, the following would tell the rm command to delete all files in the current directory that have an A, B and/or C in them:

 
rm *[ABC]*
 

The following would tell the rm command to delete all files that have digits (zero to nine) in them i.e. at least one numeral in filename:

 
rm *[0-9]*
 

The following would tell the rm command to delete all files had an extension that begins with c or h:

 
rm *.[ch]*
 

And the following would tell rm command to delete all filenames in the current directory that consist of images followed by a two-digit number:

 
rm images[0-9][0-9].png
 

More examples

## Delete all perl files ##
rm *.pl
 
## Delete all c source code files ##
rm *.c
 
## Delete all c source code files only if name begins with a lower case letter a ##
rm a*.c
 
## Delete all files that have an .html or a .txt extension ##
rm *.html *.txt
 

How do I delete all files and sub-directories from $HOME/personl-files/?

Use the following rm command syntax:

 
rm -rf /path/to/dir/*
rm -rf $HOME/personl-files/*
 
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 3 comments… read them below or add one }

1 KevinT April 24, 2013 at 2:34 pm

Maybe he is asking how to remove a directory?

Reply

2 ergodic April 26, 2013 at 1:50 am

How do I delete all files and sub-directories from $HOME/personl-files/?
Use the following rm command syntax:
m -rf /path/to/dir/*
rm -rf $HOME/personl-files/*
Is “m -rf /path/to/dir/*” a typo?
Should it be “rm -rf /path/to/dir/*” instead?

Reply

3 KevinT April 26, 2013 at 7:08 am

Please check out “man rm”

Reply

Leave a Comment

Tagged as: , ,

Previous Faq:

Next Faq: