How to delete files and folders in Linux

The basics of the line are absolutely essential for every future system administrator and advanced user of Linux. Today we will cover a basic function: deleting files and directories using the command line.

The rmdir command

The command used to delete empty directories in Linux is rmdir.

The basic syntax of this command is easy to understand. Here is an example:

rmdir DirectoryName

  • rmdir is the command.
  • is an optional switch that changes the way the command behaves.
  • DirectoryName is the folder you are going to delete.

If no option is specified, rmdir it simply removes the directory whose name was mentioned as the destination. Before using this command, you will need to login to your VPS server using SSH. Here is one that will help you do it.

Delete folders in Linux using the rmdir command

Before using the command rmdirwe suggest you review the files contained in a folder with the command ls. In our case, we have a folder called Dir1.

rmdir dir1

This command will delete the empty folder called Dir1. Pretty simple, right?

You can also remove multiple folders by separating their names with spaces. For example:

rmdir Addr1 Addr2 Addr3

After executing this command, the folders will be deleted Dir1, Dir2 Y Dir3.

Now suppose we have a folder called Dir3, which has subfolders and files inside. If we use the following command:

rmdir dir3

We will see an error like this:

rmdir: failed to remove ‘dir3’: Directory not empty

Which warns us that the folder is not empty, so rmdir it only works with empty directories.

See also  Top 7 WordPress Contact Form Plugins

The rmdir it’s a smart feature, and it only allows you to delete empty folders to avoid accidental data loss. Remember that it is almost impossible to recover deleted data on any Linux distribution.

The option -p allows you to delete the folder and its parent folders.

rmdir -p Dir1/Dir2/Dir3

This command will remove Dir3 and its mother folders Dir2 Y Dir1.

The option -v generates a diagnostic text for each processed folder. Using this option will generate a confirmation listing all the folders that were deleted.

The rm command

The command rmdir it is ideal for safely deleting unused and empty folders. If you want to delete files or folders that contain files, you will need to use the command rm.

The basic syntax of this command is similar to rmdir:

rm Filename

Delete a file in Linux using the rm command

Use the command rm to remove the file named article.txt:

rm article.txt

If we have a folder called Dir1 that contains subfolders and files, we will have to add the modifier -r. The command would look like this:

rm -r Dir1

The option -r recursively deletes the folders and their contents.

Another useful option is -Yo, which will ask you to individually confirm the files to be deleted. So you can avoid any nasty mistakes.

You can also remove empty folders using the option -d. The following command will delete an empty folder called Dir1:

rm -d Dir1

You can use a wildcard

and regular expansions to match multiple files. For example, the following command will delete all pdf files located in the current folder.

See also  7 Methods To Speed ​​Up WordPress - Explanatory Video

rm *.pdf

You can use variations of all the commands listed above to delete Linux files with other extensions like .txt, .doc, .odt, etc. The option -F

allows you to forcibly delete the entire contents of a folder. The command would look like this:

rm -rf Dir1 The above command will recursively and forcefully delete everything residing in the folder Dir1

without asking anything in the terminal. You can also delete multiple folders at once. The following command will delete three folders,Dir1 , Dir2 YDir3

in a single command.

rm -rf Dir1 Dir2 Dir3 Congratulations! You have successfully mastered all the basic functions of the commands rm Y

rmdir.

conclusion On Linux, accidentally deleting a single file can cause serious problems. This is why it is essential to master the two main commands to delete files and folders: rm Yrmdir

. In this article we have learned about these two commands and the various options that can be used with them.

We hope this article has been useful to you. Remember that after deleting a Linux file or folder, it cannot be recovered, so you have to be very careful. Good luck!

Gustavo is passionate about creating websites. He focuses on the application of SEO strategies at for Spain and Latin America, as well as the creation of high-level content. When he is not applying new WordPress tricks you can find him playing the guitar, traveling or taking an online course.

Loading Facebook Comments ...
Loading Disqus Comments ...