Lab 02: Linux Commands (1)
The content of this lab are based on the main reference book:
The Linux Command Line, Fourth Internet Edition, William E. Shotts, Jr.
http://linuxcommand.org/tlcl.php
LINUX
Lab Outline
§ Basic Commands
§ System Commands
§ Files Commands
§ Exploring the System
§ Manipulating Files And Directories
§ Working With Commands
LINUX
What is the shell?
§ The shell is a program that takes keyboard commands and passes them to the operating system to
carry out.
§ Almost all Linux distributions supply a shell program from the GNU Project called bash.
§ The name “bash” is an acronym for “Bourne Again SHell”, a reference to the fact bash is an
enhanced replacement for sh, the original Unix shell program written by Steve Bourne.
Terminal Emulators
§ When using a graphical user interface, we need another program called a terminal emulator to
interact with the shell.
§ If we look through our desktop menus, we will probably find one, it's likely called simply “terminal”
on our menu.
§ There are a number of other terminal emulators available for Linux, but they all basically do the same
thing; give us access to the shell.
§ While it may vary in appearance somewhat depending on the distribution, it will usually include your
username@machinename, followed by the current working directory, and a dollar sign.
Basic Commands
df
§ To see the current amount of free space on your disk drives:
Basic Commands
Free
§ 'free' shows you information about the machine's memory.
§ This includes physical memory (RAM), swap as well as the shared memory and
buffers used by the kernel.
• free Displays the total amount of free and used physical and swap
memory in the system, as well as the buffers used by the kernel.
• df displays the amount of disk space available on the file system
containing each file name argument.
Basic Commands
History
§ If we press the up-arrow key, we will see that the previous command
appears after the prompt. This is called command history. Most Linux
distributions remember the last 500 commands by default.
§ Press the down-arrow key and the previous command disappears.
exit
§ We can end a terminal session by either closing the terminal emulator
window, or by entering the exit command at the shell prompt:
Basic Commands
cal: Display ca le nd a r for specific m o n t h or y e a r ( cu r r e nt m o nt h by
default)
§ cal <month> <year> :
§ Specific month of a year
e.g. cal 8 2018
LINUX (UBUNTU)
Basic Commands
date: To display sy ste m d a t e a n d ti m e
Formatted by + sign:
§ "The number of days since the year's beginning is :
§ date +%j : 221
§ %j gives day of year.
LINUX (UBUNTU)
Basic Commands
date: To display sy ste m d a t e a n d ti m e
Format command
YYYY-MM-DD date -I
YYYY-MM-DD_hh:mm:ss date +%F_%T
YYYYMMDD_hhmmss date +%Y%m%d_%H%M%S
YYYY-MM-DD (Short day) date +%F\(%a\)
YYYY-MM-DD (Long day) date +%F\(%A\)
LINUX (UBUNTU)
Basic Commands
• Options :
• a = Abbrevated weekday.
• A = Full weekday.
m = Month of the year.
• b = Abbrevated month. M = Minute.
• B = Full month. P = Display AM or PM
• c = Current day and time. S = Seconds
• C = Display the century as a decimal number. T = HH:MM:SS format
• d = Day of the month. u = Week of the year.
• D = Day in “mm/dd/yy‟ format y = Display the year in 2 digit.
Y = Display the full year.
• h = Abbreviated month day.
Z = Time zone .
• H = Display the hour.
• L = Day of the year.
LINUX (UBUNTU)
Basic Commands (Navigation)
pwd (Print working directory):
§ command for printing currently working directory
ls (Listing)
§ To list the files and directories in the current working directory.
LINUX (UBUNTU)
Basic Commands (Navigation)
cd
§ To change your working directory (where we are standing in)
§ To do this, type cd followed by the pathname of the desired working directory.
§ A pathname is the route we take along the branches of the tree to get to the directory we
want.
§ Pathnames can be specified in one of two different ways;
§ Absolute pathnames
§ Relative pathnames.
LINUX (UBUNTU)
Basic Commands (Navigation)
Absolute Path Relative Path
§ An absolute path is defined as specifying the § Relative path is defined as path related to the
location of a file or directory from the root present working directory(pwd).
directory(/).
§ Suppose I am located in /home/user1 and I
§ In other words we can say absolute path is a want to change directory to
complete path from start of actual filesystem /home/user1/Documents.
from / directory.
§ I can use relative path concept to change
directory to Documents.
• Where an absolute pathname starts
from the root directory and leads to
its destination.
• Relative pathname starts from the
working directory.
LINUX (UBUNTU)
Basic Commands (Navigation)
§ The "." symbol refers to the working directory and the ".." symbol refers to the working
directory's parent directory.
§ Let's change the working directory to /home/nida again:
§ Okay, now let's say that we wanted to change the working directory to the parent of
/home/nida which is /home. We could do that two different ways. Either with an absolute
pathname, Or, with a relative pathname
§ Likewise, we can change the working directory in two different ways.
§ Either using an absolute pathname:
§ Or relative pathname (you can omit the "./". It is implied) .
LINUX (UBUNTU)
Basic Commands (Navigation)
n Apply the following
commands and see
the output
§ Changing Directories
Used to change Directory. Examples of cd command
§ To navigate into the root directory, use: "cd/ "
§ To navigate to your home directory, use : "cd"
§ To navigate up one directory level, use: "cd .."
§ To navigate up tow directory level, use: "cd ../.."
§ To naviga te to the previous directory (or back), use: "cd -"
§ To Changes the working directory to the home directory of user_name, use :
cd ~user_name
LINUX (UBUNTU)
System Commands
§ Login
To get login to the system
§ Logout
To logout from the system
LINUX (UBUNTU)
System Commands
§ whoami
LINUX (UBUNTU)
System Commands
§ System Shutdown
LINUX (UBUNTU)
Files Commands
§ ls: To List directory contents (files a n d directories stored in th e cu rren t
directory)
§ Besides the current working directory, we can specify the directory to list, like so:
§ Or even specify multiple directories. In this example we will list both the user's home
directory (symbolized by the “~” character) and the /usr directory:
LINUX (UBUNTU)
Files Commands
§ ls –l: To List directory contents in longer format:
Field Meaning
-rw-r--r-- Access rights to the file. The first character indicates the type of file. Among the
different types, a leading dash means a regular file, while a “d” indicates a directory.
The next three characters are the access rights for the file's owner, the next three are
for members of the file's group, and the final three are for everyone else. The full
meaning of this is discussed later (Permissions).
1 File's number of hard links.
root The username of the file's owner.
root The name of the group which owns the file.
32059 Size of the file in bytes.
2007-04-03 11:05 Date and time of the file's last modification.
oo-cd-cover.odf Name of the file.
LINUX (UBUNTU)
Files Commands
§ file : To determine a file type. When invoked, the file command will print a brief description of
the file's contents.
§ less : Is a program to view text files. It provides a convenient way to examine files that contain
human-readable text.
less filename
§ If the file is longer than one page, we can scroll up and down. To exit less, press the “q”
key.
§ The most common keyboard commands used by less:
Command Action
Page Up or b Scroll back one page Page
Down or space Scroll forward one page
Up Arrow Scroll up one line
Down Arrow Scroll down one line
G Move to the end of the text file
1Gor g Move to the beginning of the text file
/characters Search forward to the next occurrence of characters
n Search for the next occurrence of the previous search
h Display help screen
LINUX (UBUNTU) q Quit less
Files Commands
§ more : Displays a file, one screenful (spacebar) a t a time or scroll one line a t a
time (RETURN)
§ - Options: -d p r o m p t s u s e r to continue a t bottom of screen
Less Is More
• The less program was designed as an improved replacement of an earlier Unix program more.
• Less falls into the class of programs called “pagers,” programs that allow the easy viewing of long
text documents in a page by page manner.
• Whereas the more program could only page forward, the less program allows paging both
forward and backward and has many other features as well.
LINUX (UBUNTU)
Files Commands
Important Facts About Filenames
1. Filenames that begin with a period character are hidden.
• This only means that ls will not list them unless you say ls -a.
• When your account was created, several hidden files were placed in your home directory to
configure things for your account.
• some applications place their configuration and settings files in your home directory as
hidden files.
2. Filenames and commands in Linux, like Unix, are case sensitive.
• The filenames “File1” and “file1” refer to different files.
3. Linux has no concept of a “file extension” like some other operating systems.
• You may name files any way you like.
• The contents and/or purpose of a file is determined by other means.
• Although Unix-like operating system don’t use file extensions to determine the
contents/purpose of files, some application programs do.
4. Though Linux supports long filenames which may contain embedded spaces and
punctuation characters, limit the punctuation characters in the names of files you create to
period, dash, and underscore.
• Most importantly, do not embed spaces in filenames.
• If you want to represent spaces between words in a filename, use underscore characters.
LINUX (UBUNTU)
Manipulating Files And Directories
This section will introduce the following commands:
● mkdir– Create directories
● rmdir – Remove directories
● mv– Move/rename files and directories
● cp– Copy files and directories
● rm– Remove files
LINUX (UBUNTU)
Manipulating Files And Directories
§ mkdir: To create directories
§ Single directory or multiple directories:
mkdir dir1
mkdir dir1 dir2 dir3
LINUX (UBUNTU)
Manipulating Files And Directories
§ rmdir: To remove a directory.
LINUX (UBUNTU)
Manipulating Files And Directories
§ mv: To Move/rename files and directories.
§ to move or rename file or directory “item1” to “item2”
mv item1 item2
§ to move one or more items from one directory to another
mv item... directory
LINUX (UBUNTU)
Manipulating Files And Directories
§ cp: To make a copy of a file or a directory.
§ to copy the single file or directory “item1” to file or directory “item2”
cp item1 item2
§ to copy multiple items (either files or directories) into a directory.
cp item... directory
LINUX (UBUNTU)
Manipulating Files And Directories
• rm: To delete existing files or directories.
LINUX (UBUNTU)
Working With Commands
§ help: Get help For shell builtins
§ Many executable programs support a “--help” option that displays a description of the
command's supported syntax and options. For example.
[me@linuxbox ~]$ mkdir --help
Usage: mkdir [OPTION] DIRECTORY...
Create the DIRECTORY(ies), if they do not already exist.
-Z, --context=CONTEXT (SELinux) set security context to CONTEXT Mandatory
arguments to long options are mandatory for short options too.
-m, --mode=MODE set file mode (as in chmod), not a=rwx – umask
-p, --parents no error if existing, make parent directories as
needed
-v, --verbose print a message for each created directory
--help display this help and exit
--version output version information and exit
LINUX (UBUNTU)
Working With Commands
§ man: Display A Program's Manual Page
§ Most executable programs intended for command line use provide a formal piece of
documentation called a manual or man page. A special paging program called man is used to
view them.
§ Man pages vary somewhat in format but generally contain:
§ a title,
§ the command's syntax,
§ a description of its purpose,
§ a description of each option.
§ No examples as its intended as a reference, not a tutorial.
§ Example:
§ man ls
LINUX (UBUNTU)
Working With Commands
§ whatis: T h i s c o m m a n d gives one line description a b o u t t h e c om m and .
[me@linuxbox ~]$ whatis ls
ls (1) - list directory contents
LINUX (UBUNTU)
End
LINUX