• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

Remove Files and Directories Using Linux Command Line

Written by Admin, Updated On December 10, 2022
linux commands, rm
Remove Files and Directories using Linux Command

In this tutorial, you will learn how remove files and directories using the rm command in Linux system.

Syntax of rm Command#

Below is the basic syntax of rm command:

rm [OPTIONS] [FILE_NAME]

Here,

  • OPTIONS: You can specify the options to use with rm command.
  • FILE_NAMES: File name(s) which need to be delete.

Remove Files#

Following are the multiple examples with different options to delete files using rm command:

How to Remove a File#

To remove or delete a file in Linux you can use rm command without any options.

rm test.jpg

In above example, it will remove test.jpg file from the system.

You should make sure before deleting any files because once a file has been deleted will not be restore.

If the file is write protected then you will be prompted for confirmation as given below. You should press Y and hit Enter key to continue to delete, Otherwise if file is not write protected it will be deleted without any prompt.

rm: remove write-protected regular empty file 'test.jpg'?

Delete Multiple Files#

Also you can delete multiple files using rm command without passing any options. You just need to separate file names by space.

rm test1.jpg test2.jpg test3.jpg

Once you run above command it will delete all test1.jpg,test2.jpg and test3.jpg from system.

3. Prompt Before Deleting File#

Specify -i option with rm command to show prompt before deleting a file in Linux system.

rm test1.jpg test2.jpg test3.jpg

When you run above command it will prompt you before deleting each file. If you really want to delete then press Y or N.

It will show like as below:

4. Delete Write Protected File without Prompt#

If you don’t want to see any prompt even if file is write protected or not you can use -f option with rm command.

rm -f data.pdf

In above example data.pdf file will be deleted without any confirmation even if that file is write protected.

5. Remove Multiple File with Regular Expression#

When you have requirement to delete multiple files with specific type then you should use regular expression. With rm command you can use regular expression to delete files as give below:

rm *.jpg

Above command will file all the files with .jpg extension and delete from current directory. Like this, you can specify any regular expression as per your requirements.

Remove Directories#

1. How to Remove a Directory or Folder#

When the directory is empty then you can remove using -d option with rm command. Run the below command to delete empty directory:

rm -d dirname

To delete non-empty directory run below command:

rm -r dirname

Same as file deletion, if the file is write protected then it will prompt you for confirmation.

2. Remove Multiple Directories or Folders#

You can delete multiple directories simultaneously using -r option with rm command and separate file with space one by one.

rm -r dirname1 dirname2 dirname3

Above command will delete all directories dirname1, dirname2 and dirname3.

3. Delete Write Protected Directory without Prompt#

When you do not want any type of confirmation at the time of directories deletion you should use -rf along with rm command.

rm -rf dirname

This command will delete directory without any confirmation even if file is write protected or not.

Conclusion#

Finally, you have successfully learned to Remove Files and Directories Using Linux Command.

If you have any queries you can leave comment at below comment box.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Use Shutdown Command in Linux
Next Article   How to Check Ubuntu Version with Different Methods

Related Posts

  • How to Delete (Remove) Symbolic Links in Linux

    How to Delete (Remove) Symbolic Links in Linux

    June 12, 2020
  • Linux File Command

    Linux File Command With Example

    June 12, 2019
  • Linux Uptime Command

    Uptime Command in Linux With Example

    June 12, 2019

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 22.04
    How to Install Microsoft Edge Browser on Ubuntu 22.04 March 14, 2023
  • How to Install Ruby on Ubuntu 22.04 LTS
    How to Install Ruby on Ubuntu 22.04 LTS February 27, 2023
  • How to Install LEMP Stack on Ubuntu 22.04
    How to Install LEMP Stack on Ubuntu 22.04 March 18, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04
    How to Set Up Apache Virtual Hosts on Ubuntu 22.04 March 2, 2023
  • How to Install MariaDB on Debian 11 Bullseye
    How to Install MariaDB on Debian 11 Bullseye March 8, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook