How to delete and remove files on Debian Linux

last updated in Categories , ,

I am a new Debian Linux user. How do I delete and remove a file on Debian Linux based system?

You need to use the rm command. It tries to remove the files specified on the command line. Use the rm command to delete files and directories on Debian Linux. This page shows how to delete and remove files on Debian Linux.

Command to delete and remove files on Debian Linux

The syntax is as follows for the rm and unlink command to remove files on Debian Linux:

  1. Open the Debian terminal application (bash shell)
  2. Type any one of the following command to delete a file named joey.db in the current directory
  3. rm joey.db
    OR
    unlink joey.db

Let use see all the rm command options to delete and remove files on Debian Linux.

Delete multiple files

Type the following command to delete the file named hannah.doc, vince.jpg, and dance.jpg located in the current directory:
vivek@debian-9-stretch:~$ rm hannah.doc vince.jpg dance.jpg
You can specify path too. If a file named hannah.doc located in /tmp/ directory, you can run:
vivek@debian-9-stretch:~$ rm /tmp/hannah.doc
vivek@debian-9-stretch:~$ rm /tmp/hannah.doc /home/vivek/dance.jpg /home/vivek/data/vince.jpg

Debian Linux delete a file and prompt before every removal

To get confirmation before attempting to remove each file pass the -i option to the rm command on Debian Linux:
vivek@debian-9-stretch:~$ rm -i filename1
vivek@debian-9-stretch:~$ rm -i hannah.doc

Force rm command on Debian Linux to explain what is being done with file

Pass the -v option as follows to get verbose output on Debian Linux box:
vivek@debian-9-stretch:~$ rm -v filename1
vivek@debian-9-stretch:~$ rm -v cake-day.jpg

Debian Linux delete all files in folder or directory

You need to pass the following options:
vivek@debian-9-stretch:~$ rm -rf dir1
vivek@debian-9-stretch:~$ rm -rf /path/to/dir/
vivek@debian-9-stretch:~$ rm -rf /home/vivek/oldschoolpics/

It will remove all files and subdirectories from a directory. So be careful. Always keep backups of all important data on Debian Linux.
Delete and remove files on Debian Linux

Debian Linux delete file begins with a dash or hyphen

If the name of a file or directory or folder starts with a dash (- or hyphen --), use the following syntax:
vivek@debian-9-stretch:~$ rm -- -filename1
vivek@debian-9-stretch:~$ rm -- --filename1
vivek@debian-9-stretch:~$ rm -rf --directory1
vivek@debian-9-stretch:~$ rm ./-file
vivek@debian-9-stretch:~$ rm -rf ./--directory1

Do not run ‘rm -rf /‘ command as an administrator/root or normal Debian Linux user

rm -rf (variously, rm -rf /, rm -rf *, and others) is frequently used in jokes and anecdotes about Debian Linux disasters. The rm -rf / variant of the command, if run by an administrator, would cause the contents of every writable mounted filesystem on the computer to be deleted. Do not try these commands on Debian Linux:
vivek@debian-9-stretch:~$ rm -rf /
vivek@debian-9-stretch:~$ rm -rf *

Conclusion

And there you have it, the rm command which is used to removes each given FILE Debian Linux operating systems. Please note that rm does not remove directories by default. You need to pass the -r option as described above. To see all available options for the rm command type the following man command:
rm --help
OR
man rm

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.