Intro to the Linux Shell

Shell Overview

What is the shell? Shell is actually a generic term for any interface to your computer. When talking about linux (or any other unix-like system), usually the shell refers to the command line interface (CLI). The command line interface most people are familliar with is DOS. There are actually several CLI shells that are common, however the most common default shell for the majority of the modern unix-like systems is BASH the Bourne Again Shell. The shell is sometimes referred to by several names, terminal, CLI, console, etc..

BASH Commands

Some of the most commonly used BASH commands are: cd, cat, man, ls, pwd, mkdir, touch, file, grep, help, less

Commands that give Help – One of the first things to learn how to do on any unfamiliar system is to get it to tell you how to use it. Within Linux there are a plethora of commands that provide great information on how to use the system.

help – the help command is useful in that it shows you information on what your shell can do for you. Most of this information is very specific to the shell you are using, but it will also list all of the commands built-in to your shell. It will display more information than you will be able to use right now, but it is good to know that it is there, it will help you later. Its also important to note, that if you see a command listed after help, such as cd, you can use that as an argument to help, by typing help cd.

man – the next useful command to know is man. Man is short for manual. When a program is installed, most of the time it also comes with a man page. The man pages are specific to each program. For example to access the man page for the ls command you would type man ls. You’ll notice that man pages are layed out really nice, and you can scroll up and down by using the arrow keys.

After that there is one more useful trick for getting help about commands. Almost every command in linux provides basic usage information by passing it –help. For example if you type grep –help, it will tell you how to use grep. Its usually more technical and specific than man pages so its a good resource to have in addition to man pages. In the process of learning linux there is one help source that is better than anything else: Google. If you dont know how to use something, type it into Google, it has been a personal lifesaver many times.

Commands that Navigate Files & Directories – Now that we know how to get some basic help about our system, lets see if we can start navigating.

ls – the ls command stands for list. If you type ls into your terminal, you will get a list of the files of the directory you are currently in. By default, when you open up a shell it’s current path with be your home directory. This is a place for you to store all of your files where nobody else but the root user can access them on your computer. You may notice that there are a lot of files in this directory, they are not actually files, The blue color indicates that they are directories. Directories are also sometimes referred to as folders, these terms are interchangeable.

So looking at the list of files and directories from the output of the ls command, you may want to enter a directory like the Downloads directory. In order to do this you need the change directory or CD command.

cd – the change directory command allows you to navigate into different directories. From the default home directory if you type: cd Downloads, you will enter the Downloads directory. You may notice that not much visibly changes, other than the ~, signifying the Home directory is replaced with Downloads. This means that the current working directory has changed to Downloads. This shows you the relative current directory, but how do you find out where you are in relation to anything else?

pwd – the pwd command will print the current working directory. This tells you where you are in relation to the root of the filesystem / . The single forward slash represents the root of the filesystem. The command pictured above reveals that the current directory Downloads is three directories from root. The current directory is underneath Downloads, which is underneath dan, which is underneath home, which is on the root of the filesystem /. Now that you know where you’re at, how do you get back to the home directory?

{loadposition adposition6}There are many ways to navigate a filesystem using cd. Understanding relative vs absolute paths is important. There is no right or wrong way to get to different directories. There are a few important things to note:

./  = indicates the current directory

../ = indicates one directory higher up in the filesystem

/ = indicates the root of the filesystem

~ = current user’s home directory

cd /home/dan/Downloads is an example of an absolute path – cd’ing to that will always yield the same results

cd ./Downloads or just cd Downloads, is an example of a relative path, cd’ing to that will yield different results depending on the current working directory.

Within the current example, if i wanted to get back to my home folder named “dan”, there are four basic ways of doing it.

cd ../ = going up one directory

cd /home/dan = passing the absolute path to my home directory “dan”

cd ~ = passing tilde which stands for your home directory

cd = simply issuing the cd command takes you back to your home directory

They all do the same thing, they take me back to my home directory.

Commands that find files – how do you find or search for files and directories in Linux? There are two very common commands to find files, and they work off of two different principles.

find – find is a very power program it operates by iterating through every file/folder on the entire filesystem. It has many options, and I recommend referencing the man page to master it.

find / -name bash = find a file named bash located somehwere under /

locate – locate works much more like a search engine, in that it requires indexing to occur before it returns any results. As a result, locate can find files/directories much faster, however indexing can take some time, although indexing only has to be performed after the file system changes.

updatedb – indexes the file system, updating the locate database

locate bash = find bash somewhere underneath root (/)

Video Tutorials

Linux terminal commands for Beginners –  In this series of tutorials, I cover basic, Bash shell commands for navigating, searching, getting help, creating files and directories, editing text and more. Commands that are covered are: pwd, ls, cd, mkdir, touch, rm, rmdir, man, less, more, mv, cp, cat, updatedb, sudo, locate, file.

Part 1 – Navigating and viewing directories, absolute and relative paths (pwd, cd, ls)

Part 2 – Command arguments, long format, hidden files (ls -l, ls -a)

Part 3 – Piping, viewing output, and getting help (ls, | , more, less, man, –help)

Part 4 – Making and removing files and directories (mkdir, touch, rm, rmdir)

Part 5 – Moving, copying, renaming, editing and viewing files (mv, cp, nano, cat)

Part 6 – Searching for files, elevating permissions (locate, updatedb, sudo, file)

Author: Dan

Dan teaches computer networking and security classes at Central Oregon Community College.

Leave a Reply

Your email address will not be published.