How to Delete a File in Linux



Linux


In this article, we show how to delete a file in linux.

So we don't have to manually delete a file.

We can delete a file in the linux command terminal.

We can delete a file with the rm command followed by the file that we want to delete.

So let's say we want to delete a file named file1.txt in the current working directory. To do this, we use the following code below.



So it's very basic. To delete any file in the current working directory, you use the rm command followed by the file that you want to delete.

In this case, we are deleting the file, file1.txt, from the current working directory.

Running the following code above, we get the following output shown below.


Deleting a file in linux


So we've printed out the current working directory with the statement, pwd

You can see that the current working directory, when I ran this code, was david/home/

So this is where we will delete the file from.

We then list out all contents from the current working directory and you can see that file1.txt is in the directory.

We then delete the file, file1.txt, with the statement, rm file1.txt

We then list the contents of the directory again and we see that file1.txt is now gone. It's permanently deleted.

Just like the touch command in linux can be used to create multiple files, the rm command can be used in linux to delete multiple files in linux at the same time.

So I created a directory with the files, file1.txt, file2.txt, and file3.txt.

I then delete all these files with the following code shown below.


Deleting multiple files in linux


So before I deleted the files, I listed the contents in the current working directory with the ls command.

You can see pre-deletion that file1.txt, file2.txt, and file3.txt are present in the directory.

After deletion with the rm command, they are permanently deleted.

And this is hwo to delete a file in linux.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...