Command Line Introduction

@itspoma / CURSOR Education School
v1.5

Agenda

  • Shell introduction
  • Shell evolution
  • File system organization
  • Basic command (Paths, Navigation, Files, etc)
  • Manipulation (viewing and changing)
  • Environment variables
  • Pipes And Redirection
  • The Super User

What Is "The Shell"?

program,
that takes commands from the keyboard,
perform operations,
gives output

What's a "Terminal"?

terminal emulator
is a program,
that opens a window,
and lets you interact with the shell

Philosophy


  • program do one thing,
  • programs works together,
  • programs handle text streams

Command Line Interface (CLI)

text-way to interact with PC


  • Terminal on a Mac
  • Command Prompt on Windows
  • PowerShell on Windows

CLI: Purpose

  • jump between files and directories
  • open stuff up
  • run simple scripts
  • check the status of things
  • ...

What is the Command Line?

Command Line
Bash
Bourne-Again SHell
Unix Shell

Anatomy of prompt


Romans-Macbook-Pro:~ somename$

<computer-name>:<current-directory> <username>$
<computer-name>:<current-directory> <superuser>#
              

Starting A Terminal

  • Windows:
    Start button, clicking All Programs, clicking Accessories, and then clicking Command Prompt
  • Windows:
    Wnd+R, then write "cmd", and hit Enter
  • Mac:
    Using CMD+SPACE, find and run a "Terminal" application


Testing The Keyboard


[me@linuxbox me]$

[me@linuxbox me]$ kdkjflajfks
bash: kdkjflajfks: command not found
              

Thompson shell (1971)


команда1> команда2>
команда1 | команда2
              

Bourne Shell (sh) (1977)

  • filters
  • flow control & variables
  • I/O control
  • SIGNAL control
  • optimizations (command-length max, etc)
  • 2>

Bourne again shell (bash) (1987)


#!/bin/bash
echo Hello World!
              

Korn-shell (ksh) (1983)

TENEX C Shell (tcsh)

File system (Windows)

File system (Unix)

File system (Unix)

File system (Unix): A Guided Tour

/ the root directory
/boot Linux kernel and boot loader files
/etc the configuration files for the system
/bin, /usr/bin contain most of the programs for the system
/sbin, /usr/sbin programs for system administration
/usr variety of things that support user applications
/var contains files that change as the system is running (/var/log)
/home users keep their personal work
/tmp for temporary files

Taking a Look Around


$ command -options arguments
              

$ echo "hello world"<enter>
$ ls -la
$ cd My\ Folder\ Name
              
Documents != documents

$ pwd


[me@linuxbox me]$ pwd
/home/me
              

$ ls


[me@linuxbox me]$ ls

Desktop     Xrootenv.0    linuxcmd
GNUstep     bin           nedit.rpm
GUILG00.GZ  hitni123.jpg  nsmail
              

$ ls


$ ls
$ ls /bin
$ ls -l
$ ls -l /etc /bin
$ ls -la ..
              

$ cd


[me@linuxbox me]$ cd /usr/bin
[me@linuxbox bin]$ pwd
/usr/bin [me@linuxbox bin]$ ls

