Shell Commands Sept4Update
Shell Commands Sept4Update
/>
Session 1
Introduction to the Linux CLI
(Command Line Interface)
1 0 1 1 0 1 1 0 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 0 1
</ The Shell
● GUIs are limited
○ Can only interact what the programmer allows in the UI
○ Makes easy tasks easy
● Textual interface = Full advantage
○ Also known as “Shell” (command line interface)
○ Various implementations
○ Linux / MacOS: Bourne Again SHell (bash)
○ Makes difficult tasks possible!
</ The Shell
● username@machine_name:~$
○ Your username
○ The name of the machine you are on
○ The directory you are in (~)
○ $ indicates you are a normal user
○ # ⇒ superuser (“Run as administrator”)
</ Handy Shortcuts
● <CTRL> + <ALT> + T : Open terminal
● <CTRL> + D : Close terminal
● <CTRL> + L : Clear terminal screen
● <CTRL> + A/E : Jump to beginning/end of line
● <CTRL> + K/U : Delete everything ahead/before of cursor
● <CTRL> + Y : Paste text cut by <CTRL> + K/U
● <CTRL> + LEFT / RIGHT : Move by word
● TAB : Auto-complete matching file names
● <CTRL> + C : Cancel current command
● UP / DOWN : Cycles between recently used commands
</ Copy & Paste
● Double click (or select) something with a mouse to copy
and click the middle button to paste
● <CTRL> + SHIFT + C => Copy
● <CTRL> + SHIFT + V => Paste
</ Some Basic Commands
● date
● echo
○ Simply prints the arguments passed
○ Hello world → Two separate arguments
○ “Hello world” → A single argument
○ Can use quotes (“”) or escape sequences (\)
</ Environment Variables
● Where to find these programs (date, echo, ...)?
○ Environment Variable
○ $PATH ⇒ Lists the paths to look for commands
○ Can modify it to include more directories
■ Usually in dotfiles (aka shell configuration files)
■ .bashrc, .profile, .bash_aliases
● which CMD ⇒ Prints path to CMD if it exists
</ Exploring the Shell
● pwd
○ Prints the path of the current directory (Print Working Directory)
○ Path on the shell is a sequence of folders, separated by /
○ /home/terrarium
○ / ⇒ Top of the filesystem
■ aka the “Root” directory
● cd pathname
○ Moves to the directory under pathname
○ Path can be absolute or relative
</ Paths
● Absolute path
○ Starts with /
○ Specifies the full location
● Relative path
○ Based on the current working directory
○ . current directory
○ .. parent of the current directory
● Home directory represented by ~
● Previously working directory represented by -
● Just type “cd” without anything else ⇒ Takes you to home directory
</ Which one to use?
● Use whichever is shorter!
● In scripts or programs, use absolute paths (recommended)
</ Linux File System
</ Helpful Commands
● cd : Change directory
● ls : List contents in directory
● mkdir : Make directory
● mv : Move or change name of something
● touch : Create files
● rm : Remove something
● cp : Copy something
</ Listing files and directories
● ls [OPTION]... [FILE]...
○ Lists information about directory or file
○ -a Lists the hidden files as well
○ -l List in details
○ -R List subdirectories recursively
○ -S Sort by file size
○ -F Append / for dirs, * for executables, @ for symbolic links
</ Listing files and directories
● ls -la
● The size 4096 bytes is the smallest unit of space that the
filesystem can allocate
○ aka “Default Block Size”
</ Creating directories
● mkdir [OPTION]... DIRECTORY...
○ -p Create parent directories as needed, do nothing if it exists
○ -m Provide file mode like: rwxrw-r-- (more on this later!)
○ -v Verbose output
</ Creating files
● touch [OPTION]... FILE...
</ Moving and renaming
● mv [OPTIONS]... SRC... DEST
○ -i Prompt before overwriting
○ No “-r” option
</ Moving and renaming
● mv OLD NEW
○ if 'new' is a directory: 'old' is moved inside 'new'
○ if 'new' does not exist: 'old' is renamed to 'new'
○ if 'new' is a file, and 'old' is file: 'old' replaces 'new', and
the previous content of 'new' is lost forever!!
○ if 'new' is a file, and 'old' is directory: it's an error (cannot
overwrite a file with a directory)
</ Copying files and directories
● cp [OPTION]... SRC... DEST
○ -r Recursive copy
○ -i Interactive prompt
</ Removing
● rm [OPTION]... [FILE]...
○ No way to undo!
○ -f Never prompt (needed for write-protected files)
○ -i Always prompt
○ -r Remove recursively (needed for directory removal)
○ -v Verbose print
drwxrwxrwx
r read
w write
user group other x execute
d Directory
l Symbolic link
s Socket
</ Users in Linux
● Root User: Always present. Highest privilege level
● Default User: Created during installation, usually one user with
administrative privileges.
● System Users: Non-human users that are used by various system
services and processes. Limited or no login capabilities and exist
primarily for the purpose of running background services or
daemons.
● cut -d: -f1 /etc/passwd
r read
</ Permissions w
x
write
execute
drwxrwxrwx
drwxrwxrwx