2: Command Line Basics

October 25, 2023

Objective: The goal of this lab exercise is to familiarize yourself with the fundamental concepts and practical function of the command line interface. By the end of this exercise, you will be able to create, edit, filter, and delete files and folders using command line utilities.

Creating, Editing, Filtering, and Deleting Files and Folders

This lab exercise focuses on fundamental operations performed on files and folders through the command line interface.

You will learn how to do the following:

  • How to create directories

  • Navigate the file system

  • Create and edit files

  • Filter file contents based on search criteria

  • Delete files and folders

These skills form the basis of effective file management and manipulation, which enable geospatial professionals to efficiently organize, modify, and clean their data and software projects using the command line.


Creating and Navigating Directories

First, you will focus on creating and navigating directories using the command line interface.

Please open your Terminal application and follow along.

Change to your home directory

cd ~

Create a new directory called gisc605 and change into it

mkdir -p gisc605/cli_basics
cd gisc605/cli_basics
cd ..

Creating Files

Next, you will focus on creating and employing files using the command line interface.

Create an empty new file in `lab2`

touch cli_basics/newfile.txt

Create a new file in `lab2` with some data in it

echo "this file has some data" > cli_basics/anotherfile.txt

Listing Files and Directories

Before you learn about editing and filtering, let's cover how to display the contents of directories and files using the command line interface.

List contents of a directory

ls .
ls cli_basics

List contents of a file

cat cli_basics/anotherfile.txt

Editing and Searching Files and Folders

Next, you will learn how to edit and filter files using the command line interface.

Edit a file with nano

nano cli_basics/newfile.txt

Note: This will open the file in the nano text editor. You can also use this command to create a new file, i.e. nano lab2/yetanothernewfile.txt

  1. Make any desired changes to the file using the keyboard.

  2. To save the changes and exit nano, press Ctrl + O (the letter "O," not zero). You'll be prompted to confirm the filename, so you can simply press Enter to save the changes.

  3. Finally, exit nano by pressing Ctrl + X.

Edit a file with vim

vim cli_basics/newfile.txt
  1. Once the file is open in Vim, you can navigate using the arrow keys or the h, j, k, and l keys (vim's navigation keys). Move the cursor to the location where you want to make changes.

  2. To enter insert mode, press the i key. This allows you to make edits to the file.

  3. Make the necessary changes to the file.

  4. Once you have finished editing, press the Esc key to exit insert mode and return to command mode.

  5. To save the changes and exit Vim, type :wq (write and quit) and press Enter. This command will save the changes made to the file and exit Vim.

  6. If you want to discard the changes and exit without saving, you can type :q! instead of :wq. This will forcefully quit Vim without saving any changes.

Search a directory (and all its subdirectories) for a file

find ~/gisc605/cli_basics -name "newfile.txt"

Note: Running this command will search for the file "newfile.txt" within the directory and display the path if it exists. If the file is present, you'll see the output similar to /path/to/gisc605/lab2/newfile.txt.

Search the current directory (and all its subdirectories) for a file

find . -name "newfile.txt"

Search a file for specific content

grep "this file" anotherfile.txt #case sensitive search
grep -i "THIS" anotherfile.txt #case insensitive search

Search all files in a directory for specific content

grep "this file" ~/gisc605/cli_basics/*

Search all subdirectories in a directory for specific content

grep -r "this file" ~/gisc605/

Deleting Files and Folders

Delete a file

touch filetodelete.txt
ls
rm filetodelete.txt
ls

Delete a directory

mkdir directorytodelete
ls
rm -r directorytodelete
ls

Working with Files and Directories

Make a copy of a file

touch filetocopy.txt
cp filetocopy.txt newcopiedfilelocation.txt
ls

Move a file to a new directory

touch filetomove.txt
mv filetomove.txt ~/gisc605
ls
ls ~/gisc605

Move a file and rename a file

mv ~/gisc605/filetomove.txt ~/gisc605/cli_basics/renamedfiletomove.txt
ls ~/gisc605
ls

Make a copy of a directory

mkdir directorytocopy
cp -r directorytocopy newcopieddirectorylocation
ls

Move a directory

mkdir directorytomove
mv directorytomove newcopieddirectorylocation/directorytomove
ls
ls newcopieddirectorylocation


Installing packages

Install tree with apt

sudo apt install tree

Run the tree tool in the current directory

tree .

Save the outputs of tree into a text file

tree . -o output_filename.txt

Windows only

Open a WSL directory with Windows File Explorer

explorer.exe .

Deliverable

10 points total

Last updated