Linux/Unix Commands - STUDY NOTES

Post Top Ad

Linux/Unix Commands


File Management becomes easy if we know the right commands.

Listing files (ls)

To see the list of files on our UNIX or Linux system, use the 'ls'command.

It shows the files /directories in current directory.

 

Note:
  • Directories are denoted in blue color.
  • Files are denoted in white.
  • You will find similar color schemes in different flavors of Linux.
we can use 'ls-R' to show all the files not only in directories but also subdirectories




 

NOTE: The command is case-sensitive. If you enter, "ls - r" you will get an error.

'ls -al' gives detailed information of the files. The command provides information in a columnar format. The columns provide the following information:


1st Column
File type and access permissions
2nd Column
# of HardLinks to the File
3rd Column
Owner and the creator of the file
4th Column
Group of the owner
5th Column
File size in Bytes
6th Column
Date and Time
7th Column
Directory or File name


Listing Hidden Files

Hidden items in Linux begin with – .”period” symbol at the start, of the file or directory.

Any Directory/file starting with a '.' will not be seen unless we request for it.  To view hidden files, use the command

ls  - a



Creating & Viewing Files

The 'cat' command is used to display text files. It can also be used for copying, combining and creating new text files.  Let's see how it works

To create a new file, use the command

1.      cat > filename
2.      Add content
3.      Press 'ctrl + d' to return to command prompt.



To view a file, use the command -

cat

 

The syntax to comcbine 2 files is -

cat file1 file2 > newfilename

Let's combine sample 1 and sample 2.



As soon as we insert this command and hit enter, the files are concatenated, but it doesn’t show any result. This is because Bash Shell (Terminal) is silent type.  It will never give a confirmation message like "OK" or "Command Successfully Executed". It will only show a message when something goes wrong or when an error has occurred.


In order to view the new combo file "sample" use the command

cat sample

Note: Only text files can be displayed and combined using this command.


Deleting Files

The 'rm' command removes files from the system without confirmation.  To delete a file use syntax –

rm
 
Moving and Re-naming files
In order to move a file, use the command

mv

Suppose we want to move the file "sample2" to location /home/user/Documents. Executing the command

mv sample2  /home/user/Documents

it may show 

mv: cannot move ‘sample2’ to ‘/home/user/Document’: Permission denied

mv command needs super user permission. Currently, we are executing the command as a standard user. Hence we get the above error. To overcome the error use command

sudo

Sudo program allows regular users to run programs with the security privileges of the superuser or root.

sudo sample2  /home/user/Documents

For renaming file:

mv filename newfilename 


WC Command

The wc (word count) command in Unix/Linux operating systems is used to find out number of newline countword countbyte and characters count in a files specified by the file arguments. The syntax of wc command as shown below. 

# wc [options] filenames 

The following are the options and usage provided by the command. 

wc -l : Prints the number of lines in a file.
wc -w : prints the number of words in a file.
wc -c : Displays the count of bytes in a file.
wc -m : prints the count of characters from a file.
wc -L : prints only the length of the longest line in a file.

A Basic Example of WC Command

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

[root@study_notes ~]# wc sample1.txt 
12 16 112 sample1.txt 


Count Number of Lines

To count number of newlines in a file use the option ‘-l‘, which prints the number of lines from a given file. 
[root@study_notes ~]# wc -l sample1.txt
12 sample1.txt




DD COMMAND

The dd command stands for “data duplicator” and used for copying and converting data. It is very powerful low level utility of Linux which can do much more like;

• Backup and restore the entire hard disk or partition.

• Backup of MBR (Master Boot Record)

• It can copy and convert magnetic tape format, convert between ASCII and EBCDIC formats, swap bytes and can also convert lower case to upper case.

• It can also be used by Linux kernel make files to make boot images.

Only super user can run this command because you can face a big data loss due to its improper usage, so you should be very careful while working with this utility. At that moment data loss can convert the dd utility as a “data destroyer” for you. That’s why it is recommended that beginners should not use this command on a production machine until they get familiarity on this. You must make sure that target location must have sufficient space while running this command.


SYNTAX OF DD COMMAND.

dd if=<source file name> of=<target file name> [Options]

We normally do not explain about syntax but this command syntax require some explanation. The syntax is totally different when compared to many Linux commands we know. In this syntax dd is followed by two things

if=<source> –This is a source from where you want to copy data and ‘if’ stands for input-file.

of=<destination> –This is a source from where you want to write/paste data and ‘of’ stands for output-file.

[options] –These options include, how fast data should be written, what format etc.

example.

 dd if=/dev/sda of=/dev/sdb



Mathematical Command

