You are on page 1of 8

QATAR UNIVERSITY Operating Systems Lab (CMPS 406)

College of Engineering LAB [2] Fall 2021 Semester


Dept. of Computer Science & Eng.

Basic Linux Commands


Objectives:
& Perform Unix/Linux Basic Commands.
& Get Familiar with Linux File System Structure.
& Differentiate between Absolute and Relative Paths in Linux.

Filenames
• Every file has a filename.
• Names are composed of:
Upper case letters [A-Z], lower case letters [a-z], numbers [0-9], underscore [ _ ], period [.],
comma [,] , dash [-]
• Avoid using special characters: & * @ { } [ ] ^ $ ( ) % | " ' ~ -
• Filenames are case sensitive.
• Maximum filename length is 255 characters.
• Unstructured format.
• File extensions are not required by UNIX but may be required by applications.
• Files that start with . are called ''hidden" files and are not normally displayed.
Listing Files
• Syntax:
ls [-option ] [file or directory name]
• Used to list the contents of a directory.
• At its most basic form, ls lists the filenames only
• Example:
$ ls /etc
abrt grub2.cfg prelink.conf.d
adjtime grub.d printcap
aliases gshadow profile
aliases.db gshadow- profile.d
…………………………………
• ls Accepts options which can change the way the files are listed, including:
Option Action
-a list all files including system files
-F show file type (* after executables, / after directories, nothing after plain files)
-l show extended file information (file size, permissions and owner information)
-r List file in reverse order

File System Concepts


• UNIX uses a hierarchical file structure to organize files and directories.
• Each file and directory can be explicitly defined by its pathname in the hierarchy.
What is a Path: A path is a unique location to a file or a folder in a file system of an OS. A path to a file
is a combination of / and alpha-numeric characters.

In UNIX/Linux, there are two type of paths, Absolute and Relative path.

Absolute Pathnames

• An absolute path is defined as the specifying the location of a file or directory from the root directory
(/). In other words, we can say absolute path is a complete path from start of actual filesystem from
/ directory1.
• UNIX uses a hierarchical or tree-like file structure.
• The top level of directory structure is called root (/)
• Every file can be unambiguously referenced by its absolute pathname
• Start with root directory and trace a route through each intermediate directory to the target
file/directory.
• Each level is separated by a "/". Such examples are:
/
/bin
/etc/passwd
/home/stud3/practice
/var/spool/mail
Relative Pathnames

• Relative path is defined as path related to the present working directory (pwd) 1.
• File or directory name may be specified in relation to the current location using a relative path.
• Relative pathnames start with:
o file or directory name
o .
o ..
o ~

$ pwd
/home/stud1
$ ls ..
stud1 stud2
$ cd ..
$ pwd
/home
$ ls .
stud1 stud2
$ cd ~stud2
$ pwd
/home/stud2
$ cd ../..
$ pwd
/
$ ls home
stud1 stud2
$ cd /usr/bin
$ pwd
/usr/bin
$ cd ../../home/stud1
$ pwd
/home/stud1

1
https://www.linux.com/training-tutorials/absolute-path-vs-relative-path-linuxunix/
Current Directory

• Also called working directory


• Use pwd (print working directory) to display it.
• Referenced as . in pathnames
• Example:

$ pwd
/home/stud1
$ ls
file practice note
$ ls .
file practice note

Home Directory

• Assigned to each user from personal and initialization files.


• Initial location at login
• Referenced as:
o $HOME (all shells)
o ~ (all but Bourne shell)
o ~username (all but Bourne shell)

Using the cd Command to Change Directories

• Syntax:

cd [directory_name]
• Stands for change directory
• Used to move from one location in the directory tree to another
• If no directory name is specified, then cd takes you to your HOME directory
• Example:

$ pwd
/home/stud1
$ cd memos
$ ls
$ pwd
/home/stud1/memos
$ cd ..
$ pwd
/home/stud1
$ cd ../../usr
$ pwd
/usr
$ cd ~/memos
$ pwd
/home/stud1/memos
$ cd ~stud2/memos
$ pwd
/home/stud2/memos
$ cd
$ pwd
/home/stud1
Hands on – Exercise 1:
- Issue the following commands to navigate through some common UNIX directory structures. After each
command, use the pwd command to identify your current location in the file structure.
- Use the following diagram to help you see where you are.

$ cd
$ pwd ________________________________________________
$ cd /usr/bin
$ pwd ________________________________________________
$ ls ________________________________________________
$ cd /tmp
$ pwd ________________________________________________
$ cd
$ pwd ________________________________________________
$ cd ..
$ pwd ________________________________________________
$ cd .
$ pwd ________________________________________________
$ cd
$ cd ../..
$ pwd ________________________________________________
$ cd ../etc
$ pwd ________________________________________________
$ cd /usr/bin
$ pwd ________________________________________________
$ cd ../lib
$ cd ~
$ pwd ________________________________________________

