Contents Menu Expand Light mode Dark mode Auto light/dark, in light mode Auto light/dark, in dark mode Skip to content
DeisData
Logo
DeisData

Contents:

  • Python
    • Setup and installation
    • Jupyter notebooks
    • Fundamentals
    • Data structures
    • Booleans and conditionals
    • Loops
    • NumPy
    • Pandas
    • Matplotlib and seaborn
    • Writing functions
    • Statistical tests
    • Web scraping
  • R
    • Intro to R and the Tidyverse
    • Data wrangling with the Tidyverse
    • ggplot2
    • Data Analysis
    • Text mining
  • Machine learning
    • Quick start
    • Image recognition: Model components
    • Image recognition: Improving the model
  • Unix Shell
    • Bash Setup
    • Navigating
    • Altering files and directories
    • Pipes and filters
    • Loops
    • Shell scripts
    • Searching and finding
  • Git Version Control
    • Setup and installation
    • Quick start to Git and GitHub
    • Pushing changes
    • Fetch and merge from GitHub
    • Git utilities
    • Forking in GitHub
    • Git Glossary
  • SQL
    • Setup
    • Selecting data
    • Sorting out and removing duplicates
    • Filtering
    • Calculating new values
    • Missing data
    • Aggregates
    • Combining data
    • Create and modify
    • Glossary
  • Data Services Case Studies
  • Contribute to DeisData
    • Small changes
    • reStructuredText basics
    • Structural changes
Back to top
View this page

Navigating to files and directories¶

Your challenges of the day:¶

  1. Does type case matter? Is there a difference between ls -s and ls -S?

  2. Do spaces matter? Is there a difference between ls-F and ls -F?

Questions to think about:¶

  • What is a command shell and why should I use one?

  • How can I move around on my computer?

  • How can I see what files and directories I have?

  • How can I specify the location of a file or directory on my computer?

What is Unix Shell?¶

We usually interact with our devices on a graphical user interface (GUI). Shell is a Command-Line Interface (CLI), where we type commands in the prompt $ instead of pointing and clicking. We use it to invoke complicated programs. Shell is a scripting language, and we will use the Unix Shell: Bash (Bourne Again SHell by Stephen Bourne).

Why use Bash?¶

Bash combines existing tools into powerful pipelines and handle large volumes of data automatically. We can write sequences of commands into a script, improving the reproducibility of workflows. It is essential to interface with hardware, HPCC, and remote machines.

Navigating files and directories¶

The file system is the part of the operating system responsible for managing files and directories.

  • Files hold information.

  • Directories (or folders) hold files or other directories. Think of them like places.

  • The current working directory is the place where you are in the file system when you are using the shell.

  • The root directory is the top directory that holds everything else. It is referred to by a slash / on its own. This is the leading slash in other directory paths, for example /home/fordfishman/

  • Hidden files and directories start with . like .bash_profile. They are usually configuration settings and are hidden to prevent cluttering the terminal with a standard ls command. Add the -a option see hidden files.

Example of directory structure:

Documents
├── Code
│   ├── analysis.py
│   └── functions.py
├── Data
│   └── info.csv
├── description.txt
└── .gitignore

Paths¶

The path to a file displays the directories that file is a member of. For instance, if you have a file called data.txt on your Desktop, its path could be /home/fordfishman/Desktop/data.txt. This is an absolute path because it begins at the root directory. We also use relative paths that change based on our current working directory. If the working directory is /home/fordfishman, the relative path to the file is Desktop/data.txt.

General syntax of a shell command¶

$ ls -F /

ls is the command, with an option (or switch or flag) -F and an argument /. Options start with a single dash (-) or two dashes (--) and change the behavior of the command. Arguments tell the command what to operate on (e.g. files and directories). Options and argements are refered to as parameters.

Note

Type case is important. Spaces are important between command and options. (But options can be combined with a single - and no spaces -> -aF).

Listing: ls¶

We use the ls command to list the contents of the current directory.

It has many options we can provide:

  • -F option (switch or flag) tells ls to classify the output by adding a marker to file and directory names to indicate what they are.

  • -a option displays all files, including hidden files (files starting with .)

  • -s option displays the size of files and directories

  • -S option will sort the files and directories by size

  • --help option will tell us how to use the command and what options it accepts

$ ls -F
Applications/   data_shell/      Music/
Desktop/        data_shell.zip   Pictures/
Documents/      Library/
Downloads/      Movies/

Explore more ls flags.¶

  1. What does -l option do? What if you use -l and -h?

  2. The default ls lists contents in alphabetical order. What option do I use to see them by time of last change?

Print working directory: pwd¶

To check where you are, use pwd to print the path of the working directory.

$ pwd
/home/fordfishman/

Change directory: cd¶

cd will change your working directory. cd can only see sub-directories inside your current working directory.

  • cd .. is a shortcut to move up one directory to the parent directory of the one we are in

  • cd ~/ is a shortcut to move to the current user’s home directory. For example, if my home directory is /home/fordfishman, then ~/data is equivalent to /home/fordfishman/data

$ cd ..
$ pwd
/home/

Other commands¶

  • clear: clears the terminal if it gets to cluttered

  • up and down arrows can be used to access previous commands (or scroll)

  • man will give you the manual for a command, for example man ls will tell us all about listing

  • The help option can be used with a command, for example ls --help

References¶

  • Intermediate Linux Commands

  • Software Carpentry Unix Shell

Next
Altering files and directories
Previous
Bash Setup
Copyright © 2023, Ford Fishman
Made with Sphinx and @pradyunsg's Furo
On this page
  • Navigating to files and directories
    • Your challenges of the day:
    • Questions to think about:
    • What is Unix Shell?
    • Why use Bash?
    • Navigating files and directories
    • Paths
    • General syntax of a shell command
    • Listing: ls
      • Explore more ls flags.
    • Print working directory: pwd
    • Change directory: cd
    • Other commands
      • References