Self-Guided Learning
  • Home
  • Software Installs
  • Learning Resources
  • Domain-Specific Series

Working with Files and Directories

  • Learning Resources
    • Science
    • Tools
    • Frameworks

  • The Unix Shell
    • Series Introduction
    • Installation and Resources
    • Introducing the Shell
    • Navigating Files and Directories
    • Working with Files and Directories

  • The Unix Shell
    • Series Introduction
    • Installation and Resources
    • Introducing the Shell
    • Navigating Files and Directories
    • Working with Files and Directories

  • Reproducibility
    • Series Introduction

On this page

  • File System
  • Directories and paths
    • pwd command
  • Structure
  • Listing files and directories
  • Cleaning up
    • clear command
  • History
    • history command
  • Command syntax
    • Case matters
    • Getting help
  • Useful ls flags
  • Exploring other directories
  • Cut, Sort, Join, Grep

Working with Files and Directories


File System

The file system manages and organizes our files and directories.

  • Files contain information, such as text or images.
  • Directories (what we often refer to as folders) contain files and other directories.

Figure 1: A representative file system


Directories and paths

As we saw above in Figure 1., the file system is a set of directories and files. We can think of

When we work in the command line, we are working in a specific location of the file system, called the “working directory”.

How do we know where we are? We can use a command for that.

pwd command

  • pwd stands for print working directory
  • prints out working directory location to the screen
Tip

The location within the file system is also called path, because it tells that path that can be taken from the top-most directory to the specified location.

Let’s try it. Run the pwd command in your terminal.

bash-3.2$ pwd
/Users/csifuentes
bash-3.2$

In the response above, the path of my working directory is /Users/csifuentes.


Structure

Below is a schematic representing the file system for three users: Imhotep, Larry, and Nelle.

What do you think the user paths will be?

Figure 2: The file system structure for several user directories.

What is structure for each users directory? Click here for the answer!

For Imhotep: /Users/imhotep
For Larry: /Users/larry
For Nelle: /Users/nelle

Using the examples above, let’s break down the path structures. If we look at directories, we see there are

Directory
Top level / or root
Next one down bin, data, Users, and tmp
Lowest level (home directories in this case) imhotep, larry, and nelle

Listing files and directories

When we’re using a terminal and in a specific directory, we often want to know which files and directories are in our currint working directory. How can we do that?

Click here for the answer!

We use the ls command.

Let’s try it. Run the ls command in your terminal.

bash-3.2$ ls
Applications    Movies              RNASeq
Desktop         Music               SoftwareCarpentryStuffs
Documents       Pictures            VirtualBox VMs
Downloads       Public              miniconda3
Library         RNASeqAnalysis.R    notes
bash-3.2$

That didn’t work right Let’s modify the the behavior of the ls command using the -F flag to list formats.

(base) czimacos3304:~ csifuentes$ ls -F
Applications/           Downloads/              Movies/                 VirtualBox VMs/         miniconda/              setup.sh
CytoscapeConfiguration/ Dropbox-Uploader/       Music/                  bin/                    miniconda3/             single-cell-curation/
Desktop/                EnsDb.Xtropicalis.v101/ Pictures/               course/                 notebooks/
Documents/              Library/                Public/                 igv/                    notes/

In the output, we see trailing symbols

  • / means the preceding is a directory
  • @ means the preceding is a link
  • files will have nothing preceding

Cleaning up

As we work in the terminal commands and outputs are printed to the screen. At times this becomes messy and it will take up the entire terminal screen, in time. We can clear the terminal using the clear commmand.

clear command

  • clear stands for clear :)
  • clears the current terminal shell session
  • Before clear
  • After clear
(base) czimacos3304:~ csifuentes$ ls
Applications            Downloads               Movies                  VirtualBox VMs          miniconda               setup.sh
CytoscapeConfiguration  Dropbox-Uploader        Music                   bin                     miniconda3              single-cell-curation
Desktop                 EnsDb.Xtropicalis.v101  Pictures                course                  notebooks
Documents               Library                 Public                  igv                     notes
(base) czimacos3304:~ csifuentes$ ks
bash: ks: command not found
(base) czimacos3304:~ csifuentes$ ls
Applications            Downloads               Movies                  VirtualBox VMs          miniconda               setup.sh
CytoscapeConfiguration  Dropbox-Uploader        Music                   bin                     miniconda3              single-cell-curation
Desktop                 EnsDb.Xtropicalis.v101  Pictures                course                  notebooks
Documents               Library                 Public                  igv                     notes
(base) czimacos3304:~ csifuentes$ ls -F
Applications/           Downloads/              Movies/                 VirtualBox VMs/         miniconda/              setup.sh
CytoscapeConfiguration/ Dropbox-Uploader/       Music/                  bin/                    miniconda3/             single-cell-curation/
Desktop/                EnsDb.Xtropicalis.v101/ Pictures/               course/                 notebooks/
Documents/              Library/                Public/                 igv/                    notes/
(base) czimacos3304:~ csifuentes$ clear
(base) czimacos3304:~ csifuentes$ 

