Remove Files And Directories In Linux From Terminal
Recycle Bin Red Icon On White Background. Red Flat Style Vector Illustration.

Remove Files And Directories In Linux From Terminal

Tutorial To Remove Files And Directories In Linux From Terminal Or From Command-Line.

In this Linux tutorial post, we will use various Linux commands to remove files and directories in Linux from the terminal.

We will go through the Linux commands to delete files and directories in Linux from terminal or CLI.

Remove Files And Directories In Linux From Terminal

Removing files and directories can create serious troubles. You might lose important files and data from your system. So, make sure that you run these commands wisely.

You can use the tree command to view the files and directories so that you can delete them properly. Know the directory structure in your Linux before starting to remove the files and directories.

Run the following command to install tree command in Linux:

sudo apt-get install tree

Run tree command with:

tree

Commands To Delete Files And Directories In Linux

Commands To Delete Files In Linux

Run the rm command along with your file name which you want to delete and enter:

rm file_name.txt

Just in case if the file is not in the current directory, you need to provide the file location:

rm ./pathtothefilelocation/file_name

Run the following command to delete multiple files at once in Linux:

rm filename1 filename2 filename3

You can also use the wildcards to delete the groups of files with the same format.  You will know more about this command from the example below. You will see two commands where we are deleting png files and pdf files.

rm *.png
rm *.pdf

If you want confirmation before deleting the file, run the rm command with -i option.

rm -i filename

To remove files without prompting even if the files are write-protected, use the -f option.

rm -f filename

Commands To Delete Directories In Linux

Let’s see the commands to remove directories in Linux based operating system with the following examples.

READ More Relevant Stuff:  Configure Universe, Multiverse And Restricted repo On Ubuntu 20.04 LTS

Run the following command to remove an empty directory:

rm -d directoryname

Run the following command to remove multiple directories at once:

rm -d directoryname directoryname1 directoryname2

Run the following command to remove a directory with the files inside:

rm -r directory1 directory2 directory3

Note: rm -r command removes the directories and all files and sub-directories contained within them.

Run the following command to remove password-protected directory forcefully:

rm -rf directory

Remove Directories with rmdir command

Run the following command to delete a single directory in the current directory:

rmdir directoryname

You can also provide the path to the directory:

rmdir /path/to/directory

Run the following command to delete multiple directories at once:

rmdir directoryname1 directoryname2 directoryname3

This much for now. Please let us know if there is any error in this post.  We are regularly updating our blog posts.

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 *