You are on page 1of 9

Operating Systems Lab

Lab # 03
BASIC UNIX COMMANDS

1. Some Basic UNIX Commands

i. FILE COMMANDS

touch Create a new


file.
Usage: touch <filename>
Usage:
cp Copy files.

Usage: cp [options] <source-filename> <destination-filename>


cp [options] <source-filepath > <destination filepath>

mv Move or Rename files or directories.


Usage: mv [options] <old-filepath> <new-filepath>
mv [options] <old-filename> <new-filename>
Options: -i query user for confirmation.
rm Remove
files.
Usage: rm [options] <filname>

-i query user for confirmation.


cat View complete file content.

cat <filename>
more View file contents in sections determined by the size of the terminal.

Usage: more <filename>


less View file contents in sections determined by the size of the terminal.

Has more options and search features than more.


Usage: less [options] <filename>

ii. DIRECTORY COMMANDS

cd Change directory.
Usage: cd <filename>

Prepared By: Kaleem Nawaz Khan (Instructor CS) FAST-NUCES Peshawar


Operating Systems Lab

Eg: cd my-directory
cd go to home directory
cd .. go up one directory

pwd Print working directory on the terminal.


ls List the content of a directory.

Usage: ls [options] or ls [options] <directory-path>


Options: -l list all files in long format.
(permissions, users, filesize,date, and time are displayed).
-a list all files including those beginning with a “.”
-F list files distinguishing
directories/ executables* symbolic links@

mkdir Create a new directory.

Usage: mkdir <directory-path>


rmdir Remove a directory if its empty.

Usage: rmdir <directory-path>

rm -rf directory_name will remove non empty directory

iii. TERMINAL COMMANDS

clear Clears the terminal.

echo: Write a string to standard output. Usage: echo


“string” or

echo ‘string’

iv. HELP COMMANDS

man Displays the manual page for the selected command.


Usage: man <command-name>

help Opens the default web browser in the andrew unix help web site.
http://polaris.andrew.cmu.edu/help/sys=sun4_55/env=gamma/Top-

Prepared By: Kaleem Nawaz Khan (Instructor CS) FAST-NUCES Peshawar


Operating Systems Lab

Page

v. INFORMATION COMMANDS

history Lists the commands typed during the session.

Options: -r displays the list in reverse.


hostname Displays the computer’s or server’s name on the terminal.
who Displays who is on the system.
who am i Displays the invoking user.
wc Counts and displays the number of lines, words and characters of a
file.
Usage: wc [options] <filename>
Options: - count character only.
c
-lcount lines only.
-w count words only.
date Exercise >> to be completed by students.

cal Exercise >> to be completed by students.

whatis Displays the command description.

Usage: whatis <command>


whereis Exercise >> to be completed by students.

which Exercise >> to be completed by students.

id Displays the user id and the group id of the invoking user.

tty Displays user’s terminal name.

vi. USEFUL CSHELL SYMBOLS


| Pipe the output of a command to be processed by another command.
Usage: command1 |command2
Eg: ls -l | more
> Redirect output........ to file (overwrite ).
Usage: command > filename
Eg: wc filename > new-file

Prepared By: Kaleem Nawaz Khan (Instructor CS) FAST-NUCES Peshawar


Operating Systems Lab

>> Append (the result of the command) to the end of the file.

Usage: command >> file-name


Eg: pwd >> existing-file
< Take the input for the command from a file.

Usage: command1 < filename.


& Run process in the background so that the shell remains active.

Usage: program-name &


program-name filename &
; Separate commands on the same line.

Usage: command1 ; command2


Eg: pwd ; ls
! The history commands.

!! Redo last command.


!str Redo the last command that starts with str.
!23 Redo the 23rd command.
!-2 Redo the (last command -2)
^ Quick modifier for the last command.

Usage: ^mistake^correction.
&& The logical and symbol : execute first command then if successful,

the second command.


Usage: <command1> && <command2>
|| The OR symbol : executes the first command or, if it fails, the second

command.
Usage: <command1> || <command2>
./ Runs a compiled program.

Usage: ./ program-name

vii. PERMISSIONS AND FILE STORAGE (UNIX)

chmod Set the permission on a file or a directory.


Usage: chmod [options] <who> <opcode> < permission> <file-
name>
Options: - Recursively updates permisions within a directory
R
structure.
Who: u User

Prepared By: Kaleem Nawaz Khan (Instructor CS) FAST-NUCES Peshawar


Operating Systems Lab

g Group
o other
a All
Opcode: + add permission.
- remove permission.
Permission:

r Read
w Write
x execute
Eg: chmod a +rwx public-file
adds permissions of read write and execute to all.
Eg: chmod go -wx my-file
removes write and execute to group and others.
passwd Change the password.

