Setting up

In this tutorial series, we'll see how to use the Linux Command Line.

Download and print out the Cheatsheet

Before we get our hands dirty, please print out your very own Linux Command Line Cheatsheet. It's a good reference to have, so print it out and tape it to your wall where it's clearly visible!

VMware - Recommended Setup for All Users

To make full use of this tutorial series and really learn Linux Command Line we recommend you download VMware Workstation with a copy of Ubuntu 16.04.

VMware is a great way to get Linux on your computer, regardless of platform. Here, I am creating a new VMware machine, pointing to the Ubuntu 16.04 iso file I downloaded.

If you're having trouble with any of the setup, try Googling error messages or commenting below!

Alternatives to VMware

Mac OS X Users

If you're on a Mac, you're partly in luck! Mac is built on top of a Unix platform, meaning it comes equipped with its own terminal with commands that are very similar to that of Linux.

To open the terminal, simply search for "Terminal" in your launcher, (⌘+Space).

If you want a highly customizable, and more pleasant version of the Terminal, I recommend you download iTerm2.

The Mac OS X command line is very similar to Linux Command Line, but you'll see a few commands are a tad bit different.

Windows Users

If you're on a Windows, simply download Cygwin, which allows for a similar interface as a terminal on a Unix platform.

If you're on Windows 10, it will be better to simply install the Bash Shell.

Linux Users

If you're running Linux, there is a Terminal you can launch by searching "Terminal." On Ubuntu, you can easily open this by holding Ctrl+Alt+T .

Keyboard inputs and outputs in this tutorial series

$ # All commands in this tutorial are preceded with a $. # All outputs will be printed on the next line. # Any code in italics are just example options, files or directory names.

Let's now get to it!

Introduction to the Shell $PATH, cal, exit

When we talk about the command line we actually are referring to the shell . The shell takes whatever you type in, searches for that command in the directories list in your $PATH variable then runs them through the operating system.

Checking the directories in $PATH

To take a look at the $PATH variable type echo $PATH .

$ echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

Each directory is separated by a colon (:). We'll look more in-depth about the $PATH variable, and the echo command soon.

Viewing available binaries

For now, let's look at the contents within one of the folders in our path. We can do this with the ls command. Let's look in one of our bin folders, which is short for binaries .

$ ls /usr/bin

... corelist cpan cpan2dist cpanp cpanp-run-perl cpp cpp-4.8 crc32 ...

You should get a long list of commands that are available to you. We'll learn more about folders, the file system and the ls command soon, but just know that the commands you enter in the command line actually come from executable files and not from thin air.

Now try typing some random letters into your terminal.

$ asdf

-bash: asdf: command not found

You should get an error. This is because it's not a command that lives in one of our $PATH directories.

A simple command

Now try the calendar command, cal , which outputs the current month's calendar.

May 2016 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

There are tons more commands you can use, and even more you can install and write yourself!

In this tutorial series, we'll learn the most commonly used commands, and learn some practical concepts of using the Linux Command Line. Through learning about the commands, we'll slowly begin to transition into learning about the Linux Operating System as a whole.

Exiting like a champ

To exit the terminal from the command line, use:

$ exit

Command Structure commands, options, arguments

Before we take a look into some commands, let's take a formal look at how they are structured.

Three main components of a command

There are three main components that go into a command.

1) Command

When entering a command, order matters. It's important to place the command name first - otherwise, the shell won't know how to process your submission.

2) Options

Most commands come with certain options, which modify how the command is run. To apply an option, trail it immediately after your command with a hyphen ( - ).

There is also a long option form of each short option that uses two hyphens ( -- ). We'll stick to the short option for the sake of brevity.

$ command -shortOptions $ command --longOptions

Note that some commands that we'll discuss in the future have actions instead of options. For example, when we later learn about package management, we'll see that some commands use actions words (eg. apt-get install packagename.deb instead of options.

3) Arguments

An argument is some information you pass to the command when it is started.

$ command -options arguments

You may have multiple options and arguments, as we'll discuss real soon.

How commands are interpreted

We can use the type command to see how our shell interprets our command.

1) As a shell builtin

One of the many possibile ways that the shell may interpret a command is as a shell builtin. This means that the command is called from the shell and directly executed in the shell itself.

$ type type

type is a shell builtin

2) As an external program

Commands may also be loaded and run from an external file.

$ type ls

ls is hashed (/bin/ls)

3) As an alias

Another way a command can be referenced is through user-defined aliasing. In the example below, we aliased db to guide us to our Dropbox directory. We'll learn how to implement an alias real soon.

