How to delete files and folders using Linux command line

In this tutorial, TipsMake will show you how to delete files and folders using the command line on Linux. This tutorial is compatible with all Linux distributions, so it works the same way on Ubuntu, Debian, CentOS, AlmaLinux, Rocky Linux, etc.

Delete a file on Linux

In Linux, the rm command is used to delete files and directories. Navigate to the specific directory that contains the file you want to delete. Specify a location otherwise the command will start searching in the current working directory. For example, the author of this article has a file in the /tmp/ directory that he wants to delete. To delete the desired file, open Terminal and enter the following command:

# rm file.txt

How to delete files and folders using Linux command line Picture 1

Be careful while deleting files and folders from Linux because once deleted they cannot be recovered. So it will ask you to confirm before deleting the file:

# rm –i file.txt

How to delete files and folders using Linux command line Picture 2

If you don't want to see a confirmation message for deletion, use the following command:

# rm –f file.txt

How to delete files and folders using Linux command line Picture 3

The confirmation message will not appear.

Delete multiple files on Linux

To delete multiple files on Linux, the same rm command can be used.

# rm file.txt file1.txt file2.txt

How to delete files and folders using Linux command line Picture 4

Delete a directory on Linux

To delete a directory on Linux, the same command is used. But you need to add the -r and -f options to delete a directory.

# rm –rf /data

How to delete files and folders using Linux command line Picture 5

But be careful, this will delete the entire directory, including all the files and directories inside. You can use the above command without -f , as it will not prompt for confirmation. The -r option is used to delete a directory.

If you just want to delete an empty directory, use this command instead:

rmdir /data

The command will show an error in case the directory is not empty.

Summary

In all Linux distributions, the rm command is used to delete files and directories.

  1. If -i is used with rm, you will be prompted for confirmation before deleting.
  2. If -r is used with rm, the directory will be deleted without confirmation.

See also:

4 ★ | 1 Vote

May be interested

  • Instructions for using find command in LinuxInstructions for using find command in Linux
    the find command is one of the most important and handy commands on a linux system. as its name suggests, the command can find files on a linux pc based on a variety of conditions and variables you set.
  • 5 reasons why people love the Linux command line5 reasons why people love the Linux command line
    many people are afraid of the command line. they see it as something for software developers or geeks. but the command line is merely another way of interacting with a pc, and there are some tasks that are very easy to do with the cli.
  • The Cat command in LinuxThe Cat command in Linux
    the cat command (short for 'concatenate') is one of the most frequently used commands in operating systems like linux / unix. the cat command allows users to create one or more files, view file contents, join files, and redirect output in a terminal or file.
  • 7 commands to manipulate the most basic files and folders everyone must know7 commands to manipulate the most basic files and folders everyone must know
    on windows or linux operating systems, there are a number of things that users can only do with commands. in the following article, the network administrator will introduce you to the 7 most basic commands that any user must 'understand' to perform operations faster.
  • How to synchronize files and folders on LinuxHow to synchronize files and folders on Linux
    linux administrators must keep folders synced on a regular basis. there are two simple use scenarios for synchronizing these files and folders.
  • How to check whether a Linux PC is 64-bit or 32-bit by command lineHow to check whether a Linux PC is 64-bit or 32-bit by command line
    how to check if your linux system is 32-bit or 64-bit? in this article, tipsmake.com will show you how to define it using the gui and the command line.
  • Basic Linux commands everyone needs to knowBasic Linux commands everyone needs to know
    when using linux, you should also learn basic linux commands to make operations quicker and easier. here are some basic commands on linux you should know.
  • Use CMD to delete large folders on WindowsUse CMD to delete large folders on Windows
    large directories on the system are one of the culprits that consume a lot of system space. a lot of folders that you create, after deleting, will come back later. maybe it's junk folders that you try to remove.
  • How to Create ISO Files on LinuxHow to Create ISO Files on Linux
    today's tipsmake will show you how to turn multiple files into one iso file on a linux computer. you need to use the linux command line to do it.
  • How to view hidden files and folders on LinuxHow to view hidden files and folders on Linux
    the linux operating system includes hundreds of files and folders that are hidden by default. such files are called hidden files or dot files because they always start with a dot (.).