df Displays the amount of free and used disk space.

du Displays the amount of disk usage.

viii. PROCESSES

ps Displays the active processes.


Includes the process number, process name and process time.
Options: -a
kill Terminates a process.

Usage: kill [options] <process-number>


Options: -9 absolute kill.

ix. UNIX FILTERS

fgrep A variation of grep that maches a text-string and does-not support

regular expressions.
Eg: fgrep <string> <file-name>

spell Exercise >> to be completed by students

sort Exercise >> to be completed by students

head Exercise >> to be completed by students

Prepared By: Kaleem Nawaz Khan (Instructor CS) FAST-NUCES Peshawar


Operating Systems Lab

tail Exercise >> to be completed by students

find Search the system for filenames.

Usage: find <pathname> <condition>


Eg: find /home/hoda -name seed
split Splits a file into several files of equal length.

Usage: split [options] <filename> <outfile>.


Options: - specifies the number of lines per file.
n
cmp Compare 2 files.

Usage: cmp <file1> <file2>


diff Reports the lines that differ between 2 files

Usage: diff <file> <file2>

x. Users and Groups

useradd <Username>
passwd <Username>
login <Username>
logout <Username>
groupadd <Groupname>
usermod -a -G <Groupname> <Username>
usermod -G <Groupname> <Username>
id <Username>
chown <username> <filename>

xi. File and Directory Compression

sudo apt-get install zip gzip tar

zip my_arch.zip my_folder

uzip my_arch.zip
Use the following command to compress an entire directory or a single file on Linux. It’ll also
compress every other directory inside a directory you specify–in other words,

tar -czvf name-of-archive.tar.gz /path/to/directory-or-file

Here’s what those switches actually mean:

Prepared By: Kaleem Nawaz Khan (Instructor CS) FAST-NUCES Peshawar


Operating Systems Lab

-c: Create an archive.


-z: Compress the archive with gzip.
-v: Display progress in the terminal while creating the archive, also known as “verbose” mode.
The v is always optional in these commands, but it’s helpful.
-f: Allows you to specify the filename of the archive.

Use bzip2 Compression Instead

tar -cjvf archive.tar.bz2 stuff

Extract an Archive

Once you have an archive, you can extract it with the tar command. The following command will
extract the contents of archive.tar.gz to the current directory.

tar -xzvf archive.tar.gz

It’s the same as the archive creation command we used above, except the -x switch replaces the -
c switch. This specifies you want to extract an archive instead of create one.

You may want to extract the contents of the archive to a specific directory. You can do so by
appending the -C switch to the end of the command. For example, the following command will
extract the contents of the archive.tar.gz file to the /tmp directory.

tar -xzvf archive.tar.gz -C /tmp

If the file is a bzip2-compressed file, replace the “z” in the above commands with a “j”.

tar -xjvf archive.tar.bz2

xii. Installation of Packages from Repositories (APT-GET)

There are literally thousands of Ubuntu programs available to meet the needs of Ubuntu users.
Many of these programs are stored in software archives commonly referred to as repositories.
Repositories make it easy to install new software, while also providing a high level of security,
since the software is thoroughly tested and built specifically for each version of Ubuntu.

The four main repositories are:

Main - Officially supported software.

Restricted - Supported software that is not available under a completely free license.

Universe - Community maintained software, i.e. not officially supported software.

Prepared By: Kaleem Nawaz Khan (Instructor CS) FAST-NUCES Peshawar


Operating Systems Lab

Multiverse - Software that is not free.

The apt or Advanced Packaging Tool is a package manager. The apt keeps a list of packages that
it can install in its cache (or repository). This cache has information on where the software is (ie
the URL) located, what all additional software is required to run that software and the version it
can install on the current system.

So when you say "sudo apt-get install <package>", it checks its repository for the packages name.
If the package is available in the list, it then proceeds to locate, download and install the software
and all the required dependencies.

Exercise 1

I. The boxes in blue are directories. The boxes in gray are _files. Each _files should contain a
random mark of your liking. Once done, delete all the _files/directories that you have created.
II. Count the total number of commands you entered to do this job. I managed using 6 commands.

Exercise 2

Prepared By: Kaleem Nawaz Khan (Instructor CS) FAST-NUCES Peshawar


Operating Systems Lab

i. How are we going to change it using the _mv_ command so that we get the directory tree as
below?

ii. Change your directory so that your current directory is _Physics_.


iii. From here, change your directory in only one command such that your current directory
becomes _subject. From _subject_, issue only one command such that the directory _physics_
is deleted.

Prepared By: Kaleem Nawaz Khan (Instructor CS) FAST-NUCES Peshawar

You might also like