How to Delete Files in CentOS 7

In this CentOS 7 tutorial we are going to learn how to delete files in CentOS 7.

In CentOS 7 rm command use to delete files and folders. The rm command also use in all Unix like operating systems to delete files.

rm filename

If you do not specify the absolute path, rm command will look for the file inside the current working directory or you can give the absolute path to the file you want to delete.

rm /home/user/file1

Be careful when you remove files in CentOS using rm command, because once you delete the file, there is no command to undo it.

So the safest way is to use -i option which will ask for the confirmation before removing the file.

rm -i filename

In fact, this is the default behaviour in CentOS 7 when deleting files, the rm command in CentOS 7 will ask for the confirmation even if you did not use the -i option.

If you do not want to prompt before deleting the files, Then use the -f option.

rm -f filename

This time rm command will not prompt for the confirmation.

Delete Multiple Files in CentOS 7

We can use the rm command to delete multiple files at the same time.

rm file1 file2 file3 file4

The above command will delete file1, file2, file3 and file4

How to Delete a Directory in CentOS 7

The rm command also use to Delete directories in CentOS 7, But you need to use -r and -f option to delete directories.

rm -rf folder1

The -f options is optional, but if you do not use -f option, rm command will prompt for the confirmation before removing every file and folders inside the given Directory.

Example - Delete Files in CentOS 7 using rm command

rm file1

Remove the file1 inside the current working directory.

How to Delete Files in CentOS 7

rm /var/www/html/index.html

As per the above example, rm command will remove the index.html file inside the /var/www/html/ directory.

rm file1 file2 file3

As per the above example, rm command in CentOS will remove file1, file2, and file3.

rm -rf folder1

The rm command will delete the folder1

How to Delete a Directory in CentOS 7

Summary - Delete Files and Directories in CentOS using rm command

  • In CentOS Linux, rm command use to delete files and directories.
  • If -i option is used, the rm command will prompt for the confirmation before deleting the file (Which is the default behaviour on CentOS 7).
  • If you want to delete a directory in CentOS use the -r option with the rm command.