Linux Commands
Session 1
Prepared by DR
Connecting to a Unix/Linux system
⚫ Open up a terminal:
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 2
Connecting to a Unix/Linux system
⚫ Open up a terminal:
The “prompt”
The current directory (“path”)
The host
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 3
What exactly is a “shell”?
⚫ After logging in, Linux/Unix starts another
program called the shell
⚫ The shell interprets commands the user types
and manages their execution
⚫ The shell communicates with the internal part of the
operating system called the kernel
⚫ The most popular shells are: tcsh, csh, korn, and bash
⚫ The differences are most times subtle
⚫ For this tutorial, we are using bash
⚫ Shell commands are CASE SENSITIVE!
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 4
Help!
⚫ Whenever you need help with a command
type “man” and the command name
⚫ Syntax: man <command>
⚫ e.g. man echo
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 5
Help!
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 6
Echo
Echo command is used to display the single line statement
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 7
Unix/Linux File System
NOTE: Unix file names
are CASE SENSITIVE!
var opt
root
/home/mary/
/home/john/portfolio/
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. The Path 8
Command: pwd
⚫ To find your current path or current working directory use
“pwd”, where PWD stands for Present Working Directory
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 9
Command: mkdir
⚫ To create a new directory use “mkdir”
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 10
Command: cd
⚫ To change to a specific directory use “cd”
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 11
Command: cd
⚫ “~” is the location of your home directory
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 12
Command: cd
⚫ “..” is the location of the directory below
current one or change to parent directory
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 13
Command: rmdir
⚫ To remove and empty directory use “rmdir”
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 14
Command: ls
⚫ To list the files in the current directory use “ls”
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 15
Command: ls
⚫ ls has many options
-l long list (displays lots of info)
-t sort by modification time
-S sort by size
-h list file sizes in human readable format
-r reverse the order
⚫ “man ls” for more options
⚫ Options can be combined: “ls -ltr”
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 16
Command: ls -ltr
⚫ List files by time in reverse order with long listing
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 17
General Syntax: *
⚫ “*” can be used as a wildcard in unix/linux
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 18
Displaying a file
⚫ Various ways to display a file in Unix
cat
less
head
tail
touch
more
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 19
Command: cat
⚫ Dumps an entire file to standard output
⚫ Good for displaying short, simple files
⚫ you can append the content to the
existing file content
Syntax:
cat > <filename>
cat <filename>
cat >> <filename>
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 20
Command: less
⚫ “less” displays a file, allowing forward /
backward movement within it
return scrolls forward one line, space one page
y scrolls back one line, b one page
⚫ use “/” to search for a string
⚫ Press q to quit
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 21
Command: head
⚫ “head” displays the top part of a file
⚫ By default it shows the first 10 lines
⚫ -n option allows you to change that
⚫ “head -n50 file.txt” displays the first 50
lines of file.txt
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 22
Command: head
⚫ Here’s an example of using “head”:
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 23
Command: tail
⚫ Same as head, but shows the last lines
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 24
Command: touch
⚫ touch command is a way to create empty files. You can update the
modification and access time of each file with the help of touch
command. It checks for the existance of the filename, if not it will
create.
Syntax: touch <filename>
⚫ Options:
⚫ -a => To change file access and modification time.
⚫ -m => It is used to only modify time of a file.
⚫ -r => To update time of one file with reference to the other file.
⚫ -t => To create a file by specifying the time.
⚫ -c => It doesn't create an empty file.
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 25
Command: touch
⚫ -a => To change file access and modification time.
Syntax: touch -a <filename>
⚫ -m => It is used to only modify time of a file
Syntax: touch -m <filename>
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 26
Command: touch
⚫ -r => To update time of one file with reference to the other file.
Syntax: touch –r <sourcefile> <target filé>
e.g. touch -r demo.txt Demo.txt
we want to change time-stamp of 'Demo.txt' with reference to 'demo.txt’
⚫ -t => To create a file by specifying the time.
Syntax: touch -t YYYYMMDDhhmm.ss <filename>
⚫ -c => It doesn't create an empty file.
Syntax: touch -c movie
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 27
Command: more
⚫ More: Command line displays contents in pager form that is either in
more format.
Syntax: More [options] <filename>
-num Limits the line displayed per page.
-d Displays user message at right corner.
-s Squeeze blank lines.
+/string name It helps to find the string.
+num Used to display the content from a specific line.
e.g. More -9 demo.txt
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 28
File Commands
⚫ Copying a file: cp
⚫ Move or rename a file: mv
⚫ Remove a file: rm
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 29
Command: cp
⚫ To copy a file use “cp”
⚫ Syntax: cp <sourcefile> <targetfile>
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 30
Command: mv
⚫ To move a file to a different location use “mv”
⚫ Syntax: mv <sourcefile> <targetfile>
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 31
Command: mv
⚫ mv can also be used to rename a file, as old file
will remove and new will be available
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 32
Command: rm
⚫ To remove a file use “rm”
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 33
Command: Sudo
⚫ sudo is used to switch the account from one user to another user or
can jump directly from normal user roles to root user roles.
⚫ [Dilleswar:~/linux_tutorial]$ sudo ankit
⚫ password: xxxxxx
⚫ [ankit:~/linux_tutorial]$ sudo su
⚫ password: xxxxxx
⚫ [root/]$ sudo su -
⚫ [root/]#
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 34
Command: rm
⚫ To remove a file forcebly -f
⚫ To remove a file “recursively”: rm –r
⚫ To remove all files and directories without
consent of user “rm –rf”
⚫ Be very careful, deletions are permanent
in Unix/Linux
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 35
File permissions
⚫ Each file in Unix/Linux has an associated
permission level
⚫ This allows the user to prevent others from
reading/writing/executing their files or
directories
⚫ Use “ls -l filename” to find the permission
level of that file
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 36
Permission levels
⚫ “r” means “read only” permission
⚫ “w” means “write” permission
⚫ “x” means “execute” permission
In case of directory, “x” grants permission to
list directory contents
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 37
File Permissions
User (you)
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 38
File Permissions
Group
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 39
File Permissions
“The Public”
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 40
Command: chmod
⚫ If you own the file, you can change it’s permissions with
“chmod”
Syntax: chmod [user/group/others/all]+[permission] [file(s)]
Below we grant execute permission to all:
Linux Session - 1 Intothepro Techscapes (OPC) Pvt. Ltd. 41