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

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.

0 Comments

Submit a Comment

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

Related Articles