You need to use the rm command to delete the specified files and directories. The syntax for rm is:
Tutorial details | |
---|---|
Difficulty | Easy (rss) |
Root privileges | No |
Requirements | rm |
Time | N/A |
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
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/*
🐧 4 comments so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Maybe he is asking how to remove a directory?
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?
Please check out “man rm”
Empty directory removing command:
rmdir did
Unempty directory remove command:-
rm -rf dir2