Tuesday, 8 July 2014

Linux - Command Usefull for DBA.





Found this useful?
A quick Linux cheat sheet, for those handly little commands you use all the time but sometimes your brain freezes up and a quick reference would be good to have:

File Commands

ls                       -list directory
ls -al                   -formatted listing with hidden files
ls -ltr                  -formatted listing ordered by time
cd dir                   -change to directory
cd -                     -go to directory in previously
cd                       -go to home directory
pwd                      -show current directory path
mkdir dir                -make directory dir
rm file                  -delete file
rm -r dir                -delete directory dir
rm -f file               -force remove file
rm -rf dir               -force remove directory dir
cp file1 file2           -copy file1 to file2
cp -r dir1 dir2          -copy dir1 to dir2; create dir2 if it doesn’t exist
mv file1 file2           -rename/move file1 to file2 if file2 is an existing dir, moves file1
into dir file2
ln -s file link          -create symbolic link link to file
touch file               -create or update file
cat > file               -places standard input into file
more file                -output the contents of file
head file                -output the first 10 lines of file
tail file                -output the last 10 lines of file
tail -f file             -output the contents of file as it grows, starting with the last 10 lines

Process Management

ps                       -display your currently active processes
top                      -display all running processes
kill pid                 -kill process id pid
killall proc             -kill all processes named proc
bg                       -lists stopped or background jobs; resume a stopped job in the background
fg                       -brings the most recent job to foreground
fg n                     -brings job n to the foreground

File Permissions

chmod octal file         -change the permissions of file to octal, which can be found separately for user, group, and world by adding:
● 4 – read (r)
● 2 – write (w)
● 1 – execute (x)
Examples:
chmod 777                -read, write, execute for all
chmod 755                -rwx for owner, rx for group and world For more options, see man chmod.
chown user:group file    -change the owner and group for a file

SSH

ssh user@host            -connect to host as user
ssh -p port user@host    -connect to host on port port as user
ssh-copy-id user@host    -add your key to host for user to enable a keyed or passwordless login

Searching

grep pattern files       -search for pattern in files
grep -r pattern dir      -search recursively for pattern in dir
command | grep pattern   -search for pattern in the output of command
locate file              -find all instances of file
find . file              -find file within current directory
find / file              -find file within whole system

System Info

date                      -show the current date and time
cal                       -show this month’s calendar
uptime                    -show current uptime
w                         -display who is online
whoami                    -who you are logged in as
finger user               -display information about user
uname -a                  -show kernel information
cat /proc/cpuinfo         -cpu information
cat /proc/meminfo         -memory information
man command               -show the manual for command
df                        -show disk usage
du                        -show directory space usage
du -sk | sort -n          -show sum of each directory space usage sorted by number in KB
free                      -show memory and swap usage
whereis app               -show possible locations of app
which app                 -show which app will be run by default

Compression

tar cf file.tar files     – create a tar named file.tar containing files
tar xf file.tar           – extract the files from file.tar
tar czf file.tar.gz files – create a tar with Gzip compression
tar xzf file.tar.gz       – extract a tar using Gzip
tar cjf file.tar.bz2      – create a tar with Bzip2 compression
tar xjf file.tar.bz2      – extract a tar using Bzip2
gzip file                 – compresses file and renames it to file.gz
gzip -d file.gz           – decompresses file.gz back to file

Network

ping host                 – ping host and output results
whois domain              – get whois information for domain
dig domain                – get DNS information for domain
dig -x host               – reverse lookup host
wget file                 – download file
wget -c file              – continue a stopped download

Installation

Install from source:
./configure
make
make install
dpkg -i pkg.deb           – install a package (Debian)
rpm -Uvh pkg.rpm          – install a package (RPM)

Shortcuts

Ctrl+C                    – halts the current command
Ctrl+Z                    – stops the current command, resume with fg in the foreground
or bg in the background
Ctrl+D                    – log out of current session, similar to exit
Ctrl+W                    – erases one word in the current line
Ctrl+U                    – erases the whole line
Ctrl+R                    – type to bring up a recent command
!!                        - repeats the last command
exit                      – log out of current session

Usefull Commands on AIX for Oracle DBA.




Displaying the top 10 CPU-consuming processes              
--> ps aux | head -1; ps aux | sort -rn +2 | head -10

Displaying the top 10 memory-consuming processes
--> ps aux | head -1; ps aux | sort -rn +3 | head

Displaying the top 10 memory-consuming processes using SZ
--> ps -ealf | head -1 ; ps -ealf | sort -rn +9 | head

Displaying the processes in order of being penalized          
--> ps -eakl | head -1 ; ps -eakl | sort -rn +5

Displaying the processes in order of priority              
--> ps -eakl | sort -n +6 | head

Displaying the processes in order of nice value              
--> ps -eakl | sort -n +7

Displaying the processes in order of time              
--> ps vx | head -1 ; ps vx | grep -v PID | sort -rn +3 | head -10

Displaying the processes in order of real memory use          
--> ps vx | head -1 ; ps vx | grep -v PID | sort -rn +6 | head -10

Displaying the processes in order of I/O              
--> ps vx | head -1 ; ps vx | grep -v PID | sort -rn +4 | head -10

Displaying WLM classes ( work load manager)              
--> ps -a -o pid,user,class,pcpu,pmem,args

Determining the PID of wait processes                  
--> ps vg | head -1 ; ps vg | grep -w wait


