Short answer:
Don't do that. If a file or directory is in a location that your user doesn't have access to without sudo
, there's a good reason for it, and a normal user shouldn't change the file without understanding the consequences.
Explanation:
As mentioned in the comments, your regular user should not have permission to access "system files", to attempt to reduce the chance of accidental damage to the system as well as intentional damage (from malware).
If a command is requiring sudo
, then it's likely that you are in a location where it's not safe for you to be creating or deleting files or directories.
For instance:
cd /
mkdir project
This would require sudo
, since the root folder of the system is off-limits. Yes, this is different from Windows, where a normal user can create directories at the root level of the drive.
There are very few places that a Linux system considers it safe for a normal user to change files:
Your home directory, also known as ~
, and typical at /home/<username>
. You have full permissions here.
In /tmp
, you are allowed to create files/directories. Your user will be the owner of these files. You may then change (including delete) the files that you own. However, you may not change other users' files in this directory.
For the most part, files are created in the /tmp
directory by applications (processes) that need temporary files (similar to C:\Users\<youruser>\AppData\Local\Temp
in Windows). You really shouldn't need to create files there yourself.
In addition to those two, WSL has additional locations that your normal Ubuntu user will be able to change. By default, your user will own the /mnt/c/
mount (and other drives) that WSL creates for you to access Windows drives.
This means that you can (but probably shouldn't):
cd /mnt/c/
mkdir me_my_and_i
However, the fact that your Ubuntu user has Linux permissions to files on this drive doesn't mean that you have unlimited access. WSL is still a Windows application, running with your Windows user's permissions. So you cannot (even with sudo
) change files in (for instance) C:\Windows\
and other sensitive (system) locations.
This prevents an accidental or malicious command in Ubuntu from destroying your Windows installation. Please note that you can still do a massive amount of damage to your Windows installation even with these protections in place. But the system will at least still be bootable.
Going back to the topic of the Ubuntu locations, you should understand that you can create new directories (using sudo
once) in places you really shouldn't and then give your regular user ownership of that directory. With that in place, you would have created a new location where your user has permissions to change files without needing sudo
. For instance:
# Not recommended, for example only
cd /
# won't work, permission denied:
mkdir me_myself_and_i
# Use sudo:
sudo mkdir me_myself_and_i
sudo chown <yourusername>:<yourusername> me_myself_and_i
ls -l
# Notice that the directory is owned by you - All others are owned by root
cd my_myself_and_i
# Works *without* sudo:
touch 123
rm 123
However, if you try:
cd /
rmdir me_myself_and_i
You will need sudo
. Even though you own me_myself_and_i
, removing it is a change to the root /
directory, which root
still owns (and should).
Conversely, you can even create a directory (or file) inside your own home directory that your user can't access:
cd ~
sudo mkdir me_myself_and_i
cd me_myself_and_i
touch 123
This will result in Permission denied
, since root
owns the directory that you just created.
However, your normal user can delete the newly created (root
-owned) directory (as long as it is empty), since you still own your home (the parent) directory.