Contents Menu Expand Light mode Dark mode Auto light/dark, in light mode Auto light/dark, in dark mode Skip to content
Python for Data Science 24.3.0
Logo
Python for Data Science 24.3.0
  • Introduction
  • Workspace
    • IPython
      • Start the IPython shell
      • IPython examples
      • IPython magic
      • Shell commands in IPython
      • Unix shell
        • Navigate through files and directories
        • Create, update and delete files and directories
        • Pipes and filters
        • grep and find
        • Shell variables
      • Show objects with display
      • foo.ipynb
      • Import notebooks
      • IPython extensions
      • Debugging
    • Jupyter
    • NumPy
      • Introduction to NumPy
      • ndarray – an N-dimensional array object
      • dtype
      • Arithmetic
      • Indexing and slicing
      • Transpose arrays and swap axes
      • Universal functions (ufunc)
      • Array-oriented programming – vectorisation
      • Conditional logic as array operations – where
      • Mathematical and statistical methods
      • Methods for Boolean arrays
      • Sort
      • unique and other set logic
      • File input and output with arrays
    • pandas
      • Introduction to the data structures of pandas
      • Converting Python data structures into pandas
      • Indexing
      • Date and Time
      • Select and filter data
      • Add, change and delete data
      • Manipulation of strings
      • Arithmetic
      • Descriptive statistics
      • Sorting and ranking
      • Subdividing and categorising data
      • Combining and merging data sets
      • Group operations
      • Aggregation
      • Apply
      • Pivot tables and crosstabs
      • Convert dtype
  • Read, persist and provide data
    • Open data
    • pandas IO tools
    • Serialisation formats
      • Data serialisation
      • CSV
        • CSV example
      • JSON
        • JSON example
      • Excel
      • XML/HTML
        • XML/HTML examples
        • BeautifulSoup
      • YAML
        • Example
      • TOML
        • Example
      • Pickle
        • Pickle examples
      • Protocol Buffers (Protobuf)
      • Other Formats
    • Intake
      • Install Intake
      • Intake for data scientists
      • Intake-GUI: Exploring data in a graphical user interface
      • Intake for data engineers
    • httpx
      • httpx installation and sample application
      • Create module
    • File systems
    • Geodata
    • PostgreSQL
      • Foreign Data Wrappers (FDW)
      • Procedural programming languages
      • DB-API 2.0
      • Psycopg
      • Object-relational mapping
      • SQLAlchemy
      • Alembic
      • ipython-sql
      • PostGIS
        • Install PostGIS
        • Optimising PostgreSQL for GIS database objects
        • Loading geospatial data
      • Database security
      • PostgreSQL performance
      • pgMonitor
      • pganalyze
    • NoSQL databases
      • Key-value database systems
      • Column-oriented database systems
      • Document-oriented database systems
      • Graph database systems
      • Object database systems
      • XML database systems
    • Application Programming Interface (API)
      • FastAPI
        • Installation
        • Example
        • Tips
        • Extensions
      • gRPC
        • gRPC-Example
        • Test gRPC
    • Glossary
  • Data cleansing and validation
    • Managing missing data with pandas
    • Detecting and filtering outliers
    • String comparisons
    • Deduplicate data
    • Hypothesis: Property-based testing
    • TDDA: Test-Driven Data Analysis
    • Data validation with Voluptuous (schema definitions)
    • Normalisation and Preprocessing
    • Assigning satellite data to geo-locations
  • Visualise data
  • Performance
    • iPython Profiler
    • scalene
    • perflint
    • Parallelise pandas
    • Dask
    • Introduction to multithreading, multiprocessing and async
    • Threading example
    • Multi-processing example
    • Threading and forking combined
    • asyncio example
  • Create a product
    • Manage code with Git
      • Workspaces
      • Git installation and configuration
      • Working with Git
      • Review
      • Git tags
      • Git branches
      • Git rebase
      • Undo changes
      • Git best practices
      • Git workflows
        • Git Flow
        • Feature branch workflows
        • Deployment and release branches
        • Trunk Based Development
        • Merge strategies: merge vs. squash vs. rebase
        • Change commits for a clean log
        • Monorepos and large repositories
        • Splitting and merging repos
        • CI-friendly Git Repos
      • Advanced Git
        • Git cherry-pick
        • Find regressions with git bisect
        • Git Notes
        • Git hooks
          • pre-commit framework
          • pre-commit scripts
          • Supported git hooks
          • pre-commit in CI pipelines
          • Skip hooks
          • Template for Git repositories
        • Jupyter Notebooks with Git
        • Shell configuration and command line tools
        • Batch processing
        • Git for binary files
        • Remove Git LFS
        • Visual Studio Code
          • GitLab VS Code Extension
        • GitLab
          • Roles, groups and permissions
          • Merge requests
          • GitLab CI/CD
            • GitLab Pages
            • npm deployment with rsync
            • Building Docker containers
            • Multi-arch images with Buildah
            • Migrating GitHub Actions
          • GitLab Package Registry
        • git-big-picture
        • etckeeper
        • Git’s database internals
      • Git glossary
    • Manage data with DVC
      • Create a project
      • Pipelines
      • Parameterisation
      • Trial metrics
      • View pipelines
      • Reproduce
      • Vim and IDE integration
      • FastDS
    • Python environments
      • uv
        • CI/CD pipelines
        • Dependency bot
        • Python Docker Container with uv
      • Spack
        • Spack installation
        • Combinatorial builds
        • Benefits of the build automation
        • Use case 1: managing combinatorial installations
        • Use case 2: Python and other interpreted languages
        • Future features
        • Use spack
        • Environments, spack.yaml and spack.lock
        • Spack mirrors
    • Creating programme libraries and packages
    • Document
    • Licensing
    • Citing
      • Cite data
      • Cite software
        • Create a DOI with Zenodo
        • CodeMeta
        • Citation File Format
        • Git2PROV
        • HERMES
      • Software journals
    • Testing
    • Logging
    • Check and improve code quality and complexity
      • Code-Smells and design principles
      • flake8
      • Mypy
      • Pytype
      • Wily
      • Pystra
      • Pysa
      • check-manifest
      • Black
      • isort
      • prettier
      • Rope
    • Security
  • Create web applications
  • Index
Back to top
View this page

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
Next
Create, update and delete files and directories
Previous
Unix shell
Copyright © 2019–2024, Veit Schiele
Made with Sphinx and @pradyunsg's Furo
On this page
  • Navigate through files and directories
    • ls options and arguments
      • Show all options and arguments
      • Illegal options
      • Hidden Files
    • Show directory treeThe command tree lists contents of directories in a tree-like format.
    • Change directory
      • Absolute and relative Paths