Why the heck are "." and ".." counted as directories?

C

CrazedNerd

Guest
Using , and ,, to make commands shorter makes perfect sense to me: . is the working directory and .. is the parent directory, but why are "." and ".." listed when i hit "ls -a"? Aren't they just symbols for something else? They don't show up when i ask linux to show hidden files on nautilus.
 


How would the shell know how to differentiate between a hidden file, and any other file
that starts with a "."?

In linux everything is a file. It may look like folder icons in a GUI, but really, it's just a file.
Devices are just files. Swap is just files. Drivers and Libraries are just files.
It sounds like you already you can hide a file or directory by starting the file name with a ".".

So how would you tell the shell...
.myhiddenfile <- this is a file
.secret <- this is a hidden directory
. <- this is the current directory
.. <- this is the parent directory

It already expects anything that starts with a "." to be hidden.
 
i guess the thing that throws me off the most is the "ls -d" command, when using it on a normal home folder, it just shows this:

"."

Since . and .. are clearly just symbolic, i don't see why they are listed with the ls -a command...ls -A ignores it which makes it better way to look at all the contents of the directory...it is useful to understand that putting "." in front a of a directory will hide it though...
 
As we know all hidden files in Linux start with a "." period.

For example, if you use only ls to look at the root home directory on a clean Linux installation, no files are returned. However, if you add the -a option, the ls command returns a list of files.

. and .. are actually hard links in filesystems. They might get listed along with whole list of files in the current directory including hidden files as they begin with a "." period.

They are needed so that you can specify relative paths, based on some reference path (consider "../sibling/file.txt"). They are hard links, not just symbols and there is no discrepancies if they are listed in any directory.

Therefore, . and .. exist in all directories. If you want to see all files in a directory except those, use ls -A instead of ls -a.
 
Basically, single dot (.) in Linux represents the current working directory you are in and double dot (..) represents the parent directory. "." and ".." are similar to the hard links as they increase the link count of an Inode, but we also cannot remove them since they’re built into the filesystem. Moreover, hard links to directories are not possible. Hence we cannot exactly refer to them as hard links, and the more accurate term is “name-inode maps”.

Both the "." and ".." can be accessed by ls -a or ls - all command.

So ./{fileName} refers to a file that infers that we are in our current working directory. The dot (.) operator is also known as source. In the below example, dot is the command source to read and execute commands from the filename given as argument. So dot operator is a shortcut for the shell’s built-in source command.

Code:
. setup.sh

# Above is the same as

Code:
source setup.sh

Upload.png


Contents of script.sh is:
Code:
echo "Ive used (.) to execute"

Double dot(..) refers to the parent directory. It can used in cd command for moving around directory. A ".." can also be used in a command line or in a file path to go back one directory. Typing "cd .. "goes back one directory. For example, if your current working directory is
/home/steve

Using cd .. command changes the current working directory to:
/home

Screenshot from 2022-05-20 10-11-53.png


Hopefully, you are now clear with "." and ".." .
 

Members online


Top