Linux And The Console

SPACE for next slide

Shift+SPACE for previous slide

Why are we having this session?

  • Linux CLI is the very first thing that should be taught
  • Widely used
  • Automation is fun
  • Its hard to make a GUI apps compared to CLI apps

What is *nix?

*nix represents Unix and Unix like systems

Why Linux?

  • Understand Computers in a better way
  • Developer friendly
  • It is licenced to be hacker friendly
  • It is your computer. Know how to fix it.

Why not Linux?

  • You are religious about your choices
  • You are lazy
  • You fear the console

The "Hello World!" program

$ echo 'Hello World!'
Hello World!
$ █

Everything is a FILE on *nix systems

  • Storage Devices
  • I/O Devices
  • Memory
  • You can find these in the /dev/ directory

Environment variables

These variables govern the way applications communicate with the OS to access certain resources.

Some examples are:

  • $PATH: Stores location of executables on the system
  • $PWD: Stores the current directory
  • And lots more …

Most basic commands

pwd - Shows value of $PWD

ls - List files in $PWD

cd path - Change $PWD

What is a Path?

A path defines the location of a file in the file system

Types of Path

  • Absolute - /home/username/emacs.pdf
  • Relative - username/emacs.pdf is the path relative to /home working directory

Special paths

~ Home directory
. Current directory
.. Previous directory
/ Root directory

Getting help

man ls          # Manual entry for ls
help pwd        # Shell's pwd help
/bin/pwd --help # Command specific help

Time elapsed since last startup

uptime -p
up 2 days, 23 hours, 38 minutes

"/" - The Root directory

  • The base of the file system
  • All other directories are listed inside it
  • Even the external devices are mounted inside it
  • You cannot go outside it

which cmd - Prints path of an executable

/sbin, /bin - Location of commonly used binaries

/home/username - Home folder of username

/boot - Boot files are located here

Each and every command returns an integer after execution

  • 0 represents success
  • Non-zero represents failure of the command
  • echo $? - Prints return value of last command

Make a directory

mkdir <dir_name>

Make an empty file

touch <file_name>

Show contents of a file

cat <file_name>

Remove an empty directory

rmdir <dir_name>

Use this when safety is the first priority

Remove any directory

rm -rf <dir_name>
-r Recursively
-f Force

Editors

  • ed - Simplest
  • nano - Easiest
  • vim - Best
  • Emacs - Elisp interpreter

nano (Cheatsheet)

  • ^X to exit
  • M-I to enable Auto Indent

M = ESC or Alt
^ = Ctrl

vim (Cheatsheet)

It is a modal editor.

Mode Comment
Command Pressing shortcuts
Insert Inserting text
Command line Writing commands

Remember

  • Press ESC to cancel action or go to command mode
  • Always be in command mode unless specified
  • Press i or a to get into insert mode
  • Press : to get into command line mode

:q! - Force Quit

dd - Deletes a line

v - To enter visual mode

Use h, j, k and l keys to select the region.

  • x - To cut
  • y - To copy / yank
  • p - To paste

:x - Save and Quit

:set autoindent - Auto indent

Learn advanced vim from

Controlling processes

Ctrl+C sends force quit signal to a running process

Ctrl+Z to pause a process

After pausing you have two options

  • fg resumes the process in the ForeGround
  • bg resumes the process in the BackGround
  • Or kill it :)

Run a process in the background

<full_command> &

List running processes

ps -e   # Gives simple list
ps aux  # Gives verbose list
top     # Priority based ordering of processes

Kill a process

kill 1234           # Like ^C
killall -9 firefox  # Force kill

File redirection

ls -l > ls.txt     # stdout to ls.txt
ls /x 2> lserr.txt # stderr to lserr.txt
cat < ls.txt       # stdin from ls.txt

grep - Finding a string in file(s)

grep import file1 file2  # Find "import" in file1 and file2
grep -r import dir  # Recursively search in a dir
grep -n import file1  # Show line numbers of matches
grep -i import file1  # Ignore case
grep -rI import *.txt dir *.py  # Ignores binary files

find - Finding a file in a dir

find dir -name '*.py'  # Remember the single quote
find dir -iname '*sys*'  # Case insensitive search
find dir -type f -name '*.ini'  # Only show files
find dir -type f -empty  # Show empty files only

head - Show first few lines

tail - Show last few lines

Pipes

Passing output from one program to another

ps -e | grep py

Nested commands

Use one program’s output as a parameter of another command

ls -l `which python`

Thank you

  • Make sure to RTFM
  • Then search the internet
  • Then ask someone
  • If everything fails make sure to DIY