Assignment 03 Sol (1)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 23

ASSIGNMENT-3

1. Describe three different ways of setting the permissions on a file or


directory to r--r--r-- . Create a file and see if this works.
2. Team up with a partner. Copy /bin/sh to your home directory. Type "chmod
+s sh". Check the permissions on sh in the directory listing. Now ask your
partner to change into your home directory and run the program ./sh. Ask
them to run the id command. What's happened? Your partner can type exit
to return to their shell.
3. What would happen if the system administrator created a sh file in this
way? Why is it sometimes necessary for a system administrator to use this
feature using programs other than sh?
4. Delete sh from your home directory (or at least to do a chmod s sh).
5. Modify the permissions on your home directory to make it completely
private. Check that your partner can't access your directory. Now put the
permissions back to how they were.
ANS : 1 to 5
There are nine total variables:
permission for the owner to:
1. read the file
2. write to the file
3. execute the file
permission for the owner's group members to:
1. read the file
2. write to the file
3. execute the file
permission for others to:
1. read the file
2. write to the file
3. execute the file
r = read permission
w = write permission
x = execute permission
- = no permission
-rw-rw-r-- : tells about Unix permissions given to the owner, user group and the
world.
The first part of the code is 'rw-' :- This suggests that the owner 'Home' can:
1. Read the file
2. Write or edit the file
3. He cannot execute the file since the execute bit is set to '-'.
The second part is 'rw-'. It for the user group 'Home' and group-members can:
1. Read the file
2. Write or edit the file
The third part is for the world which means any user. It says 'r--'. This means the
user can only:
1. Read the file
Number Permission Type Symbol
0 No permission ---
1 Execute --x
2 write -w-
3 Execute + write -wx
4 Read r--
5 Read + Execute R-x
6 Read + write rw-
7 Read + Write + Execute rwx

Drwxr -xr -x : d represent dictionary.


Here
for

dictionary Music drwxrw-r-- shows that , Owner can read, write and execute
Usergroup can read and write
World can only read
Setting permission to other user:

Adding execute permission to user group


Removing read permission for the user.

Command Permission
Chmod a=rwx myfile.txt rwxrwxrwx
Chmod o= myfiles.txt -rwxrwx-
Chmod g=w myfiles.txt -rwx-w--
Chmod go=r myfiles.txt -rwxr-r--
Chmod g+x myfiles.txt -rwxr-xr--
Chmod o-r myfiles.txt -rwxr-x--

Give the file owner read and write permissions and only read permissions to group
members and all other users:
Give read, write, and execute permissions, and a sticky bit to a given directory:
set read, write, and execute permissions to the file owner and no permissions for
all other users on a given directory:

change the permissions of all files and subdirectories under the directory
to755you would use:
6. Type umask 000 and then create a file called world.txt containing the words
"hello world". Look at the permissions on the file. What's happened? Now
type umask 022 and create a file called world2.txt. When might this feature
be useful?
ANS 6:
8. Use find to display the names of all files in the /home subdirectory
tree. Can you do this without displaying errors for files you can't read?

Ans 8:

To search for files based on their type, use the -type option one of the following
descriptors to specify the files type:
f: a regular file
d: directory
l: symbolic link
c: characters devices
b: block devices
p: named pipe (FIFO)
s: socket
9. Use find to display the names of all files in the system that are bigger
than 1MB.

10. Use find and file to display all files in the /home subdirectory tree, as
well as a guess at what sort of a file they are. Do this in two different
ways.
ANS 9, 10 :
Find files by size
To find files based on the size, pass the -size parameter along with the size
criteria. You can use the following surffixes to specify the file size.
b: 512-bytes block (default)
c: bytes
w: two- bytes words
k: kilobytes
M: megabytes
G: Gigabytes
following command will find all files of exactly 1024 bytes inside the /tmp
directory
$find /tmp -type f -size 1024c

Use find to display the names of all files in the system that are bigger
than 1MB.

You can even search for files within a size range. The following command
will find all files betweenn1 and 2MB
The command below will list all files in the home directory that were
modified 30 or more days ago:

You can easily filter all files under the/etc/dovecot/conf.d directory that
ends with .conf and has been modified in the last five days:

Find by permission

command will match all the files with read permissions set for either user, group,
or others.
Find . -perm /444

Find SGID files


find all SGID set files
#find. / -perm /g=s
find Read only files
#find . / -perm /u=r
find executable files
#find . / -perm / a=x
find files with 777 permissions and chmod to 644
#find . / -type f -perm 0777 -print -exec chmod 644 {}\;
find and remove single file
To find a single files called “tecmint.txt” and remove it
#find . -type f -name “tecmint.txt” -exec rm -f {}\;
find all empty files
#find . /tmp -type f -empty

11. Use grep to isolate the line in /etc/passwd that contains your login
details.

