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
home directorycd ~Create a new directory called gisc605 and change into it
gisc605 and change into itmkdir -p gisc605/cli_basics
cd gisc605/cli_basicsNavigate back/up a directory level
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.txtCreate a new file in `lab2` with some data in it
echo "this file has some data" > cli_basics/anotherfile.txtListing 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_basicsList contents of a file
cat cli_basics/anotherfile.txtEditing 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.txtNote: 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
Make any desired changes to the file using the keyboard.
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 pressEnterto save the changes.Finally, exit nano by pressing
Ctrl + X.
Edit a file with vim
vim cli_basics/newfile.txtOnce the file is open in Vim, you can navigate using the arrow keys or the
h,j,k, andlkeys (vim's navigation keys). Move the cursor to the location where you want to make changes.To enter insert mode, press the
ikey. This allows you to make edits to the file.Make the necessary changes to the file.
Once you have finished editing, press the
Esckey to exit insert mode and return to command mode.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.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 searchSearch 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
lsDelete a directory
mkdir directorytodelete
ls
rm -r directorytodelete
lsWorking with Files and Directories
Make a copy of a file
touch filetocopy.txt
cp filetocopy.txt newcopiedfilelocation.txt
lsMove a file to a new directory
touch filetomove.txt
mv filetomove.txt ~/gisc605
ls
ls ~/gisc605Move a file and rename a file
mv ~/gisc605/filetomove.txt ~/gisc605/cli_basics/renamedfiletomove.txt
ls ~/gisc605
lsMake a copy of a directory
mkdir directorytocopy
cp -r directorytocopy newcopieddirectorylocation
lsMove a directory
mkdir directorytomove
mv directorytomove newcopieddirectorylocation/directorytomove
ls
ls newcopieddirectorylocationInstalling packages
Install tree with apt
sudo apt install treeRun the tree tool in the current directory
tree .Save the outputs of tree into a text file
tree . -o output_filename.txtWindows only
Open a WSL directory with Windows File Explorer
explorer.exe .Deliverable
10 points total
Last updated