[                     lwp-request
2to3                  lwp-rget
2to3-2.6              lxterm
a2p                   lz
aalib-config          lzcat
aconnect              lzma
acpi_fakekey          lzmadec
acpi_listen           lzmainfo
add-apt-repository    m17n-db
addpart               magnifier

and many more...
              

$ file


[me@linuxbox me]$ file <name_of_file>
              

Basic commands (Unix)


$ pwd
$ hostname
$ mkdir
$ cd
$ ls
$ rmdir
$ cp
$ mv
$ cat
$ find
$ grep
$ man
$ env
$ echo
$ exit
$ sudo
$ chmod / chown
              

Man Pages


$ man ls
              

What is your computer name?


$ hostname
Zeds-MacBook-Pro.local
              

Where are we now?


$ pwd      # Your Current Directory
/Users/zedshaw

$ ls       # View Directory Contents
$ ls <directory-name>
$ ls -la 
$ ls -la .
$ ls -la *.txt

$ open .   # Open a file/directory
              

Manipulating with patterns

* everything
g* everything that begins with character "g"
b*.txt begins with "b", and ends with ".txt"
Data??? begins with the characters "Data" followed by exactly 3 more characters
[abc]* begins with "a" or "b" or "c" followed by any other characters

Directories


~/Documents/some_folder/some_other_folder/some_file.tx

. = root
.. = up
~ = home directory
              

$ cp


[me@linuxbox me]$ cp file1 file2
[me@linuxbox me]$ cp file... directory
              

$ mv


[me@linuxbox me]$ mv filename1 filename2
[me@linuxbox me]$ mv file... directory
              

$ rm


[me@linuxbox me]$ rm file...
[me@linuxbox me]$ rm -r directory...

Be careful with rm!
[me@linuxbox me]$ rm -rf /
              

$ mkdir


[me@linuxbox me]$ mkdir directory...
              

Quiz


$ cp *.txt text_files
$ mv my_dir ../*.bak my_new_dir
$ rm *~

$ ls **/*.jpg > jpg-dir.txt

$ unexisted-command 2> /dev/null
              

Directories: Let's Move Around


$ pwd

$ cd <folder-name>

$ cd ..
$ cd ~
              

Let's Make, View and Destroy Stuff


$ mkdir some/folder
$ mkdir -p some/folder/

$ touch test.txt

$ open test.txt

$ cat test.txt

$ mv test.txt test2.txt
$ rm test.txt

$ cp test.txt
              

$ alias


$ alias ll='ls -la'

$ alias rmi='rm -i'

$ alias cia='. /home/sydney/env/cia.sh'
              

$ history


$ history

$ !!       # recall the latest command
$ !1003    # recall the latest command by its number
$ !cat     # recall the latest command matching a starting string
              

$ find


$ find . -name test.js

$ find /etc -name '*.txt'
$ find . -name '*.xml'
$ find . -not -name '*.xml' -maxdepth

$ find . -type f

$ find . -mtime -1
$ find . -mmin -15

$ find . -size -1k
              

$ grep

Global Regular Expression Print
g/re/p

for finding text inside files


$ grep <string> <file or directory>

$ grep 'some text' file.js
$ grep something file.js
$ grep -r 'Dao[v1|v2]' src.js

$ grep -i
              

$ sed

$ awk


condition { actions }

$ awk 'END { print NR }' server.log
              

Working With Commands


# Display information about command type
# (executable, built-in shell, function, alias)
$ type cp
cp is /bin/cp

# Locate a command
$ which ls
/bin/ls

# Display reference page for shell builtin
$ help -m cd
...

# Display an on-line command reference
$ man ls
...
              

Permissions


$ ls -l
total 72
# Permissions  Links  Owner  Group  Size  Modification  File Name
drwxr-xr-x   2 root root  4096 Oct  5 09:31 bin
drwxr-xr-x   3 root root  4096 Oct  9 21:47 boot
drwxr-xr-x   1 root root     0 Jan  1  1970 dev
...
              

Permissions: explanation

drwxr-xr-x
d - type of file (- regular file, d directoty, l symbolic link, etc)

Permissions: types

drwxr-xr-x

  • Read (r) - see the contents of a file
  • Write (w) - modify its contents
  • Execute (x) - you wish to run it (the file is a program)
  • Anything (-)



:type : owner : group : restofworld:
:d    : rwx   : r-x   : r-x
                

Who are you?


$ whoami
joe

$ id -G -n
                

Permissions: to Octal notation

Exit status

$? = from 0 to 255
zero = success


$ echo "this works fine"
this works fine
$ echo $?
0
$ hhhhhh
bash: hhhhhh: command not found
$ echo $?
127
              

Exit status: examples

126 = permission denied

Environment Variables and $PATH


$ printenv
              

Environment Variables (Windows)

Environment Variables (Unix)


# In ~/.bash_profile
export MYSQL_DIR=/usr/local/mysql/bin
PATH=$MYSQL_DIR:$PATH
              

Environment Variables: find location


$ which open
              

~/.bash_profile

~/.bashrc

read each time when bash shell is started

  • set default environment variables
  • set custom aliases
  • prompt
  • a greeting message

&& and ||


$ command1 && command2 [&& command3]...
$ command1 || command2 || command3 || ...

$ command1 && command2 || command3
              

Chaining Commands Together, Redirection and Piping

I/O Redirection

stdin = 0
stdout = 1
stderr = 2

$ ls > my-file-list
$ ls >> my-file-list

$ sort < my-file-list

$ ls *.bak > listfile

$ ls /nosuchplace > /dev/null
ls: /nosuchplace: No such file or directory

$ ls /nosuchplace 2>/tmp/errors
$
              

Quiz


$ sort < file_list.txt > sorted_file_list.txt
              

Pipelines


$ cat books | grep "a"
Carroll, Lewis:Through the Looking-Glass
Shakespeare, William:Hamlet

$ ls | wc -l
$ ls | xargs wc -l

$ ls -lt | head
$ du | sort -nr
              

Quiz


$ find . -name '*.js' | xargs wc -l | sort
              

Bash Scripting


#!/bin/bash
$ whereis bash
              

Bash Scripting: Variables


#!/bin/bash
parametr1=$1
script_name=$0
echo "welcome to $script_name"
echo 'welcome to $script_name'
exit 0
              

user@desktop:~$ ./test1.sh param1
              

Bash Scripting: Conditions


if
  <command>
then
  <commands-if-successful>
else
  <commands-if-failed>
fi
              

Bash Scripting: Conditions


#!/bin/bash
source=$1
dest=$2

if [[ "$source" -eq "$dest" ]] then
  echo "some1"
  exit 1
else
  echo "some2"
  exit 2
endif
              

user@desktop:~$ ./test2.sh 1 2
              

The Super User


$ sudo <command>
              

Becoma a Super User


[me@linuxbox me]$ su
Password:

[root@linuxbox me]# chown you some_file
[root@linuxbox me]# exit

[me@linuxbox me]$
              

Advanced commands

  • sort
  • ln
  • curl
  • wget

Additional materials