Wait processes bound to CPUs                      
--> ps -mo THREAD -p 516,774,1032,1290

Determining which processes are using the most real memory      
--> svmon -Pu -t 3|grep -p Pid|grep '^.*[0-9]'

Determining which processes use the most paging space          
--> svmon -gP -t 3 |grep -p Pid|grep '^.*[0-9]'

Displaying memory used by a WLM class                  
--> svmon -W shared

Finding out most utilized segments                  
--> svmon -S

Finding out what files a process or command is using          
--> svmon -pP

Finding out which segments use paging space              
--> svmon -gS
--> svmon -D sid (We can use the -D option to display frame information about each segment.)

10 sFTP Command Examples to Transfer Files on Remote Servers in Linux



10 sFTP Command Examples to Transfer Files on Remote Servers in Linux

File Transfer Protocol (FTP) was widely used protocol to transfer files or data remotely in unencrypted format which is not secure way to communicate. As we all know that File Transfer Protocol is not at all secure because all transmissions happens in clear text and the data can be readable by anyone during sniffing the packets on the network.
10 sftp command examples
So, basically FTP can be used in limited cases or on the networks that you trust. Over the period of time SCP and SSH addresses this security ambiguity and added an encrypted secure layer while transferring data between remote computers.
SFTP (Secure File Transfer Protocol) runs over SSH protocol on standard port 22 by default to establish a secure connection. SFTP has been integrated into many GUI tools (FileZilla, WinSCP, FireFTP etc.).
Security Warnings: Please don’t open SSH port (Secure SHell) globally as this would be a security breaches. You can only open for specific IP from where you are going to transfer or manage files on remote system or vice versa.
  1. 5 Best Practices to Secure and Protect SSH Server
  2. 10 Wget Command Examples in Linux
This article will guide you 10 sftp command examples to use it through interactive command-line interface.
1. How to Connect to SFTP
By default, same SSH protocol is used to authenticate and establish a SFTP connection. To start an SFTP session, enter the username and remote hostname or IP address at the command prompt. Once authentication successful, you will see a shell with an sftp> prompt.
[root@tecmint ~]# sftp tecmint@27.48.137.6

Connecting to 27.48.137.6...
tecmint@27.48.137.6's password:
sftp>
2. Getting Help
Once, you in the sftp prompt, check the available commands by typing ‘?‘ or ‘help‘ at command prompt.
sftp> ?
Available commands:
cd path                       Change remote directory to 'path'
lcd path                      Change local directory to 'path'
chgrp grp path                Change group of file 'path' to 'grp'
chmod mode path               Change permissions of file 'path' to 'mode'
chown own path                Change owner of file 'path' to 'own'
help                          Display this help text
get remote-path [local-path]  Download file
lls [ls-options [path]]       Display local directory listing
ln oldpath newpath            Symlink remote file
lmkdir path                   Create local directory
lpwd                          Print local working directory
ls [path]                     Display remote directory listing
lumask umask                  Set local umask to 'umask'
mkdir path                    Create remote directory
put local-path [remote-path]  Upload file
pwd                           Display remote working directory
exit                          Quit sftp
quit                          Quit sftp
rename oldpath newpath        Rename remote file
rmdir path                    Remove remote directory
rm path                       Delete remote file
symlink oldpath newpath       Symlink remote file
version                       Show SFTP version
!command                      Execute 'command' in local shell
!                             Escape to local shell
?                             Synonym for help
3. Check Present Working Directory
The command ‘lpwd‘ is used to check the Local present working directory, whereas ‘pwd‘ command is used to check Remote working directory.
sftp> lpwd
Local working directory: /
sftp> pwd
Remote working directory: /tecmint/
  1. lpwd – print the current directory on your system
  2. pwd – print the current directory on the ftp server
4. Listing Files
Listing files and directories in local as well as remote system.
On Remote
sftp> ls
On Local
sftp> lls
5. Upload File
Put single or multiple files in remote system.
sftp> put local.profile
Uploading local.profile to /tecmint/local.profile
6. Upload Mutiple Files
Putting multiple files on in remote system.
sftp> mput *.xls
6. Download Files
Getting single or multiple files in local system.
sftp> get SettlementReport_1-10th.xls
Fetching /tecmint/SettlementReport_1-10th.xls to SettlementReport_1-10th.xls
Get multiple files on a local system.
sftp> mget *.xls
Note: As we can see by default with get command download file in local system with same name. We can download remote file with different name specifying the name at the end. (This applies only while downloading single file).
7. Switching Directories
Switching from one directory to another directory in local and remote locations.
On Remote
sftp> cd test
sftp>
On Local
sftp> lcd Documents
8. Create Directories
Creating new directories on local and remote locations.
sftp> mkdir test
sftp> lmkdir Documents
9. Remove Directories
Remove directory or file in remote system.
sftp> rm Report.xls
sftp> rmdir sub1
Note: To remove/delete any directory from remote location, the directory must be empty.
10. Exist sFTP Shell
The ‘!‘ command drop us in local shell from where we can execute Linux commands. Type ‘exit‘ command where we can see sftp> prompt return.
sftp> !

[root@sftp ~]# exit
Shell exited with status 1
sftp>
Conclusion
The SFTP is a very useful tool for administrating servers and transferring files to and from (Local and Remote). We hope this tuts will help you to understand the usage of SFTP in some extent.