Arithmetic operations are the most common in any kind of programming language. Linux operating system provides the bc command and expr command for doing arithmetic calculations. 

bc command

The bc command evaluates expressions similar to the c programming language. The bc command supports the following features. 

Arithmetic operators

• Increment and decrement operators

Assignment operators

Comparision or Relational Operators

Logical or Boolean operators

Math Functions

Conditional statements

Iterative statements

Functions


Arithmetic operator Examples:

1. Finding Sum of Two expressions

> echo "2+5" | bc
7

2. Difference of Two numbers

> echo "10-4" | bc
6

3. Multiplying two numbers

> echo "3*8" | bc
24

4. Dividing two numbers 

When you divide two numbers, the bc command Ignores the decimal part and returns only the integral part as the output. See the below examples

> echo "2/3" | bc
0

> echo "5/4" | bc
1

Use the scale function to specify the number of decimal digits that the bc command should return.

> echo "scale=2;2/3" | bc
.66

5. Finding the remainder using modulus operator

> echo "6%4" | bc
2

6. Using exponent operator

> echo "10^2" | bc
100

Here the expression is evaluated as 10 to the power of 2.


Assignment Operator Examples:

Assigns 10 to the variable and prints the value on the terminal.

> echo "var=10;var" | bc

Increment the value of the variable by 5

> echo "var=10; var+=5;var | bc

15

Increment Operator Examples: 

++var : Pre increment operator. The variable is incremented first and then the result of the variable is used.

var++ : Post increment operator. The result of the variable is used first and then the variable is incremented.


> echo "var=5;++var" | bc
6

> echo "var=5;var++" | bc
5


Decrement Operator Examples

Similar to the increment operators, there are two types of decrement operators.

•  --var : Pre decrement operator. The variable is decremented first and then the result of the variable is used.

  var-- : Post decrement operator. The result of the variable is used first and then the variable is decremented.


> echo "var=5;--var"| bc
4

> echo "var=5;var--"| bc
5


Relational Operators Examples: 

• expr1 < expr2 : Result is 1 if expr1 is strictly less than expr2.

expr1 <= expr2 : Result is 1 if expr1 is less than or equal to expr2.

expr1 > expr2 : Result is 1 if expr1 is strictly greater than expr2.

expr1 >= expr2 : Result is 1 if expr1 is greater than or equal to expr2.

expr1 == expr2 : Result is 1 if expr1 is equal to expr2.

expr1 != expr2 : Result is 1 if expr1 is not equal to expr2.


> echo "10 > 5" | bc
1

> echo "1 == 2" | bc
0


Logical Operator Examples:

Logical operators are also mostly used in conditional statements. The result of the logical operators is either 1 (True) or 0 (false) ! expr : Result is 1 if expr is 0. 

• expr && expr : Result is 1 if both expressions are non-zero.

expr || expr : Result is 1 if either expression is non-zero.


> echo "4 && 10" | bc
1

> echo "0 || 0" | bc
0

Conditional Statement Examples: 

Conditional statements are used to take decisions and execute statements based on these decisions. Bc command supports the if condition. The syntax of if statement is

if(condition) {statements} else {statements}

The following example shows show to use the if condition

> echo 'if(1 == 2) print "true" else print "false"' | bc
False


Important Points:

Bc command treats the semicolon (;) or newline as the statement separator.

To group statements use the curly braces. Use with functions, if statement, for and while loops.

If only an expression is specified as a statement, then bc command evaluates the expression and prints the result on the standard output.

If an assignment operator is found. Bc command assigns the value to the variable and do not print the value on the terminal.

A function should be defined before calling it. Always the function definition should appear first before the calling statements.

If a standalone variable is found as a statement, bc command prints the value of the variable. You can also Use the print statement for displaying the list of values on the terminal.



Expr Command

See how to use the expr command in unix or linux system for doing arithmetic operations.

The syntax of expr command is

expr [expression]

Note: You have to provide the space between the values and the operands. Otherwise the expr command may throw error or print them as a string.


Arithmetic Operator Examples:

1. Sum of numbers


$ expr 5 + 3
8

$ expr 1 + 2 + 3
6

$ expr 5+3
5+3


Here in the third expr command, space is not provided between the literals. The expr command treated it as a string and printed on the terminal.

2. Difference between two numbers


$ expr 10 - 6
4


3. Multiplying numbers


$ expr 7 \* 9
63


Here the * is shell builtin operator, that is why it needs to escaped with backslash.


4. Dividing numbers


$ expr 6 / 4
1


The division operator returns only the arithmetic quotient.


5. Remainder or modulus


$ expr 6 % 4
2


Comparison or Relational Operator Examples:

