OS Lab - Week 2
OS Lab - Week 2
Syntax:
cd [directory]
● cd /: this command is used to change directory to the root directory, The root
directory is the first directory in your filesystem hierarchy.
$PWD is an environment variable which stores the path of the current directory.
Syntax:
pwd
Command: ls
ls is a Linux shell command that lists directory contents of files and directories.Some
practical examples of ls command are shown below.
Syntax:
ls
Syntax:
file [option] [filename]
● -b, –brief : This is used to display just file type in brief mode.
Syntax:
touch file_name
Touch command to create multiple files: Touch command can be used to create the
multiple numbers of files at the same time. These files would be empty while
creation.
Syntax:
touch File1_name File2_name File3_name
Syntax:
cp [OPTION] Source Destination
cp [OPTION] Source-1 Source-2 Source-3 Source-n Directory
● -i(interactive): i stands for Interactive copying. With this option system first
warns the user before overwriting the destination file. cp prompts for a
response, if you press y then it overwrites the file and with any other option
leave it uncopied.
● -r or -R: Copying directory structure. With this option cp command shows its
recursive behavior by copying the entire directory structure recursively.
● Copying using * wildcard: The star wildcard represents anything i.e. all files
and directories. Suppose we have many text document in a directory and
wants to copy it another directory, it takes lots of time if we copy files 1 by 1 or
command becomes too long if specify all these file names as the argument,
but by using * wildcard it becomes simple.
Command: mv
mv stands for move. mv is used to move one or more files or directories from one
place to another in file system like UNIX.
Syntax:
mv [Option] source destination
● -i (Interactive): the -i option makes the command ask the user for
confirmation before moving a file that would overwrite an existing file, you
have to press y for confirm moving, any other key leaves the file as it is. This
option doesn’t work if the file doesn’t exist, it simply rename it or move it to
new location.
Command: mkdir
mkdir command in Linux allows the user to create directories (also referred to as
folders in some operating systems ). This command can create multiple directories at
once as well as set the permissions for the directories. It is important to note that the
user executing this command must have enough permissions to create a directory in
the parent directory, or he/she may recieve a ‘permission denied’ error.
Syntax:
mkdir [options...] [directories ...]
Syntax:
rm [OPTION]... FILE...
● -i (Interactive Deletion): Like in cp, the -i option makes the command ask
the user for confirmation before removing each file, you have to press y for
confirm deletion, any other key leaves the file un-deleted.
● Delete file whose name starting with a hyphen symbol (-): To remove a
file whose name begins with a dash (“-“), you can specify a double dash (“–“)
separately before the file name. This extra dash is necessary so that rm does
not misinterpret the file name as an option.
Command: locate
locate command in Linux is used to find the files by name. There is two most widely
used file searching utilities accessible to users are called find and locate. The locate
utility works better and faster than find command counterpart because instead of
searching the file system when a file search is initiated, it would look through a
database. This database contains bits and parts of files and their corresponding paths
on your system.
Syntax:
locate [OPTION]... PATTERN...
Examples:
Syntax:
find [where to start searching from] [expression determines what to find] [-options]
[what to find]
Examples:
● Search text within multiple files: find ./ -type f -name "*.txt" -exec grep
'linux' {} \;
Command: which &
whereis
which command in Linux is a command which is used to locate the executable file
associated with the given command by searching it in the path environment variable.
Syntax:
which [filename1] [filename2] ...
Syntax:
whereis [options] filename…
Command: tar
The Linux ‘tar’ stands for tape archive, is used to create Archive and extract the
Archive files. tar command in Linux is one of the important command which provides
archiving functionality in Linux. We can use Linux tar command to create compressed
or uncompressed Archive files and also maintain and modify them.
Syntax:
tar [options] [archive-file] [file or directory to be archived]
Examples:
● Creating an uncompressed tar Archive using option -cvf : tar cvf file.tar
*.txt
● Extracting files from Archive using option -xvf : tar xvf file.tar
● gzip compression on the tar Archive, using option -z : tar cvzf file.tar.gz
*.txt
● Extracting a gzip tar Archive *.tar.gz using option -xvzf : tar xvzf
file.tar.gz
Questions?
02
Using Text Editors
Creating and Manipulating text files in the CLI & GUI
Using Text Editors
Although the desktop provides a beautiful graphical user interface (GUI) that’s a pleasure to work in, much
goes on outside that interface. Most Linux system configuration files are text files. Additionally, Linux gives
you the ability to create shell scripts and interact with the operations of several programs — all by using text
files.
When all is working as it should, you can edit (and even create) those files with graphical tools, but it’s highly
recommended that you also know how to edit them outside that interface, should a problem exist that keeps
the X Window System from loading. In the interface or not, you’ll be using text editors, which are programs
designed to interact with text files.
The vi editor
The vi editor (pronounced “vee eye”) is one of the oldest and most popular visual
text editors available for UNIX operating systems. Its Linux equivalent (known as vim,
which is short for “vi improved”) is therefore standard on almost every Linux
distribution. Although the vi editor is not the easiest of the editors to use when
editing text files, it has the advantage of portability.
To open an existing text file for editing, you can type vi filename (or vim filename),
where filename specifies the file to be edited.
Syntax:
vi [options] [text file]
Examples:
To open an existing text file for editing, you can type nano filename, where filename
specifies the file to be edited.
Syntax:
nano [options] [text file]
Examples:
● Create a new text file that you will save later: nano
● Open a text file and go to a specific line and column : nano +5,10 file.txt
Gnome Text Editor /
gedit
gedit is the default text editor of the GNOME desktop environment and part of the
GNOME Core Applications. Designed as a general-purpose text editor, gedit
emphasizes simplicity and ease of use, with a clean and simple GUI, according to the
philosophy of the GNOME project. It can be considered GNOME’s counterpart for
Microsoft Windows’ notepad.
To open an existing text file for editing, you can type gedit filename, where filename
specifies the file to be edited.
Syntax:
gedit [options] [text file]
Examples:
● Create a new text file that you will save later: gedit
● Open a text file and go to a specific line and column : gedit +5:10
file.txt
Questions?