Command Line

What is command line?

The command line is a way to interact with the computer using specific words called “commands”. When using the command line, we refer to folders as directories. Files and directories on your computer are organized into a filesystem.

File system

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

  • The first directory in the filesystem is the root directory. It is the parent of all other directories and files in the filesystem.
  • Each parent directory can contain more child directories and files.
  • Each directory can contain more files and child directories.

How to open the command line on your computer

To access the command line, we use the terminal.

  • On Linux: Open the programs menu and search for “Terminal”. You can also open the terminal by pressing CTRL + ALT + T.
  • On Mac: Open your applications folder and find “Terminal”.

Common command lines

Present Working Directory (pwd)

To display the name of the directory you are currently in, use the pwd (print working directory) command.

List files and directories (ls)

To display the contents of the directory you are currently in, on Linux and Mac, use the ls command. When you type ls, the command line looks at the folder you are in, and then “lists” the files and folders inside it.

ls command has several more options. Here are three common options:

ls -a

Use ls -a to list all files contents, including hidden files and directories. Files started with a dot are hidden, and don’t appear when using ls alone.

ls -l

The -l option lists files and directories as a table. There are four rows and seven columns separated by spaces. Here’s what each column means:

  • Access rights. These are actions that are permitted on a file or directory.
  • Number of hard links. This number counts the number of child directories and files. This number includes the parent directory link (..) and current directory link (.).
  • The username of the file’s owner. Here the username is cc.
  • The name of the group that owns the file. Here the group name is eng.
  • The size of the file in bytes.
  • The date & time that the file was last modified.
  • The name of the file or directory.

ls -t

  • Use ls -t order files and directories by the time they were last modified.

ls -alt

ls -alt lists all contents, including hidden files and directories, in long format, ordered by the date and time they were last modified.

Change Directories (cd)

To navigate to another directory, you can use cd command. cd stands for “change directory”. The cd command takes a directory name as an argument, and switches into that directory.

cd jan/memory/ 
  • cd.. will navigate up to the parent of the current directory.
  • Change directories up two levels cd../..

Create Directories (mkdir)

To create a new directory, you can use the mkdir command.

The mkdir command stands for “make directory”. It takes in a directory name as an argument, and then creates a new directory in the current working directory.

mkdir content

Create new files (touch)

To create a new file, use the touch command. touch new-file.txt.

Rename (rm)

To rename a directory or file, use the mv command

mv folder/old-file.txt folder/new-file.txt.

Copy files (cp)

The cp command copies files or directories. To copy a file into a directory, use cp with the source file as the first argument and the destination directory as the second argument.

$ cp a.txt b.txt

Move files (mv)

To move a file into a directory, use mv with the source file as the first argument and the destination directory as the second argument.

Delete files (rm)

  • rm removes files
  • rm -r removes directories
rm test.txt
rm -r content-folder

Redirect commands

The common redirection commands are:

  • > redirects standard output of a command to a file, overwriting previous content.
  • >> redirects standard output of a command to a file, appending new content to old content.
  • < redirects standard input to a command.
  • | redirects standard output of a command to another command.

cat Display

cat displays the contents of one or more files to the terminal

$ cat hello.txt

grep searches for a text pattern and outputs it.

grep 'anna' names.txt

sort

sort sorts lines of text alphabetically.

sort names.txt

uniq

uniq stands for “unique” and filters out adjacent, duplicate lines in a file.

sed

sed searches for a text pattern, modifies it, and outputs it. It is similar to “find and replace”.

$ sed 's/snow/rain/' forests.txt 

In the expression ’s/snow/rain/':

  • s: stands for “substitution”. it is always used when using sed for substitution.
  • snow: the search string, the text to find.
  • rain: the replacement string, the text to add in place.

Bash scripting

Bash (or shell) scripting can automate repetitive tasks. Any command that can be run in the terminal can be run in a bash script. The beginning of your script file should start with #!/bin/bash. For example, in script.sh file, run this:

#!/bin/bash

1. Variables

Variables are assigned using an equals sign with no space. For example: greeting="hello" To access the value of a variable, we use a dollar sign ($)

echo $greeting

2. Conditionals

You can use conditionals to control the set of commands. Conditionals use if, then, else, fi syntax.

first_greeting="Nice to meet you!"
later_greeting="How are you?"
greeting_occasion=1

if [ $greeting_occasion -lt 1 ]
then
  echo $first_greeting
else
  echo $later_greeting
fi

In the example above, if the variable is less than 1, use first_greeting. Otherwise, we use later_greeting.

Bash scripts use a unique set of comparison operators:

  • Equal: -eq
  • Not equal: -ne
  • Less than or equal: -le
  • Less than: -lt
  • Greater than or equal: -ge
  • Greater than: -gt
  • Is null: -z

When comparing strings, we should put the variable into quotes (") to prevent errors if the variable is null or contains spaces. The common operators for comparing strings are:

  • Equal: ==
  • Not equal: !=
if [ "$hello" == "$name"]

3. Loops

Three types of loops can be used: for, while, and until.

3.1 For loop

for name in $hello
do
  echo $name
done
3.2 While loop
#!/bin/bash
first_greeting="Nice to meet you!"
later_greeting="How are you?"
greeting_occasion=0
while [ $greeting_occasion -lt 3 ]
do
  if [ $greeting_occasion -lt 1 ]
  then
    echo $first_greeting
  else
    echo $later_greeting
  fi
  greeting_occasion=$((greeting_occasion + 1))
done

3.3 Until loop

until [ $index -eq 5 ]
do
  echo $index
  index=$((index + 1))
done

4. Input

  • Input arguments can be passed to a bash script after the script name, separated by spaces (myScript.sh “hello” “how are you”).
  • Input can be requested from the script user with the read keyword.
echo "What's your name?"
read name
echo "Your name is $name"

5. Aliases

Aliases can be created in the .bashrc or .bash_profile using the alias keyword. It allows calling your scripts without the full filename.

alias hello='./hello.sh'