EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Linux rm Command

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » Linux Tutorial » Linux rm Command

Linux rm Command

Introduction to Linux rm Command

In the Linux operating system, rm command is used to remove or delete the files or directories. Similarly, we can also use the “unlink” command to remove the single file only. The Linux rm command-line utility was written by Paul Rubin, David MacKenzie, Richard M. Stallman, and Jim Meyering.

Note: By default, the rm command will not remove the directories. 

Syntax of rm command

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

rm [ OPTION ] FILE NAME | DIRECTORY NAME

  • rm: Using the rm keyword in the command | syntax. It will take the two sets of arguments as an OPTION and FILE/DIRECTORY NAME and delete it subject to file/directory permission and given option.
  • OPTION: We can provide the different flags as an option to the rm command.
  • FILE: Input file or directory name.

How does Linux Rm Command work?

As the name suggested, rm command is useful to remove or delete the file or directory from the Linux filesystem. As per the file or directory permission, the “rm” command will delete the file. The “rm” command is accepting the two arguments as OPTION and File/Directory name then remove the File/Directory as per the OPTION and file permissions.

Note: If you are newer in the Linux environment, please run the “rm” command very carefully. It will be a bit difficult to recover the file or directory once it will remove/delete it from the Linux Operating system. 

Examples to Implement Linux Rm Command

Below are the examples mentioned:

Example #1. Rm Command

With the help of rm command, we are able to delete the file from the Linux filesystem.

Code:

pwd
ls
rm file.txt

Explanation: We are having a sample directory. In the same directory, we have a “file.txt” file (refer screenshot 1 (a)). With the help of the rm command, we are able to remove the file. But using the default “rm” command, it asking the file delete confirmation. If we will enter “n” (no) then only the file will not delete (refer screenshot 1 (b)). If we will press “y” (yes) then the file will delete. (refer screenshot 1 (c)).

Output:

Linux rm Command - 1

Linux rm Command - 2

Linux rm Command - 3

Example #2. delete empty directory

With the help of rm command, we are able to delete the different types of directory present on Linux filesystem. Here, we are deleting the empty directory. We need to use the “-d” option in the rm command.

Code:

mkdir data
ll data/
rmdir data
rm -d data

Explanation: We are creating one sample directory (“data”)and no data present in it i.e. it is an empty directory (refer the screenshot 2 (a)). Now we are able to delete the empty directory in 2 different ways. In the first way, we can “rm” command with “-d” option, and second way, we can use the “rmdir” command to delete the empty directory (refer the screenshot 2 (b)).

Popular Course in this category
Linux Training Program (16 Courses, 3+ Projects)16 Online Courses | 3 Hands-on Projects | 160+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (6,013 ratings)
Course Price

View Course

Related Courses
Kali Linux Training (3 Courses, 3+ Projects)Red Hat Linux Training Program (4 Courses, 1+ Projects)

Output :

Linux rm Command - 4

Linux rm Command - 5

Linux rm Command - 6

Example #3. with “-rf” option

In rm command, we are having the functionality to delete or remove the number of files forcefully and recursively in the same working directory. No need to provide the number of file names to “rm” command. We need to use the “-rf” option in the rm command.

Code:

rm -rfdata

Explanation: In the above command, we are able to delete the number of files and sub-directory from the working directory. To delete the number of files forcefully, we need to use “-rf” option with “rm” command. In the data directory, we are having 3 different files and three sub-directories. We are deleting all the items for it.

Output:

“-rf” option

Example #4. with user interactive “-ri” option

In rm command, we are having the functionality to delete the files as per the user inputs. If the user provides the inputs as “y” (yes) then only the file will delete otherwise not. We need to use the “-ri” option in the rm command.

Code:

rm -ri data/

Explanation: As per the above command, we are recursively deleting all the files from the directory with user inputs. In the “data” directory, we are having three files and deleting the files as per the user inputs.

Output :

user interactive “-ri” option

Example #5. Prompt Option before delete

In the Linux rm command, there is the facility to prompt the number of files that are present in the directory. If we will pass the user input as “y”(yes) then in the next instance all the files will remove. We need to use the “-I” option in the rm command.

Code:

