How to Delete Read-Only Files on Linux

There are several ways to help you delete files read-only on Linux. If it's a file you own, you can change the file permissions or use the sudo command to delete the original file. However, if you cannot delete files with write permissions (or see a 'Read Only File System' error message), you need to change the drive with the appropriate permissions. This article will show you how to delete read-only files on any Linux distribution, including Ubuntu and Linux Mint.

Change file permissions

Picture 1 of How to Delete Read-Only Files on Linux

Open a terminal window. If a file that you own is set to read-only, you cannot delete the file unless you manually set it to write permission. If you use a window manager, press Ctrl + Alt + T to open a terminal window now.

Use this method if you want to delete a file that you own but do not have permission to delete (for example, if you are the owner or in a group with permission to read the file).

If you are logged in remotely and are viewing the command console, proceed to the next step.

Picture 2 of How to Delete Read-Only Files on Linux

Use cd to enter the directory with the file to be deleted. For example, if the file you want to delete is stored in the documents folder of the 'home' folder, type cd documents or cd /home/username/documents.

Picture 3 of How to Delete Read-Only Files on Linux

Use ls -al to see the permissions of the files in the directory. Using ls -l shows you a list of files in the directory, along with the owners of each file and their permissions. Adding a to ls -l also shows you hidden files and folders.

Picture 4 of How to Delete Read-Only Files on Linux

View the permissions of the file you want to delete. Permissions appear before the filename, such as: r--r--r--. The owner's name is shown at the back, followed by the group name.

r is read, w is write, and x is execute.

The first three characters in the access permission (for example r--) are the access permission of the file owner. So, in this example, the file owner has read permission, but not write, execute, or delete permission to the file.

The next three characters are group access. If you are a member of a group and that group has write permission to the file, you can delete the file even if you are not the owner.

The last three characters are universal access, which is meant for everyone.

Picture 5 of How to Delete Read-Only Files on Linux

Use chmod -v u+rw filename to give you read and write permissions. You can omit r if you already have read permission. Once you have write permission, you can delete the file.

If you are not the owner of the file but have root access to the system, you can use sudo chmod -v u+rw the filename to give you the proper permissions.

To see the file's new permissions, use the ls -al command again.

Picture 6 of How to Delete Read-Only Files on Linux

Use rm filename to delete the file. You now have write permissions and can delete files.

If you can't delete the file after gaining write access, perhaps the partition is read-only. Learn how to fix the read-only filesystem to fix the problem.

Using sudo

Picture 7 of How to Delete Read-Only Files on Linux

Open a terminal window. You can use the sudo command to delete read-only files that you cannot delete with your user account. If you want to use the window management tool, press Ctrl + Alt + T to open a terminal window right away.

Use this method if you are not the owner of the file to be deleted and you want to delete the file instead of changing the permissions.

If you are logged in remotely and are viewing the command console, proceed to the next step.

Picture 8 of How to Delete Read-Only Files on Linux

Use cd to enter the directory of the file you want to delete. For example, if the file you want to delete is stored in the documents folder of the 'home' folder, you would use the command cd documents or cd /home/username/documents.

Picture 9 of How to Delete Read-Only Files on Linux

Use ls -al to view the contents of the directory. This command will display all files in the current directory, along with the owner name and permissions of each file.

Picture 10 of How to Delete Read-Only Files on Linux

Use sudo rm filename to delete the file. You will be asked for a password to use the highest level of access. Once the password is accepted, the read-only file will be deleted.

If you can't delete the file with sudo, the partition is probably read-only. Learn how to fix the read-only filesystem to fix the problem.

Fix read-only file system

Picture 11 of How to Delete Read-Only Files on Linux

Open a terminal window. There are several reasons why you might get the error rm: cannot remove '(filename)' : Read only file system when you want to delete a file. Start by pressing Ctrl + Alt + T to open a terminal window if you use a window management tool.

If you are logging in remotely, go to the next step.

Picture 12 of How to Delete Read-Only Files on Linux

Use df -h to see all mounted devices. You need to know the exact mount point of the drive that is causing you problems. This shows you the entire mounted drive.

Picture 13 of How to Delete Read-Only Files on Linux

Use the mount command. Replace the mount point with the actual mount point, such as /media/usbdisk. If you want to delete files on a USB or network drive, such as old backup data, the drive where the file is stored will be mounted with read-only access.

If you see ro in the results, the file system is read-only. Mounting the drive should fix the problem. To do this, you would use the command mount -o remount,rw the mount point. You can delete the file immediately afterwards.

If the access is rw, the drive is mounted with read-write access, and you can delete files without problems. This usually means that there is a problem with the file system on the drive. Let's continue with the remaining steps.

If you see remount-ro, the filesystem is in trouble and the drive is mounted with read-only access to limit damage. It also means that there is a problem with the file system on the drive. Let's continue with the remaining steps.

Picture 14 of How to Delete Read-Only Files on Linux

If access is set correctly, unmount the drive. To do this, you would use the command sudo umount device . Replace device with the device name, such as /dev/sdd1.

Since you cannot unmount the root filesystem, you cannot check the root partition's filesystem unless you are booted into recovery mode. If the error occurs on the root filesystem, boot into recovery mode first.

Picture 15 of How to Delete Read-Only Files on Linux

Use the command sudo fsck -n device to check the drive. This is an operation that checks the integrity of the drive without making any further changes. If the drive is working properly, you should see "clean" in the results. If the drive has an error, the information will also be displayed here.

If you see errors, back up the drive before continuing in case the filesystem cannot be repaired.

Picture 16 of How to Delete Read-Only Files on Linux

Use the command sudo fsck device to fix the error. If there are errors, you will be asked to correct them.

Even if you can fix the error, back up your data in case the drive fails.

Picture 17 of How to Delete Read-Only Files on Linux

Mount the drive after fixing the error. If you can fix the error, mounting the drive will assist you in deleting the file. Use mount -o remount,rw mount point to make sure the drive is mounted with read and write access. You can edit and delete files on the drive immediately afterwards.

Update 29 April 2022
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile