I investigated what permissions I needed to delete linux files.

File permissions for linux are difficult, aren't they? I've investigated what permissions I need to delete a file, so I'll leave a note.

First, create a verification tool to use.

clear_folder.sh


#!/usr/bin/env bash -Ceuo pipefail

for ii in 640 641 642 643 644 645 646 647
do
    rm -rf ${ii}
done

create_folder.sh


#!/usr/bin/env bash -Ceuo pipefail

for ii in 640 641 642 643 644 645 646 647
do
    mkdir -m ${ii} ${ii} && cd ${ii}
    for kk in 640 641 642 643 644 645 646 647
    do
        touch ${kk}
        chmod ${kk} ${kk}
    done
    cd ..
done

check-f.sh


#!/usr/bin/env bash -Ceuo pipefail

for ii in 640 641 642 643 644 645 646 647
do
    for kk in 640 641 642 643 644 645 646 647
    do
        if [[ -f ${ii}/${kk} ]]; then
            echo ${ii}/${kk}
        fi
    done
done

check-rm-f.sh


#!/usr/bin/env bash -Ceuo pipefail

for ii in 640 641 642 643 644 645 646 647
do
    for kk in 640 641 642 643 644 645 646 647
    do
        rm -f ${ii}/${kk} && echo ${ii}/${kk}
    done
done

Create folders and files (root)

id
uid=0(root) gid=0(root) groups=0(root)

sh create_folder.sh

ls -al
drwxrwxrwx.10 root root 199 August 30 00:00 .
dr-xr-xr-x.18 root root 235 August 29 23:19 ..
drw-r-----.2 root root 94 August 30 00:00 640
drw-r----x.2 root root 94 August 30 00:00 641
drw-r---w-.2 root root 94 August 30 00:00 642
drw-r---wx.2 root root 94 August 30 00:00 643
drw-r--r--.2 root root 94 August 30 00:00 644
drw-r--r-x.2 root root 94 August 30 00:00 645
drw-r--rw-.2 root root 94 August 30 00:00 646
drw-r--rwx.2 root root 94 August 30 00:00 647

find . -type f -ls
52669665    0 -rw-r-----1 root root 0 August 30 00:00 ./640/640
52669666    0 -rw-r----x 1 root root 0 August 30 00:00 ./640/641
52669667    0 -rw-r---w-1 root root 0 August 30 00:00 ./640/642
52669669    0 -rw-r---wx 1 root root 0 august 30 00:00 ./640/643
52669670    0 -rw-r--r--1 root root 0 August 30 00:00 ./640/644
52669671    0 -rw-r--r-x 1 root root 0 August 30 00:00 ./640/645
52669672    0 -rw-r--rw-1 root root 0 August 30 00:00 ./640/646
52669675    0 -rw-r--rwx 1 root root 0 august 30 00:00 ./640/647
 1273542    0 -rw-r-----1 root root 0 August 30 00:00 ./641/640
 1273543    0 -rw-r----x 1 root root 0 August 30 00:00 ./641/641
 1273544    0 -rw-r---w-1 root root 0 August 30 00:00 ./641/642
 1273545    0 -rw-r---wx 1 root root 0 august 30 00:00 ./641/643
 1273546    0 -rw-r--r--1 root root 0 August 30 00:00 ./641/644
 1273547    0 -rw-r--r-x 1 root root 0 August 30 00:00 ./641/645
 1273548    0 -rw-r--rw-1 root root 0 August 30 00:00 ./641/646
 1273549    0 -rw-r--rwx 1 root root 0 august 30 00:00 ./641/647
17935011    0 -rw-r-----1 root root 0 August 30 00:00 ./642/640
17935012    0 -rw-r----x 1 root root 0 August 30 00:00 ./642/641
17935013    0 -rw-r---w-1 root root 0 August 30 00:00 ./642/642
17935014    0 -rw-r---wx 1 root root 0 august 30 00:00 ./642/643
17935015    0 -rw-r--r--1 root root 0 August 30 00:00 ./642/644
17935016    0 -rw-r--r-x 1 root root 0 August 30 00:00 ./642/645
17935017    0 -rw-r--rw-1 root root 0 August 30 00:00 ./642/646
17935018    0 -rw-r--rwx 1 root root 0 august 30 00:00 ./642/647
33755963    0 -rw-r-----1 root root 0 August 30 00:00 ./643/640
33755966    0 -rw-r----x 1 root root 0 August 30 00:00 ./643/641
33755967    0 -rw-r---w-1 root root 0 August 30 00:00 ./643/642
33755968    0 -rw-r---wx 1 root root 0 august 30 00:00 ./643/643
34138697    0 -rw-r--r--1 root root 0 August 30 00:00 ./643/644
34138698    0 -rw-r--r-x 1 root root 0 August 30 00:00 ./643/645
34138699    0 -rw-r--rw-1 root root 0 August 30 00:00 ./643/646
34138700    0 -rw-r--rwx 1 root root 0 august 30 00:00 ./643/647
52669678    0 -rw-r-----1 root root 0 August 30 00:00 ./644/640
52669679    0 -rw-r----x 1 root root 0 August 30 00:00 ./644/641
52669680    0 -rw-r---w-1 root root 0 August 30 00:00 ./644/642
52669681    0 -rw-r---wx 1 root root 0 august 30 00:00 ./644/643
52669682    0 -rw-r--r--1 root root 0 August 30 00:00 ./644/644
52669683    0 -rw-r--r-x 1 root root 0 August 30 00:00 ./644/645
52669684    0 -rw-r--rw-1 root root 0 August 30 00:00 ./644/646
52669685    0 -rw-r--rwx 1 root root 0 august 30 00:00 ./644/647
 1273551    0 -rw-r-----1 root root 0 August 30 00:00 ./645/640
 1273552    0 -rw-r----x 1 root root 0 August 30 00:00 ./645/641
 1273553    0 -rw-r---w-1 root root 0 August 30 00:00 ./645/642
 1273554    0 -rw-r---wx 1 root root 0 august 30 00:00 ./645/643
 1273555    0 -rw-r--r--1 root root 0 August 30 00:00 ./645/644
 1273556    0 -rw-r--r-x 1 root root 0 August 30 00:00 ./645/645
 1273557    0 -rw-r--rw-1 root root 0 August 30 00:00 ./645/646
 1273558    0 -rw-r--rwx 1 root root 0 august 30 00:00 ./645/647
17935020    0 -rw-r-----1 root root 0 August 30 00:00 ./646/640
17935021    0 -rw-r----x 1 root root 0 August 30 00:00 ./646/641
17935022    0 -rw-r---w-1 root root 0 August 30 00:00 ./646/642
17935023    0 -rw-r---wx 1 root root 0 august 30 00:00 ./646/643
17935024    0 -rw-r--r--1 root root 0 August 30 00:00 ./646/644
17935025    0 -rw-r--r-x 1 root root 0 August 30 00:00 ./646/645
17935026    0 -rw-r--rw-1 root root 0 August 30 00:00 ./646/646
17935027    0 -rw-r--rwx 1 root root 0 august 30 00:00 ./646/647
34670676    0 -rw-r-----1 root root 0 August 30 00:00 ./647/640
35401047    0 -rw-r----x 1 root root 0 August 30 00:00 ./647/641
35401048    0 -rw-r---w-1 root root 0 August 30 00:00 ./647/642
35401051    0 -rw-r---wx 1 root root 0 august 30 00:00 ./647/643
35401052    0 -rw-r--r--1 root root 0 August 30 00:00 ./647/644
35401060    0 -rw-r--r-x 1 root root 0 August 30 00:00 ./647/645
35401061    0 -rw-r--rw-1 root root 0 August 30 00:00 ./647/646
35401062    0 -rw-r--rwx 1 root root 0 august 30 00:00 ./647/647

Validate file existence check -f that is always used with sh (general user)

id
uid=1001(koryo) gid=1001(koryo) groups=1001(koryo)

sh check-f.sh
641/640
641/641
641/642
641/643
641/644
641/645
641/646
641/647
643/640
643/641
643/642
643/643
643/644
643/645
643/646
643/647
645/640
645/641
645/642
645/643
645/644
645/645
645/646
645/647
647/640
647/641
647/642
647/643
647/644
647/645
647/646
647/647

If you look closely, you should see all the files under the 641, 643, 645, 647 folders. That is:

Conclusion 1: If you have x permission in the file folder, you can check the existence of the file. Even if the file itself has no permissions.

What happens when I use rm in sh to delete a file? (General user)

id
uid=1001(koryo) gid=1001(koryo) groups=1001(koryo)

sh check-rm-f.sh
rm:	`640/640'Cannot be deleted: No permission
rm:	`640/641'Cannot be deleted: No permission
rm:	`640/642'Cannot be deleted: No permission
rm:	`640/643'Cannot be deleted: No permission
rm:	`640/644'Cannot be deleted: No permission
rm:	`640/645'Cannot be deleted: No permission
rm:	`640/646'Cannot be deleted: No permission
rm:	`640/647'Cannot be deleted: No permission
rm:	`641/640'Cannot be deleted: No permission
rm:	`641/641'Cannot be deleted: No permission
rm:	`641/642'Cannot be deleted: No permission
rm:	`641/643'Cannot be deleted: No permission
rm:	`641/644'Cannot be deleted: No permission
rm:	`641/645'Cannot be deleted: No permission
rm:	`641/646'Cannot be deleted: No permission
rm:	`641/647'Cannot be deleted: No permission
rm:	`642/640'Cannot be deleted: No permission
rm:	`642/641'Cannot be deleted: No permission
rm:	`642/642'Cannot be deleted: No permission
rm:	`642/643'Cannot be deleted: No permission
rm:	`642/644'Cannot be deleted: No permission
rm:	`642/645'Cannot be deleted: No permission
rm:	`642/646'Cannot be deleted: No permission
rm:	`642/647'Cannot be deleted: No permission
643/640
643/641
643/642
643/643
643/644
643/645
643/646
643/647
rm:	`644/640'Cannot be deleted: No permission
rm:	`644/641'Cannot be deleted: No permission
rm:	`644/642'Cannot be deleted: No permission
rm:	`644/643'Cannot be deleted: No permission
rm:	`644/644'Cannot be deleted: No permission
rm:	`644/645'Cannot be deleted: No permission
rm:	`644/646'Cannot be deleted: No permission
rm:	`644/647'Cannot be deleted: No permission
rm:	`645/640'Cannot be deleted: No permission
rm:	`645/641'Cannot be deleted: No permission
rm:	`645/642'Cannot be deleted: No permission
rm:	`645/643'Cannot be deleted: No permission
rm:	`645/644'Cannot be deleted: No permission
rm:	`645/645'Cannot be deleted: No permission
rm:	`645/646'Cannot be deleted: No permission
rm:	`645/647'Cannot be deleted: No permission
rm:	`646/640'Cannot be deleted: No permission
rm:	`646/641'Cannot be deleted: No permission
rm:	`646/642'Cannot be deleted: No permission
rm:	`646/643'Cannot be deleted: No permission
rm:	`646/644'Cannot be deleted: No permission
rm:	`646/645'Cannot be deleted: No permission
rm:	`646/646'Cannot be deleted: No permission
rm:	`646/647'Cannot be deleted: No permission
647/640
647/641
647/642
647/643
647/644
647/645
647/646
647/647

The following files have been deleted: (all files under 643,647) 643/640,641,642,643,644,645,646,647 647/640,641,642,643,644,645,646,647

Conclusion 2: If the file folder has w permission, it can be deleted even if the file does not have permission.

that's all

Recommended Posts

I investigated what permissions I needed to delete linux files.
What I did to get started with Linux commands
I tried to reintroduce Linux
What I learned about Linux
What I was addicted to when dealing with huge files in a Linux 32bit environment
[Linux] How to subdivide files and folders
I want to know how LINUX works!
I want to use Linux on mac
[linux] Split files to the specified size
Don't use rm command to delete files
How to find large files on Linux
What I always add to my ~ / .bashrc
What to do after installing Linux (Ubuntu)
What I was addicted to Python autorun
What I did to save Python memory
Linux permissions
I tried to synthesize WAV files using Pydub.
I want to visualize csv files using Vega-Lite!
What I did when updating from Python 2.6 to 2.7
I tried to operate Linux with Discord Bot
I want to log file I / O on Linux
I want to get started with the Linux kernel, what is the list head structure?