We can create multiple files without wasting disk space(zero size files) at once.
Deleting files:
1]$rm:-- To delete a file.
Syn:$rm filename
Ex: $rm sample
-- It deletes the file permanently
2]$rm -i filename
Ex: $rm –i sample
rm: remove regular file `sample`? y
Multiple files deletion:
3]Syn: $rm f1 f2……
Ex:$rm s1 s2 s3
--It deletes files without asking for any conformation
$rm a1 a2 a3
Working with directories:
pwd: To display the present working directory.
Syn:$pwd
1]To create a new directory.
Syn:$mkdir dirname
Ex:$mkdir abc
2] cd:--To change a directory.
Syn:$cd dirname
Ex: $cd abc
3]$cd ..
This is used to come out of current directory
cd / : It will switch to the root directory
cd ~ : To switch to your home directory.
cd - : Switches from current path to previous path.
Creating multiple directories:
Syn:$mkdir dir1 dir2…….
Ex:$mkdir d1 d2 d3 d4
Removing directory:
=> $rmdir:– This command is used to remove a directory, directory must be empty and should not contain any files & sub directories.
Syn:$rmdir dirname
Ex:$rmdir abc
=>$rm –r dirname
--It deletes recursively entire directory structure.
Ex:$rm -r abc
$rm –ri abc[It asks for the confirmation before deleting the file]
=>$rm –rf dirname
rm -rf filename
It delete file with a conformation message yes or no. Deletes the file if you select yes option and no for not deleting the file.
To come out of current working directory.
f = force delete
r = recursive delete
It deletes file at any cost even if the user doesn't have read/write permissions.
Coping a file:
cp :-To copy a file.
Syn:$cp sourcefile[must be existed file] targetfile[existed or new file]
Ex:$cp sample sample1
Coping multiple files into a directory:
$cp file1 file2………direct name
Ex: $cp emp sample a1 abc [last file must be a directory]
Copying a directory:
Syn: $cp –R sourcedir targetdir
EX: $cp –R abc xyz
$mv:- To rename a file [or] to move a file.
Syn:$mv oldfile newfile
Ex:$mv a1 samle
Creating hiden files:
$cat>.sample
----------
----------
Ctrl+d
$mv emp .emp : to hide a file
$mv .emp emp : to unhide a file
$mkdir .abc : to create a hidden directory
Viewing list of files:
1]$ ls -> It lists all files & directories in the current directory.
2]$ ls -a -> It lists all files & directories along with hidden files.
3]$ls -r -> It lists all files & directories in reverse order/descending order.
4]$ls -R -> It lists recursively all files & sub directories.
5]ls -t -> It lists all files & sub directories based on date & time of creation.
6]$ls | more -> It lists all files & sub directories page by page.
7]$ls -F -> It lists all files & sub directories along with file type.
File name type colour
abc ordinary black
abc/ dir dark blue dark blue
emp@ link sky blue
emp* exe green
Zip red
8]$ls –l -> It lists all files & sub directories in long listed format i.e with 9 fields.
1.File type
2.File permission
3.No.of links
4.Owner name
5.Group name
6.File size in bytes
7.Date
8.Time
9.File name
1]file type:
- Regular file
d directory file
l link file
b block Special file
c character Special file
Viewing long files:
$more : It displays the given file contents page by page
Syn:$more filename
Ex:$more sample
------------
------------ page1
------------
:
-----------
------------ page2
-----------
:
-----------
------------ page3
-----------
:q –quick termination
$less : It displays the given file contents page by page like more but it allows backward movement as well as forward movement in the file.
Syn:$less filename
Ex:$less sample
------------
------------ page1
------------
:
-----------
------------ page2
-----------
:
-----------
------------ page3
-----------
:q –quick termination
$head : It displays the first n lines from the file.
Syn:$head –n filename
Ex:$head -10 sample
$tail : It displays the last n lines from the file.
Syn:$tail –n filename
Ex:$tail -10 sample
$tail -f : It displays updated lines from the file.
Syn:$tail -f filename
Ex:$tail -f sample
---------- <lines that are getting updated>
-----------
ctl+c
$wc : It counts no.of lines words and characters.
Syn:$wc filename
Ex:$wc sample
output: 1065 7588 49412 sample [ Line count, Word Count, Character Count ]
$wc –l filename [displays line count]
$wc –w filename [displays word count]
$wc –c filename [displays character count]
$wc –lw filename [displays lines&words count]
$wc –lc filename [displays lines&characters count]
$wc –wc filename [displays words&characters count]
Comparing files:
1]$cmp: It compares two files character by character.
Syn:$cmp file1 file2
If the files are Same -- doesn’t return any value.
If the files are Diff -- returns char position & line number.
2]$comm: It displays comman lines b/w 2 files.
Syn:$comm file1 file2
3]$diff: It displays diff lines b/w 2 files.
Syn:$diff file1 file2
Zip files:
1]$gzip: This command is used to zip the file. The output file extension would be .gz
Syn:$gzip filename
Ex:$gzip sample
Output: sample.gz (File extension is .gz)
2]$zcat: This is used for to open zip file in readable format.
$zcat sample.gz
3]$gunzip: To unzip the file which is .gz format.
Syn:$gunzip filename
Ex:$gunzip sample.gz
Sample
4] $zip: This command is also used to zip a file but the file format would be .zip
Syn:$zip options filename.zip filename .....
Ex: $zip sample.zip sample
Output: sample.zip (File extension is .zip)
Ex: $zip -r sample sample1 sample2 sample2
Output file: sample.zip ( sample.zip contains sample1, sample2 and sample3)
5] $unzip: This is command is used to unzip a file which is in .zip format
Syn:$unzip filename
Ex:$unzip sample.zip
Output: sample
Administrator Commands:
Login: root
Password:*****
1 1]useradd – To create a new user
#useradd username
Ex: #useradd pavan ( pavan user is created, set the password for user using passwd command)
2]passwd- To create a password
#passwd username
#passwd pavan
Enter new password : pavan123
Retype password : pavan123
3]userdel- to delete a user
#userdel username
Ex: #userdel pavan
4]groupadd- to create a new group
#groupadd groupname
Ex: #groupadd weblogic
5]groupdel- to delete a new group
#groupdel groupname
6]Add an existing user to a Group
Syn: usermod -a -G groupname username
Ex: usermod -a -G weblogic pavan ( pavan will be added to weblogic group)
7] To view a users groups
Syn: id username
Ex: id pavan
uid=505(pavan) gid=506(pavan) groups=506(pavan),505(weblogic)
8] Change user primary Group
Syn: usermod -g groupname username
Ex: usermod -g weblogic pavan ( pavan's primary group will be changed as weblogic)
id pavan
uid=505(pavan) gid=505(weblogic) groups=505(weblogic)
9] To view user group names
Syn: groups username
Ex: groups pavan
Output: pavan : weblogic jboss ( pavan user groups are : weblogc and jboss)
10] To view all the groups in the system
Syn: groups
Ex: groups
Output: weblogic jboss
11] Add a new user and assign to the group at once
Syn: useradd -g groupname username
Ex: useradd -g jboss kalyan
#init 0 : will shutdown the system
#init 6 : will restart the system
Wild card characters [or]meta characters
( * ? - [] . )
1] * it matches zero or more characters
2] ? it matches any single character
3] – it matches any single character in the given list
4] [] it matches any single character in the given range
5] . it matches any single character except the line character
Examples:
1] ls t* –it lists the file starting with ‘t’
2] ls *s- ending with ‘s’.
3] ls b*k- starting with’b’ ending with ‘k’.
4] ls a? –[?any single character.]
[exactly 2 character . first letter’a’ second letter may be any.]
5]ls bhms?? -it list the file starting with b[or]h[or]m[or]s followed by any of characters [only 2 characters]
6]ls [abcdefgh]
7]ls [b-k] [p-t] [d-m]*
8]rm * –delete all files in the current directory.
9]rm t* - delete all files starting with ‘t’.
10]cp abc/a1/a11 xyz/x1
Filter commands
grep[globally search a regular expression and print]
*It is used for to search a string regular expression in a given file or files.
Syn: grep pattern filename
Ex:
1] grep pavan sample – It will search pavan string in a sample file
2] grep hello sample – It will search hello string in a sample file
3] grep pavan a1 a2 a3 – It will search pavan string in a1,a2,a3 files
a1:------
a2:------
a3:-----
It is used to search the string in multiple files.
4]grep pavan * ->searches the string in all files which are under current dir.
a1:------
a3:--------
a3:--------
emp:-------
sample:------
grep command options:
1 1)grep -i pavan sample -- ignore case sensitive
2 ----
5 ----
7 ----
2 2)grep –c pavan sample -- Prints count of the lines
3.
3)grep –n pavan sample -- Prints lines along with line numbers
2:--------
5:-------
7:--------
3 4)grep -l pavan * -- lists only file names
a1
a3
emp
sample
5)grep-v “pavan soft” sample –-- It prints the lines that are not matching
Regular Expression:
Any String contains wild card characters known as regular expression or pattern
The patterns are classified into 3 types.
1 1) Character pattern
2 2) Word pattern
3 3) Line pattern
The default pattern is character pattern
Examples:
1] grep “pav*” sample
2] grep “b[aeiou]ll” sample
3] grep “b..d” sample
4] grep “pavan” sample
Word pattern: \< \>
\< -- starting of the word
\> -- End of the word
5] grep “\<pavan\> sample – Searches the exact word pavan
6] grep “\<pavan” sample – Searches word starting with pavan
7] grep “pavan\>” sample – searches word ending with pavan
8] grep “\<[0-9][0-9][0-9][0-9]\>” sample
Line Pattern:
^ - Start of the line
$ - End of the line
1] grep “^d” sample – Searches line starting with d
2] grep “^the” sample – Searches the starting with the
3] grep “^\<the\>” sample – Line contains only the
4] grep “t$” sample --- line ending with t
5] grep “[0-9]$” sample – Line ending with digit
6] grep “^[bkt]” sample – Line starting with b or k or t
7] grep “^unix$” sample – Line should contain only “unix”
8] grep “^.” Sample – It will display line starting with any character
9] grep “\$$” sample – Line ending with ‘$’
10] grep “^$” sample – Blank lines
11] grep –v “^$” sample – Prints all non empty lines
grep –v “^$” sample >temp
mv temp sample
fgrep: [faster grep]
It is used for to search multiple strings. But it doesn’t allow to search regular expressions ( wild card search */?/./[]/- is not allowed )
Ex: fgrep “pavan
>unix
>linux “ sample
It searches either pavan or unix or linux
egrep:[ Extended grep]
It is a combination of grep and fgrep
Ex: egrep “pavan
>unix
>linux “ Sample
egrep “^d” sample
sed: It is used to search and replace the string
syn: sed “s/oldsting/newstring/g” filename
ex:
sed “s/unix/linux/g” sample
sed “s/UNIX/Linux/gi” sample
sed “s/^unix/linux/gi” sample
sed “s/^$/hellounix/g” sample
sed “s/unix//gi” sample
tr: To translate a character or to replace a character
>-redirecting o|p
<-redirecting i|p
$tr “a” “t” <sample
-where ever ’a’ is there it will replace with ‘t’
$tr “aciou” “AEIOU” <sample.
$tr “[a-z]” “ [A-Z]” <sample
-the hole file translated into upper case
-$tr “[a-z A-Z]” “[A-Z a-z]” <sample
- where ever lower case is there that will be translated into uppercase
-where ever uppercase is there that will be translated into lowercase
$tr “[a-z]” “[A-Z]” <sample >temp
$mv temp sample.
Flat file-normal text file
Delimiter – filed separator.
Default delimiter – tab key.
In a file data enter with delimiter known as flat file the default delimiter is tab key delimiter filed separate.
Enter key means record separate
$cat>stud
101 Priya 515 unix Hyd
102 pavan 444 linux sec
Ctrd.
Custom delimiter flat file
$cat >emp
101,priya,5000,10
102,pavan,6000,12 Ctrld.
Fixed width flat file
In a file every record has the same length.
$cat > account
1111nnnnnntttttttbbbbbb
1-5 –Ac no
6-11 –Ac name
12-15 -acc type
16-20 –Balance
Cut command:
It is used for to retrieve required fields (or) character from a given file.
Syn: $cut –f 2, 4 stud.
$cut –f 2-5 stud.
$cut –c 1-10 stud.
$cut –c 6-11, 16-20 account
$ cut –d”,” –f 2, 4 emp.
Paste: It is used for to join two or more files horizontally by using delimiter.
Cat>states
AP
TN
KR
Cat>cities
HYD
CHEN
BAN
Syn: $paste states cities
AP HYD
TN CHEN
KR BAN
$ paste –d”,” states cities
Sort: It is used for to sort file contents.
By default it sorts the file contents based on ascii value
- The default mode is ascending order
$ sort filename
$ sort sample
a
de
ha
he
te
(1)$ sort –r sample - It displays the file in reverse order
(2)$ sort –u sample - displays unique lines ( won't display duplicate lines )
(3)$ sort num
file - num
130
150
1
230
24
25
26
27
38
678
output:
1
100
130
150
230
24
25
26
27
38
678
(4)$ sort –n num [ numeric comparsion ]
1
24
25
26
27
38
100
130
150
230
678
5] sort –nu num [ numeric comparison ignore duplicates ]
15
23
125
225
456
1025
Uniq:
It displays unique lines in the given file but the file contents should be in sorted order
Syn: uniq filename
Ex:
1] uniq sample
o/p
aaa
bbb
ddd
hhh
ppp
2] uniq –u sample
It displays non duplicated lines
o/p
bbb
ppp
3] uniq –d sample
It displays only duplicated lines
o/p
aaa
ddd
hhh
4] How to delete duplicate lines from a file.
Note : File should be in sorted order first
uniq –u sample>temp
mv temp sample
Piping:
Pipe(|): It is used to combine two or more commands . The output or result of the first command sequence is used as the input to the second command sequence.
who | wc –l -- It counts no of users working in the server
1] Count total no of files in the current directory.
ls | wc –l
2] Display all sub directories in the current directory
ls -l | grep “^d”
3] Count total no of sub directories in the current directory
ls –l | grep “^d” |wc –l
3] Display the lines from 11-30 from sample file
head -30 sample | tail -20
4] Display unix students names & phone no’s in ascending order of name
grep unix stud | cut –f 2,3 | sort
Links
tee:
It is used for to write data to the screen and to the file
grep unix stud | cut –f 2,3 | sort | tee f1
File Permissions:
By Default below permissions will be assigned to the file or directory when you login with the user and create a file
Default file permissions rw-|rw-|r-- [ 664 ]
Default Directory Permisions rwx|rwx|r-x [ 775 ]
u g o
chmod: It is used for to change file permissions
Syn: chmod who/[+1-1=]/ permission file name.
Who:
User (or) owner - u
Others - o
Group - g
All - a
Permission
+ add permission
- deny permission
= assign permission
ls -l sample
rw-rw-r--
(1)$ chmod g+x sample
rw-rwx-r--
(2)$ chmod u+x ,g-w sample
rwx-r-x- -
(3)$ chmod g=w sample
rwx-w-r--
It will assign write permission only and removes other permission
octal code:
read-4
write-2
execute-1
$ chmod 756 sample
u g o
rwx-r-x-rw
$ chmod 643 sample
rw-r---wx
$ chmod 755 sample
rwx-r-x-r-x
chown: To change user/owner of the file
chown oracle sample
-rw-r--r-- 1 oracle root 0 Feb 1 02:37 sample
chgrp: To change the group of the file
-rw-r--r--. 1 oracle oracle 0 Feb 1 02:37 sample
Umask: To change default file permissions
The default umask is ‘002’
umask:
o/p 002
Default file permissions RF-> rw-rw-r—(644)
Default directory permission DF-> rwxrwxr-x(775)
umask 026
touch a1 a2 a3 (640)
mkdir d1 d2 d3 (751)
umask 042
touch x1 x2 x3 (624)
mkdir y1 y2 y3 (735)