Identifying the File Type

UNIX does not enforce rules about file contents and filename extensions, making it difficult to identify
the file type from the file name.
file
• Used to look inside a file and identify its type
• Syntax:
file filename

• Some common file types


ASCII text - shell commands - program text - mc68k executable not stripped - empty - /bin/sh
script - English text - directory - data
Example:

$ file /etc
/etc: directory
$ file /bin/cat
/bin/cat: ELF 32-bit LSB executable, Intel 80386, version (SYSV), for
GNU/Linux 2.2.5, dynamically linked (uses shared libs), stripped
Viewing Files Using cat and more
There are two basic commands used to display the contents of an ASCII text file: cat and more.
cat
• Displays the contents of a file all at once
• Useful for small files
• Short for "concatenate"
• Can be used to join files together
• No flow control (must use ^S and ^Q to pause and restart the output)
• Syntax:

cat file [ file … ]

• Example:

$ cat /etc/bashrc
# /etc/bashrc
# System wide functions and ………

more
• Displays a file one screen at a time
• Useful for viewing large files
• More flexible than cat
• Syntax:

more file [ file... ]

• Example:

$ more /etc/bashrc
……………………
--More--(41%)
• Prompt usually indicates the percentage of the file that has been viewed.
• Some built-in commands (very similar to vi commands):

Name Function
<space> Display next screen
<return> Display next line
b Display previous screen (back)
q or Q or <interrup > Exit from more
= Display current line number
/pattern Search for next occurrence of pattern
n Search for next occurrence of previously searched pattern
!<cmd> Execute <cmd> in a subshell
v Start up /usr/bin/vi at current line
h Help, display this message
:f Display current filename and line number
Hands on – Exercise 2:
1. At the command prompt, enter: ls This lists the files in your current directory.
2. Enter ls -? (or ls --help) to display available options for the ls command
3. To display the contents of the hello.cpp file, enter: cat hello.cpp
4. Display the contents of the /etc/passwd file. ________________________________
5. Enter the following command to view the /etc/passwd file a page at a time:
more /etc/passwd
6. Hit RETURN to display the next line.
7. Hit the SPACEBAR to proceed to the next screen.
8. Press q to quit and return to the prompt.
9. Read the UNIX on-line manual page for the more command by entering: man more
The more utility is used to paginate the manual pages. Use the SPACEBAR and RETURN key to move
through the pages.
10. Identify the option used to display command help while in more.
____________________________________________________________________
11. Quit from the manual page and return to the prompt.
12. Enter the following commands and identify the differences:
ls /bin/cat ______________________________________________
ls -F /bin/cat ___________________________________________
ls -l /bin/cat ___________________________________________

Listing active users with who

who
• Shows all active users on the system
• Syntax:
who [ -u ]
o Displays: Username, terminal, Login time,
o option –u adds idle time
o whoami displays your login information
o Example
$ who
$ who -u
$ who am i
$ whoami
$ who -H
$ who -b

Hands on – Exercise 3:
1. See who is logged in to the system by typing: who
2. Show who you are logged in as using:
who am i
3. Check the man pages and explain what who –b does
_____________________________________________________________________
_____________________________________________________________________

Reading the Unix clock with date

date
• Displays the system date and time
• Syntax:
date [ +format_string ]

• Default display format


$ date
Sat Sep 15 18:56:48 AST 2012

Creating Files Using touch


touch
• Used to create empty files or update the time stamp on existing files
• Syntax:
touch filename
• Example:
$ ls .lock
ls : .lock: No such file or directory
$ touch .lock
$ ls -l .lock .bashrc
$ touch .bashrc

Filename expansion using wildcards

• The shell interprets special characters as wildcards which represent characters in filenames
Character Meaning Example
? Any single character file?
?????
* 0 or more characters a*
*.doc
[…] Range or class of characters (! negates [abc]*
range or class) [a-z]*[0-9]
*[!A-Za-z0-9]

Example:
$ ls
$ ls memo*
$ ls ?.c
$ ls ?????
$ ls part[2346]
$ ls part[1-46]
$ ls *[1-46]

Hands on – Exercise 4:
1. Show the system date and time by typing: date
2. Use touch filename to create the following zero-length files in your directory:
file1 file2 file3
file4 file5 file3a
files chapter1 Friends
filelist Foes
(Hint: touch accepts multiple filenames as arguments.)
Use ls to list the files in your directory.
3. Use the *, ? and [ ] wildcard characters as shown below to list specific files only:
ls file* ________________________________________________
ls file[1-3] ________________________________________________
ls [A-Z]* ________________________________________________
ls file? ________________________________________________
ls * ________________________________________________

You might also like