Wednesday, June 11, 2014

Basic Linux commands for DBAs - Part III


Search Related

Command
Description And Example
 grep
 The grep command is used to search for a text in the file or from a list
 
 $ grep "linux" file1.txt           -- Search for lines containing "linux"
 $ grep kalyan/etc/passwd   -- Search for lines containing "kalyan"
 
-i- case insensitive serach
 
$ grep -i "kalyan"  file1.txt     --Search for lines containing "kalyan" and ignore the cas
 
-n displays line number
 
$ grep -i -n "linux" t1.txt     -- Search for lines containing "linux" and ignore the case and displays line number on which line it find the word linux
 
Search string in multiple files
 
$ grep -i "linux" *.txt           -- Search for lines containing "linux" in all *.txt files
 
-c Displays Occurrences counts from
 
$ grep -ic "linux" *.txt          -- Search for lines containing "linux" in all .txt files and displays only occurrences count
 
-v Inverts the search to display lines that doesn't match
 
$ grep -vic "linux" *.txt           -- Displays lines that doesn't has "linux" word on it and displays only occurrences count
 
Regular Expressions with Grep
 
^d        -- Displays lines start with d
 
$ grep "^t" -in t1.txt               -- Displays lines start with T and -i- is used to ignore the case
 
$ grep "^[TL]" -in t1.txt           --Displays lines start with either T or L
 
$ grep "[^0-9]" -in t1.txt           --Displays lines contains atleast one-numeric value, the meaning of ^ caret will change if you                
                                                        enclosed with square braces.
 
$ ps -ef | grep "mysql"            --Displays processes which has mysql in it.
 
$ ls - l | grep "^d"                     --Displays only directories
 
$ grep "x$" t2.txt                      --Displays lines ends with "x"
 
$ grep "5\.." t1.txt                    --Suppose we have data in a file with version numbers like 5.1, 5.x and 5.5 like that, It will search those lines and give you output
 
$ grep "[A-Z][A-Z]"  t1.txt         --Displays lines containing two capital letters (Ex : EA, SA etc.,)
 
 
 find
Find command is used to locate files in the linux / unix server
 
Syntax  : fine where-to-look criteria what-to-do
 
All arguments to find command are optional.
 
$ find                              -- Displays paths of all files in the current directory and sub-directories.
 
$ find . -print                 -- Displays exact same result as above command
 
$ find / -name "test.txt" -- Search for the file test.txt on all the current file system
 
$ find . -name "Kalyan*" -- List filenames starts with Kalyan, If you wan to ignore case then use -i.
 
$ find . -name "*.txt"      -- Lists  only .txt files
 
We can also specify multiple locations as below to search for any particular file
 
$ find /var/lib /usr/tmp $HOME -name "*.txt"           -- File locations must be separated by space.
 
-type option is a parameter to separate the files and directories which can be used along with find command, This can be used only if you want to make your search specific to one particular type
 
$ find . -type f -name "t1.txt"                --Search for a file with name t1.txt in the current location
 
$ find . -type d -name "binaries"          -- Search for a directory name binaries in the current location
 
$ find . -type f -name "*.java"
 
$ find . -type f -perm 0555 -print           -- Displays files which has permissions 555 (read and execute for all )
 
$ find . -type f -perm 0777 -print -exec chmod 644 {} \; -- Search for files which has 777 and changes permissions to 644
 
$ find . -type f -name "t1.txt" -exec rm -f {} \;     -- find and remove t1.txt file in the current folder
 
$ find . -type f -empty            -- Find all empty files
 
$ find . -type d-empty            -- Find all empty directories
 
-user  -- This option helps to find files based on the owner type
 
$ find . -user root       -- Displays files whose owner is root in the current directory
 
$ find . -group bin         -- Displays files whose group is bin in the current directory
 
$ find . -group bin -name "t2.txt"       -- Search files by group
 
-size and -mtime  -- Size option is helps to search files by size and mtime helps to search files by modification time
 
$ find . -size +10M          --Find files bigger than 10 MB size in current directory
 
$ find . -size -10M           --Find files lesser than 10 MB size in current directory
 
$ find . -mtime 1             --Search files and folders that are modified in last 24 hours
 
$ find . -mtime 10           --Search for files and folders that are modified in last 10 days
 
$ find . -mtime 10 -type f    -- Search for only files that are modified in last 10 days
 
$ find . -mtime 10 -type d    -- Search for only directories that are modified in last 10 days
 
Advanced
 
$ find . -type f \( -name "*.txt" -o -name "*.t" \)   -- Search files with multiple extentions
 
Find and Copy
 
find . -type f \( -name "*.txt" -o -name "*.t" \) -exec cp {} txtfiles
 
 find . -type f -name "*.txt" -exec grep -i "linux" {} \;
 

 

Friday, June 6, 2014

Basic Linux commands for DBAs - Part II




Permissions Related

