workshops

Last Updated: 2023-10-11

Introduction to the Unix Shell Part 2: Working with Files and Directories

Setup

Before beginning this lesson, if you do not already have the shell software installed, you will need to download and install it

Overview

This session will be geared around building a directory hierarchy, and so we will be following a series of interactive steps exercises as we learn new Unix commands.

Creating a directory

To begin, use the cd command to move to the Desktop directory on your computer.

mkdir

On your Desktop, create a new personal directory (*choose whatever name you like)

A quick note on file naming

After creating this new directory, move from your Desktop to the new directory using the cd command.

Creating a text file

Nano is a text editor that can be used to create and edit plain text files via the command line. When we say ‘plain text’, it can only work with plain character data, and doesn’t work with tables, images, or any other media. There are many different text editors to choose from, but for the purpose of this lesson, we are going to use Nano because it’s one of the least complex. As you move forward in your Unix journey, feel free to explore and play with different brands of text editors. For the purpose of this lesson, we will be working on .txt files, but there are other plain-text file types that you are also free to explore.

Nano syntax:

Exercise 1

On your desktop, in your personal directory:

More Unix commands

touch

cp

Copies a file. There are a few different ways that this command can be used:

Starting in the position illustrated in top tree diagram, this command will result in the what’s presented the bottom diagram. Feel free to create this hierarchy on your own machine and play around!

Exercise 2


cp

cp Country/Kenny-Chesney/front-porch.wav Party/fp.wav

Exercise 3


cp

cp Country/Kenney-Chesney/front-porch.wav Country/Kenney-Chesney/fp.wav


Exercise 4


We’re now moving on to commands that are a bit more dangerous than those that have already been discussed, in that once they’re executed, there’s no way to undo them. Considering this, always ensure that you’ve backed up all your important files so you won’t have to deal with any unnecessary headaches.

source


mv

Move a file from one destination to another. This command works with the same syntax as the cp command, noting that files are being moved rather than being copied. The one unique function that mv offers in that it can be used to change a file’s name, using the following pattern:


Exercise 5

In your personal directory, create a new directory called “books” Move one of the files containing your 3 favourite books to this new directory, keeping the same file name


The mv command can also be used to change a file’s name while moving it to a new directory.


Now we’re going to move to a command that contains some danger, in that it will delete a file, with no undo button. Be cautious with this command, as you won’t be able to retrive files that are deleted this way.

source

rm


In addition to the commands that we’ve discussed, there are a number of “flags” in Unix that allow us to change the behaviour of a command. For the purpose of this lesson we’ll only be discussing one flag, but for a comprehensive list of Unix flags, see the “Resources” section at the bottom of the page.

-r flag

This is called the “recursive” flag, and allows commands to operate on multiple files or whole directories (the previous commands only work on single files on their own).

To use the -r flag, and any other Unix flag, simply place if after a command, and before an argument:

Syntax: {command} + {-flag} + {directory/files}

Erase a directory

Copy a directory


Moving back to the directory structure you’ve created on your personal machine, we’ve made a of a mess! But that’s ok, because it’s very common for our files to get messy when working on projects. With that said, let’s finish things up cleaning our files up and putting together a clean structure.

source


Exercise 8

Create three new .txt files:

Recreate the following directory structure with the files you’ve been working with. The my-movies, my-music, my-books, and my- foods directories contain the .txt files of your favourites.

References & Resources