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

CrazedNerd

Active Member
Joined
Mar 31, 2021
Messages
363
Reaction score
165
Credits
3,247
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.
 


dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
1,393
Reaction score
1,020
Credits
8,573
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.
 

KGIII

Super Moderator
Staff member
Gold Supporter
Joined
Jul 23, 2020
Messages
6,183
Reaction score
5,559
Credits
49,836
OP
CrazedNerd

CrazedNerd

Active Member
Joined
Mar 31, 2021
Messages
363
Reaction score
165
Credits
3,247
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...
 

needafatima

New Member
Joined
May 19, 2022
Messages
3
Reaction score
5
Credits
37
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.
 

prajwalshavi21

New Member
Joined
May 19, 2022
Messages
2
Reaction score
3
Credits
31
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 ".." .
 
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Members online


Top