HowTo: Unix rm All Files In a Directory

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:

ADVERTISEMENTS

Tutorial details
Difficulty Easy (rss)
Root privileges No
Requirements rm
Time N/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/*
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
4 comments… add one
  • KevinT Apr 24, 2013 @ 14:34

    Maybe he is asking how to remove a directory?

  • ergodic Apr 26, 2013 @ 1:50

    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?

    • KevinT Apr 26, 2013 @ 7:08

      Please check out “man rm”

  • Arch fan Jul 18, 2020 @ 7:32

    Empty directory removing command:

    rmdir did

    Unempty directory remove command:-
    rm -rf dir2

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.