LINUX BASICS
#burningkeyboards
@denis_ristic
LINUX BASICS
WHAT IS LINUX?
‣ An operating system is software that manages all of the hardware resources associated with your desktop or laptop.
‣ The OS is comprised of a number of pieces:
‣ The Bootloader: The software that manages the boot process of your computer. For most users, this will simply be a
splash screen that pops up and eventually goes away to boot into the operating system.
‣ The kernel: This is the one piece of the whole that is actually called “Linux”. The kernel is the core of the system and
manages the CPU, memory, and peripheral devices. The kernel is the “lowest” level of the OS.
‣ Daemons: These are background services (printing, sound, scheduling, etc) that either start up during boot, or after
you log into the desktop.
‣ The Shell: This is a command process that allows you to control the computer via commands typed into a text
interface.
‣ Graphical Server: This is the sub-system that displays the graphics on your monitor. It is commonly referred to as the
X server or just “X”.
‣ Desktop Environment: This is the piece of the puzzle that the users actually interact with. There are many desktop
environments to choose from (Unity, GNOME, Cinnamon, Enlightenment, KDE, XFCE, etc). Each desktop
environment includes built-in applications (such as file managers, configuration tools, web browsers, games, etc).
‣ Applications: Desktop environments do not offer the full array of apps. Just like Windows and Mac, Linux offers
thousands upon thousands of high-quality software titles that can be easily found and installed.
2
LINUX BASICS
DIRECTORY STRUCTURE
3
LINUX BASICS
FILE SYSTEM
‣ "On a UNIX system, everything is a file; if something is not a file, it is a
process."
‣ This statement is true because there are special files that are more than
just files (named pipes and sockets, for instance), but to keep things
simple, saying that everything is a file is an acceptable generalization.
‣ A Linux system makes no difference between a file and a directory,
since a directory is just a file containing names of other files.
‣ Programs, services, texts, images, and so forth, are all files. Input and
output devices, and generally all devices, are considered to be files,
according to the system.
4
LINUX BASICS
FILE SYSTEM
dristic @ MACBOOK [~] ➜ ls -la
total 13936
drwxr-xr-x+ 88 dristic staff 2992 12 Srp 21:02 .
drwxr-xr-x 6 dristic staff 204 3 Ožu 12:26 ..
-rw-r--r-- 1 dristic staff 266 2 Svi 2016 .499481.padl
drwxr-xr-x 3 dristic staff 102 9 Stu 2016 AndroidStudio2.0
‣ - Regular file
‣ d Directory
‣ l Link
‣ c Special file
‣ s Socket
‣ p Named pipe
‣ b Block device
5
LINUX BASICS
SPECIAL FILES
‣ ~/.profile is the shell startup file; .profile will get executed whenever
you login. It is used to set environment variables, aliases and shell
options such as $PATH
‣ ~/.ssh/config is the SSH client configuration file.
‣ ~/.history is a file that contains history of executed commands
6
LINUX BASICS
BASIC COMMANDS
‣ awk Pattern-directed scanning and processing language
‣ cat Display file’s contents to the standard output device
‣ cd Change to directory.
‣ chkconfig Turn on or off services on boot time.
‣ chmod Change a file’s permissions.
‣ chown Change who owns a file.
‣ clear Clear a command line screen/window for a fresh start.
‣ cp Copy files and directories.
‣ date Display or set the system date and time.
‣ df Display used and available disk space.
‣ du Show how much space each file takes up.
7
LINUX BASICS
BASIC COMMANDS
‣ file Determine what type of data is within a file.
‣ find Search for files matching a provided pattern.
‣ grep Search files or output for a particular pattern.
‣ kill Stop a process. If the process refuses to stop, use kill -9 pid.
‣ less View the contents of a file one page at a time.
‣ ln Create a shortcut.
‣ locate Search a copy of your filesystem for the specified filename.
‣ ls List directory contents.
‣ man Display the help information for the specified command.
‣ mkdir Create a new directory.
8
LINUX BASICS
BASIC COMMANDS
‣ mv Rename or move file(s) or directories.
‣ passwd Change the password or allow (for the system administrator) to change any
password.
‣ ps Display a snapshot of the currently running processes.
‣ pwd Display the pathname for the current directory.
‣ rm Remove (delete) file(s) and/or directories.
‣ rmdir Delete empty directories.
‣ sed Short for "stream editor", allows you to filter and transform text.
‣ service Stop, start, restart or find the status of system services.
‣ ssh Remotely log in to another Linux machine, over the network.
‣ su Switch to another user account.
9
LINUX BASICS
BASIC COMMANDS
‣ tail Display the last n lines of a file (the default is 10).
‣ tar Store and extract files from a tarfile (.tar) or tarball (.tar.gz or .tgz).
‣ top Displays the resources being used on your system.
‣ touch filename Create an empty file with the specified name.
‣ wc Word, line, character, and byte count
‣ who Display who is logged on.
‣ stat The stat utility displays information about the file pointed to by file.
‣ id Returns current user identity
‣ crontab Maintains crontab files for individual users
‣ nohup Invoke a utility immune to hangups
10
LINUX BASICS
BASIC COMMANDS
‣ netstat Show network status
‣ ping send ICMP ECHO_REQUEST packets to network hosts
‣ telnet user interface to the TELNET protocol
‣ dig DNS lookup utility
‣ traceroute print the route packets take to network host
‣ vi a programmers text editor
‣ nano editor, an enhanced free Pico clone
‣ apt-get is the command-line tool for handling packages
‣ yum is an interactive, rpm based, package manager
‣ dpkg is a tool to install, build, remove and manage Debian packages.
11
LINUX BASICS
CD EXPLAINED
‣ cd Returns you to your login directory
‣ cd ~ Also returns you to your login directory
‣ cd / Takes you to the entire system's root directory
‣ cd /root Takes you to the home directory of the root
‣ cd /home Takes you to the home directory, where user login directories are usually
stored
‣ cd .. Moves you up one directory
‣ cd /dir1/subdir This absolute path takes you directly to subdir, a subdirectory
of dir1
‣ cd ../../dir3/dir2 This relative path takes you up two directories, then to dir3,
then to the dir2 directory
12
LINUX BASICS
CHMOD EXPLAINED
chmod <people><+/-><permissions>
Example: chmod o-w (deny others from editing the file)
Example: chmod u+rwx (give the owner full control)
r - Read
w - Write
x - Execute
u - The owner of the file
g - The group that the file belongs to
o - Anybody who is not one of the above
a - All users
+ - Add permissions
- - Remove permissions
: 1st octet represents permissions for the owner.
r w x T : 2nd octet represents permissions for the group.
Owner: 4 2 1 7 : 3rd octet represents permissions for everyone else
Group: 0 0 0 0 : For each octet, start at 0 and:
Other: 0 0 0 0 : +4 for read permission.
: +2 for write permission.
Command: chmod 700 : +1 for execute permission.
13
LINUX BASICS
CRONTAB EXPLAINED
crontab -e
# Minute Hour Day of Month Month Day of Week Command
# (0-59) (0-23) (1-31) (1-12 or Jan-Dec) (0-6 or Sun-Sat)
0 2 12 * * /usr/bin/find
14
LINUX BASICS
PIPING AND REDIRECTION
ls | head -3 | tail -1
echo “Hello World!” > file.txt
echo “Hello World!” >> file.txt
wc -l < file.txt
ls -l video.mpg blah.foo 2> errors.txt
ls -l video.mpg blah.foo > myoutput 2>&1
ls | head -3 | tail -1 > myoutput
15
LINUX BASICS
WILDCARDS
* - represents zero or more characters
? - represents a single character
[] - represents a range of characters
ls
barry.txt blah.txt bob example.png firstfile foo1 foo2
foo3 frog.png secondfile thirdfile video.mpeg
ls b*
barry.txt blah.txt bob
16
LINUX BASICS
CTRL + *
‣ Ctrl+D: Used to terminate input or exit the terminal or shell
‣ Ctrl+Z: Used to suspend foreground processes
‣ Ctrl+C: Used to kill foreground processes
17
LINUX BASICS
BACKGROUND & FOREGROUND
$ yes > /dev/null
^Z
[1]+ Stopped yes > /dev/null
$ jobs
[1]+ Stopped yes > /dev/null
$ yes > /dev/null &
[2] 20126
$ jobs
[1]+ Stopped yes > /dev/null
[2]- Running yes > /dev/null &
$ bg %1
[1]+ yes > /dev/null &
$ jobs
[1]- Running yes > /dev/null &
[2]+ Running yes > /dev/null &
$ fg %
yes > /dev/null
^Z
[2]+ Stopped yes > /dev/null
18
LINUX BASICS
EXECUTING FILES
$ php myscript.php
$ php -f myscript.php
$ vi myscript.php
#!/usr/bin/env php
<?php
....
$ ./myscript.php
$ mv myscript.php myscript
$ myscript
command not found: myscript
$ ./myscript
19
LINUX BASICS
LINUX RESURCES
‣Linux intro
‣http://www.tldp.org/LDP/intro-linux/html/
intro_10.html
‣Linux tutorial
‣http://ryanstutorials.net/linuxtutorial/
‣OhMyZsh
‣https://github.com/robbyrussell/oh-my-zsh
20