Ans 11:

12. Use find and grep and sort to display a sorted list of all files in the /home
subdirectory tree that contain the word hello somewhere inside them.

Ans 12:

13. Use locate to find all filenames that contain the word emacs. Can you
combine this with grep to avoid displaying all filenames containing the
word lib?

Ans 13:

15. Archive the contents of your home directory (including any


subdirectories) using tar and cpio. Compress the tar archive with
compress, and the cpio archive with gzip. Now extract their contents.
ANS 15:
Extract the file :

Create compressed tar archive file in Linux :


NOTE:-
Untar multiple .tar, .tar.gz, .tar.tbz file in Linux :
This command will extract or untar multiple files from the tar, tar.gz and tar.bz2
archive file. For example the above command will extract “file1” “fiile2” from the
archive files.
$ tar xvf file.tar "file1 "file2"
or
$ tar zxvf file1.tar.gz "file1" "file2"
or
$ tar jxvf file2.tar.tbz "file1" "file2"

Check size of existing tar, tar.gz, tar.tbz file in Linux : ?The above
command will display the size of archive file in Kilobytes(KB).
$ tar czf file.tar | wc -c
or
$ tar czf file1.tar.gz | wc -c
or
$ tar czf file2.tar.tbz | wc -c

Update existing tar file in Linux


$ tar rvf file.tar *.c
Output :
os1.c

list the contents and specify the tarfile using option -tf :
This command will list the entire list of archived file. We can also list for specific
content in a tarfile
$ tar tf file.tar
Output :
example.cpp
Applying pipe to through ??grep command?? to find what we are looking
for :
This command will list only for the mentioned text or image in grep from archived
file.
$ tar tvf file.tar | grep "text to find"
or
$ tar tvf file.tar | grep "filename.file extension"

We can pass a file name as an argument to search a tarfile :


This command views the archived files along with their details.
$ tar tvf file.tar filename

Viewing the Archive using option -tvf


$ tar tvf file.tar
Output :
-rwxrwxrwx root/root 191 2017-09-17 02:20 os2.c
-rwxrwxrwx root/root 218 2017-09-17 02:20 os3.c
-rwxrwxrwx root/root 493 2017-09-17 02:20 os4.c

USING CPIO:
Untar using CPIO:

17. Type mount (with no parameters) and try to interpret the output.

Ans 17 :
The “mount” command is used to connect an external device file system to the
system's file system.
18. list the different editors available in linux and what are the difference
between those and vi editor.
Ans 18:
In this section, we are going to discuss the top 20 text editors for Linux. Further,
we will talk about the latest text editors and will compare them with the
traditional text editors such as Viand nano This will help you with selecting the
editor of your choice.
1. Vi/VIM editor
2. Nano editor
3. Gedit editor
4. Sublime text editor
5. VSCode
6. GNU emacs
7. Atom editor
8. Brackets editor
9. Pico editor
10. Bluefish
11. Kate/Kwrite
12. Notepad ++
13. Eclipse
14. gVIM editor
15. Jed editor
16. Geany editor
17. Leaf Pad
18. Light Table
19. Medit text editor
20. CodeLite

Vi EDITOR:
Vim editor is one of the most used and powerful command-line based editor of the
Linux system. By default, it is supported by most Linux distros. It has enhanced
functionalities of the old Unix Vi editor. It is a user-friendly editor and provides the
same environment for all the Linux distros. It is also termed as programmer's
editor because most programmers prefer Vi editor.
Vi editor has some special features such as Vi modes and syntax highlighting that
makes it powerful than other text editors. Generally, it has two modes:
Command Mode: The command mode allows us to perform actions on files. By
default, it starts in command mode. In this mode, all types of words are
considered as commands. We can execute commands in this mode.
Insert Mode: The insert mode allows to insert text on files. To switch from
command mode to insert mode, press the Esc key to exit from active mode
and 'i' key.

19. What is the difference between ctrl + C,ctrl +z and ctrl + d?


ANS 19:
ctrl z is used to pause the process. It will not terminate your program, it will keep
your program in background. You can restart your program from that point where
you used ctrl z.
ctrl c is used to kill a process. It terminates your program.
Ctrl D: Used to terminate input or exit the terminal or shell
means end of file. It only works at the beginning of a line and has no effect if the
program isn't reading input from the terminal. In your experiment, ^D told the
shell that you weren't going to type any more commands, so it exited; then the
terminal exited because its subprogram had terminated.

20. Find keys to insert text in vim terminal.


ANS 20:
Key to insert the text in vim editor.
a - Append text following current cursor position
A - Append text to the end of current line
i - Insert text before the current cursor position
I - Insert text at the beginning of the cursor line
o - Open up a new line following the current line and add text there
O - Open up a new line in front of the current line and add text there

You might also like