Linux Commands To Remove Files And Directories

Linux Commands To Remove Files And Directories

Insight: Linux Commands To Remove Files And Directories

Let’s go through this tutorial to remove files and directories in Linux using command line. During this tutorial, we will use the rm command to remove files and directories. We will guide you with the rm examples.

Linux Commands To Remove Files And Directories

Let’s start this Linux tutorial. You need to have sudo privilege to excute this command:

1. Remove single file.

Run the following command to remove a single file in Linux from command terminal.

rm filename

Meanwhile, If your file is write protected, System will ask you to confirm before executing the remove operation.

2. Remove multiple files at once.

Run the following rm command to remove multiple files at once.

rm file_name1 file_name2 file_name3

Or you can also use a wildcard (*). For example to remove all .mp3 files in the current directory, use the following rm command.

rm *.mp3

3. Remove files even if the files are write-protected.

Run the the following command to remove files even if the files are write protected.  For example, you can remove it with the -f (force) option.

rm -f filename(s)

4. Remove file after confirming.

Run the following command to confirm each file before deleting.

rm -i filename(s)

5. Remove empty directory.

READ More Relevant Stuff:  11 Bash Script Examples

Run the following command in your terminal to remove empty  directory.

rm -d dirname

6. Remove non-empty directories and all the files inside it.

Open your terminal and run the following command.

rm -r dirname

7. To remove multiple directories at once.

Run the following command to remove multiple directories at once.

rm -r dirname1 dirname2 dirname3

 

 

All right, This much for now. You can bookmark this post for your future use as we will regularly update this post with more rm examples.  Meanwhile, Let us know if there is any mistakes or errors in this article.

Summarry: Linux Commands To Remove Files And Directories.

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply

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