CS505 LINUX
CS505 LINUX
Submitted to
RAJIV GANDHI PROUDYOGIKI VISHWAVIDYALAYA, BHOPAL
(M.P)
In Partial Fulfilment of the Requirement for the Award of Degree of
BACHELOR OF TECHNOLOGY
With Specialization In
Submitted by:
Jayesh Shrivastava (0929CS223D08)
3nd year — 5'h semester
Submitted to:
Prof. Ajay Shivhare
Professor, Department of Computer Science & Engineering and AI & DS
SRIIT, Banmore
You’ll be surprised to know that the most popular programming language C came into
existence to write the Unix Operating System.
Linux is Unix-Like operating system.
The most important part of the Linux is Linux Kernel which was first released in the early
90s by Linus Torvalds.There are several Linux distros available (most are open-source and
free to download and use) such as Ubuntu, Debian, Fedora, Kali, Mint, Gentoo, Arch and
much more.
Now coming to the Basic and most usable commands of Linux/Unix part. (Please note that all
the linux/unix commands are run in the terminal of a linux system.Terminal is like command
prompt as that of in Windows OS)
Linux/Unix commands are case-sensitive i.e Hello is different from hello.
Basic unix commands:
1. who : The ‘$ who’ command displays all the users who have logged into the system
currently. As shown above, on my system I am the only user currently logged in.The thing
tty2 is terminal line the user is using and the next line gives the current date and time
$ who
Output: harssh tty2 2017-07-18 09:32 (:0)
2. pwd : The ‘$pwd’ command stands for ‘print working directory’ and as the name says,it
displays the directory in which we are currently (directory is same as folder for Windows OS
users).
In the output, we are harssh directory(folder for Windows OS that are moving to
Linux),which is present inside the home directory.
$ pwd
Output: /home/harssh
3. mkdir : The ‘$ mkdir’ stands for ‘make directory’ and it creates a new directory.We have
used ‘$ cd’ (which is discussed below) to get into the newly created directory and again on
giving ‘$ pwd’ command,we are displayed with the new ‘newfolder’ directory.
$ mkdir newfolder
$ cd newfolder
$ pwd
Output: /home/harssh/newfolder
4. rmdir : The ‘$ rmdir’ command deletes any directory we want to delete and you can
remember it by its names ‘rmdir’ which stands for ‘remove directory’.
$ rmdir newfolder
5. cd : The ‘$ cd’ command stands for ‘change directory’ and it changes your current
directory to the ‘newfolder’ directory.You can understand this a double-clicking a folder and
then you do some stuff in that folder.
$ ls
Output: Desktop Documents Downloads Music Pictures Public Scratch Templates Videos
7. touch : The ‘$ touch’ command creates a file(not directory) and you can simple add an
extension such as .txt after it to make it a Text File.
$ touch example
$ ls
Output: Desktop Documents Downloads Music Pictures Public Scratch Templates Videos
example
Note: It is important to note that according to the Unix File structure, Unix treats all the stuff
it has as a ‘file’, even the directories(folders) are also treated as a file.You will get to know
more about this as you will further use Linux/Unix based OS
8. cp : This ‘$ cp ‘ command stands for ‘copy’ and it simply copy/paste the file wherever you
want to.In the above example, we are copying a file ‘file.txt’ from the directory harssh to a
new directory new.
$ cp /home/harssh/file.txt /home/harssh/new/
9. mv : The ‘$ mv’ command stands for ‘move’ and it simply move a file from a directory to
another directory.In the above example a file named ‘file.txt’ is being moved into a new
directory ‘new’
$ mv /home/harssh/file.txt /home/harssh/new
10. rm : The ‘$ rm ‘ command for remove and the ‘-r’ simply recursively deletes file. Try ‘$
rm filename.txt’ at your terminal
$ rm file.txt
11. chmod : The ‘$ chmod’ command stands for change mode command.As there are many
modes in Unix that can be used to manipulate files in the Unix environment.Basically there
are 3 modes that we can use with the ‘chmod’ command
1. +w (stands for write and it changes file permissions to write)
2. +r (stands for read and it changes file permissions to read)
3. +x (generally it is used to make a file executable)
$ chmod +w file.txt
$ chmod +r file.txt
$ chmod +x file.txt
12. cal : The ‘$ cal’ means calendar and it simply display calendar on to your screen.
$ cal
Output : July 2017
Su Mo Tu We Th Fr Sa
1
2345678
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
13. file : The ‘$ file’ command displays the type of file.As I mentioned earlier Linux treats
everything as a file so on executing the command file on a directory(Downloads) it displays
directory as the output
$ ls
Output: Desktop Documents Downloads Music Pictures Public Scratch Templates Videos
$ file Downloads
Output: Downloads: directory
14. sort : As the name suggests the ‘$ sort’ sorts the contents of the file according to the
ASCII rules.
$ sort file
15. grep : grep is an acronym for ‘globally search a regular expression and print it’.The ‘$
grep’ command searches the specified input fully(globally) for a match with the supplied
pattern and displays it.
In the example, this would search for the word ‘picture’ in the file newsfile and if found,the
lines containing it would be displayed on the screen.
$ man pwd
17. lpr : The ‘$ lpr’ command send a file to the printer for printing.
$ lpr new.txt
18. passwd : The ‘$ passwd’ command simply changes the password of the user.In above
case ‘harssh’ is the user.
$ passwd
Output: Changing password for harssh.
(current) UNIX password:
19. clear : The ‘$ clear’ command is used to clean up the terminal so that you can type with
more accuracy
$ clear
20. history : The ‘$ history’ command is used to get list of previous commands may be
obtained by executing the following command. you can also use parameters like !n to re-
execute the nth command, !! to executes the most recent command, and !cp this will execute
the most recent command that starts with cp.
$ history
At last, I want to say that these are the most basic and essential commands that are used in the
Linux operating system. You will need them even if you get to advance the Unix. If you want
to master them just keep on practicing them.
Also, it is not possible to cover all the Unix commands because they are so many in number.
You can find more, just google it and you will find most of them. Also if you want to master
Unix operating system, Learn Unix Shell Scripting/Bash Scripting. Trust me there are many
awesome tutorials on the internet for them.
First reach the directory where your .c or .cpp file is present (assume its name is new.c or
new.cpp). Please note that in order to compile C you will need GCC compiler and in order to
compile C++ you will need g++. I will tell you below how to install them.
For C:
Please write comments if you find anything incorrect, or you want to share more information
about the topic discussed above.
Command syntax errors: Unix commands are sensitive to syntax errors, which can lead to
unexpected behavior or errors. Users must be careful to use the correct command syntax and
to include all required parameters.
Permissions issues: Unix commands are subject to file permissions, which can restrict access
to certain files or directories. Users may need to use the chmod or chown commands to
change file permissions or file ownership to access certain files.
Security risks: Some Unix commands can be used to compromise system security, such as
allowing unauthorized access to files or executing malicious code. Users must be careful to
use Unix commands only for authorized tasks and to avoid running unknown scripts or
commands.
Lack of user interface: Unix commands are typically executed from the command line, which
can be intimidating for users who are used to graphical user interfaces. Users may need to
spend time learning the syntax and usage of Unix commands to be effective.
Limited documentation: While Unix commands are well-documented, some commands may
not have complete or clear documentation. Users may need to rely on online resources or
community forums to find answers to specific issues.
Q2. Study & use of commands for performing arithmetic operations with
Unix/Linux.
Ans-
Bash script supports 11 arithmetic operators. All the operators with their uses is given
below:
result=
+ Addition It adds two operands
a+b
result=
* Multiplication Multiply two operands
a*b
16/3
Return the quotient after diving first
/ Division
operand from second operands result = 5
16/ 3
Return remainder after dividing first
% Modulo
operand from second operand result = 1
x= 13
x= 13
x= 10
x = 31
x= 31
Divide the operand with given
Remainder by dividing x%=3
%= constant value and return the
with constant
remainder result = 1
3**2
The result is second operand raised
** Exponentiation
to the power of first operand. result = 9
Addition
Code:
Sum=$((10+3))
echo "Sum = $Sum"
Output:
Subtraction
Code:
Difference=$((10-3))
echo "Difference = $Difference"
Output:
Multiplication
Code:
Product=$((10*3))
echo "Product = $Product"
Output:
Division
Code:
Division=$((10/3))
echo "Division = $Division"
Output:
Modulo
Code:
Modulo=$((10%3))
echo "Modulo = $Modulo"
Output:
Exponentiation
Code:
Exponent=$((10**2))
echo "Exponent = $Exponent"
Output:
1. Double Parenthesis
This could be used for arithmetic expansion. Let’s see an example to see the use of double
parenthesis.
Code:
#!/bin/bash
first=10
second=3
Output:
13
13
7
7
2. Using let command
let command is used to perform arithmetic operations.
Example
code:
#!/bin/bash
x=10
y=3
sum=`expr $a + $b`
echo $sum
sub=`expr $a - $b`
echo $sub
mul=`expr $a \* $b`
echo $mul
div=`expr $a / $b`
echo $div
Output:
13
7
30
3
Q3. Create a file called wlcc.txt with some lines and display how many
lines, words and characters are present in that file.
Output
Q4. Append ten more simple lines to the wlcc.txt file created above and
split the appended file into 3 parts. What will be the names of these split
files? Display the contents of each of these files. How many lines will be
there on the last file?
1. Open a terminal.
2. Navigate to the directory where wlcc.txt is located.
3. Use a text editor like nano or vim to append ten more lines to wlcc.txt.
For example, using nano:
nano wlcc.txt
4. Add ten more lines, save, and exit the text editor.
Next, to split the file into three parts, you can use the split command. For example:
split -l 10 wlcc.txt
This command will split wlcc.txt into files with 10 lines each. The names of the split files
will be something like xaa, xab, and xac.
To display the contents of each file, you can use the cat command. For example:
cat xaa
cat xab
cat xac
The number of lines in the last file (xac) will depend on the total number of lines in wlcc.txt
and the number of lines you added. If the original wlcc.txt had, for example, 30 lines, and
you added 10 more, then xac will have 10 lines.
If you're using a different operating system or environment, the specific commands and steps
may be slightly different, but the general approach should be similar. Let me know if you
need help with a different environment.
Q5. Given two files each of which contains names of students. Create a
program to display only those names that are found on both the files
Certainly! You can accomplish this using common command-line tools like sort, uniq, and
comm. Here's a step-by-step guide:
1. Sort and Uniquify the Files: Open a terminal and navigate to the directory
containing your files.
sort file1.txt | uniq > sorted_file1.txt
sort file2.txt | uniq > sorted_file2.txt
This will sort and remove duplicates from each file, creating sorted_file1.txt and
sorted_file2.txt.
1. Find Common Names: Use the comm command to find common names between the
two files.
comm -12 sorted_file1.txt sorted_file2.txt
This will display the names that are found in both files.
If you want to save the common names to a file, you can use redirection:
comm -12 sorted_file1.txt sorted_file2.txt > common_names.txt
Now, you should have a file called common_names.txt containing the names that are
found in both file1.txt and file2.txt.
Remember to replace file1.txt and file2.txt with the actual file names, if they are
different in your case.
Q6. Given two files each of which contains names of students. Create a
program to display only those names that are found on both the files
You can use the ls command along with the -i option to display the inode number of a file in
Linux. If you want to do this programmatically, you can use the subprocess module in
Python to run the command. Here's an example program:
import subprocess
def get_inode(file_path):
try:
result = subprocess.run(['ls', '-i', file_path], capture_output=True, text=True)
output = result.stdout.strip().split()
inode = output[0]
return inode
except FileNotFoundError:
return None
file_path = input("Enter the path of the file: ")
inode = get_inode(file_path)
• -v: Verbose mode, which displays a message for each file processed.
• -c: Report only when a change is made.
Permissions:
• u: User (owner)
• g: Group
• o: Others (everyone else)
• a: All (equivalent to ugo)
• +: Adds the specified permissions.
• -: Removes the specified permissions.
• =: Sets the permissions explicitly.
Examples:
1. Grant read and write permissions to the owner of a file:
bashCopy code
chmod u+rw file.txt
2. Remove write permission from the group:
bashCopy code
chmod g-w file.txt
3. Allow everyone to execute a script:
bashCopy code
chmod +x script.sh
4. Grant full permissions to the owner, read and execute to the group, and only read to
others:
bashCopy code
chmod u+rwx,g+rx,o+r file.txt
5. Recursively grant read and execute permissions to all files and directories in a folder:
bashCopy code
chmod -R u+rx folder/
6. Set specific permissions using numeric mode (octal representation):
• 4: Read permission
• 2: Write permission
• 1: Execute permission
For example, to grant read, write, and execute to the owner, and read-only to others:
bashCopy code
chmod 755 file.txt
Caution:
Be careful when modifying permissions, especially with the chmod -R command, as it can
potentially alter a large number of files and directories. Improper use of this command can
lead to security issues or unintended consequences.
Always ensure you have the necessary permissions before attempting to change the
permissions of a file or directory.
Remember that changing file permissions may require administrative privileges or
appropriate ownership of the file or directory.
Examples:
1. Grant read and write permissions to the owner of a file:
c. Network Configuration:
• Set up network interfaces, configure IP addresses, and set DNS servers.
3. Customizations:
a. Desktop Environment (Optional):
• If using a graphical interface, customize the desktop environment, themes, and
appearance settings to your preference.
b. Shell Customization:
• Personalize the shell prompt, configure environment variables, and set aliases for
frequently used commands.
c. Package Management:
• Use package managers (e.g., apt, yum, pacman) to install, update, and remove
software packages.
d. Software Installation:
• Install additional software and packages based on your requirements.
e. User Profiles:
• Customize user profiles, including setting preferences for shell, environment
variables, and application configurations.
f. Services and daemons:
• Configure and customize system services and daemons (e.g., web server, database
server) based on your needs.
g. System Monitoring and Logging:
• Set up monitoring tools and configure logging to keep track of system health and
activity.
4. Backups and Disaster Recovery:
• Implement a backup strategy to safeguard important data and create a plan for disaster
recovery.
5. Additional Security Measures:
• Implement additional security measures such as setting up intrusion detection
systems, enabling two-factor authentication, and regular security audits.
Keep in mind that specific steps and commands may vary depending on the Unix/Linux
distribution you are using. Always refer to the official documentation and community
resources for detailed instructions and best practices.
Q11. Write a shell script that accepts any number of arguments and prints
them in the reverse order
You can create a shell script to achieve this. Here's an example script that accepts any number
of arguments and prints them in reverse order:
#!/bin/bash
• Stateful Inspection: Keeps track of active connections and allows related traffic.
• Proxy Services: Acts as an intermediary between internal and external systems.
• Intrusion Prevention System (IPS): Detects and prevents known threats.
4. Installation of a Firewall:
• Hardware Firewall: Purchase a dedicated hardware firewall device and follow the
manufacturer's installation instructions.
• Software Firewall: Install a firewall application on your operating system. For
Linux, iptables is commonly used.
Proxy Server:
1. What is a Proxy Server?
• A proxy server acts as an intermediary between a client and the internet, forwarding
requests from the client to the server and vice versa.
2. Types of Proxy Servers:
• Forward Proxy: Sits between clients and the internet to provide anonymity and control
access.
• Reverse Proxy: Sits between servers and the internet to enhance security and
performance.
3. Proxy Server Functions:
• Caching: Stores copies of frequently accessed resources to reduce bandwidth usage.
• Filtering: Filters requests based on defined rules, allowing or blocking certain content.
• Anonymity: Masks the client's IP address when accessing websites.
4. Installation of a Proxy Server:
• Squid Proxy Server (Linux):