Home » Linux Command » How to Remove Files and Folder or Directories Using Linux Command Line (CLI)

How to Remove Files and Folder or Directories Using Linux Command Line (CLI)

Last updated Sep 2, 2021

On this tutorial, we’ll present you methods to use the rmunlink, and rmdir instructions to take away recordsdata and directories in Linux.

How you can Take away Recordsdata

To take away (or delete) a file in Linux from the command line, use both the rm (take away) or unlink command.

The unlink command permits you to take away solely a single file, whereas with rm you’ll be able to take away a number of recordsdata without delay.

Be further cautious when eradicating recordsdata or directories, as a result of as soon as the file is deleted, it can’t be simply recovered.

  • To delete a single file, use the rm or unlink command adopted by the file identify:

$ unlink filename
$ rm filename

If the file is write-protected, you’ll be prompted for affirmation, as proven beneath. To take away the file sort y and hit Enter. In any other case, if the file just isn’t write-protected, will probably be deleted with out prompting.

Output:
rm: take away write-protected common empty file 'filename'?
  • To delete a number of recordsdata without delay, use the rm command adopted by the file names separated by area.
$ rm filename1 filename2 filename3

You can too use a wildcard (*) and common expansions to match a number of recordsdata. For instance, to take away all .pdf recordsdata within the present listing, use the next command:

$ rm *.pdf

When utilizing common expansions, first listing the recordsdata with the ls command so as to see what recordsdata can be deleted earlier than operating the rm command.

  • Use the rm with the -i choice to substantiate every file earlier than deleting it:

$ rm -i filename(s)
  • To take away recordsdata with out prompting even when the recordsdata are write-protected cross the -f (drive) choice to the rm command:
$ rm -f filename(s)
  • You can too mix rm choices. For instance, to take away all .txt recordsdata within the present listing with out a immediate in verbose mode, use the next command:
$ rm -fv *.txt

How you can Take away Directories (Folders)

In Linux, you’ll be able to take away/delete directories with the rmdir and rm.

rmdir is a command-line utility for deleting empty directories whereas with rm you’ll be able to take away directories and their contents recursively.

  • To take away an empty listing, use both rmdir or rm -d adopted by the listing identify:

$ rm -d dirname
$ rmdir dirname
  • To take away non-empty directories and all of the recordsdata inside them, use the rm command with the-r (recursive) choice:
$ rm -r dirname

If a listing or a file throughout the listing is write-protected, you’ll be prompted to substantiate the deletion.

  • To take away non-empty directories and all of the recordsdata with out being prompted, use rm with the -r (recursive) and -f choices:
$ rm -rf dirname
  • To take away a number of directories without delay, use the rm -r command adopted by the listing names separated by area.
$ rm -r dirname1 dirname2 dirname3

Identical as with recordsdata you may as well use a wildcard (*) and common expansions to match a number of directories.

Conclusion

By now it’s best to have a very good understanding of methods to use the Linux rmrmdir and unlink instructions and it’s best to be capable of safely take away recordsdata and directories from the command line.

Related Posts

The 30 most useful Linux commands

This article will introduce some practical and common Linux or Unix command , which is the Linux system administrators normally use the command . This article is not a complete list, but a brief list of commands that can be useful...

15 Practical examples of tcpdump command

15 Practical examples of tcpdump command

The tcpdump is the most essential and powerful command-line network packet sniffing tool. We use it to analyze network packets, capture or filter TCP/IP packets that transferred and received over the network on a specific network interface. It is available in almost...

15 Useful “df” Commands to Check Disk Space in Linux

15 Useful “df” Commands to Check Disk Space in Linux

There are several Linux tools available on the internet to check disk space utilization; we can use any of them. But, Linux has a best built-in command-line utility called “df”. The “df” is the short form of “disk filesystem”, we can get detailed information on used...

15 Practical examples of echo command

15 Practical examples of echo command

If you are writing a bash script, you know echo command very well. It is use to display text lines on an output screen or a file. The echo command is the most common command used by all Linux users, and it is built-in Linux command. echo command syntax: echo...

Follow Us

Our Communities

More on Linux Command

The Ultimate Managed Hosting Platform
Load WordPress Sites in as fast as 37ms!

0 Comments

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

eleven + eleven =

Shares