You can use the following comparison operators with the expr command: 

• Val1 < Val2 : Returns 1 if val1 is less than val2. otherwise zero.

Val1 <= Val2 : Returns 1 if val1 is less than or equal to val2. otherwise zero.

Val1 > Val2 : Returns 1 if val1 is greater than val2. otherwise zero.

Val1 >= Val2 : Returns 1 if val1 is greater than or equal to val2. otherwise zero.

Val1 = Val2 : Returns 1 if val1 is equal to val2. otherwise zero.

Val1 != Val2 : Returns 1 if val1 is equal to val2. otherwise zero.

val1 | val2 : Returns val1 if val1 is neither null nor zero. Otherwise val2.

val1 & val2 : Returns val1 if both val1 and val2 is neither null nor zero. Otherwise 0.


Note: You have to escape most of the operators with backslash as they are shell built in.


$ expr 1 \< 2
1

$ expr 1 \<= 1
1

$ expr 2 \> 5
0

$ expr 2 \>= 5
0

$ expr 7 = 7
1

$ expr 9 != 1
1

$ expr 2 \| 5
2

$ expr 0 \| 5
5

$ expr 2 \& 5
2

$ expr 6 \& 3
6

$ expr 6 \& 0
0

$ expr 0 \& 3
0


String Function Examples:


1. Length of string


The length function is used to find the number of characters in a string.


$ expr length linux
5

$expr length linux\ system
12

$expr length "linux system"


If you have spaces in your string escape them with backslash or quote them with double quotes.


2. Find Substring

You can extract a portion of the string by using the substr function. The syntax of substr function is


substr string position length


Here position is the character position in the string. length is the number of character to extract from the main string. An example is shown below:


$ expr substr unixserver 5 6
server


3. Index of the substring

You can find the position of a string in the main string using the index function. The syntax of index function is shown below:

index string chars

If the chars string is found in the main string, then the index function returns the position of the chars. Otherwise it returns 0. See the following examples:

$ expr index linux nux
3
$expr index linux win
0

4. Matching a regexp

The match function is used to find anchored pattern match of regexp in the string. The syntax of match function is shown below:


match string pattern

The match function returns the number of characters in the pattern is a match is found. Otherwise, it returns 0. Alternative syntax is


string : pattern

The following examples shows how to use the match function:


$ expr match linuxserver lin
3

$ expr match linuxserver server
0

Here in the second expr, the pattern (server) exists in the main string. However, the pattern does not start from the beginning of the main string. That’s why the match function returns 0.



Directory Manipulations


Creating Directories

Directories can be created on a Linux operating system using the following command

mkdir

This command will create a subdirectory in our present working directory, which is usually our "Home Directory".

For example,

mkdir mydirectory


If we want to create a directory in a different location other than 'Home directory', we could use the following command -


mkdir

For example:

mkdir /tmp/MUSIC

will create a directory 'Music' under '/tmp' directory.

Removing Directories

In order to remove a directory, use the command -




rmdir

Example

rmdir mydirectory

will delete the directory mydirectory.





Tip: Ensure that there is no file / sub-directory under the directory that we want to delete. Delete the files/sub-directory first before deleting the parent directory.

 



Renaming Directory

The 'mv' (move) command (covered earlier) can also be used for renaming directories. Use the below given format:

mv directoryname newdirectoryname


Other Important Commands

The 'Man' command

Man stands for manual. It is similar to HELP file found in popular softwares.

To get help on any command that we do not understand, we can type

man

The terminal would open the manual page for that command.


The History Command

History command shows all the commands that have used in the past for the current terminal session. This can help us to refer to the old commands we have entered and re-use them in our operations again.


The clear command

This command clears all the clutter on the terminal and gives a clean window to work on, just like when we launch the terminal.


Common commands:


cat: Copies a file to the standard output

cd: Changes the current directory

chmod: Changes file permissions

chown: Changes file ownerships

cp: Copies files

dd: Copies blocks of data

df: Reports disk space usage by device and available space

diff: Compares two text files

• du: Reports disk space usage by directory

file: Displays the type of data in a file

find: Finds files based on specified criteria

grep: Searches for text in a file

ln: Links a filename to an alias name

ls: Displays the contents of a directory

mkdir: Creates a directory

more: Displays a text file, one page at a time

mount: Mounts a file system

mv: Renames or moves a file

pwd: Displays the current directory

rm: Deletes files

rmdir: Deletes directories

sort: Sorts lines in a text file

split: Splits a file into smaller parts

umount: Unmounts a file system

wc: Counts the words and lines in a file




https://books2notes.blogspot.in/p/file-system.html
 

No comments:

Post a Comment

Post Bottom Ad