rm -I data/*

Explanation: As per the above command, we have 6 files in the “data” directory. With the help of the “I” option, we are getting the prompt before deleting the number of files form the current working directory. If we will pass the user input as “n” (no) then the files would not be deleted, if we pass the input as “y” (yes) then all the files would be deleting.

Output:

Prompt Option before delete

Example #6. with Regular Expression Option

In the Linux rm command, we can use the regular expression to delete the files from the filesystem. We need to use the “-f” option in the rm command.

Code:

rm -f data/file{1..5}
ls data/

Explanation: As per the above rm command, we have used the regular expression to delete the 5 files from the “data” directory. In the data directory, there are a total of 9 files. We have deleted initial 5 files and remaining files are present in the directory.

Output:

Regular Expression Option

Example #7 – Delete Large Number of Files

In a Linux or UNIX operating system, there are a huge amount of files in the directory. It would be difficult to delete all the files in the traditional way. We need to use the different way to delete those files in single rm command.

Code:

find ~/data/ -type f -exec rm {} \;
ls data/

Explanation: As per the above command, we are deleting the huge number of files in single rm command. With the help of find command, we are finding the files and pass the argument (array) to the rm command. Accordingly, the rm command will delete the number of files.

Output:

Delete Large Number of Files

Linux rm Command - 12

Conclusion

We have seen the uncut concept of “Linux Rm Command” with the proper example, explanation and command with different outputs. The default rm command is used to delete the number of files or directory recursively. But we need to use the rm command very carefully.

Recommended Articles

This is a guide to Linux rm Command. Here we discuss an introduction to Linux rm Command, appropriate syntax and how does the commands works with respective examples. You can also go through our other related articles to learn more -.

  1. Linux Directory Structure
  2. PS command in Linux
  3. Help Command in Linux
  4. Linux uniq

Linux Training Program (16 Courses, 3+ Projects)

16 Online Courses

3 Hands-on Projects

160+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Linux Tutorial
  • Linux Commands
    • CAT Command in Linux
    • PS command in Linux
    • Mkdir Command in Linux
    • rmdir command in Linux
    • mv command in Linux
    • Linux rm Command
    • CHMOD Command in Linux
    • Zip Command in Linux
    • Linux Route Command
    • SCP Command in Linux
    • du Command in Linux
    • Less Command in Linux
    • Copy Command in Linux
    • Head Command in Linux
    • Help Command in Linux
    • Linux History Command
    • hostname Command in Linux
    • GREP Command in Linux
    • Linux Curl Command
    • Linux List Users
    • Linux List Directories
    • Linux Directory Permissions
    • Find Directory Linux
    • Linux User Permission
    • Linux Add User
    • Linux Delete User
    • Linux uniq
    • Linux Filter Commands
    • Linux sort Command
    • Linux Sort by Size
    • Remove dir Linux
    • Linux tr Command
    • Linux Touch Command
    • Linux More Command
    • Linux Cal Command
    • Linux OD
    • Linux Zcat
    • Linux tracepath
    • Linux Sleep
    • Linux bg Command
    • Linux WC
    • Linux Read
    • Linux Write
    • Linux Rename Command
    • Linux Free Command
    • Linux tac
    • Linux Shred
    • Linux wget
    • Linux Date
    • Linux Untar
    • Linux JQ
    • Linux Xargs
    • Linux Mount Command
    • Linux mdadm
    • Linux comm
    • Linux tee Command
    • Tail Command in Linux
    • Linux gzip
    • Linux Directory Structure
    • Linux Alias Command
    • Linux Inode
    • Linux Ping
    • Linux Watch Command
    • Linux Split Command
    • Linux Size
    • Linux Network Stack
    • Linux Diff Command
    • Vim Command in Linux
    • Linux nslookup
  • Basic
    • Introduction to Linux
    • What is Linux
    • Careers in Linux
    • How to Install Linux
    • How to Install Ubuntu
    • How to Install Ubuntu Server
    • How To Install Debian
    • Advantage Of Linux
    • What is Raid in Linux
    • What is Raid 5?
    • Raid 5 vs Raid 6
    • Careers in Linux Administration
    • What is Shell in Linux?
    • Bash Shell in Linux
    • Linux Emulator
    • Linux Operators
    • Linux Apps
    • Linux Keyboard Shortcuts
    • Linux Crontab
    • What is Raid Storage
    • What is Vmware
    • What is RedHat
    • What is Debian
    • Uses of Ubuntu
    • Career In RedHat
    • Linux Distributions
    • Linux From Scratch
    • Linux Format
    • Cheat Sheet Linux
  • Advanced
    • Install VMware
    • Install GRUB
    • Chown Command in Linux
    • Linux Network Command
    • Configuring Linux Network Environment
    • DNS Configuration in Linux
    • Linux Users
    • Create User in Linux
    • DHCP Server in Linux
    • FTP Server in Linux
    • Linux Regular Expression
    • IP Command in Linux
    • Set IP Address in Linux
    • Tar Command in Linux
    • Dynamic Host Configuration Protocol
    • Linux Kill Signals
    • Linux File Permissions
    • Linux File System
    • Linux Create File
    • Linux Concatenate Files
    • Linux Join
    • Linux Process Management
    • Linux Proxy Server
    • Linux System Commands
    • Linux System Logging
    • NFS in Linux
    • Linux LVM
    • Linux Repository
    • Samba Server in Linux
    • Top Commands in Linux
    • Types of Shells in Linux
    • Shell Script Types For Linux
    • GCC Command in Linux
    • Monolithic Kernel
    • YARN Package Manager
    • Linux Sed Replace
    • Linux List Groups
    • Linux Container
    • Linux Find File by Name
    • Create Folder in Linux
    • Linux Move Folder
    • Linux Error Codes
    • Linux Automount
    • Linux HugePages
  • Kali Linux
    • Install Kali Linux
    • Kali Linux Uses
    • Kali Linux Commands
    • Kali Linux Repository
    • Kali Linux Packages
    • Kali Linux root password
    • Kali Linux Alternative
  • Interview Questions
    • Linux Interview Questions
    • Linux System Administration Interview
    • Red Hat Interview Questions

Related Courses

Kali Linux Training

Linux Training Course

Red Hat Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Linux Training Program (16 Courses, 3+ Projects) Learn More