Operating System Long Questions and AnswersHere in this section of Operating System Long Questions and Answers,We have listed out some of the important Long Questions with Answers on Lists of Useful Unix/Linux Commands which will help students to answer it correctly in their University Written Exam.

 

Lists of Useful Unix Commands that may be asked in Written Exams.

1) cal:– Displays a calendar

Syntax:- cal [options] [ month ] [year]

 

Description:-

  • cal displays a simple calendar. If arguments are not specified, the current month is displayed.

 

The switching options are as follows:

-1 Display single (current) month output. (This is the default.)

-3 Display prev/current/next month output

-s Display Sunday as the first day of the week (This is the default.)

-m Display Monday as the first day of the week

-j Display Julian dates (days one-based, numbered from January 1)

-y Display a calendar for the current year

 

Example:-

$cal

or

$cal 02 2019

Feb 2019

Su Mo Tu We Th Fr Sa

1 2 3 4 5 6

7 8 9 10 11 12 13

14 15 16 17 18 19 20

21 22 23 24 25 26 27

28 29

 

2) clear :- It clears the terminal screen.

Syntax: – clear

 

Description:-

  • Clear clears your screen if this is possible, including its scroll back buffer.
  • Clear ignores any command-line parameters that may be present.

 

3) pwd :- Displays path from root to current directory

Syntax: – pwd [options]

 

Example:

$ pwd

/home/exradar/progs

 

4) cd:- It is used to change the directory.

Syntax: – cd [directory]

 

Description:-

  • Used to go back one directory on the majority of all UNIX shells. It is important that the space be between the cd and directory name or ..

 

Example:-

$ pwd

/home/exradar

$ cd progs

$ pwd

/home/exradar/progs

$ cd ..

/home/exradar

 

5) ls:- Lists the contents of a directory

 

Syntax :- ls [options]

 

Description :-

-a Shows you all files, even files that are hidden (these files begin with a dot.)

-A List all files including the hidden files. However, does not display the working directory (.) or the parent directory (..).

-d If an argument is a directory it only lists its name not its contents

-l Shows you huge amounts of information (permissions, owners, size, and when last modified.)

-p Displays a slash ( / ) in front of all directories

-r Reverses the order of how the files are displayed

-R Includes the contents of subdirectories

 

Example:-

$ ls – l

-rw-r—– 1 trainee trainee 23 Jan 19 10:10 file1.txt

 

Explanation:

  • If first character is – then it is normal file
  • If it is d then it is directory
  • Field 1 – File Permissions: Next 9 character specifies the files permission. Each 3 characters refers to the read, write, execute permissions for user, group and world In this example, -rw-r—– indicates read-write permission for user, read permission

for group, and no permission for others.

  • Field 2 – Number of links: Second field specifies the number of links for that file. In this example, 1 indicates only one link to this file.
  • Field 3 – Owner: Third field specifies owner of the file. In this example, this file is owned by username ‘trainee’.
  • Field 4 – Group: Fourth field specifies the group of the file. In this example, this file belongs to ”trainee’ group.
  • Field 5 – Size: Fifth field specifies the size of file. In this example, ’13′ indicates the file size.
  • Field 6 – Last modified date & time: Sixth field specifies the date and time of the last modification of the file. In this example, ‘Jan 19 10:20′ specifies the last modification time of the file.
  • Field 7 – File or directory name: The last field is the name of the file or directory. In this example, the file name is file1.txt

6) exit :- It is used to terminate a program, shell or log you out of a network normally.

Syntax :- exit

 



 

7) echo :- It prints the given input string to standard output.

Syntax: – echo string

 

Example:-

$ echo “hi.. hello unix”

hi.. hello unix

 

8) who :- who command can list the names of users currently logged in, their terminal, the time they have been logged in, and the name of the host from which they have logged in.

Syntax: – who [options] [file]

 

Description:-

am i Print the username of the invoking user, The ‘am’ and ‘i’ must be space separated.

-b Prints time of last system boot.

-d print dead processes.

-H Print column headings above the output.

-l

Include idle time as HOURS:MINUTES. An idle time of . indicates activity within the last minute.

-m Same as who am i.

-q Prints only the usernames and the user count/total no of users logged in.

 

Example:-

$ who

examradar/1 2019-01-10 10:42 (:0.0)

examradar/2 2019-02-22 11:00 (:0.0)

 

  • Here first column shows user name, second shows name of the terminal the user is working on. Third& fourth column shows date and time of logging, last column shows machine name.

 

9) who am i:– Print effective userid

Syntax: – who am i

 

Description: – Print the user name associated with the current effective user id.

 

Example:-

$ who am i

examradar/3 2019-02-22 12:00 (:0.0)

 

  • Here first column shows user name, second shows name of the terminal the user is working on. Third & fourth column shows date and time of logging, last column shows machine name.

 

10) mkdir:– This command is used to create a new directory

Syntax: – mkdir [options] directory

 

Description:-

-m Set permission mode (as in chmod)

-p No error if existing, make parent directories as needed.

-v Print a message for each created directory directory The name of the directory that you wish to create

 

Example:-

$ mkdir aaa

  • The above command will create directory named aaa under the current directory. We can also create number of subdirectories with one mkdir command.

 

