How to Delete Files and Directories in Linux from Command Line

This guide will teach you how to quickly and easily delete files and directories in Linux using the command line.
One of Linux ’ s basic file system administration tasks involves creating, modify, and deleting unlike types of files and directories. consequently, knowing some all-important tools and concepts for file deletion is handy and can save you time .
Related: 20 Basic Linux Commands for Beginners Explained with Examples
This article will show you respective ways to delete files and directories in Linux. We will besides provide brief information on the respective flags and options you can use while deleting files and directories from your Linux organization.

Delete files Using the rm Command

To delete a charge, you need to use the rm ( remove ) command and tell it what file or files you want it to delete. It has the comply general syntax :


For example, to delete a single file named file.jpg, type the postdate in the command agate line :

rm file.jpg

The rm command displays a ratification dialogue for write-protected files. differently, the dominate will immediately delete the file. To make rm always prompt before deleting a file, you can use -i flag :

rm -i file.jpg

The rm control in Linux can besides delete more than one charge. Bypassing multiple filenames separated by a space as arguments to rm, you can delete multiple files :

rm file1.jpg file2.jpg file3.jpg

You can use the -f ( coerce ) flag to delete write-protected files without asking for confirmation :

rm -f file.jpg

In accession, the rm command besides supports regular expressions. If you want to delete all three files ( file1.jpg, file2.jpg, and file3.jpg ), you can use :

rm file*.jpg

If you need it, here ’ s the man page for the rm command .

The unlink control besides deletes a given file. This is another though not indeed democratic, way of deleting a file in Linux .
You can use the unlink command to permanently delete a unmarried file named file.jpg by typing the pursue :

unlink file.jpg

You are probably wondering what the remainder between rm and unlink is .
Above all, both commands are wrappers to the like fundamental officiate, an unlink() system call. But the unlink dominate suffers from the follow restrictions :

  • Unable to delete directories.
  • Unable to recurse.
  • Can only take one argument at a time.
  • Has no options other than --help and --version.
  • Less sanity checking.

For more about the unlink command in Linux, confer its manual page.

Delete Directories Using the rm Command

By adding the -r ( recursive ) choice to the rm command in Linux, you can delete a directory and all its contents ( files, subdirectories, etc. ) .
For model, to remove a directory named myfiles, type the following in the command trace :

rm -r myfiles/

The rm command would ask you to validate the operation if the stipulate directory or a charge inside it is write-protected. To remove a directory without confirmation :

rm -rf myfiles/

To delete multiple directories ( for model, myfiles1, myfiles2, and myfiles3 ), type rm -rf followed by the directory names or path to directories, separated by a space, as follows :

rm -rf myfiles1/ myfiles2/ myfiles3/

Delete Directories Using the rmdir Command

Something important to note here is that the rmdir command is used when deleting empty directories in Linux. If you need to remove a non-empty directory, use the rm command .
If a stipulate directory is not empty, the output will display an error, as shown below .

rmdir: failed to remove 'myfiles/': Directory not empty

To remove a single empty directory, type rmdir followed by the directory name or path to the directory as follows :

rmdir myfiles/

To remove multiple directories ( for exercise, myfiles1, myfiles2, and myfiles3 ), type rmdir followed by the directory names or path to directories, separated by a space, as follows :

rmdir myfiles1/ myfiles2/ myfiles3/

If the instruction finds contented in any list directories, it will skip it and move on to the next one .
With -p options added to the rmdir command, each directory controversy is treated as a pathname, of which all components will be removed if they are already empty, starting from the concluding part .
For example, the postdate command will delete both : the rear myfiles directory and its subdirectory subdir .

rmdir -p myfiles/subdir/

If you need it, here ’ s the man page for the unlink command.


By now, you should clearly understand how to delete files and directories in Linux from the command line .
It is authoritative to remember that when you delete a file or directory in Linux using rm, unlink, and rmdir, it is immediately removed alternatively of moving towards Trash. therefore, you will need to be careful while using these commands as you will not recover the absent files .
Practice the examples mentioned in this article, and you should be cook .

reservoir :
Category : Tutorial

Related Posts

Trả lời

Email của bạn sẽ không được hiển thị công khai.