$ type db

db is aliased to `cd ~/Dropbox'

Commenting in the shell

To comment in the shell, use the pound ( # ) symbol. Anything after # will be ignored.

The Linux File System pwd, ls, cd

What is a File System?

Computers organize their files using a tree-like structure known as a file system .

Linux has a single file tree system, which starts with the root directory ( / ). On a Windows, the "root" would be something like C:\ .

The following image shows what a Linux File System would look like.

The Linux file system

In Linux, almost everything is represented as a file (or directory) on the File System. This includes USB devices and hard disks, which are stored under /dev .

Naming files

A filename can contain uppercase or lowercase letters, numbers and most punctuation characters. Note that filenames are case sensitive!

Special characters

Some files begin with a dot ( . ) and are used for configuration settings. Others can end with a tilden ( ~ ), denoting that it is a backup file. Furthermore, we'll see how we can use ? and * to denote special cases when we learn about wildcards and globbing. Although you can name your files with these symbols, it's highly not recommended.

Checking your Current Directory

You can check to see where you are along this file system with the pwd command (short for "print working directory").

$ pwd

/home/JohnDoe/Dropbox/FunStuff

Here, we can see that we are in our FunStuff directory, which is within our Dropbox folder.

Navigating the File System cd

Now that we are able to see the directories we can navigate to, we can move into them. To navigate into a directory, use the cd command, and specify a pathname:

$ cd path/to/directory

Relative and Absolute paths

There are two ways you can specify a path - relative and absolute .

Relative paths

When we talk about relative paths, we are referencing relative to the current working directory (where we are in the file system).

For example, if we were in the Dropbox directory, and wanted to move into FunStuff , the relative path would simply be FunStuff .

Note that we can only call this relative path if we are in a certain directory.

Absolute paths

Absolute paths, on the other hand, start at the root of the File System ( / ) and can called wherever you are in the file system.

For example, /home/Dropbox/FunStuff/ would be an absolute path since it starts at the root directory and we can reference it from anywhere along the file system.

Directory shortcuts

Here are some shortcut notations to directories.

/ Root Directory . Current Directory .. Parent Directory ~ Home Directory - Previous Directory

Try using cd command with one of the above directory shortcuts!

$ cd / # change to root directory $ cd - # change to folder you were previously in $ cd .. # change to the parent directory (one directory up the file system)

Listing files ls, -a, -l

Listing directory files with ls

When you start up a terminal session, you are placed within your home directory (denoted by a ~ ). To list contents of your current working directory, simply use the ls command (short for list).

$ ls

file1.txt file2.txt file3.txt file4.txt

Here, we can see that four text files reside in our current working directory.

Additionally, you can list files within other directories by specifying the pathname after the ls command.

$ ls ~/Dropbox

Books Library Movies School Work

Listing hidden files with the -a option

Hidden files are usually those that have to do with the configuration of certain programs. They are kept hidden from the user, and have a . preceding its name.

For example, the .bash_profile file, located in your home directory, is run every time you start a new terminal to configure your environment. Furthermore the .vimrc file configures your Vim settings. However, you can't see these files with a normal ls command. To list the hidden files use the option -a .

$ ls -a

. Applications Library .. Desktop Movies .bash_history Documents Music .bash_profile Downloads Public .vimrc Dropbox

Can you see your secret files now?

Listing file details with the -l option

To see more details of the files, we can pass the -l option.

You should see a list of files with the following format (don't worry if your date format isn't exactly the same):

$ ls -l

total 12 drwx------ 6 JohnDoe staff 204 Jan 7 10:30 Applications drwx------+ 37 JohnDoe staff 1258 Mar 20 17:58 Desktop drwx------+ 6 JohnDoe staff 204 Mar 13 08:57 Documents drwx------+ 29 JohnDoe staff 986 Mar 22 00:06 Downloads drwx---rwx@ 25 JohnDoe staff 850 Mar 20 15:38 Dropbox drwx------@ 51 JohnDoe staff 1734 Mar 17 17:42 Library drwx------+ 3 JohnDoe staff 102 Dec 29 00:00 Movies drwx------+ 3 JohnDoe staff 102 Dec 29 00:00 Music drwxr-xr-x 4 JohnDoe staff 136 Mar 13 11:15 NetBeansProjects drwx------+ 4 JohnDoe staff 136 Dec 29 20:39 Pictures drwxr-xr-x+ 5 JohnDoe staff 170 Dec 29 00:00 Public drwx------ 4 JohnDoe staff 136 Mar 20 11:31 VirtualBox VMs

Let's break this down per attribute.

drwxr--rwx Access file rights. We will talk about this when we go over permissions. 1 The file's number of hard links. We will talk about links in our links lesson. JohnDoe The file's owner. root The file's group owner. 1267 Size of file in bytes (kb). Dec 21 16:02 Date and time of file's last modification. helloWorld.txt Name of file.

Other options to use with ls

There are other options you can use, but -l and -a are the most common.

-A List all entries except for implied . and .. -F Indicator samp to see what type of file it is. / for directory, @ for symbolic link, = for socket, | for pipe. -G Colorized output. Can also use --color on some distros. -R Recursively list subdirectories. -S Lists and sorts results by file size. -t Sorts by modification time. -r Sorts in reverse-alphebetical order.

Combining Options To combine options, simply places the two together: $ ls -la

Copying, Moving and Renaming Files and Directories cp

Copying files

To copy a file into another directory, use the cp command with the file name as the first argument, and the target path name as the second.

$ cp fileName destination

You may rename the file by attaching it to the end of the path.

$ cp fileName destination/newFileName

This takes a file, and sends a copy of it to the specified path, with the new file name.

And to copy into the same directory with a new name:

$ cp fileName newFileName

Copying Directories

To copy directories and the contents within, we must apply the -r option, which is the command to copy recursively.

The following command would copy all contents of dir1 into dir2 .

$ cp -r dir1 dir2

Copying multiple files in one go! We can copy multiple items at once into a directory. As long as the last argument is the path name, we're good. $ cp file1 file2 file3 pathName

Additional options

Here are some additional options for fine-tuning.

-f Force overwriting. -i Interactive mode. The program will ask before overwriting any files. -p Preserves ownership and permissions. -a Perform archive. Preserves ownership and copies links as is. -u Update. Only copy the file if original is newer than target (or if target does not exist).

Moving and Renaming files mv

Moving and renaming are performed with the same command, mv .

Moving

To move a file, use the mv command.

Our last argument should be the target path to that directory.

$ mv file1 file2 file3 dir1/

Creating good habits

A good habit to form is to make sure that your target path ends with a / if you are using a directory. Having the / at the end tells the shell that the destination is a directory. Check out how the shell notifies you that the directory does not exist when we accidently commit a typo.

$ mv file1 /home/JohnDoe/diretcry/

mv: rename file1 to /home/JohnDoe/diretcry/: No such file or directory.

If we were to omit the / , the shell would assume that we are trying to move our file to /home/JohnDoe and rename our file to diretcry .

Renaming

We can use mv command to move a file to your current directory with a new name (in other words, rename it).

$ mv oldFileName newFileName

Extra options

mv shares common options with cp .

-i Interactive mode. Before overwriting a file, prompt the user. -u Update. Copy only files that either don't exist or are newer than the existing corresponding files in the destination directory. -v Verbose. Ouput messages as they are being copied.

Making Files and Directories touch, mkdir

Creating empty files

To make an empty file, use the touch command.

$ touch file1 file2 file3

Managing a file's timestamp

The touch command is built for much more than just creating empty files. It can be used to update timestamps. Specify a particular option to modify a certain time attribute.

-a Modifiy the access time. -m Change the modification time. -c Do not create any files that already exist. -t timestamp Set the timestamp in the form MMDDhhmm[[CC]YY][.ss] . MM MM is month, DD is day, hh is hour, mm is minute, CC[YY] is year, and ss is second. -r reffile Replicate the timestamps from another file.

This command can also be used to update the access date or modification date of a file or directory.

You can access the modification time with a simple ls -l command.

Making directories

To make a directory, simply use the mkdir command:

$ mkdir dir1 dir2 dir3

This makes a directory named dir1 , dir2 and dir3 .

Removing files and directories

To remove (aka delete) a file, use the rm command.

$ rm file1

To remove an entire directory, add the -r option, which means to delete recursively. You may also use the rmdir command, but this only works if the directory is empty.

$ rm -r dir1

Deleting without the -r option results in an error.

Be careful when deleting! Be careful when you delete! There is no undelete command, although some distros implement some sort of recyling bin.

Options for removing

Here are some useful options to use for the rm command.

-f Force. Ignore nonexistent files (for use with wildcards) -i Interactively. Prompts user for permission for each file to be deleted. -v Verbose. Displays informative messages as deletions are being performed.

Great work! Now let's turn to more intermediate commands.