Cannot remove file that I am the owner of

I am using both Cygwin and WSL (Ubuntu) on Windows. I ran a command that created a file in Cygwin, and then when I view the same file in WSL, it says that I own it and the directory containing it, and that I have all permissions to it. However, when I try to delete it, I get permission denied

> sudo rm file1.txt
rm: cannot remove 'file1.txt': Permission denied
> whoami
jeff
> ls -la
total 4
drwxrwxrwx 1 jeff jeff 512 Dec 1 14:55 .
drwxrwxrwx 1 jeff jeff 512 Dec 1 14:55 ..
-rwxrwxrwx 0 jeff jeff 999 Dec 1 14:40 file1.txt
> cd ..
> ls -la
total 0
drwxrwxrwx 1 jeff jeff 512 Dec 1 14:55 .
drwxrwxrwx 1 jeff jeff 512 Dec 1 14:55 ..
drwxrwxrwx 1 jeff jeff 512 Dec 1 14:55 folder1

I have tried running rm with and without sudo, removing the containing folder, and disowning then owning the file and parent folder again, what else can I try, assuming that using Cygwin or Windows to remove the file is not an option?

15

1 Answer

The information in the comments shows a potentially major problem. For starters, please remember to edit your question with the information you provided in the comments. That's critical information to anyone understanding your issue that should be a part of the question, rather than the comments. (Feel free to edit this answer to remove the italicized area once the question has been updated).

Now on to the likely problem ...

Danger! Danger! As @Jeffmagma said in the comments, this doesn't look like WSL2. Since the file is exposed in C:\wsl\rootfs (visible to Cygwin at /cygdrive/c/wsl/rootfs), that looks to be a WSL1 filesystem rather than a WSL2 virtual drive (which would show up as an ext4.vhdx).

And that falls into this Microsoft Warning:

There is one hard-and-fast rule when it comes to WSL on Windows:

DO NOT, under ANY circumstances, access, create, and/or modify Linux files inside of your %LOCALAPPDATA% folder using Windows apps, tools, scripts, consoles, etc.

Creating/changing Linux files in your Appdata folder from Windows will likely result in data corruption and/or damage your Linux environment requiring you to uninstall & reinstall your distro!

... which is actually a bit poorly worded. It references "AppData", but that's the default location for WSL installations.

In this case, it looks like you've created a WSL1 instance at C:\WSL. There's nothing at all wrong with that, but Microsoft's warning still applies. Attempting to create, update, or delete a file using a Windows application (e.g. Cygwin) in that path is almost certain to cause issues.

And unfortunately it sounds like it may very well have caused filesystem corruption in this case.

To attempt recovery, I would try:

  • Another wsl --export of this WSL1 instance
  • Then wsl --import it into a new instance. I'm assuming you are familiar with this process since it's likely what you used to create the instance at C:\wsl\ in the first place. I would add --version 2 to the end of the --import command, personally.
  • See if (a) the problem-file exists in the new instance (it may be culled by the export, actually), and (b) if it does exist, see if you can delete it.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like