LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-11-2014, 12:08 PM   #1
Zaheen Abbas
LQ Newbie
 
Registered: Jan 2014
Posts: 4

Rep: Reputation: Disabled
What is . and .. in Linux?


Hi,

Can any one please tell me what the .(single dot) and ..(double dot) are in Linux. What I am known to this is .(dot) means to be in the current director. When we do #cd . it doesn't give me an error and remains in the same directory. If we give the #cd .. then it takes me one directory back to the current directory. Is the code for single . and double .. written separately? Shouldn't you all think cd . should give an error or a proper message that its of no use as why someone wants to change the same directory where he is already there. I am sure cd . gets executed and lots of unnecessary process in the background. Question may not look suitable to you as you may aware of its ins and outs , but I am not.

Thanks
 
Old 03-11-2014, 12:11 PM   #2
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian Jessie 8.4
Posts: 5,873

Rep: Reputation: 348Reputation: 348Reputation: 348Reputation: 348
Single dot means PWD (present working directory).

Double dot means parent directory of the pwd.
 
1 members found this post helpful.
Old 03-11-2014, 12:17 PM   #3
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
They are directory links.

The UNIX directory tree (and linux, for most of the filesystems) uses a doubly linked list to represent the directory hierarchy.

Within a directory, the file "." points to the directory itself. The file ".." points to the directory this directory is in. Thus the directory name (as in /home/name) contains two entries - one for itself (the "." file points to the same directory that name points to) and the directory /home has a link pointing to the same direcory "name". To maintain the backward link, the directory name contains an entry for its parent "..", which points to the directory /home.

One advantage this has is that relative path specifications work: things like "../name2" point to the same place that "/home/name2" points to. This becomes more useful the deeper the tree gets.

In another place that the "." file gets used is with some obnoxious file names. Sometimes (typos or other) a file gets created with a leading "-" character. One easy way to refer to these files is with a "./-" path. This prevents the "-" character from being interpreted by the command as an option indicator.
 
1 members found this post helpful.
Old 03-11-2014, 12:22 PM   #4
DavidMcCann
Senior Member
 
Registered: Jul 2006
Location: London
Distribution: CentOS, Salix
Posts: 4,165

Rep: Reputation: 1223Reputation: 1223Reputation: 1223Reputation: 1223Reputation: 1223Reputation: 1223Reputation: 1223Reputation: 1223Reputation: 1223
You may not want to change to the current directory, but you do need to be able to refer to it easily. For example, when you compile a program you got as source code, there is usually a script called "configure" which gets information about your system so that the compilation is done properly. To run it, you use
./configure
Why? Because the OS only looks for programs in a very limited list of places (use "echo $PATH" to find them): think how slow it would be if it searched the whole computer every time! That list does not include the working directory, so we refer to it by "./" That's a big improvement on having to type a command something like
/home/david/downloads/thingy-13.2.4/.configure
 
Old 03-11-2014, 01:34 PM   #5
Zaheen Abbas
LQ Newbie
 
Registered: Jan 2014
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thank You all for your replies. The question is using "#cd ." should be giving some proper message or an error as we never want a directory to be changed to itself. I mean to say, is there an scope for this?
 
Old 03-11-2014, 01:53 PM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Why would you want it to generate an error? Just because it's pointless doesn't mean it's a problem. Do you want the shell to generate an error every time you hit enter without typing a command?
 
Old 03-11-2014, 06:00 PM   #7
joe_2000
Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 808

Rep: Reputation: 216Reputation: 216Reputation: 216
Quote:
Originally Posted by Zaheen Abbas View Post
Thank You all for your replies. The question is using "#cd ." should be giving some proper message or an error as we never want a directory to be changed to itself. I mean to say, is there an scope for this?
Thank god Linux is not deciding for me what I want! Like ...
Code:
"The last Windows update requires the computer to be restarted... Restart now or remind me in 10 minutes?"
 
Old 03-11-2014, 06:09 PM   #8
joe_2000
Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 808

Rep: Reputation: 216Reputation: 216Reputation: 216
Wink

