I read Windows - What are "." and ".." in a directory? - Super User
It explains:
The single dot
.
is also used if you want to pass the current directory as an argument to a command.
I am very confused. Can somebody give an example?
I read Windows - What are "." and ".." in a directory? - Super User
It explains:
The single dot
.
is also used if you want to pass the current directory as an argument to a command.
I am very confused. Can somebody give an example?
The question is about Windows but you tagged linux so my answer applies to Linux.
Every time you invoke a command, there's some working directory your're in at the moment; .
denotes this directory. pwd
is a general command to know what this directory is. While working in a shell like bash
you can "go" to another directory with cd /another/directory/path
.
Some commands (like cd
above) accept directories (i.e. their paths) as argument(s). Now I'm feeling like talking about obvious basics; but your question seems to be basic, so here we go. The general syntax to pass an argument to some_command
is like:
some_command argument
or even arbitrary number of arguments (four in this example):
some_command argument1 argument2 "argument3 with spaces" argument4
You can use .
as an argument instead of the path returned by pwd
. If the command interprets it as a path to directory then it should be understood as the current working directory. Although cd .
has little sense because it makes your shell "go" to the same directory it's already in, the syntax is perfectly valid. Useful example is
df .
It reports disk space usage of the filesystem where your current working directory resides.
There are commands that operate on current working directory by default: ls
is equivalent to ls .
, du
is equivalent to du .
. Note this is not always the case: df
prints all available filesystems while df .
prints just one of them.
I wrote "if the command interprets it as a path to directory" because some commands don't. E.g. in bash
the command
tr . , <<< foo.bar
will print foo,bar
(tr
replaces .
with ,
in the given foo.bar
string). This is because tr
interprets .
as a single-character string.
This shows .
is not translated to current directory path by the shell. Compare this to ~
which is translated to the value of $HOME
(try echo ~
in bash
). The interpretation of .
as a certain path lays way deep inside the design of directory tree in Linux (broader: in UNIX).
So every command can interpret .
on its own. But when it's meant to be a directory path, the convention is it should be the current working directory. Linux understands this and provides tools to support this "conversion".
Also note ls -a
almost always prints .
among the "real" directory content. It gets interesting when your current working directory gets deleted. In this case ls -a
prints nothing, but ls .
or df .
still work, as if .
was there.
A slightly contrived Windows example:
you can use .
to set the attributes of the current directory:
C:\Path\to\Some\Directory> md sub1 sub2
C:\Path\to\Some\Directory> attrib * /s /d
C:\Path\to\Some\Directory\sub1
C:\Path\to\Some\Directory\sub2
C:\Path\to\Some\Directory> cd sub2
C:\Path\to\Some\Directory\sub2> attrib +r .
C:\Path\to\Some\Directory\sub2> cd ..
C:\Path\to\Some\Directory> attrib * /s /d
C:\Path\to\Some\Directory\sub1
R C:\Path\to\Some\Directory\sub2
Here we set the read-only attribute on the sub2
directory
while we’re in it.
attrib +r .
is shorter and easier to type
than attrib +r ..\sub2
or attrib +r C:\Path\to\Some\Directory \sub2
.