Can you delete the file?

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.

What is deleting a file?

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.

Can you erase the contents of the file?

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.

Can you erase the file path?

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.

View the contents of the directory

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.

Summary

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.

Confirmation environment

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

Can you delete the file?
Delete the substring
Until you can read the error log
Run unit tests the moment you save the file
Until you can use the Google Speech API
In bash, "Delete the file if it exists".
You can read the analog meter with the example MNIST.
You can read the analog meter with the example MNIST.
[Python] You can save an object to a file by using the pickle module.
Download the file in Python
Can you solve sports scheduling?
Introducing sites where you can see trends in the 2019 framework
What you can do with the Python standard library statistics
Unzip the internet zip file
File access under the directory
Parse the response so you can scrape Wantedly's own story
Try and learn iptables, until you can browse the web
Consider what you can do with Python from the Qiita article
You can also use virtualenv from the IntelliJ IDEA Python plugin
"CheckiO" where you can study Python while solving problems
Introducing sites where you can see trends in the 2019 framework
Can you delete the file?
Kalman filter that you can understand
Extract the xz file with python
Save the binary file in Python
Follow the file hierarchy with fts
"Obake" can be sung by "you"
Simply view the Jupyter notebook file
Download the file deployed with appcfg.py
The story of the "hole" in the file
The minimum Cement information you need
Extract the targz file using python
Open the file with the default app
If you ride the Titanic ... die.
Split the pyramid framework ini file
Get the file path using Pathlib
Python | What you can do with Python
Delete tweets for the specified period
If you are told cannot by Python import, review the file name
You can see the transition of points in the J League on the graph!
Read the file with python and delete the line breaks [Notes on reading the file]
Starting with Django 1.8, you can choose Jinja2 as the template engine #djangoja