LAB ACTIVITY 3 : COMMAND LINE IN LINUX
CODE & COURSE : DFN 4023 OPEN SOURCE OPERATING SYSTEM
PROGRAMME : DNS
SUB-TOPIC : 3.2, 3.3
CLO : CLO2 : Use the various graphical and command line
utilities to perform daily tasks and administration to ensure
the system works properly. (P3, C3, PLO2, PLO1)
DURATION : 2 HOURS
Learning Outcomes:
By the end of this lab, students should be able to:
1. Use basic command line in Linux
Theory :
A command is an instruction given by a user telling a computer to do something. the shell is
a program that takes your commands from the keyboard and gives them to the operating
system to perform
Activity 4(A) : Student will be able to use Linux command line.
1. Run terminal from your system.
Click Application -> Accessories ->Terminal
2. List information about
files. ls
Syntax:
ls [Options]... [File]...
Examples:
# List the contents of your home
directory $ ls
# list everything in current directory:
$ ls ~
# list everything in a vertical list:
$ ls–al
total 128
drwxr-xr-x 22 root root 4096 2015-05-20 00:55 .
drwxr-xr-x 3 root root 4096 2014-08-12 04:39 ..
-rw-r--r-- 1 root root 220 2014-08-12 04:39 .bash_logout
-rw-r--r-- 1 root root 3103 2014-08-12 04:39 .bashrc
drwx------ 4 root root 4096 2015-05-20 00:56 .cache
drwxr-xr-x 5 root root 4096 2014-08-11 21:51 .config
drwx------ 3 root root 4096 2014-08-11 21:50 .dbus
drwxr-xr-x 2 root root 4096 2014-08-11 21:50 Desktop
-rw-r--r-- 1 root root 41 2015-05-20 00:55 .dmrc
the first column is the file type
d = directory and - = file
# List the directories in the current
directory: $ ls -d */
# list ALL subdirectories
$ ls *
3. Change Directory - change the current working directory to a specific Folder.
cd
Syntax:
cd [Options] [Directory]
Examples:
#Move to the / directory
$ cd /
#Change to another folder
$ cd /var/log
#me back one folder $
cd ..
#Quickly get back
$ cd –
#Back to your home folder
$ cd
4. Print Working Directory (shell builtin)
pwd
Syntax:
pwd [-LP]
Examples:
# List the current directory
$ cd /var/log
$ pwd
/var/log
5. Print who is currently logged in
who
Syntax:
who [Options] [file] [am i]
Examples:
# Print the current user
$whoami
user1
6. Format and display help pages.
man/ info/ help
Syntax:
man [-acdfFhkKtwW] [--path] [-m system] [-p string] [-C config_file]
[-M pathlist] [-P pager] [-B browser] [-H htmlpager] [-S section_list]
[section] name ...
Examples:
# Display the ls manual page
$ man ls
7. Create new or empty files.
touch
Syntax:
touch [Options]... file...
Examples:
#Create a new file
$ touch file1.txt
$ls
Desktop Downloads file1.txt Pictures Templates
Documents examples.desktop Music Public Videos
#Create a file2.txt and file3.txt simultaneously
$ touch file2.txt file3.txt
$ls
Desktop Downloads file1.txt file3.txt Public Videos
Documents examples.desktop file2.txt Pictures Templates Music
8. Editing File
cat
Syntax:
cat >> [File]...
Examples:
#Edit file1.txt
$ cat >> file1.txt
Learning command line in linux is fun !!!
Ctrl – D (to exit from text mode)
#Edit file1.txt
$ cat >> file1.txt
PUO is the best
Ctrl – D (to exit from text mode)
#Edit a file2.txt $
cat >> file2.txt
My name is ………………………………………..
I’m proud to be PUO student
Ctrl – D (to exit from text mode)
9. Concatenate and print (display) the content of files.
cat
Syntax:
cat [Options] [File]...
Examples:
#Display a file:
$ cat file1.txt
Learning command line in linux is fun !!!
PUO is the best
#Concatenate two files:
$ cat file1.txt file2.txt > union.txt
$ cat union.txt
Learning command line in linux is fun !!!
PUO is the best
My name is ………………………………………..
I’m proud to be PUO student
10. Create new directory(s), if they do not already exist.
mkdir
Syntax:
mkdir [Options] folder...
Examples:
#In your own home directory make subdirectory
dir1 $mkdir dir1
#In your own home directory make subdirectories dir2 and dir3 simultaneously
$mkdir dir2 dir3
11. Copy files and directories.
cp
Syntax:
cp [Options]... Source Dest
cp [Options]... Source... Directory
Examples:
#Copy file1.txt to dir1
$ cp file1.txt dir1
#Copy dir1 to dir2
$ cp dir1 dir2
12. Move or rename files or directories.
mv
Syntax:
mv [Options]... SourceDest
mv [Options]... Source... Directory
Examples:
#Rename file1.txt as orange.txt
mv file1.txt orange.txt
#Move orange.txt to dir3 :
mv orange.txt dir3
13. Remove files (delete/unlink)
rm
Syntax:
rm [Options]... file...
Example:
#Remove file orange.txt
$ rm orange.txt
Remove directory, this command will only work if the directories are empty.
rmdir
Syntax:
rmdir [Options]... folder(s)...
Example:
#Remove directory dir3
$ rmdir dir3
#Remove directory dir2
$ rmdir dir2
rmdir: dir2: Directory not empty
#Remove directory dir2 and it subdirectory
$rmdir –r dir2
14. Output the first part of files, prints the first part (10 lines by default) of each file.
head
Syntax:
head [Options]... [file]...
15. Output the last part of files, print the last part (10 lines by default) of each
FILE; tail reads from standard input if no files are given or when given a FILE
of - tail
Syntax:
tail [Options]... [file]...
Examples:
head or tail to do the following:
# Display the first 10 lines of the file /etc/login.defs
$head -10 /etc/login.defs
#Display the last 4 lines of the file /etc/passwd
$tail -4 /etc/passwd
#Display the last 4 lines from a file /etc/group then tail to get the last 10:
$head -4 /etc/group | tail -10
16. Display output one screen at a time, Search through output.
less
Syntax:
less [Options]... [file]...
#Open multiple files by passing the file names as arguments.
$ less file1 file2
#While viewing file1, use :e to open the file2 as shown below.
$ less file1
:e file2
#Start printing from 3rd line of the file
$less +3 index.php
Note: The Linux commands less are similar to cat, but with less you can scroll the file
instead of showing the enter file at once. So if you have larger files you want to view that
are longer than your screen or terminal then you can use less commands instead of cat.