Shell - Navigation

Jan. 6, 2021 pexels-pixabay-35888.jpg Vuong Huynh

LCL-fileTrees-01.png

List of Command-Line Commands from Codecademy

A good document from Cardiff University


A command is a directive to the computer to perform a specific task.

When using the command line, we refer to folders as directories. Files and directories on your computer are organized into a filesystem.


Filesystem

A filesystem organizes a computer’s files and directories into a tree structure:

  1. The first directory in the filesystem is the root directory. It is the parent of all other directories and files in the filesystem.
  2. Each parent directory can contain more child directories and files. In the filesystem on the right, blog/ is the parent of 2014/, 2015/, and hardware.txt.
  3. Each directory can contain more files and child directories. The parent-child relationship continues as long as directories and files are nested.

We will use the tree structure from the picture provided by Codecademy as above.

In the terminal, the first thing you see is $. This is called a shell prompt. It appears when the terminal is ready to accept a command.


ls

When you type ls, the command line looks at the directory you are in, and then “lists” all the files and directories inside of it.

When we type:

$ ls

the terminal would display our current directory’s files and directories:

2014  2015  hardware.txt

ls -a lists all contents in the working directory, including hidden files and directories.

ls -l lists all contents of a directory in long format. Here’s what each column means.

ls -t orders files and directories by the time they were last modified.


pwd

pwd stands for "print work directory" and it shows the "present working directory".

Suppose we are in blog directory, if we type:

$ pwd

the screen will show:

/home/ccuser/workspace/blog


cd

cd stands for "change directory". Just as you would click on a folder in Windows Explorer or Finder, cd switches you into the directory you specify. In other words, cd changes the working directory.

Let’s say the directory we change into is 2015:

$ cd 2015

The cd command takes a directory name as an argument and switches into that directory.

Instead of using cd twice in order to move from 2015 to memory, we can use it once and give it a longer argument:

$ cd jan/memory

To move up one directory, we use cd ..:

$ cd ..

Here, cd .. navigates up from jan/memory/ to jan/.

To move upwards in the filesystem we use cd .., and to move downwards we use cd next-directory. We combine directions using a /:

$ cd ../next-directory

To navigate a level up in the filesystem we use cd .. and to combine directions, we use a /. So to navigate up two levels we would use:

$ cd ../..

The command below will navigate to the root directory:

$ cd /


mkdir

mkdir is making directories (folders)

$ mkdir media

It takes in a directory name as an argument and then creates a new directory in the current working directory. Here we used mkdir to create a new directory named media/ inside our current working directory.

We could make the new directory from our current position by using a / to combine arguments:

$ mkdir media/tv

We have just made a tv directory inside our child directory media without cd media command.


touch

We can create a new file using the command touch:

$ touch keyboard.txt

It takes in a filename as an argument and then creates an empty file with that name in the current working directory. Here we used touch to create a new file named keyboard.txt.

We can use a / to direct to a directory and create a new file inside it:

$ touch media/mouse.txt


cp

cp copies files or directories from one directory to another directory.

$ cp cpu.txt memory/

Here, we copy the file cpu.txt and place it in the memory/ directory


mv

mv moves a file into a directory.

$ cp cpu.txt memory/

Here, we move (cut) the file cpu.txt and place it in the memory/ directory


rm

rm removes or deletes files. Below, we remove the file cpu.txt from the current working directory

$ rm cpu.txt

rm -r deletes a directory and all of its child directories.


nano

nano is a command-line text editor. It works just like a desktop text editor like TextEdit or Notepad, except that it is accessible from the command line and only accepts keyboard input.

nano command will create a new file if that filename does not exist.


alias

The alias command allows you to create keyboard shortcuts, or aliases, for commonly used commands.

$ alias pd="pwd"


Wildcards (*)

The wildcard * selects all of the files in the current directory.

$ cp * media/

Copy all files to media/ directory

$ cp m*.txt media/

Copy all files start with m and end with .txt to media/ directory


Helper Commands

clear is used to clear your terminal, which is useful when it’s full of previous commands and outputs. It doesn’t change or undo your previous commands, it just clears them from the view. You can scroll upwards to see them at any time.

tab can be used to autocomplete your command. When you are typing the name of an existing file or directory, you can use tab to finish the rest of the name.

The up and down arrows ( and ) can be used to cycle through your previous commands. will take you up through your most recent commands, and will take you back through to the most recent one.

To copy and paste in shell terminal, we use CTRL INSERT and SHIFT INSERT


Project: Bicycle World

In this project, you’ll use the commands you learned to navigate and edit the filesystem of this special shop.

The starting filesystem is shown below. (Bicycle World recently changed owners, who named the main directory bicycle-world-ii.)

bicycle-world-ii
|—— brands.txt
|—— freight/
|   |—— messenger/
|   |—— porteur/
|—— mountain/
|   |—— downhill/
|   |   |—— heavyweight/
|   |   |—— lightweight/
|   |—— hardtail/
|—— racing/
    |—— road/
    |—— track/


Print the working directory.

$ pwd
/home/ccuser/workspace/bicycle-world-ii


List the files and directories in the working directory.

$ ls
brands.txt  freight  mountain  racing


Change directories to the freight/ directory and list all its content.

$ cd freight

$ ls
messenger  porteur


Change directories to the porteur/ directory.

$ cd porteur


Change directories up two levels to the bicycle-world-ii/ directory.

List the files and directories in the bicycle-world-ii/ directory.

$ cd ../..
$ ls
brands.txt  freight  mountain  racing


Change directories to the mountain/downhill/ directory.

$ cd mountain/downhill


Make a file called dirt.txt.

$ touch dirt.txt


Make a file called mud.txt.

$ touch mud.txt

List the files and directories in the downhill/ directory.

$ ls
dirt.txt  heavyweight  lightweight  mud.txt


Downhill biking is dangerous: In the downhill/ directory, make a directory called safety/.

$ mkdir safety


Change directories to the bicycle-world-ii/ directory.

$ cd ../..


List the contents of the bicycle-world-ii/ directory.

$ ls
brands.txt  freight  mountain  racing


The shop is adding a new type of bike!

In bicycle-world-ii/, make a directory called bmx/.

$ mkdir bmx


Without changing directories from bicycle-world-ii/, make a file in the bmx/ directory called tricks.txt.

$ touch bmx/tricks.txt

$ ls
bmx  brands.txt  freight  mountain  racing