Navigate through files and directories¶
First let us find out where we are by running a command called pwd
:
[1]:
!pwd
/Users/veit/cusy/trn/Python4DataScience/docs/workspace/ipython/unix-shell
Here, the response is the iPython chapter of the Jupyter tutorial in my home directory /Users/veit
.
On Windows the home directory will look like C:\Documents and Settings\veit
or C:\Users\veit
and on Linux like /home/veit
.
To see the contents of our directory, we can use ls
:
[2]:
!ls
create-delete.ipynb index.rst
create-delete.ipynb.license pipes-filters.ipynb
file-system.ipynb pipes-filters.ipynb.license
file-system.ipynb.license shell-variables.ipynb
grep-find.ipynb shell-variables.ipynb.license
grep-find.ipynb.license
a trailing
/
indicates a directory@
indicates a link*
indicates an executable
Depending on your default options, the shell might also use colors to indicate whether an entry is a file or a directory.
ls
options and arguments¶
[3]:
!ls -F ../
debugging.ipynb
debugging.ipynb.license
display.ipynb
display.ipynb.license
examples.ipynb
examples.ipynb.license
extensions.rst
importing.ipynb
importing.ipynb.license
index.rst
magics.ipynb
magics.ipynb.license
mypackage/
myscript.py
shell.ipynb
shell.ipynb.license
start.rst
tab-completion-for-anything.png*
tab-completion-for-anything.png.license
tab-completion-for-modules.png*
tab-completion-for-modules.png.license
tab-completion-for-objects.png*
tab-completion-for-objects.png.license
unix-shell/
ls
is the command, with the option -F
and the argument ../
.
Options either start with a single dash (
-
) or two dashes (--
), and they change the behavior of a command.Arguments tell the command what to operate on.
Options and arguments are sometimes also referred as parameters.
Each part is separated by spaces.
Also, capitalisation is important, for example
ls -s
will display the size of files and directories alongside the names,while
ls -S
will sort the files and directories by size.
[4]:
!ls -s
total 224
24 create-delete.ipynb 8 index.rst
8 create-delete.ipynb.license 16 pipes-filters.ipynb
96 file-system.ipynb 8 pipes-filters.ipynb.license
8 file-system.ipynb.license 16 shell-variables.ipynb
24 grep-find.ipynb 8 shell-variables.ipynb.license
8 grep-find.ipynb.license
[5]:
!ls -S
file-system.ipynb create-delete.ipynb.license
grep-find.ipynb file-system.ipynb.license
create-delete.ipynb grep-find.ipynb.license
pipes-filters.ipynb pipes-filters.ipynb.license
shell-variables.ipynb shell-variables.ipynb.license
index.rst
Show all options and arguments¶
ls
comes with a lot of other useful options. Using man
you can print out the built-in manual page for the desired UNIX/Linux-command:
[6]:
!man ls
LS(1) General Commands Manual LS(1)
NAME
ls – list directory contents
SYNOPSIS
ls [-@ABCFGHILOPRSTUWabcdefghiklmnopqrstuvwxy1%,] [--color=when]
[-D format] [file ...]
DESCRIPTION
For each operand that names a file of a type other than directory, ls
displays its name as well as any requested, associated information. For
each operand that names a file of type directory, ls displays the names
of files contained within that directory, as well as any requested,
associated information.
If no operands are given, the contents of the current directory are
displayed. If more than one operand is given, non-directory operands are
displayed first; directory and non-directory operands are sorted
separately and in lexicographical order.
…
Illegal options¶
If you try to use an option that isn’t supported, the commands will usually print an error message, for example for:
[7]:
!ls -z
ls: invalid option -- z
usage: ls [-@ABCFGHILOPRSTUWXabcdefghiklmnopqrstuvwxy1%,] [--color=when] [-D format] [file ...]
Hidden Files¶
With the -a
option you can display all files:
[8]:
!ls -a
. grep-find.ipynb
.. grep-find.ipynb.license
.ipynb_checkpoints index.rst
create-delete.ipynb pipes-filters.ipynb
create-delete.ipynb.license pipes-filters.ipynb.license
file-system.ipynb shell-variables.ipynb
file-system.ipynb.license shell-variables.ipynb.license
In addition to the hidden directories ..
and .
, you may also see a directory called .ipynb_checkpoints
. This file usually contains snapshots of the Jupyter notebooks.
Show directory treeThe command tree
lists contents of directories in a tree-like format.¶
[9]:
!tree
.
├── create-delete.ipynb
├── create-delete.ipynb.license
├── file-system.ipynb
├── file-system.ipynb.license
├── grep-find.ipynb
├── grep-find.ipynb.license
├── index.rst
├── pipes-filters.ipynb
├── pipes-filters.ipynb.license
├── shell-variables.ipynb
└── shell-variables.ipynb.license
1 directory, 11 files
Change directory¶
At first it may seem irritating to some that they cannot use !cd
to change to another directory.
[10]:
!pwd
/Users/veit/cusy/trn/Python4DataScience/docs/workspace/ipython/unix-shell
[11]:
!cd ..
[12]:
!pwd
/Users/veit/cusy/trn/Python4DataScience/docs/workspace/ipython/unix-shell
The reason for this is that Jupyter uses a temporary subshell. If you want to change to another directory permanently, you have to use the magic command %cd
. However, this also requires the Python package pickleshare
.
[13]:
%cd ..
/Users/veit/cusy/trn/Python4DataScience/docs/workspace/ipython
[14]:
!pwd
/Users/veit/cusy/trn/Python4DataScience/docs/workspace/ipython
With the %automagic
function, these can also be used without the preceding %
character:
[16]:
%automagic
Automagic is ON, % prefix IS NOT needed for line magics.
[17]:
cd ..
/Users/veit/cusy/trn/Python4DataScience/docs/workspace
Absolute and relative Paths¶
[18]:
cd .
/Users/veit/cusy/trn/Python4DataScience/docs/workspace
[19]:
cd ../..
/Users/veit/cusy/trn/Python4DataScience
[20]:
cd ..
/Users/veit/cusy/trn
[21]:
cd /
/
[22]:
cd
/Users/veit
[23]:
cd ~
/Users/veit
[24]:
cd /Users/veit
/Users/veit