Command
Description And Example
chmod
With help of this we can change the file permissions of a file, which can be assigned permissions to user, group and others
4 - (read), 2 - (write) and 1 - (execute).
chmod 777 filename -- read, write and execute for all
chmod 755 filename --rwx for owner, rw for group and others.
-rwxrw-rw-
First "-" indicates it’s a normal file if first character is "d" then it is a directory, first set or rwx (read, write and execute) belongs to owner, and second set of rwx belongs to group and third set of rwx belongs to others.
chown
Helps to change the owner and group of the file
$ chown filename
$ chown kalyan file1 -- It changes the owner of file to kalyan login
How to change the owner and group
$ chown :[groupname] filename
$ chown kalyan:users file1 -- It changes the owner as "kalyan" and group as "users" for file1
How to change owner to all files in any folder
$ chown -R
$ chown -R Kalyan:Users /usr2/kalyan/jpgs
chgrp
Helps to change the group of the file, The difference between chown and chgrp is, using chown you can change the owner and group of a file, but using chgrp you can change on the group of the file not the owner
$ chgrp filename
$ chgrp bin /usr2/kalyan/myfile -- It changes the group of myfile to bin
$ chgrp -R bin /usr2/kalyan/executables/ - It changes the group to bin for all files in the given path



Disk Space Related



Command
Description And Example
df
The df command stands for "disk filesystem", It displays how much space is occupied and how much space is available on file systems not directories and files
$ df -- Displays file systems usage statistics when you run without any argument
$ df -h -- Displays in human readable format
$ df -h test -- It displays usage information of the filesystem where test resides, it wont give you size of test directory.
-i- -- Displays inode information instead of block usage
$ df -i. -- Displays inode information instead of block usage
Switches
-a -- Shows all file system disk usage
-k -- Displays output in Kilobytes
-i- --Displays inode information
-t --Displays disk space usage for each of the specified type of file system
-T --Displays file system type
du
The du command stands for "disk usage", this command will display usage information of a directory and sub-directory and displays the blocks that are occupied.
Switches
-a -- Write counts for all files, not just directories
-h -- Displays output in human readable format
-c -- To get the total usage in the output
-s -- To get only total size in form of summary
$ du -h -- Displays usage information of the given directory
$ du -sh --Displays only size of the directory (nothing else will be printed)
$ du -ahc --Displays all files human readable format including total size of the folder
$ du -sh --exclude="*.txt" --Exclude the .txt files and provides the size of remaining all files


Thursday, June 5, 2014

Basic Linux Commands for DBAs


General Linux Commands for DBAs
 

Command      
Description
Examples
 pwd
Displays current working directory
$ pwd
/usr/kalyan
 ls
List the current directory contents
$ ls
 ls -l
List the current directory contents with extra details
$ ls -l
 ls
Displays files for the specified path
$ ls
 cd ~ / cd
User home directory
$ cd  / $ cd ~
 cd -
Previous current directory
$ cd -
 clear
Clear screen
$ clear
 touch                                                                
Creates an empty file
$ touch abc
 rm
Removes specified file
$ rm abc
 rm -rf
-f force -r recursive, Removes complete directory with its contents
$ rm -rf
 cp file1 file2
Creates a copy of file1 to file2, if destination is file it creates a duplicate copy, if destination is directory it moves the file into that directory.
$ cp file1 file2
 cp -r dir1 dir2
Creates a duplicate copy of whole directory
$ cp -r dir1 dir2
 mv file2 file3
Renames given file to new file
$ mv file2 file3
 cat
Displays contents of the file
$ cat file1.txt
 more
Displays contents of the file page by page, for next page {Space}  to quit {q}
$ more file1.txt
 head
Displays first 10 lines from the file
$ head file1.txt
$ head -20 file1.txt (first 20 lines)
 tail
Displays last 10 lines from the file
$ tail file1.txt
$ tail -20 file1.txt (last 20 lines)
 tail -f
Displays contents of the file as it grows, starting with last 10 lines, to quit {ctrl+c}
$ tail -f Kalyan2.err
 date
Displays current date and time
$ date
 man
Display help for the given command
$ man cat
 command > filename
Redirects output to specified filename
$ date > date.txt
 command >> filename
Appends the redirected output to existing file
$ date >> date.txt
 sort file1
Sort the contents of the file
$ sort file1
 | (Pipe)
Pipes the output to other command
$ cat file1 | sort
 ps
Displays all current processes
$ ps
 top
Displays all running processes
$ top
 kill
Kills the process Id
$ kill
cal
Displays current months calendar
$ cal
 uptime
Displays current uptime
$ uptime
 w
Displays who is online (All the users who are connected to this host)
$ w
 whoami
Displays the current logged in user name
$ whoami
 finger
Displays information about the user
$ finger kalyan
 whereis
Displays possible locations of the given application
$ whereis mysql
$ whereis perl
 which
Displays which app runs
$ which mysql
$ which perl
 uname
Prints the system information
$ uname
 uname -a
Prints Kernel information
$ uname -a
 uname -o
Displays operating system
-o Operating System
-v Kernel version
-n Node Name
-m Machine
-p Processor
-a All
$ uname -o
 wc
Displays number of newlines, words and bytes in the file
$ ls -lrt | wc - l
Displays count for ls -lrt
 
 set
Displays the name and value of each shell vairable
$ set
 $HOME
To view value in any environment variable
$ HOME
 printenv
Displays all environment variable
$ printenv