Mod 01 Introduction to Linux
Mod 01 Introduction to Linux
Linux
Shak Hossain
Topics for Today
● Linux Overview
● Linux Interaction - Shell and Commands
● I/O redirection (pipes, etc.)
● Navigating the file system
● Processes and job control
● Editors
● Creating and Running Code
About Me
● Shak Hossain
● Adjunct Professor
How about you?
wc bash emacs
● Bird’s eye view
multitasking
tcsh
cat device file grep
access Hardware system
sh
Kernel awk
sort
Shell
Utilities
Who is Linux?
~1991
Where is Linux?
Remote Server
Connecting from
Different Platforms
SSH X-Win SFTP
Microsoft MobaXterm
Windows https://mobaxterm.mobatek.net
● Recommended:
○ MobaXterm
http://mobaxterm.mobatek.net/
● Alternatives:
○ SSH/X-Windows: X-Win32
https://www.bu.edu/tech/services/support/desktop/distribution/xwindows/
○ SFTP: Filezilla
https://filezilla-project.org/
Apple MacOS
● SSH: Terminal
○ Built in to macOS
Applications > Utilities > Terminal
● X-Windows: XQuartz
○ Download: https://www.xquartz.org/ (Note: This install
requires a logout.)
● X-Windows: XQuartz
○ Download: https://www.xquartz.org/
○ Note: This install requires a logout.
● Provides:
○ Built-in commands
○ Programming control structures
○ Environment variables
username@hostname ~]$
username@hostname ~]$
Example: cal –j 3 1999. “cal” is the command, “-j” is an option (or switch), “3”
and “1999” are arguments/parameters.
… at its heart is the idea that the power of a system comes more from the
relationships among programs than from the programs themselves. Many
UNIX programs do quite trivial things in isolation, but, combined with other
programs, become general and useful tools.
Commands: Selected text processing utilities
● awk Pattern scanning and processing
● cat language Display file(s)
● cut Extract selected fields of each line of a
● diff file Compare two files Just a few of the
● grep Search text for a pattern commands for
● head Display the first part of text processing
● less files
● sed Display files on a page-by-page basis
● sort Stream editor (esp. search and
● split replace) Sort text files
● tail Split files
● tr Display the last part of a file
● uniq Translate/delete characters
● wc Filter out repeated lines in a
file Line, word and character
count
Variables and Environment Variables
● Variables are named storage locations.
○ USER=augustin
○ foo=“this is foo’s value”
● BASH built-ins
○ A little different from other commands
○ Just type the command ‘help’
○ Or ‘man bash’
● Examples:
○ cd /usr/local Change directory to /usr/local/lib
○ cd ~ Change to home directory (could just type ‘cd’)
○ pw Print working (current) directory
d
○ cd .. Change directory to the “parent” directory
○ cd / Change directory to the “root”
○ ls –d pro* Listing of only the directories starting with “pro”
The ls Command
● Useful options for the “ls” command:
○ ls -a List all files, including hidden files beginning with a “.”
○ ls -ld * List details about a directory and not its contents
○ ls -F Put an indicator character at the end of each name
○ ls –l Simple long listing
○ ls –lR Recursive long listing
○ ls –lh Give human readable file sizes
○ ls –lS Sort files by file size
○ ls –lt Sort files by modification time (very useful!)
Some Useful File Commands
● cp [file1] [file2] copy file
● mkdir [name] make directory
● rmdir [name] remove (empty)
● mv [file] [destination] directory move/rename
● rm [file] file remove (-r for
● file [file] recursive) identify file
● less [file] type
● head -n N [file] page through file
● tail -n N [file] display first N lines
● ln –s [file] [new] display last N lines
● cat [file] [file2…] create symbolic
● tac [file] [file2…] link display file(s)
● touch [file] display file in reverse order
● od [file] update modification time
display file contents, esp.
binary
Manipulating files and directories
● Examples:
○ cd # The same as cd ~
○ mkdir test
○ cd test
○ echo ‘Hello everyone’ > myfile.txt
○ echo ‘Goodbye all’ >> myfile.txt
○ less myfile.txt
○ mkdir subdir1/subdir2 # Fails. Why?
○ mkdir -p subdir1/subdir2 # Succeeds
○ mv myfile.txt subdir1/subdir2
○ cd ..
○ rmdir test # Fails. Why?
○ rm –rf test # Succeeds
Symbolic links
● Sometimes it is helpful to be able to access a file from multiple locations
within the hierarchy. On a Windows system, we might create a “shortcut.” On
a Linux system, we can create a symbolic link:
● Examples:
○ find ~ -name bu –type d # search for “bu” directories in ~
○ find . –name my-file.txt # search for my-file.txt in .
○ find ~ -name ‘*.txt’ # search for “*.txt” in ~
● Quiz:
○ Can you use find to locate a file called “needle” in your haystack
directory?
○ Extra credit: what are the contents of the “needle” file?
Processes & Job Control
Processes and Job Control
● As we interact with Linux, we create numbered instances of running programs
called “processes.” You can use the ‘ps’ command to see a listing of your
processes (and others!). To see a long listing, for example, of all processes
on the system try:
● To see all the processes owned by you and other members of the class, try:
[username@scc1 ~]$
mycommand & ← process id
[1] 54356
[username@scc1 ~]$
Process Control Practice
● Let’s look at the “countdown” script, in your scripts folder for practice
● Also, try running ‘jobs’ to see any jobs running in the background from this
bash shell.
● To kill the job, use the ‘kill’ command, either with the five-digit process id:
○ kill 54356
● Or, you can use the job number (use ‘jobs’ to see list) with ‘%’:
○ kill %1
Backgrounding a running job with C-z and
‘bg’
● Sometimes you start a program, then decide to run it in the background.
▪ Windows Users:
• MobaXterm (Recommended):
– https://mobaxterm.mobatek.net
• Putty:
– https://www.putty.org
Linux:
▪ Linus Torvalds created the Linux kernel in the early 1990s
• Linus released the first kernel under the GPL (General Public License)
▪ Debian/RedHat/SUSE/…
• The major difference is the software package management on these distros.
Structure of Linux OS
Linux
Shell
• Here is where you • User software
start interacting with and applications
the OS • High-level programs
Supplement
• Multiuser environments
al
Software
Core
Linux
Tools
• OS core commands
• System-level programs
• GNU tools • Process/Thread
• Compilers scheduling
Linux • Memory Management
Kernel • File and I/O management
• Device drivers
• System calls
Linux Shell
What is the shell?
▪ The shell is a program that takes commands from user’s keyboard and passes
them to the operating system to execute.
• Many shell programs are available for Linux:
• bash, sh, csh, tcsh, ksh, zsh, …
• For this tutorial, we will use bash, but other shells are conceptually similar
quanah:$ command
What is Terminal?
▪ Terminal is a program that opens in a window and lets users interact with the
shell.
Working with
Files and
Directories
Understanding Linux Directory Structure
/
/
boot
/boot
etc
/etc
tmp
/tmp
hom
/home
e /home/mahmadia
mydi
mahmadi /home/mahmadia/mydir
ra /home/eraider
vareRaider /var
lustr /lustre
e /lustre/work
work /lustre/scratch
.. scratc
Basic Directory Operations
Where am I?
▪ pwd command to print working
directory
quanah:$ pwd
/home/username
▪ ls -l show file
details
quanah:$ ls -l
▪ Always starts with / and goes all the way to the file or directory
• For instance:
quanah:$ cat /home/mahmadia/mydir/file1.txt
▪ If you set write permission for a directory, you can (create/delete) new
entries
▪ If you set read permission for a directory, you can list (ls) the contents
▪ If you set execute permission for a directory, you can (cd) into the directory
Linux Essential Commands 2
File/Directory Ownership and Permission
Examples:
▪ chmod command changes the rwx mode bits of a file or directory
2. Check the manual of the “man” command and see what type
of manuals are available on Linux
Coming in Part 2 of this Training …
Part 2: Introduction to more advanced topics in
Linux
❖ Linux Essential Commands (Part 2)
❖ Text Editors in Linux
❖ Linux Environment Variables
❖ Basic Bash Scripting in Linux
Course Agenda
Part 2: Introduction to more advanced topics in
Linux
❖ Linux Essential Commands (Part 2)
❖ Text Editors in Linux
❖ Linux Environment Variables
❖ Basic Bash Scripting in Linux
Review
Command Description
pwd Prints Current Working Directory
ls Lists the contents of a directory
cd Change the current path to the destination directory
mkdir Makes a new directory
rmdir Removes an empty directory
cp Copy file or directory
mv Move/Rename file or directory
rm Delete file or directory
cat Concatenates and prints the contents a file
Review
Command Description
echo Write arguments to the standard output
wc word, line, character, and byte count
man Search and open the manual page of a Linux command
more Paging through text one screenful at a time
less Improved version of more allows backward/forward movement
head Display first lines of a file
tail Display last lines of a file
grep Print lines in a file matching a pattern
history See the commands you have typed so far
Linux Essential
Commands
(Part 2)
Linux Essential Commands 2
Define the file type:
▪ Unlike Windows, files extensions rarely define the type of a file in Linux.
• For example: “file.txt” many not be a TEXT file.
▪ file command displays the file type along with a brief summary of the
file contents.
quanah:$ file myfile
myfile: ASCII text
quanah:$ file /home/mahmadia/program.py
/home/mahmadia/program.py: Python script, ASCII text
executable quanah:$ file test.tar.gz
test.tar.gz : gzip compressed data, from Unix, last modified:
Wed Sep 4 14:04:10 2019
Linux Essential Commands 2
CRLF Line Terminator:
▪ Windows text editors such as notepad add a Carriage Return Line Feed
(CRLF) character at the end of each line of the text which causes
problems with many Linux applications.
▪ dos2unix command fixes the CRLF issue in text files from Windows.
• -r : Add the directories and subdirectories contents into the zip file
quanah:$ ls
mydir
test1.txt
quanah:$ zip -r archive.zip ./*
adding: ./test1.txt (deflated
62%) adding: ./mydir/ (deflated
0%) quanah:$ ls
archive.zip mydir test1.txt
Linux Essential Commands 2
Compression and File Packaging:
▪ unzip command lists and extracts the contents of a zipped
file
• -l : Lists the contents of a zipped file
quanah:$
ls
archive.zi
p
quanah:$ unzip
archive.zip quanah:$ ls
archive.zip mydir test1.txt
Linux Essential Commands 2
Compression and File Packaging:
▪ Other compression commands available in
Linux
Command Description Decompression File Ext.
zip Packages and compresses files unzip .zip
and directories
gzip A GNU tool for compressing or gunzip .gz
expanding files/directories gzip -d
bzip2 Compresses files using the bunzip2 .bz,
Burrows- Wheeler block sorting text bzip2 -d .bz2
compression algorithm.
xz Similar to gzip and bzip2 unxz .xz
Linux Essential Commands 2
Archiving:
▪ tar command saves many files and directories into a single “archive”
file
• tar OPTIONS dest_file src1 src2 …
Exercise #5
This is a Linux text editor exercise
We finally made it!
5. Save the file and exit
6. Try to display the contents of the file on your screen
7. Change the permission of the file as below:
• Set read/write permission for the owner
• Set read-only permission to your group
Environment
Variables in
Linux
Linux Environment Variables
What is the environment variable?
▪ Environment Variables stores any user-defined or system-defined information
that can be accessed within the shell.
▪ Environment Variables are useful for passing data to programs or being used in
shell scripts.
▪ Defining a variable is very simple (do not put spaces around = symbol)
quanah:$ VAR_NAME=“This is a variable”
▪ You can add a directory into the PATH variable of your own
environment
quanah:$ export PATH="/home/username/bin:$PATH"
/home/username/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin
export WORK=/lustre/work/mahmadia
export
SCRATCH=/lustre/scratch/mahmadia
export PATH=“$PATH:$WORK/bin”
Basic Bash
Scripting In
Linux
Basic Bash Scripting in Linux
What is Bash Script?
▪ Bash script is an executable file contains Bash shell commands which
could be used to automate and simplify things.
• Shell script is a text file starts with (#!) followed by the path to the
shell interpreter (i.e. /bin/bash)
quanah:$ vim
myscript.sh #!/bin/bash
echo “Hello World!”
quanah:$ chmod +x
myscript.sh quanah:$
./myscript.sh
Hello World!
Basic Bash Scripting in Linux
Control flows
▪ The syntax of the if-then-else clause is as
following:
if [ $var -eq 1 ]; then
…
elif [ $var -ne 1 ]; then
…
elif [ $var -gt 1 ]; then
…
elif [ $var -lt 1 ]; then
…
fi
Basic Bash Scripting in Linux
Loops
▪ The syntax of the for…in loop is as
following:
for VARIABLE in 1 2 3 … N; do
…
done