0

Is it possible to delete . and ..?

I read somewhere there are some patches for the linux kernel which let you do that. I have no idea where I read it and I don't know how to search for it because google and duckduckgo ignore the .s.

5
  • 3
    Are you asking for a specific reason, i.e. do you have an application case where you would actually want/need to do that? Commented Jan 21, 2021 at 13:11
  • I am asking out of curiosity. Commented Jan 21, 2021 at 13:14
  • Why would you do this? Do you know that .. refers to the parent directory, and . to the current directory? Commented Jan 21, 2021 at 13:19
  • Do you want to remove actual directories (example)? or entries in some directory only? To see the difference: suppose you're in /foo/bar/ and you manage to remove .. It may be you remove /foo/bar/; or it may be /foo/bar/ still exists but there is no longer . in it. Commented Jan 21, 2021 at 13:20
  • I know what . and . refer to. I wan't to remove . and .. in the directory not the directory itself Commented Jan 21, 2021 at 13:29

1 Answer 1

4

Technically, it is possible; for example, on an ext4 file system in e2test.img:

$ sudo mount e2test.img /mnt/temp
$ sudo mkdir /mnt/temp/dir{1,2}
$ sudo umount /mnt/temp
$ debugfs -w e2test.img
debugfs 1.45.6 (20-Mar-2020)
debugfs:  unlink dir1/..
debugfs:  unlink dir1/.
debugfs:  quit
$ sudo mount e2test.img /mnt/temp
$ ls -a /mnt/temp/dir{1,2}
/mnt/temp/dir1:

/mnt/temp/dir2:
.  ..

The shell isn’t particularly confused:

$ cd /mnt/temp/dir1
$ pwd
/mnt/temp/dir1
$ cd ..
$ pwd
/mnt/temp

As might be expected, e2fsck isn’t happy about this:

$ e2fsck e2test.img
e2fsck 1.45.6 (20-Mar-2020)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Missing '.' in directory inode 113793.
Fix<y>? yes
Setting filetype for entry '.' in ... (113793) to 2.
Missing '..' in directory inode 113793.
Fix<y>? yes
Setting filetype for entry '..' in ... (113793) to 2.
Pass 3: Checking directory connectivity
'..' in /dir1 (113793) is <The NULL inode> (0), should be / (2).
Fix<y>? yes
Pass 4: Checking reference counts
Pass 5: Checking group summary information

e2test.img: ***** FILE SYSTEM WAS MODIFIED *****
e2test.img: 14/128016 files (0.0% non-contiguous), 18478/512000 blocks

The exact behaviour will depend on the specific file system being used. Some don’t even store . and .. entries, but emulate them instead (POSIX requires . and .. to be understood and resolve to the appropriate directories, but doesn’t require them to be physically present).

4
  • 1
    POSIX doesn't require them. It requires . to resolve to the current directory and .. to resolve to the parent, but not readdir() to include any of them in its result. Actually, the world would be a better place if readdir() no longer returned them. See also: austingroupbugs.net/view.php?id=1228 Commented Jan 21, 2021 at 13:59
  • @Stéphane yes, I only wanted to explain why a file system would emulate them; thanks for the clarification, I’ve updated the answer. Commented Jan 21, 2021 at 14:12
  • I don't think it's good to conflate the emulation of . and .. when resolving paths (which is required by POSIX compat and is done by the kernel at VFS level) and their emulation of . and .. when listing directories (which is done by most filesystems, though not required by POSIX, and not at all useful or reliable). If you re-created . as a named pipe or regular file in debugfs, stat . would still show it as a directory. A much better question IMHO would've been: what programs would break if filesystems no longer faked the . and .. entries when listing directories? Commented Jan 22, 2021 at 9:48
  • In the (unlikely) case someone actually tries that, notice that debugfs is a very blunt tool: mknod /dir/pipe p will not create a named pipe inside the /dir directory, but one named /dir/pipe (yes, including the slashes as part of the filename) inside the current directory. Commented Jan 22, 2021 at 10:42

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.