6

The single period . means current working directory

The double period .. means parent of the current working directory

I can only think of . being used in relative pathname etc cd ./bin/usr

How are the above . & .. used in the shell command line

  • For more background I encourage you to read the computing section of Wikipedia on the topic: Dot_(Unix). The computing section – slm Mar 5 '14 at 15:23
  • Also take a look at this course material from a introductory Unix class: teaching.idallen.com/cst8207/13w/notes/….. – slm Mar 5 '14 at 15:27
  • 1
    Two Unix gurus are arguing about how many unique uses of the period there are in Unix. One carefully counts them up in his mind and announces there are 22. The other is sure he can think of 23. The first challenges the second to name all 23 uses. He begins, "1. In a file of English language text a period can be used to end a sentence." The first guru slaps his forehead and says, "You're right. There are 23." – Justsalt Feb 11 '16 at 19:42
6

How are the above . & .. used in the shell command line?

In many ways depending on context. Simple illustrations:

  • For path resolution with the builtin cd command for example like you said
  • Literally, inside a pattern, for instance in the context of pathname expansion
  • In a pattern to replace any single one character with a command like grep

Consider man cd, man path_resolution and this explanation on how the builtin cd command works internally (man cd here):

(4). If the first component of the directory operand is dot or dot-dot, proceed to step 6.

(6). Set curpath to the string formed by the concatenation of the value of PWD, a slash character, and the operand.

(8). The curpath value shall then be converted to canonical form as follows, considering each component from beginning to end, in sequence:

a. Dot components and any slashes that separate them from the next component shall be deleted.

b. For each dot-dot component, if there is a preceding component and it is neither root nor dot-dot, the preceding component, all slashes separating the preceding component from dot-dot, dot-dot and all slashes separating dot-dot from the following component shall be deleted. ...

and this, which is a step-by-step example of how the steps above seemingly apply to changing directory from /home/user to ../../boot/memtest86:

# echo $PWD
/home/user
(4). cd ../../boot/memtest86
(6). curpath = /home/user/../../boot/memtest86
(8)b 
/home/user/../../boot/memtest86
/home/../boot/memtest86   //step 1
/        boot/memtest86   //step 2
curpath = /boot/memtest86 //result

By trimming dot components to the same extent as the working directory depth, inevitably this yields a path expressed from the root. The steps for a single dot case etc. were skipped since it concatenates like above, then removes ./ which is very easy to imagine as we're in the current directory. So this illustrates how . and .. referencing the current and parent directory is implemented in this command's context internally, which is not through "substitution" but rather something akin to stream editing...


You can investigate pathname expansion and pattern matching in bash

After word splitting, unless the -f option has been set (see The Set Builtin), Bash scans each word for the characters *, ?, and [...] If one of these characters appears, then the word is regarded as a pattern, and replaced with an alphabetically sorted list of file names matching the pattern. ...

In a pattern for a pathname like this, any character other than *,? and [...] matches itself. So this means a dot can match itself literally:

ls .*

...will expand to .. ,. (because * includes also null) and accordingly will list current and parent directory and also any file or directory in the current directory which literally starts with a dot (.*) and is followed by something else of whatever length. As explained in the manual you have some level of control over the expansion behavior etc. (using *glob options to set and shopt).


Here is an example of the character being used in a pattern (as it's grep pattern file1) to select any one character, and is of course unrelated to file or directory names:

ls -la | grep .

This prints the directory contents so it catches everything (grep -v . is used to catch empty lines). Generally with patterns in such expressions and most certainly that is the case with grep, a single dot matches any single character. Finally consider these special characters examples in the dot . section for a quick reminder of some of the different use cases.


So a dot can be used in path resolution, as a literal character which is part of a pattern, it can replace a single character in an expression with grep and it can be even be a bash builtin used to execute commands from a file etc. How it is used on the command line varies with context...


1. This is of course about the syntax of a specific command. If a command accepts a file/path as an argument then using the dot in its stead, as others have shown, expands to the current working directory which is quite convenient.

4

besides the use for working directory . also can be used as source, such as if your .bashrc changed, then you can use . .bashrc in terminal to make .bashrc take effect. it is the same as source .bashrc

3

I think it is important to point out that when you use either . to specify the current directory or .. to specify the parent directory, this is something which is generally interpreted by the underlying system calls used by the shell (or the program it runs) rather than by the shell itself. This is in contrast to using ~ which is expanded by the shell into the user's home directory when used at the start of a word.

There are several good answers here, so I won't copy them other than to say that of course the common case that is interpreted by the shell itself is to source a script in the same shell rather than running it in a separate process. In bash the builtin source is an alternative, however this does not exist in other POSIX shells so you are forced to use .

To add one of my own, you need to include the ./ at the start of a program or script that you want to run in the current directory (when the current directory is not in PATH). Eg ./myscript Although note that it is not so much the . as the presence of the / that causes this to work. If / is present the shell skips any attempt to locate in in PATH etc (except looking for an alias) and passes directly to the system for execution. The only reason for the . is so as not to have to specify the full path.

  • An exception is for cd/pushd in csh and POSIX shells which have their own interpretation of .. and don't rely on the system to expand it (except with -P). – Stéphane Chazelas Mar 5 '14 at 20:21
3

. is also used whenever you need to just specify the current directory. For instance, finding a file in current directory (and subdirectories) is how I call find 99% of the time:

find . -name '*.pdf'

or:

cp /some/file .

This dot could also be a double dot to search in one directory up.

So, what I'm trying to say, it's very common to specify directories like that, especially the current one.

3

. runs a shell script in the current environment and then returns (see IBM's explanation here). If you run

. ./"yourscripthere"

The script will be run in your actual shell instead of spawning a new one. That way, any changes made by that script (variable, functions, etc...) will continue after you run it.

2

You often want to refer to some location relatively to you current directory.

If you want to go up one directory you simply say cd .., If you want to copy a file to the current directory you can say cp /some/file ..

Whenever you need to specify some location you can use an absolute location starting with /, or a relative location. You don't need to specify the current very often as many commands default to it. (For example a simple ls really defaults to ls ..) Other commands (like cp) require you to explicitly specify the path. In this case . might be exactly what you want.

2

Also, if there is a file whose name starts with a -, you cannot use it as a parameter to most commands as it would be interpreted as an option. E.g. stat -x throws the error invalid option, but stat ./-x works well.

  • Nice hint, I never though about it that's a nice idea – Kiwy Mar 5 '14 at 9:33
  • the other way to achieve the same result is stat -- -x – Walter Tross Nov 14 '18 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.