Practicing bash

Files and directories

In the following code examples, you need to type the command, but not include the command prompt (e.g., brun@workbench-1:~$) which just shows that the computer is ready to accept a command.

Setup

Let’s start by downloading the quarto document from GitHub so you can save your commands in it:

  • create a cli_intro directory in your home directory on workbench-1
  • download the qmd file
# got to you home directory
cd ~

# create the new directory
mkdir cli_intro

# go inside it
cd cli_intro

# Download the qmd from GitHub
curl --output cli-handson-files.qmd https://raw.githubusercontent.com/brunj7/eds214-handson-cli/main/cli-handson-files.qmd

# Do a check that it works
ls -F.   # What does `-F` do!?

Let’s keep going!!

  • create a README.md file inside the cli_intro directory & add the following title: # Tutorial files related to CLI to it (check out echo)
    • Hint: you can do this in one line!
  • Show the contents of that file using cat (concatenate)
# brun@workbench-1:~$ cd ~/cli_intro

A little more file manipulations

Get a file from a remote server

  • download the file 10min-loop.R to your home directory on workbench-1
  • create directory code inside your cli_intro folder
  • move mv this file to your cli_intro/code directory
  • list the files in the directory with ls (list)
  • look where we are in the filesystem using pwd (print working directory)
  • get an overview of the directory contents using tree
# brun@workbench-1:~$ cd ~
# brun@workbench-1:~$ curl --output 10min-loop.R https://aurora.nceas.ucsb.edu/~brun/10min-loop.R

Try with a folder:

  • download the data to workbench-1 from https://aurora.nceas.ucsb.edu/~brun/sampledata.zip
  • unzip the folder
# brun@workbench-1:~$ cd ~/cli_intro
# brun@workbench-1:~/cli_intro$ curl --output sampledata.zip https://aurora.nceas.ucsb.edu/~brun/sampledata.zip 
# brun@workbench-1:~/cli_intro$ ls
# brun@workbench-1:~/cli_intro$ unzip sampledata.zip 
# brun@workbench-1:~/cli_intro$ tree .

Your should end up with something like this:

# brun@workbench-1:~/cli_intro$ tree
.
├── code
   └── 10min-loop.R
├── README.md
└── sampledata
    ├── paleo-mammals.txt
    ├── paleo-mammals-v2.txt
    ├── paleo-mammals-v3.txt
    ├── stem.csv
    └── tree.csv

2 directories, 7 files

Your turn

Now, let’s organize those data!!

  • create a new directory data in the cli_intro directory
  • move the sampledata directory inside the data directory
  • create two directories mammals and plots inside the data directory
  • move using cp to copy all the mammals files from the sampledata folder to the mammals subdirectory; hint: you can use the wildcard *
  • move using mv the other files files from the sampledata folder to the plots subdirectory
  • double check it is done using cd and ls
  • remove rm the sampledata directory; hint: rmdir can only remove empty directories

Your should end up with something like this:

# brun@workbench-1:~/cli_intro$ tree
.
├── code
   └── 10min-loop.R
├── data
   ├── mammals
   │   ├── paleo-mammals.txt
   │   ├── paleo-mammals-v2.txt
   │   └── paleo-mammals-v3.txt
   └── plots
       ├── stem.csv
       └── tree.csv
└── README.md

4 directories, 7 files

Bonuses

  • add a text file data_listing.txt in the data folder that lists all the files in it
  • add a text file nb_lines_listing.txt in the data folder that counts the number of lines in each files and orders the files with highest being first
  • can you make the cli-intro a git repository?

BTW, do you wonder what are the differences between the various mammals files?

diff paleo-mammals.txt paleo-mammals-v2.txt

Aknowledgements

Adapted from Matt Jones, OSS 2017, https://github.com/NCEAS/oss-lessons


Back to main Repo