Another thought just crossed my mind... Assuming for a second that it did generate an error message... What would that message be?!?
Code:
user@host:~$ cd .
bash: cd: .: Could not change to current directory. You are now in directory /not/your/current/directory 
user@host:/not/your/current/directory$
Sorry, just kidding... ;-P
 
Old 03-11-2014, 06:39 PM   #9
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
It gets even worse... If your current directory is /home/username/whatever, and you do a cd to /home/username/whatever, should you also get an error???

Or how about "cd /home/username/whatever/."...

There are also some scripts that do a "cd /opt/somewhere" so that built in relative files will work (not the best practice, but still it happens). What happens if you are already in /opt/somewhere...

The way it works is proper. Just because something has a no operation doesn't mean it is wrong.

Last edited by jpollard; 03-11-2014 at 06:42 PM.
 
Old 03-11-2014, 08:58 PM   #10
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,961

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
Quote:
Originally Posted by suicidaleggroll View Post
Do you want the shell to generate an error every time you hit enter without typing a command?
I'm just thankful that it doesn't spend a couple of minutes searching through the repositories to see if there is some package it can suggest that provides the "(null)" command.
 
Old 03-12-2014, 10:00 AM   #11
sgosnell
Senior Member
 
Registered: Jan 2008
Location: Baja Oklahoma
Distribution: Debian
Posts: 1,054

Rep: Reputation: 276Reputation: 276Reputation: 276
IIRC, this isn't just in Linux - the . and .. are the same in Windows, if you use the command line. Back in the day, when it was just DOS, that's the way I recall it working.
 
Old 03-12-2014, 10:40 AM   #12
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,121

Rep: Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270
actually cd . and cd .. are not implemented separately, they are both links to the current dir and parent dir. cd (the command) will try do change into the dir given and will not try to decide if the user was stupid or not (will not be checked if the argument passed is identical to anything, it will just take time and resources for nothing).
 
Old 03-12-2014, 03:20 PM   #13
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Quote:
Originally Posted by sgosnell View Post
IIRC, this isn't just in Linux - the . and .. are the same in Windows, if you use the command line. Back in the day, when it was just DOS, that's the way I recall it working.
I don't think that was in the filesystem... That was interpreted by the command.

The UNIX/Linux/Solaris/... filesystems have these links in the filesystem itself.
 
Old 03-12-2014, 11:37 PM   #14
ReaperX7
LQ Guru
 
Registered: Jul 2011
Location: California
Distribution: Slackware64-Current
Posts: 6,378
Blog Entries: 15

Rep: Reputation: 1953Reputation: 1953Reputation: 1953Reputation: 1953Reputation: 1953Reputation: 1953Reputation: 1953Reputation: 1953Reputation: 1953Reputation: 1953Reputation: 1953
./ means that you are targeting the current directory.

../ means you are targeting the parent directory of the one you are in.
 
Old 03-13-2014, 09:54 AM   #15
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,961

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
Quote:
Originally Posted by jpollard View Post
I don't think that was in the filesystem... That was interpreted by the command.

The UNIX/Linux/Solaris/... filesystems have these links in the filesystem itself.
In a DOS FAT filesystem, both "." and ".." entries are present in subdirectories, but not in the root directory. Here is a hexdump of a subdirectory containing a single file, MYDATA
Code:
00006200   2E 20 20 20  20 20 20 20  20 20 20 10  00 00 00 00  .          .....
00006210   00 00 00 00  00 00 15 3E  6D 44 03 00  00 00 00 00  .......>mD......
00006220   2E 2E 20 20  20 20 20 20  20 20 20 10  00 00 00 00  ..         .....
00006230   00 00 00 00  00 00 15 3E  6D 44 00 00  00 00 00 00  .......>mD......
00006240   4D 59 44 41  54 41 20 20  20 20 20 20  00 00 00 00  MYDATA      ....
00006250   00 00 00 00  00 00 1A 3E  6D 44 04 00  06 00 00 00  .......>mD......
00006260   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................
00006270   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off




All times are GMT -5. The time now is 07:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration