oss
oss
oss
Example: ls
Result: Lists the names of files in your default directory, in alphabetical order.
Example: ls -l
Result: Gives a "long listing" of the files in your directory. In addition to the file name, the long listing
shows protection information, file owner, number of characters in file, and the date and time of the
last change to the file.
Example: ls -a
Result: Causes all your files to be listed, including those files that begin with a period (i.e., hidden
files).
2. Pwd command:-
Pwd This command is used to show the current working directory. For example, /home/foobar
pwd –L This command is used to show the logical path of the current working directory with the
name of the symbolic link if any. For example, if existing in a dir /home/symlinked, it's a
symlink to /home/realdir; it would display /home/symlinked.
pwd –R This command is used to show the physical path of the current working directory without
the name of the symbolic link if any. For example, if existing in a dir /home/symlinked, it's a
symlink to /home/realdir; it would display /home/realdir.
3. Tty command :-
Syntax
/usr/bin/tty [ -s ]
Description
The tty command writes the name of your terminal to standard output.
If your standard input is not a terminal and you do not specify the -s flag, you get the
message Standard input is not a tty.
4. cat command:-
The cat command reads each File parameter in sequence and writes it to
standard output. If you do not specify a file name, the cat command reads from
standard input. You can also specify a file name of - (dash) for standard input.
The basic usage of the cat command is showing the file contents. Simply, we
need to type "cat" followed by the filename to show the file contents using the
terminal window: $ cat [filename].
5. Who command:-
Display who is on the system.
Syntax
The basic syntax is as follows:
who
who am i
who [options] [File]
who --help
who --version
who | grep 'userNameHere'
6. Who am i:-
Displays your login name without root authority.
Syntax
The basic syntax is as follows
whoami
whoami --version
whoami --help
examples To simply display your username, type:
$ whoami
7. Rm command :-
rm
*extension
-f -f stands for force. It ignores files that do not exist and overrides the confirmation
prompts. However, it will not delete files through a directory when it is write-
protected.
-v -v stands for verbose. It prints what the rm command is doing in the terminal.
-d -d stands for directory. It removes an empty directory and works only if the named
directory is empty.
--one-file- It only deletes files on a similar file system like the argument and will avoid the
system mounted file system.
8. Touch command :-
To create multiple files just type all the file names with a single touch command
followed by enter key. For example, if you would like to create 'myfile1' and
'myfile2' simultaneously, then your command will be: touch myfile1 myfile2.
9. cd command :-
The cd command, also known as chdir (change directory), is a command-line
shell command used to change the current working directory in various operating
systems.
The "cd" stands for 'change directory. ' It is one of the most frequently used
commands in the Linux terminal. Syntax: cd <dirname> .
10. mkdir command :-
The mkdir stands for 'make directory'. With the help of mkdir command, you can
create a new directory wherever you want in you r system. Just type "mkdir <dir
name> , in place of <dir name> type the name of new directory, you want to
create and then press enter.
11.Rmdir command:-
The rmdir command removes the directory, specified by the Directory parameter,
from the system. The directory must be empty before you can remove it, and you
must have write permission in its parent directory. Use the ls -al command to
check whether the directory is empty.
2. Usage of commands: cal, cat(append), cat(concatenate), mv, cp, man, date.
1.cal command:-
The cal command displays a calendar of the specified year or month. The Year
parameter names the year for which you want a calendar. Since the cal
command can display a calendar for any year from 1 through 9999, you must
enter the full year rather than just the last two digits.
2.cat(append) command:-
You can use cat with redirection to append a file to another file. You do this by
using the append redirection symbol, ``>>''. To append one file to the end of
another, type cat, the file you want to append, then >>, then the file you want to
append to, and press <Enter>.
3.cat(concatenate):-
The 'cat' command can be used to concatenate the contents of multiple files in a
single new file. Syntax: cat <filename1> <filename2>.... > <newFilename> .
4.mv command:-
The mv command moves files and directories from one directory to another or
renames a file or directory. If you move a file or directory to a new directory, it
retains the base file name. When you move a file, all links to other files remain
intact, except when you move it to a different file system.
5.cp command:-
Use the cp command to create a copy of the contents of the file or directory
specified by the SourceFile or SourceDirectory parameters into the file or
directory specified by the TargetFile or TargetDirectory parameters.
6.man command:-
The man command provides reference information on topics, such as
commands, subroutines, and files. The man command provides one-line
descriptions of commands specified by name. The man command also provides
information on all commands whose descriptions contain a set of user-specified
keywords.
7.date command:-
date command displays the current time and date (of the system) in the given
FORMAT. date command is also used by system administrator to set the system
date. Invoking 'date'command with no FORMAT argument displays system date
in dafault FORMAT. The default FORMAT depends on the 'LC_TIME'
environment variable.
3. Usage of commands: chmod, grep, tput(clear, highlight), bc.
chmod: In Unix operating systems, the chmod command is used to change the access mode of a file.
The name is an abbreviation of change mode. Which states that every file and directory has a set of
permissions that control the permissions like who can read, write or execute the file. In this the permissions
have three categories: read, write, and execute simultaneously represented by `r`, `w` and `x`. These letters
combine together to form a specific permission for a group of users.
The `chmod` command is used to modify this permission so that it can grant or restrict access to directories
and files.
Syntax:
chmod [options] [mode] [File_name]
Options Description
Apply the permission change recursively to all the files and directories within the
`-R` specified directory.
It will display a message for each file that is processed. while indicating the
`-v` permission change that was made.
It works same as `-v` but in this case it only displays messages for files whose
`-c` permission is changed.
`-h` Change the permissions of symbolic links instead of the files they point to.
grep: The grep filter searches a file for a particular pattern of characters, and displays all lines that contain that
pattern. The pattern that is searched in the file is referred to as the regular expression (grep stands for global search
for regular expression and print out).
Syntax:
grep [options] pattern [files]
Options Description
-c : This prints only a count of the lines that match a pattern
-h : Display the matched lines, but do not display the filenames.
-i : Ignores, case for matching
-l : Displays list of a filenames only.
-n : Display the matched lines and their line numbers.
-v : This prints out all the lines that do not matches the pattern
-e exp : Specifies expression with this option. Can use multiple times.
-f file : Takes patterns from file, one per line.
-E : Treats pattern as an extended regular expression (ERE)
-w : Match whole word
-o : Print only the matched parts of a matching line, with each such part on a separate output line.
-A n : Prints searched line and nlines after the result.
-B n : Prints searched line and n line before the result.
-C n : Prints searched line and n lines after before the result.
bc: bc command is used for command line calculator. It is similar to basic calculator by using which we can do basic
mathematical calculations.
Arithmetic operations are the most basic in any kind of programming language. Linux or Unix operating system
provides the bc command and expr command for doing arithmetic calculations. You can use these commands
in bash or shell script also for evaluating arithmetic expressions.
Syntax:
bc [ -hlwsqv ] [long-options] [ file ... ]
Options:
-h, {- -help } : Print the usage and exit
-i, {- -interactive } : Force interactive mode
-l, {- -mathlib } : Define the standard math library
-w, {- -warn } : Give warnings for extensions to POSIX bc
-s, {- -standard } : Process exactly the POSIX bc language
-q, {- -quiet } : Do not print the normal GNU bc welcome
-v, {- -version } : Print the version number and copyright and qu
4. Write a shell script to check if the number entered at the command line is prime or not.
Output:
Enter Number: 3
3 is a prime number
Enter Number: 10
10 is not a prime number.
5. Write a shell script to display the multiplication table of any number.
Output:
Enter the number: 8
8*1=8
8 * 2 = 16
8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
8 * 10 = 80
6. Write a shell script to find the sum of digits of a given number.
echo $sum
Output:
Enter a number
100
1
Enter a number
786
21
7. Write a shell script to find a factorial of a number.
fact=1
echo $fact
Output:
Enter a number
3
6
Enter a number
4
24
Enter a number
5
120
8. Write a shell script to find the LCD (Least Common Divisor) of two numbers.
read a b
m = $a
if [ $b -lt $m ]
then
m = $b
fi
while [ $m -ne 0 ]
do
x = `expr $a % $m`
y = `expr $b % $m`
if [ $x -eq 0 -a $y -eq 0 ]
then
break
fi
m = `expr $m - 1`
done
Output:
gcd of 25 and 15 is 5
9. Write a shell script to perform the task of basic calculator.
Output:
Enter two numbers:
5.6
3.4
Enter Choice:
1. Addition
2. Subtraction
3. Multiplication
4. Division
3
5.6 * 3.4 = 19.0
10. Write a shell script to find the power of a given number.
pow()
{
a=$1
b=$2
c=1
if((b==0));
then
res=1
fi
if((a==0));
then
res=0
fi
if((a >= 1 && b >= 1));
then
while((c <= b))
do
res=$((res * a))
c=$((c + 1))
done
fi
echo "$1 to the power $2 is $res"
}
A=2
B=4
pow $A $B
Output:
24
2 to the power 4 is 16
11. Write a shell script to check whether the number is Armstrong or not.
Output:
Enter the number: 153
153 is armstrong number
Enter the number: 123
123 is not armstrong number