Session 2: Basics of the Command Line

Track, organize and share your work: Version control of code & data with Git & DataLad

Course at AUDICTIVE Priority Program

Slides | Source

License: CC BY 4.0 DOI

10:00

1 Logistics & Admin

Schedule

No Time Title Contents
1 09:30 - 10:00 Introduction to Version Control Logistics and course admin
Introduction to reproducibility
Introduction to version control
Introduction to Git
2 10:00 - 10:45 Basics of the Command Line File systems and navigation
Benefits of the command line
Basic command line commands
3 10:45 - 11:00 Setup & configuration of Git Setup & configuration of Git
4 11:00 - 12:00 Basics of Git Initializing a Git repository
Practicing basic Git commands
Tracking changes wih Git
Ignoring files with .gitignore
Good commit messages
5 12:00 - 13:00 Lunch Break Enjoy your lunch!
6 13:00 - 14:00 Integration with GitHub / GitLab Introduction to remote repositories
Managing repositories on GitHub / GitLab
Pushing and pulling changes
Cloning a remote repository
7 14:00 - 15:00 Version Control of Data with DataLad Version control of (large) data with DataLad
Nesting modular datasets with DataLad
Establishing provenance and reproducibility with DataLad
8 16:00 - 16:30 Summary & Outlook Summary of course contents
Outlook to more related topics
Discussing open questions

Course exercise: Building an online recipes book

https://lennartwittkuhn.com/recipes/

2 Last session: Introduction

Last sessions’s learning objectives

After the last session, you should now be able to answer the following questions:

💡 You know what version control is.
💡 You can argue why version control is useful (for research).
💡 You can name benefits of Git compared to other approaches to version control.
💡 You can explain the difference between Git and GitHub.

Any questions?

3 Basics of the Command Line

This session: Basics of the Command Line

Source: Wikimedia Commons (free license)

Learning objectives

💡 You can name the advantages of command-line interfaces for Git.
💡 You can navigate directories using absolute and relative paths.
💡 You can use shortcuts like the tilde or dots to navigate your file system.
💡 You can apply arguments and flags to customize command-line commands.
💡 You can use wildcards (*) for file selection.
💡 You can combine command-line commands.

Reading

Command Line

Cheatsheet

Command Description
pwd Displays the path of the current working directory
cd <PATH> Changes the current working directory to <PATH>
cd ~ Changes the current working directory to the user’s home directory
cd .. Moves up one folder
cd ../.. Moves up two folders
clear Clears the contents of your command line window
ls Lists files and folders in the current working directory
ls <PATH> Lists files and folders in <PATH>
ls -a Lists all files (including hidden files) in the current working directory
ls -alh Lists all files in a long format that is easy to read for humans
[Command] --help Displays all possible flags for a specific command (on Windows)
man [Command] Displays all possible flags for a specific command (on macOS)
mkdir <FOLDER> Creates a new folder called <FOLDER>
mkdir <FOLDER1> <FOLDER2> Creates two separate folders called <FOLDER1> and <FOLDER2>
touch <FILE> Creates a new empty file called <FILE>
open <FILE> Opens the file called <FILE> (on macOS)
start <FILE> Opens the file called <FILE> (on Windows)
echo "example text" >> file.txt Writes “example text” into file.txt
cat <FILE> Displays the content of <FILE>
mv FILE.txt <FOLDER> Move FILE.txt into <FOLDER>
mv <FOLDER_OLD> <FOLDER_NEW> Renames <FOLDER_OLD> to <FOLDER_NEW>
ls -alh *.csv Uses a wildcard to list all .csv files in the current working directory
rm -r <FOLDER> Removes the folder <FOLDER>
history Display the command history of the current terminal session
history > history.txt Saves the entire command history to a file named history.txt (overwrites existing content in history.txt)
history >> history.txt Adds the entire command history to the end of the file named history.txt
tree Displays a graphical representation of the directory structure
wget <URL> Downloads a file from the specified to the current directory

Tasks

In this session, you will work on the following tasks:

  1. Reading: Read the chapter(s) Command Line in the Version Control Book.
  2. Implementation: Try out the commands in the chapter.
  3. Exercises: Work on the exercises for the recipes project.

As always:

  1. Try out the commands of this session and play around with them.
  2. Check whether you have achieved the learning objectives.
  3. Ask questions!
  4. Let’s git started!

recipes project

At the end of this session, you should have accomplished the following:

  1. You used the command line to create a folder on your computer called recipes.
  2. You used the command line to create a file called recipes.txt inside the recipes folder.

Please keep the recipes folder! We will continue to use it in the following sessions.

Exercises

Identify a folder on your computer where you (want to) keep course-related files. If you don’t have one, choose a suitable location in your file system.

  1. Navigate into the chosen location using the command line.
  2. Use the command line to display the path of your chosen folder. What is the output?

Create files and folders

  1. Within your chosen folder, create a new subfolder and give it a relevant name.
  2. Navigate into the newly created subfolder.
  3. Create a new text file and name it appropriately.
  4. Confirm that the files was created by listing the contents of the folder.

Bonus exercises

Writing and opening files

Add the title “My Favorite Recipes” to recipes.txt. Which command or method did you use?

Creating multiple files

Go back to your main course-related folder. Using the command line, create three new files: notes.txt, assignments.txt, and schedule.txt. Provide the command you used.

Wildcards

Use a wildcard pattern to list all .txt files in your folder. Which command did you use?

Combining commands

Imagine, that you are in a folder containing multiple text files with various extensions, including .txt, .md, and .docx. You want to move all the .txt files to a subfolder named text_files while keeping the other file types in the current directory. Write a series of command-line commands to accomplish this task, including creating the text_files subdirectory. Explain each step in your solution.

Solutions: Navigate the file system

Code
1cd /path/to/your/chosen/directory
1
Navigate into the chosen location. Replace /path/to/your/chosen/directory with a path on your computer. A good location might be within your /Documents folder or a dedicated /Projects or /University folder. This can help to easily find course-related files on your computer.

Solutions: Create files and folders

Code
#!/bin/bash

2pwd
3mkdir recipes
4cd recipes
5touch recipes.txt
6ls
2
Use pwd to display the path of your current working directory.
3
Use mkdir to create a new subfolder and give it a name, within your chosen folder.
4
Use cd to navigate into the newly created subfolder.
5
Use touch to create a new file and name it appropriately.
6
Use ls to confirm that the files was created by listing the contents of the folder.