How to Remove Files and Folders Using the Linux Command Line

Removing the files and folders is necessary to manage your disk space efficiently. These commands are very useful and are used daily by system administrators, developers, and regular users familiar with them. The command line is a very powerful tool that can speed up many processes and save time compared to using the GUI. In this post we will show you how to remove files and folders on your Linux system using the command line.

In the next paragraphs we will explain the commands for removing files and folders with real examples. With this convenience comes risk of course – it can be dangerous since if you recursively remove subdirectories along with the files by mistake, you will lose those files forever. Make sure to follow along closely to prevent any unintentional data loss from occurring. Let’s get started!

Prerequisites

  • A server with a Linux-based Operating System
  • User privileges: root or non-root user with sudo privileges

Update the System

In our blog post we will use Ubuntu 22.04 as it is a very common OS choice for servers. Since our install is a fresh install, we need to update the packages to their latest version available with the following command:

sudo apt-get update -y && sudo apt-get upgrade -y

Once done, we are ready to continue removing our files and folders.

Using the “rm” command

The rm command is a shortcut of the word remove and is used for removing single or multiple files or directories depending on the arguments passed in the command when executing it. While using the rm command we must be cautious because it can cause issues in your system that cannot be solved or restored. Let’s go over the rm command usage with real-world examples.

Removing a single file can be done with the following command:

rm index.php

To remove multiple files, you can simply add all the files you want to delete to the end of the command, like the following:

rm index.php wp-config.php wp-cron.php

If you need to know how to remove a directory in Linux, we can use the rmdir command:

rmdir wp-content/

The directory will be removed only if the directory is empty, and no files or subdirectories exist within it.

root@host:/var/www/html/wordpress# rmdir wp-includes/
rmdir: failed to remove 'wp-includes/': Directory not empty

Now, we need to enter in that directory and remove the files and folders separately.

Instead of doing that, if you are certain that everything within the folder can be deleted permanently, we can use some options with the rm command to make that possible.

The -r option enables the Recursive flag, which tells the command that it should drill down into any subdirectories, delete all files within them, then delete the subdirectories themselves. This option is not very useful without the next option.

The -f option is for Force, which lets the command delete directories without throwing any errors. Make sure you have the necessary permissions to delete all the files and folders you plan on deleting.

Here’s an example of deleting a folder that contains files and folders within it.

rm -rf wp-includes/

Finally, the -i option lets the command interactively ask you whether to delete or keep files/directories. Like so:

rm -i test.txt
rm: remove regular empty file 'test.txt'?

These are the most used commands for removing files and folders daily. If you want to learn more about the rm command, you can execute the man rm command on your command line for more detailed info.

RM(1)                                                          User Commands                                                         RM(1)

NAME
       rm - remove files or directories

SYNOPSIS
       rm [OPTION]... [FILE]...

DESCRIPTION
       This manual page documents the GNU version of rm.  rm removes each specified file.  By default, it does not remove directories.

       If  the -I or --interactive=once option is given, and there are more than three files or the -r, -R, or --recursive are given, then
       rm prompts the user for whether to proceed with the entire operation.  If the response is not affirmative, the  entire  command  is
       aborted.

       Otherwise,  if a file is unwritable, standard input is a terminal, and the -f or --force option is not given, or the -i or --inter‐
       active=always option is given, rm prompts the user for whether to remove the file.  If the response is not affirmative, the file is
       skipped.
OPTIONS
       Remove (unlink) the FILE(s).

       -f, --force
              ignore nonexistent files and arguments, never prompt

       -i     prompt before every removal

       -I     prompt  once before removing more than three files, or when removing recursively; less intrusive than -i, while still giving
              protection against most mistakes

       --interactive[=WHEN]
              prompt according to WHEN: never, once (-I), or always (-i); without WHEN, prompt always

       --one-file-system
              when removing a hierarchy recursively, skip any directory that is on a file system different from that of the  corresponding
              command line argument

Using the “unlink” command

The unlink command is also used for removing files. Unlike the rm command, this command can only be used for files, and not directories. There are no options or flags that change functionality.

To remove a file with this command, execute the following:

unlink index.php

After successful removal of the file, the command returns a status code of zero.

If you want to know more about the command, you can simply execute man unlink on your command line.

Using the “shred” command

The shred command works differently than rm and unlink commands. It deletes the file just like the previous commands, but it “overwrites” the blocks of the file and deletes the file at the same time.

This command has some security implications. This lets you more securely erase files by overwriting their contents before releasing the filesystem blocks.

The rm and unlink commands delete the file by marking the blocks of the file as available. The actual contents of the file were never touched. Therefore, the contents of the file will stay on the disk until space is needed for something else.

There are a few different options available for the shred command, but these we think are the most relevant ones:

  • To force deletion of files (requires root or sudo), use the -f option.
  • To delete the file after overwriting it, use the -u option.
  • To hide your shredding by performing a final overwrite with zeroes, use the -z option.
  • To choose how many times you want to overwrite the file, use the -n option.

To delete a file with the “shred” command, execute the following:

shred -u index.php

Congratulations! You successfully learned how to remove files and folders using the Linux command line.

You won’t need to focus on deleting files and folders if you opt for our fast Linux cloud VPS plans. We provide plenty of resources at a reasonable price. This lets you keep all your files and not worry about getting rid of something you might need later.

Do you know any other ways to delete files and folders using Linux? Let us know in our comment section.

Leave a Comment