History

Now we have a cleared terminal screen – nice and clean. But what if we needed to know some of the commands we ran previously? Don’t worry, the terminal keeps the command history.

history command

  • history stands for history
  • prints the last x number of commands that were run
(base) czimacos3304:~ csifuentes$ history
   16  pwd
   17  ls
   18  ks
   19  ls
   20  ls -F
   21  clear
   22  history
(base) czimacos3304:~ csifuentes$ 
Tip

We can see the most recent command by tapping the “up arrow” on our keyboard. This can be very helpful when we want to run the same command (or edit the command slightly) but do not want to retype the entire command.


Command syntax

Shell commands follow a general syntax.

command option argument

  • command is the main command
  • option modifies the behavior of the command
  • argument the source and/or target of the command

Let’s take a look at a few examples:

Command Option Argument
ls -F /
mv -i /Users/path/file /other/directory
Note
  • Options use either - or -- to signal their usage.
  • Options are sometimes called flags.
  • Arguments are sometimes called parameters.
  • Depending on the command, arguments can be either a target (as in the ls command) or both a source and target (as in the mv command)

Case matters

What happens if we run the same command, but change the case? Let’s try with an example. We’ll use the ls command and the size option, s, which lists the directories and files, but add their block sizes alongside.

First, let’s run the command ls -s.

(base) czimacos3304:~ csifuentes$ ls -s
total 8
0 Applications                  0 Dropbox-Uploader              0 Pictures                      0 igv                           8 setup.sh
0 CytoscapeConfiguration        0 EnsDb.Xtropicalis.v101        0 Public                        0 miniconda                     0 single-cell-curation
0 Desktop                       0 Library                       0 VirtualBox VMs                0 miniconda3
0 Documents                     0 Movies                        0 bin                           0 notebooks
0 Downloads                     0 Music                         0 course                        0 notes
(base) czimacos3304:~ csifuentes$ 

Now let’s do the same, but with a capital s, ls -S.

(base) czimacos3304:~ csifuentes$ ls -S
Downloads               CytoscapeConfiguration  Dropbox-Uploader        igv                     Music                   miniconda
Library                 course                  EnsDb.Xtropicalis.v101  Applications            Public                  notes
Documents               miniconda3              Movies                  notebooks               VirtualBox VMs
setup.sh                single-cell-curation    Pictures                Desktop                 bin

We get a very different output. In the output above, we have the files/directories sorted by size (largest) file first.

Getting help

Command usage is not always intuitive. Additionally, we’re often not aware of all the options/flags that can be used with a particular command. If you need help with how to use a command, you can try the following (depending on the command).

Method of getting help Description Example
--help or -h option/flag Displays help menu for the command/program ls --help
man command Displays the manual for the command/program in-depth man ls

(a) Using the --help flag

(b) Using the man command

Figure 3: Different ways to interact with a computer


Useful ls flags

There are A LOT of ls options/flags. A few quite useful ones are shows below.

Flag Description
-l

Returns the results in a long format, which provides information about

  • the item type (- for file, d for directory, l for link)
  • item permissions
  • thenumber of links or files inside that item
  • the item owner
  • the item group
  • the time the item was created
  • item size
  • item name
-h Returns the results with a human-readible size value

Figure 4: Using the -lh options/flags.

Above the results are now listed, one item per line, in alphabetical order, and the additional information provided when using the -l flag/option. Using the -h flag/option, the sizes are much easier to read (in bytes or kilobytes, megabytes, and gigabytes) as well.

Note

Multiple options or flags can be used at the same time by simply putting them one after the other, as above.


Exploring other directories

Cut, Sort, Join, Grep

Navigating Files and Directories
Cookie Preferences