When you need to delete an unnecessary file on the server, when you ask "What permissions do you need to delete the file?", Some people make a mistake.
Let's see the correct answer to that question and why.
About the operation of deleting the file
Consider the two patterns of. When I say "delete a file", I usually think of the latter, but when I ask for that condition, I often make the mistake of answering the latter condition.
First, let's clarify the expected value of the state obtained by each operation.
If you erase the contents of the file, the expected value is that the contents of the file are empty:
$ ls -s path/to/file
0 path/to/file
$ wc -c path/to/file
0 path/to/file
$ file path/to/file
path/to/file: empty
On the other hand, if you delete the file path, the expected value is that you can no longer access the file with the existing path:
$ ls path/to/file
ls: cannot access 'path/to/file': No such file or directory
$ cat path/to/file
cat: path/to/file: No such file or directory
Specific operation examples to achieve each are shown:
Let's look at the relationship between permissions and whether these operations are possible.
You need write permission on the file to erase the contents of the file. If you remove the write permission
$ cat path/to/file
hello
$ ls -li path/to/file
161504 -rw-r--r-- 1 yoichinakayama yoichinakayama 6 May 31 11:55 path/to/file
$ chmod -w path/to/file
$ ls -li path/to/file
161504 -r--r--r-- 1 yoichinakayama yoichinakayama 6 May 31 11:55 path/to/file
$ echo -n > path/to/file
-bash: path/to/file: Permission denied
And the contents of the file cannot be erased. With write permission
$ chmod +w path/to/file
$ ls -li path/to/file
161504 -rw-r--r-- 1 yoichinakayama yoichinakayama 6 May 31 11:55 path/to/file
$ echo -n > path/to/file
$ ls -li path/to/file
161504 -rw-r--r-- 1 yoichinakayama yoichinakayama 0 May 31 11:56 path/to/file
$ wc -c path/to/file
0 path/to/file
$ file path/to/file
path/to/file: empty
And the contents of the file can be erased. Because you're editing the contents of a file, you need write permissions for that file.
You need write permission on the parent directory to erase the file path. If you remove the write permission
$ ls -l path/to/file
-rw-r--r-- 1 yoichinakayama yoichinakayama 0 May 31 12:00 path/to/file
yoichinakayama@penguin:~$ ls -ld path/to
drwxr-xr-x 1 yoichinakayama yoichinakayama 8 May 31 11:55 path/to
yoichinakayama@penguin:~$ chmod -w path/to
yoichinakayama@penguin:~$ ls -ld path/to
dr-xr-xr-x 1 yoichinakayama yoichinakayama 8 May 31 11:55 path/to
yoichinakayama@penguin:~$ rm path/to/file
rm: cannot remove 'path/to/file': Permission denied
And the file path cannot be deleted. With write permission
$ chmod +w path/to
$ ls -ld path/to
drwxr-xr-x 1 yoichinakayama yoichinakayama 8 May 31 11:55 path/to
$ rm path/to/file
$ ls path/to/file
ls: cannot access 'path/to/file': No such file or directory
And the file path can be erased.
To find out why you needed write permissions on a directory to delete a file, look at what information is stored in the directory. In the following C language program, if you give the path of the directory as an argument, the contents of that directory will be output, so use that.
$ cat sample.c
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
DIR *dir = opendir(argv[1]);
struct dirent *ent;
if (dir == NULL) {
return 1;
}
while ((ent = readdir(dir)) != NULL) {
printf("d_ino=%d, d_name=%s\n", ent->d_ino, ent->d_name);
}
closedir(dir);
return 0;
}
$ gcc sample.c
First, try running it with the files in the directory.
$ touch path/to/file
$ ./a.out path/to
d_ino=161394, d_name=.
d_ino=161393, d_name=..
d_ino=161598, d_name=file
$ ls -li path/to/file
161598 -rw-r--r-- 1 yoichinakayama yoichinakayama 0 May 31 12:14 path/to/file
You can see that the inode number of the target file and the file name "file" are stored in the directory.
Next, let's see what happens when you delete the file path.
$ rm path/to/file
$ ./a.out path/to
d_ino=161394, d_name=.
d_ino=161393, d_name=..
The dirent structure stored in the directory has been reduced by one. That is, the directory has changed. In this example, the operation of deleting the file path path / to / file is the operation of changing the contents of the directory (inode number 161394). Therefore, you need write permission for that directory.
What are you editing with that operation? If you are aware of what information is stored where, you can understand what permissions are required when erasing the contents of a file and when erasing the file path.
I checked it on the Chromebook terminal.
$ uname -a
Linux penguin 4.19.113-08528-g5803a1c7e9f9 #1 SMP PREEMPT Thu Apr 2 15:16:47 PDT 2020 aarch64 GNU/Linux
$ gcc --version
gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Recommended Posts