Os Lab2
Os Lab2
Linux Commands
Pwd,ls,cp,rm,mkdir, vi, nano, etc
Linux Commands (for Ubuntu)
pwd: print work directory: e.g. if you are at your home directory then
it will print something like /home/<username>
ls: list directory: This command will list the items of a directory. If you
don’t specify a directory then it will list work directory, the place where
you currently are.
mkdir : make directory. It will create a new directory. We will need to
specify a name.
Syntax: mkdir direcrory_name
cd :change directory. It will change your work directly as you specify.
You will have to specify a directory.g. cd /home will change your work
directory to /home regardless where you are.
Syntax: cd direcrory_name
Linux Commands (for Ubuntu)
cd ..: change directory one level up.
cd ~ :change to home directory. Suppose if your username is tom then
cd ~ will change your work directory to /home/tom.
cp: Copy Command. It will copy a file or directory. It is similar to Copy-
Paste in GUI.
Syntax Copy File: cp file_name1 file_name2
tax Copy Directory: cp –r dir1_name dir2_name
mv: Move or rename files. It is like renaming a file or cut-paste in GUI.
Syntax: mv file_name1 file_name2
Linux Commands (for Ubuntu)
rm: remove file.
Syntax: rm file_name1
rmdir : remove empty directory. This will not remove content of the
directory but it will delete a directory if it is empty.
Syntax: rmdir Directory_name1
man: Ubuntu will help you instantly and has a built in manual. Simply
apend any command with man or type executes man. e.g. man mkdir
sudo : sudo basically allow a standard user to execute a command
with root or superuser privilege.
Linux Commands (for Ubuntu)
Vi or Vim editor Commands
Developed by: Bill Joy
The vi editor is elaborated as visual editor.
It is installed in every Unix system.
It is user-friendly and works same on different platforms.
It is a very powerful application.
An improved version of vi editor is vim.
The vi editor has two modes:
Command Mode: In command mode, actions are taken on the file.
The vi editor starts in command mode.
Insert Mode: In insert mode, entered text will be inserted into the file.
The Esc key will take you to the command mode from insert mode.
Linux Commands (for Ubuntu)
Vi or Vim editor Commands
Commands Action
:wq Save and quit
:w Save
:q
Vi or Vim editor Commands
Quit
:w fname Save as fname
ZZ Save and quit
:q! Quit discarding changes made
:w! Save (and write to non-writable file)
Linux Commands (for Ubuntu)
Vi or Vim editor Commands
Command Action
i Start typing before the current character
I Start typing at the start of current line
a
Vi or Vim editor Commands
Start typing after the current character
A Start typing at the end of current line
o Start typing on a new line after the
current line
O Start typing on a new line before the
current line
Linux Commands (for Ubuntu)
Vi or Vim editor Commands
Command Action
j To move down
k To move up
h
Vi orleft
To move Vim editor Commands
l To move right
Option Function
touch -a To change file access and modification time.
touch -m It is used to only modify time of a file.
touch -r To update time of one file with reference to the
other file.
touch -t To create a file by specifying the time.
touch -c It does't create n empty file.
Linux Commands (for Ubuntu)
date : This command will show you the current date and time of your
system (including timezone).
cal : This will show calender in the terminal.
clear : Clear Screen: Use clear command to clear the terminal screen.
exit : exit from terminal.
write() system call
write() system call is used to write to a file descriptor.
In other words write() can be used to write to any file (all hardware
are also referred as file in Linux) in the system but rather than specifying
the file name, you need to specify its file descriptor.
Syntax:
#include<unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
1.The first parameter (fd) is the file descriptor where you want to write.
2.The data that is to be written is specified in the second parameter.
3.Finally, the third parameter is the total bytes that are to be written.
write() system call
Program1: To write some data on the standard output device (by
default – monitor)
//Name the program file as “write.c”
#include<unistd.h>
int main()
{
write(1,"hello\n",6); //1 is the file descriptor, "hello\n" is the data, 6 is
the count of characters in data
}
write() system call
Program2: To write some data on the standard output device (by
default – monitor) and aslo count
//Name the program file as “write1.c”
#include<stdio.h>
#include<unistd.h>
int main()
{
int count;
count=write(1,"hello\n",6);
printf("Total bytes written: %d\n",count);
}