Minimum file permission needed to delete a file in Linux - linux

To delete a file in Linux,
What minimal permissions do we need to set on it?
What minimal permissions do we need to set on its parent directory?

You need to have x-permissions and w-permissions on the directory (and of course x-permissions on all parents), that is all.
mkdir foo
touch foo/bar
chmod 300 foo
chmod 000 foo/bar
rm -f foo/bar
But when testing you might stumble into thinking that you need more (e. g. r-permissions for the directory or whatever). This will be only due to false testing ;-)

Related

Is it possible to change directory mode so that no one can create new files in it?

Say that I have the folder /dir. Is there a command in bash that I can use so that after performing it, no one could create new files in /dir?
Edit: important to mention that after performing the command, there will be same permissions to the directory files as they were before. For example, if I have folder /dir with file a.txt in it - so after I use my desired command I cant create new files, but I can modify/delete a.txt if I want.
you could change the permission with chmod to only let people read the folders content.
chmod a-w /dir
Will remove all write permissions of all (ugo), but keep x (execute) and r (read) permissions intact.
Yes, it's pretty simple. Just chmod to read only. Here is a sample
chmod -R 0444 /path/to/your/dir/
Where the last 3 4's mean User-readonly, Group-readonly and others-readonly respectively.

Linux - Change permissions of all files & directories except 1 directory?

This has stumped me for a bit now after working on it for a while. I need to change the permission settings of all files and directories so that group and others have no read, write, or execute access except for one specific directory. So far I have tried variations of `
chmod go-rwx | chmod go+rwx 'filename'
Any tips?`
I don't believe there's a solution shorter than a 2 steps pipeline.
Otherwise, you might want to check umask.
umask changes the default permissions on a file on creation (sadly only on file created by the same shell, but you can add this to the .bash_rc/.bash_profile, so that every shell you'll open will do this by default)
umask 077
will set the mask so that every new file will have permissions like 700.
So you might want to just use this so that in the future you won't need to re-launch that pipeline.
EDIT
if the problem was the pipeline itself, then I'd do
cd && chmod -R 700 && cd 'path/to/that/directory' && chmod -R 777
with the double '&' instead of the pipe just because there's not output to pipe, so an && might do the trick

Linux folder move access

Is a Linux file system able to allow or deny the right to move a folder? Active Directory does not, as far as I can tell. I'm curious, and 3 different wordings generated no results.
Yes. If you have a folder foo/bar/, you could make the folder foo read-only, which would prevent people from moving bar:
$ chmod a-w foo
$ mv foo/bar ack
mv: cannot move ‘foo/bar’ to ‘ack’: Permission denied
The can't move bar, but they can still change what's inside of it:
$ echo hello > foo/bar/hello.txt
$ rm foo/bar/hello.txt
In linux you can use chattr to make a file or folder immutable like so:
chattr +i file
This way, not even the super can move, modify or delete the file.
To revert it you can use:
chattr -i file
This works for on ext filesystems

giving rw permission to subfolders of foo folder in linux

How can I give highest and lowest permission to all sub folders and files including in a folder?
I used "chmod +x" for a certain file but I don't know how to do this for all folders and files which are in the folder.
The lock means that you don't have write access to the files in question. My guess is that you pulled your images off a cd or something, and that their ownerships/permissions are jumbled up. You may want to straighten things up. In a terminal:
$ cd /path/to/files/
$ su -c 'chown *.jpg'
$ "root's password"
$ chmod 644 *.jpg
I hope you'll enjoy Debian!

Remove a symlink to a directory

I have a symlink to an important directory. I want to get rid of that symlink, while keeping the directory behind it.
I tried rm and get back rm: cannot remove 'foo'.
I tried rmdir and got back rmdir: failed to remove 'foo': Directory not empty
I then progressed through rm -f, rm -rf and sudo rm -rf
Then I went to find my back-ups.
Is there a way to get rid of the symlink with out throwing away the baby with the bathwater?
# this works:
rm foo
# versus this, which doesn't:
rm foo/
Basically, you need to tell it to delete a file, not delete a directory. I believe the difference between rm and rmdir exists because of differences in the way the C library treats each.
At any rate, the first should work, while the second should complain about foo being a directory.
If it doesn't work as above, then check your permissions. You need write permission to the containing directory to remove files.
use the "unlink" command and make sure not to have the / at the end
$ unlink mySymLink
unlink() deletes a name from the file system. If that name was the last link to a file and no processes have the file open the file is deleted and the space it was using is made available for reuse.
If the name was the last link to a file but any processes still have the file open the file will remain in existence until the last file descriptor referring to it is closed.
I think this may be problematic if I'm reading it correctly.
If the name referred to a symbolic link the link is removed.
If the name referred to a socket, fifo or device the name for it is removed but processes which have the object open may continue to use it.
https://linux.die.net/man/2/unlink
rm should remove the symbolic link.
skrall#skrall-desktop:~$ mkdir bar
skrall#skrall-desktop:~$ ln -s bar foo
skrall#skrall-desktop:~$ ls -l foo
lrwxrwxrwx 1 skrall skrall 3 2008-10-16 16:22 foo -> bar
skrall#skrall-desktop:~$ rm foo
skrall#skrall-desktop:~$ ls -l foo
ls: cannot access foo: No such file or directory
skrall#skrall-desktop:~$ ls -l bar
total 0
skrall#skrall-desktop:~$
Use rm symlinkname but do not include a forward slash at the end (do not use: rm symlinkname/). You will then be asked if you want to remove the symlink, y to answer yes.
Assuming it actually is a symlink,
$ rm -d symlink
It should figure it out, but since it can't we enable the latent code that was intended for another case that no longer exists but happens to do the right thing here.
If rm cannot remove a symlink, perhaps you need to look at the permissions on the directory that contains the symlink. To remove directory entries, you need write permission on the containing directory.
Assuming your setup is something like: ln -s /mnt/bar ~/foo, then you should be able to do a rm foo with no problem. If you can't, make sure you are the owner of the foo and have permission to write/execute the file. Removing foo will not touch bar, unless you do it recursively.
I also had the same problem. So I suggest to try unlink <absolute path>.
For example unlink ~/<USER>/<SOME OTHER DIRECTORY>/foo.
On CentOS, just run rm linkname and it will ask to "remove symbolic link?". Type Y and Enter, the link will be gone and the directory be safe.
I had this problem with MinGW (actually Git Bash) running on a Windows Server. None of the above suggestions seemed to work. In the end a made a copy of the directory in case then deleted the soft link in Windows Explorer then deleted the item in the Recycle Bin. It made noises like it was deleting the files but didn't. Do make a backup though!
you can use unlink in the folder where you have created your symlink
If rm cannot remove a link, perhaps you need to look at the permissions on the directory that contains the link. To remove directory entries, you need write permission on the containing directory.

Resources