Command to Delete a file or a Directory in Linux / UNIX

How to delete a file or a directory in Linux / UNIX operating system using command line?

rm command is used to remove a file or directory under Linux/Unix like operating system

Synopsis

rm [OPTION]… FILE…

1.) Delete/Remove a single file

rm file

This command will ask for your confirmation to delete the file. Press y for yes and n for no.

2.) Delete Empty Directory

rmdir directoryname

3.) Delete Directory with all subdirectories and files

rm -r directoryname

Be careful use -r option only when you want to delete all files and subdirectories under a directory.

If you don’t want to receive a confirmation for each file, then use the following command instead.

rm -rf directoryname

In the above example, the “directoryname” directory, along with all files and subdirectories, would be deleted without any prompt or message.

Add Comment