How to Delete Files and Directories in Linux Terminal

  1. Delete Files Using Terminal in Linux
  2. Delete Directories Using Terminal in Linux

We can delete Files and Directories with rm, rmdir, and unlink commands using Linux Terminal. We can use rm and unlink commands to delete files while rm and rmdir can be used to delete directories. We add flags to the rm command to delete a directory.

Delete Files Using Terminal in Linux

We can use rm and unlink commands to delete files using Terminal in Linux.

With the unlink command, we can only delete a single file at a time. However, with the rm command, we can delete multiple files simultaneously.

Delete a Single File

We use rm or unlink command with the name of the file to be deleted to delete that specific file.

unlink filename
rm filename

To delete a file abc.txt in the current directory:

unlink abc.txt

or

rm abc.txt

We will be asked whether to delete the file or not if the file is write-protected. If we are asked, we can type Y or y and hit the Enter key to delete the file.

Delete Multiple Files

We can use the rm command with multiple file names separated from each other by a space to delete multiple files simultaneously.

rm abc.txt ab.jpg abc.pdf

It will delete all the three files: abc.txt,ab.jpg and abc.pdf.

We can use the rm command followed by a wildcard * and regular expansions to delete matched multiple files.

rm *.png

It will delete all the files with the extension of .png in the current working directory.

We can use the -i flag to enable the confirmation process to delete each file.

rm -i*.png

Output:

rm: remove regular file '1.png'?

If you want to delete the file, press Y and hit Enter else press N and hit enter.

The -f flag is the opposite of the -iflag. It does not prompt for confirmation even if files are write-protected. The -f represents forcefully.

rm -f*.png

It will delete all the .png files in the current working directory even if they are write-protected.

Delete Directories Using Terminal in Linux

We can use rmdir or rm commands to delete directories using the Linux terminal.

rmdir is employed for deleting empty directories while rm can delete directories and their contents recursively.

Remove Empty Directory

We can use rmdir or rm -d commands, followed by directory names to delete empty directories.

rmdir mydir
rm -d mydir

These both commands will delete the empty directory mydir.

To delete multiple empty directories, we can use the same commands, followed by names of directories separated by space to be deleted.

rmdir mydir1 mydir2 mydir3

It will delete the empty directories mydir1, mydir2, and mydir3.

Remove Non-Empty Directory

We use the rm command with -r(recursive) flag to remove non-empty Directory in Linux using the terminal.

rm -r abc

It deletes the non-empty directory abc.

We add the -f flag to rm -r to forcefully delete or delete write protected contents without being asked for confirmation.

rm -rf abc

To delete multiple empty directories, we can use the rm -rf command followed by names of directories separated by space to be deleted.

rm -r mydir1 mydir2 mydir3

It will delete the non- empty directories mydir1, mydir2, and mydir3.

Related Article - Linux File

  • How to Create a File Using the Terminal in Linux
  • Related Article - Linux Files and Directories

  • How to Change File Permissions in Linux
  • How to Rename Files and Directories Using Linux Terminal
  • comments powered by Disqus