Delete files and folders in Linux command line


In the previous chapters of the Terminal Basics series, you learned to create new files and directories (folders).

Now let’s see how you can delete files and folders in Linux terminal.

Deleting files

To delete files, you can use the rm command as follows:

rm filename_or_path

You will not see any output if the file is deleted successfully.

Here is an example where I deleted one of the files named new_file. When I list the contents of the directory you can see that new_file does not exist anymore.

Deleting Files in Linux Terminal
Deleting a single file

You can also delete multiple files in the same command:

rm file1 file2 file3

Let me show an example of deleting two files in one command.

Deleting multiple files in a single rm command
Delete multiple files

🏋️Exercise file deletion

Let’s put into practice what you have just learned. Create a directory named practice_delete and switch to it:

mkdir practice_delete && cd practice_delete

Now create some empty files:

touch file1 file2 file3

Delete file3:

rm file3

Now let’s do something more. Run this command and change the permission on file2:

chmod u-w file1 file2

Try removing file2 now:

rm file2

Do you see a message ‘delete write-protected file‘? This is because you have removed write (modify) permission from this file.

You can press Y or the Enter key to confirm the deletion or N to reject the deletion.

If you don’t want to see this message and still delete it, you can use the force delete option -f. Try removing file1:

rm -f file1

Here is a replay of all the examples above to help you:

Deleting Files in Linux Terminal

đźš§

There is no recycle bin in the Linux command line. Once the file is deleted, you cannot undo the action to bring it back from the trash like you do in the graphical file manager. For this reason, be very careful while deleting files.

Remove but with caution

The absence of a trash can makes deleting a permanent job of sorting. This is why you need to be careful about the files you delete.

There is an interactive mode with option -i. With this, you will be asked to confirm the deletion.

rm -i filename

This is useful when you delete multiple files based on a certain pattern.

Here is an example where I interactively delete all files that match file_pattern in their name. I delete some of them and keep others in interactive mode.

Deleting files in interactive mode

đź’ˇ

I advise changing to the directory where the files are located and then deleting them. This helps reduce any potential caused by a typo in the file path.

Delete directories

There is a dedicated rmdir command for deleting directories in Linux.

rmdir dir_name

However, it can only remove empty directories. If the directory contains files or subdirectories, the rmdir command generates an error.

[email protected]:~/practice_delete$ rmdir dir2
rmdir: failed to remove 'dir2': Directory not empty

And that makes it less useful in most cases.

So how do you delete a non-empty folder then? Well, you’re using the same rm command you used earlier to delete files.

Yes, the same rm command but with the recursive option -r:

rm -r dir_name

🏋️Exercise folder deletion

Let’s put what you’ve learned into practice.

Switch to the practice_delete folder if you are not already there. Now create two directories dir1 and dir2.

mkdir dir1 dir2

Create a file in dir2:

touch dir2/file

Now try removing the directories using the rmdir command:

rmdir dir1
rmdir dir2

Since dir2 is not empty, the rmdir command will fail. Instead, use the rm command with the recursive option:

rm -r dir2

Here’s a replay of all the example commands above to help you:

Deleting Folders in Linux

đź’ˇ

Interactive delete mode is even more useful when deleting a directory with the recursive option of the rm command: rm-ri dir_name

So, you have learned how to delete files and folders using Linux commands. It’s time to practice some more.

Test your knowledge

Prepare a directory tree that looks like this:

.
├── dir1
│   ├── file1
│   ├── file2
│   └── file3
├── dir2
├── dir3
└── file

Basically, you create a file named file and three directories dir1, dir2 and dir3 in the current directory (practice_delete). And then you create files file1, file2 and file3 in dir1.

Now do the following:

  • DELETE file2.
  • Go to dir3 and force deletion of the file named file in the top directory.
  • Delete all contents of dir1 but not the directory itself.
  • List the contents of the dir.

I encourage you to discuss practical matters in the This is the FOSS community forum.

It’s going well. You learned several basic things like changing directory, checking directory contents, creating and deleting files and directories. In the next chapter, you will learn how to copy files and folders in the terminal. Stay tuned!



Source link

1 thought on “Delete files and folders in Linux command line”

Leave a Comment

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