Lab1[1]
Lab1[1]
BS Software Engineering
Fall-2024
Page 1
Lab Policy and Rules:
1. 100% attendance is mandatory. In case of an emergency, you can avail yourself of up to 3 absences.
So, don't waste your absences , save them for emergencies. If you are debarred from the course due to
low attendance, do not come to me to correct it.
2. Disturbing the class environment during lab sessions. such as by talking, using mobile phones, eating,
etc. will result in penalties (e.g., deduction of marks).
3. Lab tasks will not be accepted if you are absent. If you miss a lab, you cannot submit that lab task.
4. Lab demos for each lab task will be conducted at the end of each session. If you miss the demo for
any reason, no retake will be allowed, and that lab will be marked as 0.
5. All quizzes will be unannounced. Be prepared for surprise quizzes. Quizzes will cover content from
previous labs as well as theory lectures from the corresponding theory course.
6. You can take help from the internet for lab tasks, but simply copying and pasting without
understanding will be marked as 0. You should be able to explain the syntax or material used in your
tasks.
7. Do not ask for personal favors. If you have concerns, such as short attendance, please speak with the
relevant authority (e.g., academics).
8. Students on warning: Now is the time to study and earn a good grade. Do not ask for extra marks at
the end if you are unable to clear your warning.
1. Shell:
The shell is a command-line interpreter that provides a user interface for interacting
with the operating system. It allows users to execute commands, run scripts, and
Page 2
manage files and processes. The shell interprets the commands you type and translates
them into actions by the operating system.
2. Terminal:
The terminal is a software application that provides a text-based interface to interact
with the shell. It's where you type your commands and see the output. Essentially, the
terminal acts as a container for the shell, allowing you to interact with it.
Directory:
A folder which holds other files and folders.
Subdirectory:
A directory residing within another directory.
Home directory:
The directory selected by the linux as the working directory when a user logs in.
Its name usually matches with your login name.
Example:
/home/ahmed (path of ahmed home directory).
Path/Pathname:
A path or pathname is the specific location of a file or directory in the file
system. It tells the system where to find or place a file or directory. There are
two types of paths: absolute paths and relative paths.
Example "C:\Users\HP\Desktop\BootCamp"
a. Absolute pathname:
An absolute path specifies a file or directory's location in the file system
starting from the root directory (/). It provides the full path from the
root to the file or directory, regardless of the current working directory.
Example:
/home/user/documents/report.txt
Page 3
b. Relative pathname:
A relative path specifies a file or directory's location relative to the
current working directory. It does not start from the root directory but
rather from wherever you are currently located in the file system.
Example:
If your current working directory is /home/user:
4. cd command:
The cd (change directory) command in Linux is used to navigate the file system by
changing the current working directory to a different directory. This command is one of
the most basic and frequently used in the terminal.
Simply typing cd without any arguments will take you to your home directory:
Page 4
You can also use the tilde (~) symbol:
5. pwd command:
The pwd (print working directory) command in Linux is used to display the absolute path
of the current working directory. It shows you where you are in the file system at any
given moment. pwd always shows the absolute path, starting from the root (/).
Example:
Output:
6. mkdir command:
The mkdir (make directory) command in Linux is used to create one or more new
directories. It allows you to specify the name and location of the directories you want to
create.
Examples:
To create a directory named new_folder in the current directory.
Page 5
To create a directory within another directory (if the parent directory exists):
If you want to create a directory and any necessary parent directories that don’t already
exist, use the -p option:
7. rmdir:
The rmdir (remove directory) command is specifically used to delete empty directories.
It will only remove a directory if it is empty; if the directory contains any files or
subdirectories, rmdir will not remove it and will return an error.
Examples:
Remove an empty directory:
8. rm:
The rm (remove) command is used to delete files and directories. Unlike rmdir, rm can
delete both files and directories, and it does not require directories to be empty. The rm
command is more powerful and versatile, but it also requires careful use because it can
permanently delete files and directories.
Examples:
Remove a directory and its contents:
Page 6
9. mv:
The mv command in Linux is used for moving and renaming files and directories. It's a
versatile command that can relocate files from one directory to another or change their
names within the same directory.
Examples:
Rename a File:
Rename a Directory:
You can use wildcards (*) to move multiple files that match a certain pattern.
Page 7
10. cp:
The cp command in Linux is used to copy files and directories from one location to
another. It's a fundamental command for file management, allowing you to duplicate
files or entire directory structures.
Examples:
Copy a Single File:
11. ls:
The ls command in Linux is used to list the contents of a directory. It displays files,
directories, and other types of items within the specified directory or the current
directory if no directory is specified.
Page 8
Examples:
Running ls without any options simply lists the names of files and directories in
the current directory.
Lists all files, including hidden files (those starting with a dot ( .).
Lists all files and directories recursively, showing the contents of all
subdirectories.
The -t option sorts the files and directories by their last modification time. The
most recently modified items are listed first.
The -s option sorts files and directories by their size, displaying the largest files
first.
Single Commands
To clear the screen
Page 9
Syntax: $ clear
To view commands history
Syntax: $ history
To display first 10 lines of a file
Syntax: $ head file.txt
To display first 6 lines of a file
Syntax: $ head —n 6 file.txt
To display first 5 lines from 2 files
Syntax: $ head —n 5 title .txt file2.txt
To display last 10 lines of a file
Syntax: $ tail file.txt
To display last 6 lines of a file
Syntax: $ tail —n 6 file.txt
The wc command without passing any parameter will display a basic
result Of "file_txt' file. Result Of wc command will be (number of lines),
(number or words) and (number of chars/bytes) of the file.
Syntax: $ wc file.txt
To display the number or characters in a file
Syntax: $ wc —c file.txt
To display the number of lines
Syntax: $ wc—I file.txt
To display the number of words
Syntax: $ wc —w file.txt
TO display number Of lines with numbers
Syntax: $ nl file.txt
To sort the content of file
Syntax: $ sort file.txt
To sort the content Of file in reverse orderz
Syntax: $ sort -r file-txt
To display the calendar.
Syntax: $ cal
To display system date.
Syntax: $ date
To display the login user details
Page 10
Syntax: $ whoami
To display the contents of a file, line by line and page by page. we can use
less command.
Syntax: $ less file.txt
Note: to move up or down line by line use Arrow keys. And to move up
page by page use 'b' key and for move down use Space key. To quit form
the less command use ‘q' key.
To copy the file to another file
$ cp file1 -txt file2.txt
To copy a file within the other directory
$ cp file2.txt Documents/directory1
To move a file to another directory
$ rmv file1.txt directory2
$ mv —Documents/file1.txt directory3
To rename a file:
$ mv file1.txt file2.txt
$ mv —Documents/file1.txt —/Doeuments/file2.txt
To delete a file
$ rm file1.txt
$ rm Dir1/file2.txt
Task:
Page 11
5. Try to Remove a Directory Named `non_empty_dir` ( parent)
Note: This command will remove the directory and all its contents. Use with caution.
10. List Files Sorted by Modification Time (Most Recently Modified First)
13. Copy a Directory Named `source_dir` and Its Contents to a New Directory Named
`destination_dir`
14. Copy a File Named `file1.txt` to `file2.txt`, but Only if `file2.txt` Does Not Already
Exist
Page 12
24. If You Are in `/home/user/Projects/2024`, Navigate Up to `Projects` and Then to a
Subdirectory `old_projects`
25. From Any Directory, Change to Your Home Directory Without Using the Name of the
Home Directory
27. List All Files Including Hidden Files, with Human-Readable Sizes
28. Find and List All Files with a Specific Extension (e.g., `.txt`)
29. Find and Remove All `.log` Files in the Current Directory and Subdirectories
Page 13