lab2
lab2
Lab No 2
Overview of Ubuntu Directories and
Basic Shell Commands
CLO: 1
Rubrics for Lab:
Task 0 1 2 3
As mentioned in Student First task Two tasks All the Tasks
the Exercise could not achieved and achieved and achieved and
Task-1, 2 and 3 create the output is students have presented
file or even presented the know how properly.
don’t know properly about the Student is able
what to do. given Linux to change the
commands permission
levels on files
All the students Student did Student Student Know Student has
have to practice not submitted the the purpose of complete
all the general submitted task but have all the awareness
Linux commands the no idea of commands about the
and prepare a assigned some he/she has commands and
manual showing task commands written in presented the
the purpose of manual but work properly
each command not presented
with snapshot of propoerly
output.
Topic to be covered
• Overview of Ubuntu Directories
• Basic Shell Command on Linux for working on Terminal
a.Helping Manual in Ubuntu using command
b.Listing Files
c.Listing Hidden Files
d.Creating & Viewing Files
e.Combining files
f.Page wise content view
g.Deleting Files
h.Moving & Re-naming Files
i.Copying Files
j.Searching Contents of a file
i. Simple Search using less
ii. grep
• Directory Manipulations
a. Removing Directories
b. Renaming Directories
• Other Useful Command
a. Clear
b. History
c. Sudo
Objectives
• Students are able to understand Linux directories
• Students are able to understand and use different
Linux Shell commands.
Overview of Ubuntu Directories
We will learn the Linux file system structures and understand the meaning of individual
high-level directories.
1. / – Root
Every single file and directory starts from the root directory. Only root user has write
privilege under this directory. Please note that /root is root user’s home directory, which is
not same as /.
Contains binary executables. Common Linux commands you need to use in single-user
modes are located under this directory. Commands used by all the users of the system are
located here.
Just like /bin, /sbin also contain binary executables. But, the Linux commands located under
this directory are used typically by system administrator, for system maintenance purpose.
Contains configuration files required by all programs. This also contains startup and
shutdown shell scripts used to start/stop individual programs.
Contains device files. These include terminal devices, usb, or any device attached to the
system.
For example: log files (/var/log); packages and database files (/var/lib); emails
(/var/mail); print queues (/var/spool); lock files (/var/lock); temp files needed across
reboots (/var/tmp);
Contains binaries, libraries, documentation, and source-code for second level programs.
/usr/bin contains binary files for user programs. If you can’t find a user binary under /bin,
look under /usr/bin.
Contains boot loader related files. Kernel initrd, vmlinux, grub files are located under
/boot
For example, /srv/cvs contains CVS (Concurrent version system) related data.
Source: http://www.thegeekstuff.com/2010/09/linux-file-system-structure/
Basic Command on Linux for working on Terminal
Launch a terminal by pressing ctrl + t or press windows and type terminal
To get help on any command that you do not understand, you can type
man
The terminal would open the manual page for that command.
For an example, if we type man ls and hit enter; terminal would give us information on ls
command.
When you type this command and hit Enter you will get the detailed manual for ls command
as show in figure below.
b. Listing Files
When you first login, your current working directory is your home directory. Your home
directory has the same name as your user-name, for example, Waqar, and it is where your
Figure 3: ls Command
Note:
• Directories are denoted in blue color.
• Files are denoted in white.
• You will find similar color schemes in different flavors of Linux.
Figure 6: ls -r execution
c. Listing Hidden Files
To list all files in your home directory including those whose names begin with a dot
type following command.
ls -a
As you can see, ls -a lists files that are normally hidden.
2. Cat
If you want to create file and input in it using command line then you can use cat
command.
cat > yourfilename.extension
cat > os.txt
After this what text you enter will be written in file and you need to press Ctrl+d to
end the input in file.
3. Touch
If you want to create an empty file and don’t want it to open in any editor or
terminal then you can use touch command.
touch yourfilename.extension
touch Os.txt
To see the content of your file you can use cat command as well
cat yourfilename. extension
cat OS.txt
e. Combining Files
if you want to combine different files then you can also use cat command
Practice task1
You have to create 2 different text files. One with your name and the second one with your
registration no. Copy and paste some dummy data after that do the following task
1) Display the data of both files
2) Display data page wise of both files
3) Combine both files in new file
You have to submit screenshots of all tasks.
g. Deleting Files
The 'rm' command removes files from the system without confirmation.
rm yourfilename.extension
rm os.txt
to check if file is delete or not type ls and see there will be no file with name os.txt
What we are going to do now, is to take a file stored in an open access area of the file system
and use the mv command to move it to your Desktop.
mv filename newfilename will rename the file.
i. Copying Files
cp file1 file2 is the command which makes a copy of file1 in the current working
directory and calls it file2
What we are going to do now, is to take a file stored in an open access area of the file
system and use the cp command to copy it to your Desktop.
/science
As you can see, less finds and highlights the keyword. Type [n] to
search for the next occurrence of the word.
ii. Grep
grep is one of many standard LINUX utilities. It searches files
for specified words or patterns. First clear the screen using
clear command, then type
grep science os.txt
As you can see, grep has printed out each line containing the word science.
Or has it ????
Try typing
grep Science os.txt
The grep command is case sensitive; it distinguishes between Science and science.
To ignore upper/lower case distinctions, use the -i option, i.e. type
grep -i science os.txt
To search for a phrase or pattern, you must enclose it in single quotes (the apostrophe
symbol). For example, to search for spinning top, type
grep
-i 'spinning top' os.txt
Some of the other options of grep are:
-v display those lines that do NOT match
-n precede each matching line with the line number
-c print only the total count of matched lines
Try some of them and see the different results. Don't forget, you can use more than
one option at a time. For example, the number of lines without the words science or
Science is
grep -ivc science os.txt
k. wc (word count) of File
A handy little utility is the wc command, short for word count. To do a word count
on os.txt, type
wc -w os.txt
Each file (and directory) has associated access rights, which may be found by typing ls -l.
Also, ls -lg gives additional information as to which group owns the file (beng95 in the
following example):
drwxrw-r-- 1 ee51ab beng95 2450 Sept29 11:52 file1
In the left-hand column is a 10 symbol string consisting of the symbols d, r, w, x, -, and,
occasionally, s or S. If d is present, it will be at the left hand end of the string, and
indicates a directory: otherwise - will be the starting symbol of the string.
The 9 remaining symbols indicate the permissions, or access rights, and are taken as three
groups of 3.
•The left group of 3 gives the file permissions for the user that owns the file (or
directory) (ee51ab in the above example);
•the middle group gives the permissions for the group of people to whom the file (or
directory) belongs (eebeng95 in the above example);
•the rightmost group gives the permissions for all others.
The symbols r, w, etc., have slightly different meanings depending on whether they refer
to a simple file or to a directory.
Access rights on files.
•r (or -), indicates read permission (or otherwise), that is, the presence or absence of
permission to read and copy the file
•w (or -), indicates write permission (or otherwise), that is, the permission (or
otherwise) to change a file
•x (or -), indicates execution permission (or otherwise), that is, the permission to
execute a file, where appropriate
Some examples
-rwxrwxrwx: a file that everyone can read, write and execute
(and delete). a file that only the owner can read
and write - no-one else
-rw-------: can read or write and no-one has execution rights
(e.g. your mailbox file).
Task2
Try changing access permissions on the file os.txt
Use ls -l to check that the permissions have changed.