17 Linux Basics #burningkeyboards

  • 1.
  • 2.
    LINUX BASICS WHAT ISLINUX? ‣ An operating system is software that manages all of the hardware resources associated with your desktop or laptop. ‣ The OS is comprised of a number of pieces: ‣ The Bootloader: The software that manages the boot process of your computer. For most users, this will simply be a splash screen that pops up and eventually goes away to boot into the operating system. ‣ The kernel: This is the one piece of the whole that is actually called “Linux”. The kernel is the core of the system and manages the CPU, memory, and peripheral devices. The kernel is the “lowest” level of the OS. ‣ Daemons: These are background services (printing, sound, scheduling, etc) that either start up during boot, or after you log into the desktop. ‣ The Shell: This is a command process that allows you to control the computer via commands typed into a text interface. ‣ Graphical Server: This is the sub-system that displays the graphics on your monitor. It is commonly referred to as the X server or just “X”. ‣ Desktop Environment: This is the piece of the puzzle that the users actually interact with. There are many desktop environments to choose from (Unity, GNOME, Cinnamon, Enlightenment, KDE, XFCE, etc). Each desktop environment includes built-in applications (such as file managers, configuration tools, web browsers, games, etc). ‣ Applications: Desktop environments do not offer the full array of apps. Just like Windows and Mac, Linux offers thousands upon thousands of high-quality software titles that can be easily found and installed. 2
  • 3.
  • 4.
    LINUX BASICS FILE SYSTEM ‣"On a UNIX system, everything is a file; if something is not a file, it is a process." ‣ This statement is true because there are special files that are more than just files (named pipes and sockets, for instance), but to keep things simple, saying that everything is a file is an acceptable generalization. ‣ A Linux system makes no difference between a file and a directory, since a directory is just a file containing names of other files. ‣ Programs, services, texts, images, and so forth, are all files. Input and output devices, and generally all devices, are considered to be files, according to the system. 4
  • 5.
    LINUX BASICS FILE SYSTEM dristic@ MACBOOK [~] ➜ ls -la total 13936 drwxr-xr-x+ 88 dristic staff 2992 12 Srp 21:02 . drwxr-xr-x 6 dristic staff 204 3 Ožu 12:26 .. -rw-r--r-- 1 dristic staff 266 2 Svi 2016 .499481.padl drwxr-xr-x 3 dristic staff 102 9 Stu 2016 AndroidStudio2.0 ‣ - Regular file ‣ d Directory ‣ l Link ‣ c Special file ‣ s Socket ‣ p Named pipe ‣ b Block device 5
  • 6.
    LINUX BASICS SPECIAL FILES ‣~/.profile is the shell startup file; .profile will get executed whenever you login. It is used to set environment variables, aliases and shell options such as $PATH ‣ ~/.ssh/config is the SSH client configuration file. ‣ ~/.history is a file that contains history of executed commands 6
  • 7.
    LINUX BASICS BASIC COMMANDS ‣awk Pattern-directed scanning and processing language ‣ cat Display file’s contents to the standard output device ‣ cd Change to directory. ‣ chkconfig Turn on or off services on boot time. ‣ chmod Change a file’s permissions. ‣ chown Change who owns a file. ‣ clear Clear a command line screen/window for a fresh start. ‣ cp Copy files and directories. ‣ date Display or set the system date and time. ‣ df Display used and available disk space. ‣ du Show how much space each file takes up. 7
  • 8.
    LINUX BASICS BASIC COMMANDS ‣file Determine what type of data is within a file. ‣ find Search for files matching a provided pattern. ‣ grep Search files or output for a particular pattern. ‣ kill Stop a process. If the process refuses to stop, use kill -9 pid. ‣ less View the contents of a file one page at a time. ‣ ln Create a shortcut. ‣ locate Search a copy of your filesystem for the specified filename. ‣ ls List directory contents. ‣ man Display the help information for the specified command. ‣ mkdir Create a new directory. 8
  • 9.
    LINUX BASICS BASIC COMMANDS ‣mv Rename or move file(s) or directories. ‣ passwd Change the password or allow (for the system administrator) to change any password. ‣ ps Display a snapshot of the currently running processes. ‣ pwd Display the pathname for the current directory. ‣ rm Remove (delete) file(s) and/or directories. ‣ rmdir Delete empty directories. ‣ sed Short for "stream editor", allows you to filter and transform text. ‣ service Stop, start, restart or find the status of system services. ‣ ssh Remotely log in to another Linux machine, over the network. ‣ su Switch to another user account. 9
  • 10.
    LINUX BASICS BASIC COMMANDS ‣tail Display the last n lines of a file (the default is 10). ‣ tar Store and extract files from a tarfile (.tar) or tarball (.tar.gz or .tgz). ‣ top Displays the resources being used on your system. ‣ touch filename Create an empty file with the specified name. ‣ wc Word, line, character, and byte count ‣ who Display who is logged on. ‣ stat The stat utility displays information about the file pointed to by file. ‣ id Returns current user identity ‣ crontab Maintains crontab files for individual users ‣ nohup Invoke a utility immune to hangups 10
  • 11.
    LINUX BASICS BASIC COMMANDS ‣netstat Show network status ‣ ping send ICMP ECHO_REQUEST packets to network hosts ‣ telnet user interface to the TELNET protocol ‣ dig DNS lookup utility ‣ traceroute print the route packets take to network host ‣ vi a programmers text editor ‣ nano editor, an enhanced free Pico clone ‣ apt-get is the command-line tool for handling packages ‣ yum is an interactive, rpm based, package manager ‣ dpkg is a tool to install, build, remove and manage Debian packages. 11
  • 12.
    LINUX BASICS CD EXPLAINED ‣cd Returns you to your login directory ‣ cd ~ Also returns you to your login directory ‣ cd / Takes you to the entire system's root directory ‣ cd /root Takes you to the home directory of the root ‣ cd /home Takes you to the home directory, where user login directories are usually stored ‣ cd .. Moves you up one directory ‣ cd /dir1/subdir This absolute path takes you directly to subdir, a subdirectory of dir1 ‣ cd ../../dir3/dir2 This relative path takes you up two directories, then to dir3, then to the dir2 directory 12
  • 13.
    LINUX BASICS CHMOD EXPLAINED chmod<people><+/-><permissions> Example: chmod o-w (deny others from editing the file) Example: chmod u+rwx (give the owner full control) r - Read w - Write x - Execute u - The owner of the file g - The group that the file belongs to o - Anybody who is not one of the above a - All users + - Add permissions - - Remove permissions : 1st octet represents permissions for the owner. r w x T : 2nd octet represents permissions for the group. Owner: 4 2 1 7 : 3rd octet represents permissions for everyone else Group: 0 0 0 0 : For each octet, start at 0 and: Other: 0 0 0 0 : +4 for read permission. : +2 for write permission. Command: chmod 700 : +1 for execute permission. 13
  • 14.
    LINUX BASICS CRONTAB EXPLAINED crontab-e # Minute Hour Day of Month Month Day of Week Command # (0-59) (0-23) (1-31) (1-12 or Jan-Dec) (0-6 or Sun-Sat) 0 2 12 * * /usr/bin/find 14
  • 15.
    LINUX BASICS PIPING ANDREDIRECTION ls | head -3 | tail -1 echo “Hello World!” > file.txt echo “Hello World!” >> file.txt wc -l < file.txt ls -l video.mpg blah.foo 2> errors.txt ls -l video.mpg blah.foo > myoutput 2>&1 ls | head -3 | tail -1 > myoutput 15
  • 16.
    LINUX BASICS WILDCARDS * -represents zero or more characters ? - represents a single character [] - represents a range of characters ls barry.txt blah.txt bob example.png firstfile foo1 foo2 foo3 frog.png secondfile thirdfile video.mpeg ls b* barry.txt blah.txt bob 16
  • 17.
    LINUX BASICS CTRL +* ‣ Ctrl+D: Used to terminate input or exit the terminal or shell ‣ Ctrl+Z: Used to suspend foreground processes ‣ Ctrl+C: Used to kill foreground processes 17
  • 18.
    LINUX BASICS BACKGROUND &FOREGROUND $ yes > /dev/null ^Z [1]+ Stopped yes > /dev/null $ jobs [1]+ Stopped yes > /dev/null $ yes > /dev/null & [2] 20126 $ jobs [1]+ Stopped yes > /dev/null [2]- Running yes > /dev/null & $ bg %1 [1]+ yes > /dev/null & $ jobs [1]- Running yes > /dev/null & [2]+ Running yes > /dev/null & $ fg % yes > /dev/null ^Z [2]+ Stopped yes > /dev/null 18
  • 19.
    LINUX BASICS EXECUTING FILES $php myscript.php $ php -f myscript.php $ vi myscript.php #!/usr/bin/env php <?php .... $ ./myscript.php $ mv myscript.php myscript $ myscript command not found: myscript $ ./myscript 19
  • 20.
    LINUX BASICS LINUX RESURCES ‣Linuxintro ‣http://www.tldp.org/LDP/intro-linux/html/ intro_10.html ‣Linux tutorial ‣http://ryanstutorials.net/linuxtutorial/ ‣OhMyZsh ‣https://github.com/robbyrussell/oh-my-zsh 20