11) rmdir:- It is used to delete/remove a directory and its subdirectories.

Syntax: – rmdir [options..] Directory

 

Description:-

  • It removes only empty directory.

-p Allow users to remove the directory and its parent directories which become empty.

 

12) bc:- bc command is used for command line calculator. It is similar to basic calculator. By using which we can do basic mathematical calculations.

Syntax: – bc [options]

 

Description:-

  • bc is a language that supports arbitrary precision numbers with interactive execution of statements.
  • bc starts by processing code from all the files listed on the command line in the order listed. After all files have been processed, bc reads from the standard input.

 

All code is executed as it is read.

-q To avoid bc welcome message

-l To include math library functionalities

 

Example:-

$ bc

bc 1.06 Copyright 1991-1994,1997,1998,2000 Free Software Foundation,Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty’. 2*3

 

The above command used is for mathematical calculations.

$ bc -l

bc 1.06 Copyright 1991-1994,1997,1998,2000 Free Software Foundation,Inc. This is free software with ABSOLUTELY NO WARRANTY.For details type `warranty’.11+2

 

The above command displays the sum of ’11+2′.

$ bc calc.txt

bc 1.06 Copyright 1991-1994,1997,1998,2000 Free Software Foundation,Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty’. 13

‘calc.txt’ file have the following code:11+2. Get the input from file and displays the output.

 

13) uname:- It is used to print system information.

Syntax: – uname [options]

 

Description:-

Print certain system information.

-s print the kernel name

-n print the network node hostname

-r print the kernel release

-v print the kernel version

-m print the machine hardware name

-o print the operating system

 

Example:-

$ uname

Linux

 

14) tty:– Print the file name of the terminal connected to standard input.

Syntax: – tty

 

Description:-

  • tty writes the name of the terminal that is connected to standard input onto
  • Command is very simple and needs no arguments.

 

Example:-

$tty

/exradar/tty1

 

15) stty:- Change and print terminal line settings.

Syntax: – sty

 

Description:-

  • stty sets certain terminal I/O modes for the device that is the current standard input.
  • Without arguments, it writes the settings of certain modes to standard output.

 

Example:-

$ sty

speed 19200 baud, 25 rows, 79 columns

kill = ^U

 



 

16) cat:– It is used to create, display and concatenate file contents.

Syntax: – cat [options] [FILE]…

 

Description:-

-A Show all.

-b Omits line numbers for blank space in the output.

-e A $ character will be printed at the end of each line prior to a new line.

-E Displays a $ (dollar sign) at the end of each line.

-n Line numbers for all the output lines.

-s If the output has multiple empty lines it replaces it with one empty line.

-T Displays the tab characters in the output.

-v Non-printing characters (with the exception of tabs, new-lines and form-feeds) are printed visibly.

 

  • Two basically three uses of the cat command.

1) Create new files.

2) Display the contents of an existing file.

3) Concatenate the content of multiple files and display.

 

Example:-

$ cat file1.c

  • Above syntax will display the content of file1.c

$ cat > file1.c

  • Above syntax creates file1.c and allow us to insert content for this file.
  • After inserting content you can use ctrl+d to exit the file.
  • If file with same name exist then it will overwrite that file.

$ cat file1.c

process management

memory management

file mgmt

$ cat file1.c >> file2.c

  • It can concatenate the contents of two files. For this you have to use append output redirection operator.
  • The contents of file2.cwill be appended to file1.c.

 

 

17) cp:- cp command copy files from one location to another. If the destination is an existing file, then the file is overwritten; if the destination is an existing directory, the file is copied into the directory (the directory is not overwritten).

Syntax: – cp [options]… source destination

 

Description:-

  • Here, after cp command contents of both source file and destination file files are the same.
  • It will copy the content of source file to destination file.
  • If the destination file doesn’t exist, it will be created.
  • If it exists then it will be overwritten without any warning.
  • If there is only one file to be copied then destination can be the ordinary file or the directory file.

-a archive files

-f force copy by removing the destination file if needed

-i interactive – ask before overwrite

-l link files instead of copy

-L follow symbolic links

-n no file overwrite

-u update – copy when source is newer than dest

 

Example:-

$ cp file1 file2

  • The above cp command copies the content of file1.php to file2.php.

 

Copy folder and subfolders:

$ cp-R scripts scripts1

  • The above cp command copy the folder and subfolders from scripts to scripts1

 

 

18) rm:- It is used to remove/delete the file from the directory.

Syntax: – rm [options..] [file|directory]

 

Description:-

  • Files can be deleted with rm. It can delete more than one file with a single invocation. For deleting a single file we have to use rm command with filename to be deleted.
  • Deleted file can’t be recovered. rm can’t delete the directories. If we want to remove all the files from the particular directory we can use the * symbol.

-f Ignore nonexistent files, and never prompt before removing.

-i Prompt before every removal.

 

Example:-

$ rm myfile.txt

  • Remove the file myfile.txt. If the file is write-protected, you will be prompted to confirm that you really want to delete it.

$ rm *

  • Remove all files in the working directory. If it is write-protected, you will be prompted before rm removes it.

$ rm -f myfile.txt

  • Remove the file myfile.txt. You will not be prompted, even if the file is writeprotected; if rm can delete the file, it will.

$ rm -f *

  • Remove all files in the working directory. rm will not prompt you for any reason before deleting them.

$ rm -i *

  • Attempt to remove every file in the working directory, but prompt before each file to confirm.

 

 

19) mv:- It is used to move/rename file from one directory to another.

Syntax: – mv [options] oldname newname

 



 

Description:-

  • mv command which is short for move.
  • mv command is different from cp command as it completely removes the file from the source and moves to the directory specified, where cp command just copies the content from one file to another.
  • mv has two functions: it renames a file and it moves a group of files to a different directory. Mv doesn’t create a copy of the file , it merely renames it. No additional space is consumed on disk during renaming. For example if we rename a file os to os1 and then if we try to read file os we will get error message as it is renamed to os1 there is no existence of file named os. f mv will move the file(s) without prompting even if it is writing over an existing target. Note that this is the default if the standard input is not a terminal

-i Prompts before overwriting another file

 

Example:-

$ cat file1

Memory

Process

Files

$ mv file1 file2

  • rename file1 to file2
  • If the destination file doesn’t exist it will be created. mv can also be used to rename a directory. A group of files can also be moved to a directory. mv doesn’t prompt for overwriting destination file if it exists.

 

 

20) nl :-

nl numbers the lines in a file.

Syntax: – nl [OPTION] [FILE]

 

Example:-

$cat list.txt

apples

oranges

potatoes

lemons

garlic

$nl list.txt

1 apples

2 oranges

3 potatoes

4 lemons

5 garlic

  • In the above example, we use the cat command to display the contents of list.txt.

 

Then we use nl to number each line and display the result to standard output.

$nl list.txt > nlist.txt

$cat nlist.txt

1 apples

2 oranges

3 potatoes

4 lemons

5 garlic

  • In the above example, we run the same nl command, but redirect the output to a new file, nlist.txt. Then we use cat to display the results.

 

 

21) cut :- cut command is used to cut out selected fields of each line of a file. The cut command uses delimiters to determine where to split fields.

Syntax: – cut [options] filename

 

Description:-

file A path name of an input file. If no file operands are specified, or if a file operand is -, the standard input will be used

-c The list following -c specifies character positions

-d The character following -d is the field delimiter

-f select only these fields on each line

-b Select only the bytes from each line as specified in LIST

 

Example:-

  • For example, let’s say you have a file named data.txt which contains the following text:

one two three four five alpha beta gamma delta epsilon

  • In this example, each of these words is separated by a tab character, not spaces.

The tab character is the default delimiter of cut, so it will by default consider a field to be anything delimited by a tab.

  • To “cut” only the third field of each line, use the command:

$ cut -d’ ‘ -f 3 data.txt

three gamma

  • let’s say you want the third field and every field after it, omitting the first two fields. In this case, you could use the command:

$ cut -d’ ‘ -f 3- data.txt

three four five gamma delta epsilon

$ cut -c 3 file.txt

r

m

 

For example, to output only the third-through-twelfth character of every line of data.txt, use the command:

$ cut -c 3-12 data.txt

e two thre

pha beta g

 

 

22) paste:- paste command is used to paste the content from one file to another file. It is also used to set column format for each line.

Syntax: – paste [option] file

 

Description:-

  • Paste prints lines consisting of sequentially corresponding lines of each specified file. In the output the original lines are separated by TABs. The output line is terminated with a newline.

-d Specify of a list of delimiters.

-s Paste one file at a time instead of in parallel.

File A path name of an input file. If – is specified for one or more of the file s, the standard input will be used.

 

Example:-

$ cat > a

Unix

Linux

Windows

$ cat > b

Dedicated server

Virtual server

$ paste a b

Unix Dedicated server

Linux Virtual server

Windows

$ paste -d”|” a b

Unix|Dedicated server

Linux|Virtual server

Windows|

$ paste –s a b

Unix Linux Windows

Dedicated server Virtual server

$ paste –s –d”,” a b

Unix,Linux,Windows Dedicated server,Virtual server

 



 

23) more:- Displays text one screen at a time.

Syntax: – more [options] filename

 

Description:-

  • More command displays its output a page at a time. For example we are having a big file with thousands of records and we want to read that file then we should use more command. Consider a file named employees then we will use following

command to read its contents a page at a time.

-c Clear screen before displaying.

-e Exit immediately after writing the last line of the last file in the argument list.

-n Specify how many lines are printed in the screen for a given file.

+n Starts up the file from the given number.

 

Example:-

$ more -c myfile.txt

  • Clears the screen before printing the file .

$ more -3 myfile.txt

  • Prints first three lines of the given file. Press Enter to display the file line by line.

$ more +/”hope” myfile.txt Display the contents of file myfile.txt, beginning at the first line containing the

string “hope”.

 

 

24) cmp:- It compares two files and tells you which line numbers are different.

Syntax: – cmp [options..] file1 file2

 

Description:-

  • Let’s create a file named os2. And use cmp command to compare os and os1files.

– c Output differing bytes as characters.

– l

Print the byte number (decimal) and the differing byte values (octal) for each difference.

– s Prints nothing for differing files, return exit status only.

– c Output differing bytes as characters.

 

Example:-

$ cat file1

memory

process

files

$ cat > file2

memory

process

files mgmt

$ cmp file1 file2

File1 file2 differ: char 21, line 3

  • The two files are compared byte by byte and the location of the first mismatch is echoed to the screen. cmp doesn’t bother about possible subsequent mismatches.

 

 

25) comm:- compare two sorted files line by line

Syntax:- comm [option]… FILE1 FILE2

 

Description:-

  • Compare sorted files FILE1 and FILE2 line by line.
  • Requires two sorted files and lists differing entries in different columns. produces three text columns as output:

1 Lines only in file1.

2 Lines only in file2.

3 Lines in both files.

-1 suppress lines unique to FILE1

-2 suppress lines unique to FILE2

-3 suppress lines that appear in both files

 



 

  • For example let’s create two files named file1 and file2 with following data.

$ cat > file1

tim caffey

john barnand

josef [1] + Stopped

$ cat > file2

errica whiller

tim caffey

tom whilley

  • Now let’s use comm. Command with these two files.

$ comm file1 file2

tim caffey

john barnand

josef

errica whiller

tom whilley

  • In the above output we can see that first column contains two lines unique to the first file and second column contains three lines unique to the second file and the third column contains two lines common to both the files. Comm. Can produce the

single column output using 3 options -1,-2 or -3. To drop a particular column, simply use its column number as a prefix.

 

Example:-

$ cat file1

f1.c

f2.c

f3.c

f4.c

f5.c

$ cat file2

f1.c

f3.c

f4.c

f6.c

f7.c

  • Now, When you run the comm command on these files, this is what you get:

$ comm file1 file2

f1.c

f2.c

f3.c

f4.c

f5.c

f6.c

f7.c

  • The output is split in 3 columns. Column1 indicates files which are unique in file1, column 2 indicates files unique to file2. Column 3 indicates files common between them. comm command provides some real good options with which you can filter

the output better.

  • Now, say you want to find out only the list of files which were there in the older version but not in the newer version:

$ comm -23 file1 file2

f2.c

f5.c

  • The option -23 indicates to remove the second and third columns from the comm command output, and hence we are left with only the first column which is the files unique in file1.
  • Similarly, to find out the list of files which were not there in the old version, but has been added in the new version:

$ comm -13 file1 file2

f6.c

f7.c

  • As explained above, -13 option tells to remove the first and third columns from the comm output.
  • Finally, to know the list of files which have been retained, or common in both the versions:

$ comm -12 file1 file2

f1.c

f3.c

f4.c

  • When you apply comm command on files, the files should be sorted. This command works only on sorted files.

 

 

26) diff:- It is used to find differences between two files.

Syntax: – diff [options..] fileone filetwo

 

Description:-

  • Diff is the third command that can be used to display file differences. Unlike its fellow members ,cmp and comm. , it also tells us which lines in one file have to be changed to make the two files identical.

-b Ignore any changes which only change the amount of whitespace (such as spaces or tabs).

-w Ignore whitespace entirely.

-B Ignore blank lines when calculating differences.

-y Display output in two columns.

-i Ignore changes in case.consider upper- and lower-case letters equivalent.

 

Example:-

$ cat 1.txt

aaa

bbb

ccc

ddd

eee

fff

ggg

$ cat 2.txt

bbb

c c

ddd

eee

fff

ggg

hhh

$ diff 1.txt 2.txt

1d0

< aaa

3c2

< ccc

> c c

7a7

> hhh

  • Lines like “1d0” and “3c2” are the coordinates and types of the differences between the two compared files, while lines like “< aaa” and “> hhh” are the differences themselves.
  • diff change notation includes 2 numbers and a character between them.

Characters tell you what kind of change was discovered:

  • d – a line was deleted
  • c – a line was changed
  • a – a line was added
  • Number to the left of the character gives you the line number in the original (first) file, and the number to the right of the character tells you the line number in the second file used in comparison.
  • So, looking at the two text files and the diff output above, you can see what happened:

1d0

< aaa

  • This means that 1 line was deleted. < aaa suggests that the aaa line is present only in the original file.

3c2

< ccc

> c c

  • And this means that the line number 3 has changed. You can see how this confirms that in the first file the line was “ccc”, and in the second it now is “c c”.

7a7

> hhh

  • Finally, this confirms that one new line appeared in the second file, it’s “hhh” in the line number 7.

 

 

27) chmod:- chmod command allows you to alter / Change access rights to files and directories.

Syntax:- chmod [options] [MODE] FileName

 



 

Description:-

  • chmod command is used to set the permissions of one or more files for all three categories of users (user,group and others ). It can be run only by the user and super user. Command can be used in two ways. Let’s first take a look at the

abbreviations used by chmod command.

Category Operation Permission

u User + Assigns permission r Read permission

g Group – Removes

permission

w Write permission

o Others = Assigns absolute x Execute permission

File Permission :

# File Permission

0 none

1 execute only

2 write only

3 write and execute

4 read only

5 read and execute

6 read and write

7 set all permissions

Relative permissions

  • When changing permissions in a relative manner, chmod only changes the permissions specified in the command line and leaves the other permissions

unchanged. In this mode it uses the following syntax:

  • let’s first check the permission of file shortlist. It shows read/write permission.

Let’s assign execute permission to this file for owner.

$ ls –l

-rw-r–r—1 shortlist None 241 Feb 21 04:02

$ chmod u+x shortlist

$ ls -l

-rwxr–r– 1 shortlist None 241 Feb 21 04:02

  • Here chmod uses expression u+x means – u shows user category that is user, + shows we need to assign the permission and x shows execute permission.Sameway we can assign other permissions.

Absolute permissions

  • Sometimes you don’t need to know what a file’s current permissions are, but want to set all nine permission bits explicitly. The expression used by chmod here is a string of three octal numbers. Each type of permission is assigned a number as shown:

Read permission-4

Write permission-2

Execute permission -1

  • For each category we add numbers that represent the assigned permission. For instance , 6 represents read and write permission , and 7 represents all permissions.

 

Example:

$ chmod 666 myfile or(chmod u=rw,g=rw,o=rwmyfile) shortlist

  • Shows read and wrie (4 +2) permissions for all three types of users.

 

 

28) chown:- Command for system V that changes the owner of a file.

Syntax: – chown [options] newowner filename/directoryname

 

Description:-

-R

Change the permission on files that are in the subdirectories of the directory that you are currently in.

-c Change the permission for each file.

-f

Prevents chown from displaying error messages when it is unable to change the ownership of a file.

 

Example:-

$ ls -l demo.txt

-rw-r–r– 1 root root 0 Feb 20 10:10 demo.txt

$ chownvivek demo.txt

-rw-r–r– 1 vivek root 0 Feb 20 10:10 demo.txt

 

 

29) chgrp:- chgrp command is used to change the group of the file or directory. This is an admin command. Root user only can change the group of the file or directory.

Syntax:- chgrp [options] newgroup filename/directoryname

 

Description:-

-R

Change the permission on files that are in the subdirectories of the directory

that you are currently in.

-c Change the permission for each file.

-f Force. Do not report errors.

 

Example:-

$ ls -l demo.txt

-rw-r–r– 1 root root 0 Feb 20 10:10 demo.txt

$ chgrp Tom demo.txt

-rw-r–r– 1 root Tom 0 Feb 20 10:10 demo.txt

 

 

30) file:- file command tells you if the object you are looking at is a file or a directory.

Syntax:- file [options] directoryname/filename

 

Description:-

  • File command is used to determine the type of file, especially of an ordinary file. We can use it with one or more filenames as arguments. For example we can use file command to check the type of the os1 file that we have created.

$ file os1

os1: short english text

 

 

31) finger:- finger command displays the user’s login name, real name, terminal name and write status (as a ”*” after the terminal name if write permission is denied), idle time, login time, office location and office phone number.

Syntax:- finger [username]

 

Description:-

-l Force long output format

-s Force short output format

 

Example:-

$ finger Julian

Login: abc Name: (null)

Directory: /home/abc Shell: /bin/bash

On since Mon Nov 1 18:45 (IST) on :0 (messages off)

On since Mon Nov 1 18:46 (IST) on pts/0 from :0.0

New mail received Mon  9 11:40 2018 (IST)

Unread since Wed Jan 8 10:00 2018 (IST)

No Plan.

 

 

32) sleep:- Delay for a specified amount of time

Syntax: – sleep NUMBER[SUFFIX]

 

Description:-

  • The sleep command pauses for an amount of time defined by NUMBER.
  • SUFFIX may be “s” for seconds (the default), “m” for minutes, “h” for hours, or “d” for days.

 

Example:-

  • To sleep for 5 seconds, use:

$ sleep 5

  • To sleep for 2 minutes, use:

$ sleep 2m

  • To sleep for 3 hours, use:

$ sleep 3h

  • To sleep for 5 days, use:

$ sleep 5d

 

 

33) kill:- kill command is used to kill the background process.

Syntax:- kill [options] pid

 

Description:-

  • The command kill sends the specified signal to the specified process or process group.
  • If no signal is specified, the TERM signal is sent. The TERM signal will kill processes which do not catch this signal.
  • For other processes, it may be necessary to use the KILL (9) signal, since this signalcannot be caught.

pid. Specify the list of processes that kill should signal

-s send the specified signal to the process

-l list all the available signals.

-9 Force to kill a process.

 

 

34) Users :- users command displays currently logged in users.

Syntax:- users

 

Output:-

$ users

trainee trainee

 

 

35) ps:- It is used to report the process status. ps is the short name for Process Status.

Syntax:- ps [options]

 

Description:-

-a List information about all processes most frequently requested: all those except process group leaders and processes not associated with a terminal

-A List information for all processes. Identical to -e, below

-f Generate a full listing

-j Print session ID and process group ID

-l Generate a long listing

 

Example:-

$ps

 

PID TTY TIME CMD

291 console 0:00 bash

 

 

36) ln :- ln command is used to create link to a file (or) directory. It helps to provide soft link for desired files.

Syntax:- ln [options] existingfile(or directory)name newfile(or directory)name

 

Description:-

What Is A Link?

  • A link is an entry in your file system which connects a filename to the actual bytes of data on the disk. More than one filename can “link” to the same data. Here’s an example. Let’s create a file named file1.txt:

$ echo “This is a file.” > file1.txt

  • This command echoes the string “This is a file”. Normally this would simply echo to our terminal, but the > operator redirects the string’s text to a file, in this case file1.txt
  • When this file was created, the operating system wrote the bytes to a location on the disk and also linked that data to a filename, file1.txt so that we can refer to the file in commands and arguments.
  • If you rename the file, the contents of the file are not altered; only the information that points to it.
  • The filename and the file’s data are two separate entities.
  • What the link command does is allow us to manually create a link to file data that already exists.
  • So, let’s use link to create our own link to the file data we just created. In essence, we’ll create another file name for the data that already exists.

$ link file1.txt file2.txt

  • The important thing to realize is that we did not make a copy of this data. Both filenames point to the same bytes of data on the disk. Here’s an illustration to help you visualize it:
  • If we change the contents of the data pointed to by either one of these files, the other file’s contents are changed as well. Let’s append a line to one of them using

the >>operator:

$ echo “It points to data on the disk.” >> file1.txt

  • Now let’s look at the contents of file1.txt:

$ cat file1.txt

This is a file.

  • now let’s look at the second file, the one we created with the link command.

$ cat file2.txt

This is a file.

  • ln, by default, creates a hard link just like link does. So this ln command:

$ ln file1.txt file2.txt

  • It is the same as the following link command. Because, both commands create a hard link named file2.txt which links to the data offile1.txt.

$ link file1.txt file2.txt

  • However, we can also use ln to create symbolic links with the -s option. So the command:

$ ln -s file1.txt file2.txt

  • It will create a symbolic link to file1.txt named file2.txt. In contrast to our hard link example, here’s an illustration to help you visualize our symbolic link:
  • You should also be aware that, unlike hard links, removing the file (or directory) that a symlink(symbolic linkl) points to will break the link. So if we create file1.txt:

$ echo “This is a file.” > file1.txt

  • Now, create a symbolic link to it:

$ ln -s file1.txt file2.txt

  • we can cat either one of these to see the contents:

$ cat file1.txt

This is a file.

$ cat file2.txt

This is a file.

  • But, if we remove file1.txt:

$ rm file1.txt

  • we can no longer access the data it contained with our symlink:

$ cat file2.txt

cat: file2.txt: No such file or directory

-s Makes it so that it creates a symbolic link

-f If the destination file or files already exist, overwrite them.

 

 

37) head:- head command is used to display the first ten lines of a file, and also specifies how many lines to display.

Syntax:- head [options] filename

 

Description:-

  • Head command displays the top of the file. When used without an option , it displays the first ten lines of the file.

-n To specify how many lines you want to display.

-n number The number option-argument must be a decimal integer whose sign affects the location in the file, measured in lines.

-c number The number option-argument must be a decimal integer whose sign affects the location in the file, measured in bytes.

 

Example:-

$ head myfile.txt

  • Display the first ten lines of myfile.txt.

$ head -15 myfile.txt

  • Display the first fifteen lines of myfile.txt.

$ head -c 20 myfile.txt

  • Will output only the first twenty bytes (characters) of myfile.txt. Newlines count as a single character, so if head prints out a newline, it will count it as a byte.

$ head -n 5K myfile.txt

  • Displays the first 5,000 lines of myfile.txt.

 

 

38) tail:- tail command is used to display the last or bottom part of the file. By default it displays last 10 lines of a file.

Syntax: – tail [options] filename

 

Description:-

l To specify the units of lines.

-b To specify the units of blocks.

-n To specify how many lines you want to display.

-c number The number option-argument must be a decimal integer whose sign affects the location in the file, measured in bytes.

-n number The number option-argument must be a decimal integer whose sign affects the location in the file, measured in lines.

 

Example: –

$ tail index.txt

  • It displays the last 10 lines of ‘index.txt’.

$ tail -2 index.txt

  • It displays the last 2 lines of ‘index.txt’.

$ tail -n 5 index.txt

  • It displays the last 5 lines of ‘index.txt’.

$ tail -c 5 index.txt

  • It displays the last 5 characters of ‘index.txt’.

 

 

39) sort:- It is used to sort the lines in a text file.

Syntax:- sort [options] filename

-b Ignores spaces at beginning of the line

-c Check whether input is sorted; do not sort

-r Sorts in reverse order

-u If line is duplicated only display once

-r Reverse the result of comparisons.

 

Example:-

$ cat > data.txt

apples

oranges

pears

kiwis

bananas

$ sort data.txt

apples

bananas

kiwis

oranges

pears

  • Note that this command does not actually change the input file, data.txt. If you want to write the output to a new file, output.txt, redirect the output like this:

$ sort data.txt > output.txt

  • it will not display any output, but will create the file output.txt with the same sorted data from the previous command. To check the output, use the cat

command:

$ cat output.txt

apples

bananas

kiwis

oranges

pears

  • You can also use the built-in sort option -o, which allows you to specify an output file:

$ sort -o output.txt data.txt

  • Using the -o option is functionally the same as redirecting the output to a file; neither one has an advantage over the other.

Sorting In Reverse Order :

  • You can perform a reverse-order sort using the -r flag. For example, the following  command:

$ sort -r data.txt

pears

oranges

kiwis

bananas

apples

Checking For Sorted Order

  • If you just want to check to see if your input file is already sorted, use the -c option:

$ sort -c data.txt

  • If your data is unsorted, you will receive an informational message reporting the line number of the first unsorted data, and what the unsorted data is.

 

 

40) find:- Finds one or more files assuming that you know their approximate path.

Syntax :- find [options] path

 

Description:-

  • Find is one of the powerful utility of Unix (or Linux) used for searching the files in a directory hierarchy

path A path name of a starting point in the directory hierarchy

-maxdepth Descend at most levels (a non-negative integer) levels of directories below the command line arguments.

-i ignore the case in the current directory and sub-directories.

size Find file based on size

 

Example:-

$ find -name “sum.java”

./bkp/sum.java

./sum.java

$ find -iname “sum.java”

./SUM.java

./bkp/sum.java

./sum.java

$ find -maxdepth 1 -name “sum.java”

./sum.java

  • Find Files Under Home Directory
  • Find all the files under /home directory with name myfile.txt.

$ find /home -name myfile.txt

/home/myfile.txt

$ find . -size 10M

  • Display files whose size is exactly 10M

$ find . -size +10M

  • Display files larger than 10M size

$ find -newer “sum.java”

  • Display the files which are modified after the modification of a give file.

$ find . -perm 777

  • display the files based on the file permissions.
  • This will display the files which have read, write, and execute permissions. To know the permissions of files and directories use the command “ls -l”.

$ find -name ‘*’ -size +1000k

  • The system would search for any file in the list that is larger than 1000k.

 

 

41) uniq:- Report or filter out repeated lines in a file.

Syntax:- uniq [option] filename

 

Description: –

-c Precede each output line with a count of the number of times the line occurred in the input

-d Suppress the writing of lines that are not repeated in the input

-D Print all duplicate lines

-f Avoid comparing first N fields

-i Ignore case when comparing

-s Avoid comparing first N characters.

-u Prints only unique lines

-w Compare no more than N characters in lines

 

Example:-

$ cat example.txt

Unix operating system

unix operating system

unix dedicated server

linux dedicated server

  • The default behaviour of the uniq command is to suppress the duplicate line. Note that, you have to pass sorted input to the uniq, as it compares only successive lines.

$ uniq example.txt

unix operating system

unix dedicated server

linux dedicated server

  • The -c option is used to find how many times each line occurs in the file. It prefixes each line with the count.

$ uniq -c example.txt

2 unix operating system

1 unix dedicated server

1 linux dedicated server

  • You can print only the lines that occur more than once in a file using the -d option.

$ uniq -d example.txt

unix operating system

  • The -D option prints all the duplicate lines.

$ uniq -D example.txt

unix operating system

unix operating system

 

 

42) tr:- Translate characters.

Syntax:- tr [options] set1 [set2]

 

Description:-

-c Complement the set of characters specified by string1

-d Delete all occurrences of input characters that are specified by string1

-s replaces repeated characters listed in the string1 with single occurrence

Set1 First string or character to be changed

set2 Second string or character to change the string1

 

Example:-

  • Convert lower case letters to upper case
  • The following tr command translates the lower case letters to capital letters in the give string:

$ echo “linux dedicated server” | tr “[:lower:]” “[:upper:]”

LINUX DEDICATED SERVER

$ echo “linux dedicated server” | tr “[a-z]” “[A-Z]”

LINUX DEDICATED SERVER

  • The -c option is used to replace the non-matching characters with another set of characters.

$ echo “unix” | tr -c “u” “a”

uaaa

  • You can squeeze more than one occurrence of continuous characters with single occurrence. The following example squeezes two or more successive blank spaces into a single space.

$ echo “linux server” | tr -s ” ”

linux server

  • The following example removes the word linux from the string.

$ echo “linuxserver” | tr -d “linux”

Server

 

 

43) history:- history command is used to list out the recently executed commands in the number line order.

Syntax:- history [options]

 

Description:-

  • The history command performs one of several operations related to recentlyexecuted commands recorded in a history list. Each of these recorded commands is referred to as an “event”.

-c clear the history list by deleting all of the entries.

 

Example:-

  • To list the recently executed commands:

$ history

  • This command is used to list the history.

To find specific command in history list:

$ history | grep cd

33 cd Pictures/

37 cd ..

39 cd Desktop/

61 cd /usr/bin/

68 cd

  • This command is used to list only cd commands from history list.

To copy history list to file

$ history -a history.txt

  • This command is used to copy history list to history.txt file .

To Clear all history:

$ history -c

  • This command can be used to clear all history from history list.

 

 

44) write:- Send a message to another user.

Syntax:- write person [ttyname]

 

Description:-

  • The write utility allows you to communicate with other users, by copying lines from your terminal to theirs.
  • When you run the write command, the user you are writing to gets a message of the format:

Message from [email protected] on yourtty at hh:mm …

  • Any further lines you enter will be copied to the specified user’s terminal. If the other user wants to reply, they must run write as well.
  • When you are done, type an end-of-file or interrupt character. The other user willsee the message ‘EOF’ indicating that the conversation is over.person If you wish to talk to someone on your own machine, then person is just the person’s login name. If you wish to talk to a user on another host, then person is of the form ‘[email protected]’.

ttyname If you wish to talk to a user who is logged in more than once, the ttyname argument may be used to indicate the appropriate terminal name, where ttyname is of the form ‘ttyXX’ or ‘pts/X’.

 

 

45) grep:- It selects and prints the lines from a file which matches a given string or pattern.

Syntax:- grep [options] pattern [file]

 

Description:-

  • This command searches the specified input fully for a match with the supplied pattern and displays it.
  • While forming the patterns to be searched we can use shell match characters, or regular expressions.
  • Let us begin with the simplest example of usage of grep.

-i Ignore case distinctions

-v Invert the sense of matching, to select non-matching lines.

-w Select only those lines containing matches that form whole words.

-x Select only matches that exactly match the whole line.

-c print a count of matching lines for each input file.

 

Example:-

$ grep “Error” logfile.txt

  • This searches for the string “Error” in the log file and prints all the lines that has the word “Error”.

$ grep “string” file1 file2

  • Searching for a string in multiple files.

$ grep -i “UNix” file.txt

  • The -i option enables to search for a string case insensitively in the give file. It matches the words like “UNIX”, “Unix”, “unix”.

$ grep -w “world” file.txt

  • By default, grep matches the given string/pattern even if it found as a substring in a file. The -w option to grep makes it match only the whole words.

$ grep -c “sting” file.txt

  • We can find the number of lines that matches the given string/pattern

$ grep -l “string” *

  • We can just display the files that contain the given string/pattern.

$ grep -n “string” file.txt

  • We can make the grep command to display the position of the line which contains the matched string in a file using the -n option

 

 

46) pwd:-Displaying your current directory name (Print working directory).

Syntax:-pwd [options]

 

Description:-

  • At the time of logging in user is placed in the specific directory of the file system. You can move around from one directory to another, but any point of time, you are located in only one directory. This directory is known as your current directory. pwd command tells your current directory.

 

Example:-

$ pwd

/home/abc

 

 

47) wc:- Word Count (wc) command counts and displays the number of lines, words,character and number of bytes enclosed in a file.

Syntax: – wc [options] [filename]

 

Description:-

  • This command counts lines, words and characters depending on the options used. It takes one or more filenames as its arguments and displays four-columnar output. For example let’s read our os1 file. And we use wc command with that filename.

-l print the newline counts.

-w print the word counts.

print the byte counts.

-m print the character counts.

-L print the length of the longest line.

 

Example:-

$cat myfile.txt

Red Hat

CentOS

Fedora

Debian

Scientific Linux

OpenSuse

Ubuntu

Xubuntu

Linux Mint

Pearl Linux

Slackware

Mandriva

  • The ‘wc‘ command without passing any parameter will display a basic result of ”myfile.txt‘ file. The three numbers shown below are 12 (number of lines), 16 (number of words) and 112 (number of bytes) of the file.

$wc myfile.txt

12 16 112 myfile.txt

  • To count number of newlines in a file use the option ‘-l‘, which prints the number of lines from a given file. Say, the following command will display the count of newlines in a file. In the output the first filed assigned as count and second field is the name of file.

$wc -l myfile.txt

12 myfile.txt

  • Using ‘-w‘ argument with ‘wc‘ command prints the number of words in a file. Type the following command to count the words in a file.

$wc -w myfile.txt

16 myfile.txt

  • When using options ‘-c‘ and ‘-m‘ with ‘wc‘ command will print the total number of bytes and characters respectively in a file.

$wc -c myfile.txt

112 myfile.txt

  • The ‘wc‘command allow an argument ‘-L‘, it can be used to print out the length of  longest (number of characters) line in a file. So, we have the longest character line (‘Scientific Linux‘) in a file.

$wc -L myfile.txt

16 myfile.txt

 

 

48) man:- man command which is short for manual, provides in depth information about the requested command (or) allows users to search for commands related to a particular keyword.

Syntax:- man commandname [options]

 

Description:-

-a Print a one-line help message and exit.

-k Searches for keywords in all of the manuals available.

 

Example:-

$ man mkdir

  • Display the information about mkdir command

 

 

49) | (Pipeline command):- Used to combine more than one command. Takes output of 1st command as input of 2nd command.

Syntax:- commmand1| command2|……

 

Example:-

$ls -l | grep “Jan ”

-rw-r–r– 1 trainee 336 Jan 10 10:41 calc1.sh

-rw-r–r—1 trainee 410 Jan 10 12:28 calc.sh

drwxr-xr-x 7 trainee 4096 Jan 12 12:30 Desktop

drwxr-xr-x 7 trainee 12288 Jan 15 12:40 Downloads

drwxr-xr-x 3 trainee 4096 Jan 17 12:10 lab

drwxr-xr-x 2 trainee 4096 Jan 20 12:05 shellscript

-rw-r–r—1 trainee 81 Jan 21 12:33 temp3.sh

 
You may be interested in:
Operating System Short Descriptive Questions and Answers

Operating System Multiple Choice Questions Answers (MCQs)

Operating System Online Tests

Windows Operating System Online Tests

Linux Operating System Online Tests

Operating System Tutorials