{"id":1884,"date":"2013-02-20T16:46:58","date_gmt":"2013-02-20T08:46:58","guid":{"rendered":"http:\/\/rmohan.com\/?p=1884"},"modified":"2013-02-20T17:43:34","modified_gmt":"2013-02-20T09:43:34","slug":"linux-commands-for-reference","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=1884","title":{"rendered":"Linux commands for Reference"},"content":{"rendered":"<p>find . -type f -exec grep &#8216;NMX_FXNG_AND_CONTRACT_DBF&#8217; {} \\;<\/p>\n<p>history<\/p>\n<p>\u00a0history|awk &#8216;{print $2}&#8217;|awk &#8216;BEGIN {FS=&#8221;|&#8221;} {print $1}&#8217;|sort|uniq -c|sort -r<\/p>\n<p>\u00a0history|tail -1000|awk &#8216;{print $2}&#8217;|awk &#8216;BEGIN {FS=&#8221;|&#8221;} {print $1}&#8217;|sort|uniq -c|sort -r<\/p>\n<p>-daystart\u00a0\u00a0 \u00a0This flag starts at the beginning of the day.<br \/>-atime\u00a0\u00a0 \u00a0The time the file was last accessed &#8212; in number of days.<br \/>-ctime\u00a0\u00a0 \u00a0The time the file&#8217;s status last changed &#8212; in number of days.<br \/>-mtime\u00a0\u00a0 \u00a0The time the file was last modified &#8212; in number of days.<br \/>-amin\u00a0\u00a0 \u00a0The time the file was last accessed &#8212; in number of minutes. (It is not available on all implementations.)<br \/>-cmin\u00a0\u00a0 \u00a0The time the file&#8217;s status last changed &#8212; in number of minutes. (It is not available on all implementations.)<br \/>-mmin\u00a0\u00a0 \u00a0The time the file was last modified &#8212; in number of minutes. (It is not available on all implementations.)<br \/>-type\u00a0\u00a0 \u00a0This flag describes the type of file, such as d for directories.<br \/>-userX\u00a0\u00a0 \u00a0Files belonging to user X.<br \/>-groupX\u00a0\u00a0 \u00a0Files belonging to group X.<br \/>-newerX\u00a0\u00a0 \u00a0Files that are newer than file X.<\/p>\n<p>find \/home\/$1\/mail\/*\/mail\/.spam\/cur -type f -mtime +7 -exec rm {} \\;<br \/>find \/home\/$1\/mail\/*\/mail\/.spam\/new -type f -mtime +7 -exec rm {} \\;<br \/>\u00a0<\/p>\n<p>Here&#8217;s how to list all the files in your home directory tree that were modified exactly one hour ago:$ find ~ -mmin 60 \\! -type d<\/p>\n<p>Giving a negative value for a flag means to match that number or sooner. For example, here&#8217;s how to list all the files in your <br \/>home directory tree that were modified exactly one hour ago or any time since<\/p>\n<p>find ~ -mmin -60 \\! -type d<\/p>\n<p>$ date<br \/>Mon Oct 23 09:42:42 EDT 2006<br \/>$ touch -t 10230842 temp<br \/>$ ls -l temp<br \/>-rw-r&#8211;r&#8211;\u00a0\u00a0\u00a0 1 joe\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 joe\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0 Oct 23 08:42 temp<br \/>$ find ~ -newer temp \\! -type d<\/p>\n<p>find \/ -user `whoami` -daystart -atime -1 \\! -type d<\/p>\n<p>Give different values for the various time flags to change the search times. You can also combine flags. For instance, <br \/>you can list all the files in your home directory tree that were both accessed and modified between now and seven days ago:<br \/>find ~ -daystart -atime -7 -mtime -7\u00a0 \\! -type d<\/p>\n<p>find mtime<\/p>\n<p>find . -mtime 0 # find files modified between now and 1 day ago<br \/># (i.e., within the past 24 hours)<br \/>find . -mtime -1 # find files modified less than 1 day ago<br \/># (i.e., within the past 24 hours, as before)<br \/>find . -mtime 1 # find files modified between 24 and 48 hours ago<br \/>find . -mtime +1 # find files modified more than 48 hours ago<\/p>\n<p>find . -mmin +5 -mmin -10 # find files modified between<br \/># 6 and 9 minutes ago<\/p>\n<p>* find top 10 largest files in \/var:<\/p>\n<p>$ find \/var -type f -ls | sort -k 7 -r -n | head -10<\/p>\n<p>* find all files having size more than 5 GB in \/var\/log\/:<\/p>\n<p>$ find \/var\/log\/ -type f -size +5120M -exec ls -lh {} \\;<\/p>\n<p>* find all today\u2019s files and copy them to another directory:<\/p>\n<p>$ find \/home\/me\/files -ctime 0\u00a0 -print -exec cp {} \/mnt\/backup\/{} \\;<\/p>\n<p>* find all temp files older than a week and delete:<\/p>\n<p>$ find \/temp\/ -mtime +7-type f | xargs \/bin\/rm -f<\/p>\n<p>* find and rename all mp3 files by changing their uppercase names to lowercase:<\/p>\n<p>$ find \/home\/me\/music\/ -type f -name *.mp3 -exec rename &#8216;y\/[A-Z]\/[a-z]\/&#8217; &#8216;{}&#8217; \\;<\/p>\n<p>Grep<\/p>\n<p>some examples of grep command:<\/p>\n<p>* Print Apache\u2019s documentroot directory name:<\/p>\n<p>$ grep -i documentroot\u00a0 \/etc\/httpd\/conf\/httpd.conf<\/p>\n<p>* View file contents without comments and empty lines:<\/p>\n<p>$ grep -Ev \u201c^$|^#\u201d \/etc\/my.cnf<\/p>\n<p>* print only IP address assigned to the interface:<\/p>\n<p>$ ifconfig eth0 | grep &#8216;inet addr:&#8217; | cut -d&#8217;:&#8217; -f2 | awk &#8216;{ print $1}&#8217;<\/p>\n<p>* How many email messages sent for a particular date:<\/p>\n<p>$ cat \/var\/log\/maillog | grep &#8220;status=sent&#8221; | grep &#8220;May 25&#8221; | wc -l<\/p>\n<p>* Find out a running process\/daemon from process list (thanks to staranneph for recalling this):<\/p>\n<p>ps -ef | grep mysql<\/p>\n<p>* You can also note cpu\/mem usage by using above. like in below command output, you can see that Plesk\u2019s statistics process is utilizing more than 18% cpu alone:<\/p>\n<p>[root@myserver ~]# ps aux | grep statistics<br \/>root\u00a0\u00a0\u00a0\u00a0\u00a0 8183 18.4\u00a0 0.0\u00a0 58384\u00a0 2848 ?\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 D\u00a0\u00a0\u00a0 04:05\u00a0\u00a0 3:00 \/usr\/l<\/p>\n<p>source<\/p>\n<p>$ touch -d &#8220;Aug 1 2006&#8221; file.start<br \/>$ touch -d &#8220;Sep 1 2006&#8221; file.end<br \/>$ find \/usr\/share -daystart -newer file.start \\! -daystart -newer file.end<\/p>\n<p>\u00a0egrep -i &#8220;err|panic|crit|warn&#8221; \/var\/log\/messages<\/p>\n<p>User management<\/p>\n<p>groupadd sshd<br \/>useradd -g sshd -d \/var\/empty -s \/bin\/false sshd<br \/>groupadd -r dovecot<br \/>useradd -r -g dovecot -s \/sbin\/nologin dovecot<br \/>groupadd -r vmail<br \/>useradd -r -g vmail -s \/sbin\/nologin vmail<\/p>\n<p>Disk space issue<\/p>\n<p>ls -ltrhS | grep &#8220;Apr\u00a0 [2-4]&#8221; | awk -F&#8217; &#8216; &#8216;{print $9}&#8217; | xargs rm -f<\/p>\n<p>Memory Usage\u00a0 Command<\/p>\n<p>ps -eo pmem,pcpu,rss,vsize,args | sort -k 1 -r | more<\/p>\n<p>\u00a0Cpu Usage<\/p>\n<p>ps -eo pcpu,pid,args | sort -k 1 -r | head -10<\/p>\n<p>Sometime Apache process, keeps on execution (Seems like Hangs), so generally trying to get the exact PHP file that is running by Apache Process, So here is my Try.<\/p>\n<p>I used Strace to get the opened files by the apache process. (Get PID of <br \/>Apache process that is taking time, though you can also get it From top\u00a0 command)<\/p>\n<p># pstree -p -n | grep http<br \/>(This will show each files that is being processed by that Apache Proc)<\/p>\n<p># strace -p &lt;PID of Apache&gt;<br \/>The list of files could also be get using lsof, but that could not be of full use, as you need the files continuus<\/p>\n<p>To see all the memory used by a process we use pmap(process mapping ).<br \/>#pmap pid-of-that-application<\/p>\n<p>pmap 1946<\/p>\n<p>Example :<br \/># pmap `pgrep apache` | grep total<\/p>\n<p>Linux System Monitoring Tools<\/p>\n<p>#1: top &#8211; Process Activity Command<\/p>\n<p>Commonly Used Hot Keys<\/p>\n<p>The top command provides several useful hot keys:<br \/>Hot Key\u00a0\u00a0 \u00a0Usage<br \/>t\u00a0\u00a0 \u00a0Displays summary information off and on.<br \/>m\u00a0\u00a0 \u00a0Displays memory information off and on.<br \/>A\u00a0\u00a0 \u00a0Sorts the display by top consumers of various system resources. Useful for quick identification of performance-hungry tasks on a system.<br \/>f\u00a0\u00a0 \u00a0Enters an interactive configuration screen for top. Helpful for setting up top for a specific task.<br \/>o\u00a0\u00a0 \u00a0Enables you to interactively select the ordering within top.<br \/>r\u00a0\u00a0 \u00a0Issues renice command.<br \/>k\u00a0\u00a0 \u00a0Issues kill command.<br \/>z\u00a0\u00a0 \u00a0Turn on or off color\/mono<\/p>\n<p>What are the CPU states found in &#8220;top&#8221; output?<br \/>Cpu(s): 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st<\/p>\n<p># us -&gt; User CPU time: The time the CPU has spent running users\u2019 processes that are not niced.<br \/># sy -&gt; System CPU time: The time the CPU has spent running the kernel and its processes.<br \/># ni -&gt; Nice CPU time: The time the CPU has spent running users\u2019 process that have been niced.<br \/># wa -&gt; iowait: Amount of time the CPU has been waiting for I\/O to complete.<br \/># hi -&gt; Hardware IRQ: The amount of time the CPU has been servicing hardware interrupts.<br \/># si -&gt; Software Interrupts.: The amount of time the CPU has been servicing software interrupts.<\/p>\n<p>#2: vmstat &#8211; System Activity, Hardware and System Information<\/p>\n<p>The command vmstat reports information about processes, memory, paging, block IO, traps, and cpu activity.<\/p>\n<p>vmstat 3<\/p>\n<p>Display Memory Utilization Slabinfo<\/p>\n<p># vmstat -m<\/p>\n<p>Tail, Vmstat and Date\u00a0 in Loop, Output every 10 Sec<br \/># vmstat 1 1;for ((;;));do date; vmstat 10 2 | tail -n1;done<\/p>\n<p>3: w &#8211; Find Out Who Is Logged on And What They Are Doing<\/p>\n<p>w command displays information about the users currently on the machine, and their processes.<br \/># w username<br \/># w vivek<\/p>\n<p>#4: uptime &#8211; Tell How Long The System Has Been Running<\/p>\n<p># uptime<\/p>\n<p>#5: ps &#8211; Displays The Processes<\/p>\n<p>Show Long Format Output<\/p>\n<p># ps -Al<\/p>\n<p>Print All Process On The Server<\/p>\n<p># ps ax<br \/># ps axu<\/p>\n<p>Memmory commands\u00a0\u00a0 cpu commands<\/p>\n<p>Print Security Information<\/p>\n<p># ps -eo euser,ruser,suser,fuser,f,comm,label<\/p>\n<p>Set Output In a User-Defined Format<\/p>\n<p>ps axo stat,euid,ruid,tty,tpgid,sess,pgrp,ppid,pid,pcpu,comm<\/p>\n<p>ps -eopid,tt,user,fname,tmout,f,wchan<\/p>\n<p>Display sorted process taking most CPU in descending order<br \/># ps -eo pcpu,pid,user,args | sort -k 1 -r | head -10<\/p>\n<p>CPU COMMAND<\/p>\n<p>10 cpu usage command<\/p>\n<p>ps -e -o pcpu,cpu,nice,state,cputime,args &#8211;sort pcpu | sed &#8216;\/^ 0.0 \/d&#8217;<\/p>\n<p>####Watch changeable data continuously<\/p>\n<p>watch -n.1 &#8216;cat \/proc\/interrupts&#8217;<\/p>\n<p>cat \/proc\/interrupts<\/p>\n<p>Check CPU Temperature<br \/># echo `date +%b-%d-%H:%M:%S` | tr -d &#8216;\\ 012&#8217; ; echo -n &#8216; &#8216;; sensors | awk &#8216;\/CPU Temp:\/{ print $3 }&#8217;<\/p>\n<p>Check those commands which have been used most<br \/># history|awk &#8216;{print $2}&#8217; |awk &#8216;{print $1}&#8217; | sort | uniq -c | sort -rn | head -10<\/p>\n<p>Memmory commands<\/p>\n<p>Free Memory\u00a0 on Linux at Runtime<br \/># sync<br \/># echo 3 &gt; \/proc\/sys\/vm\/drop_caches \u00a0<\/p>\n<p>Display Only The Process IDs of Lighttpd<\/p>\n<p># ps -C lighttpd -o pid=<br \/>OR<br \/># pgrep lighttpd<br \/>OR<br \/># pgrep -u vivek php-cgi<br \/>Display The Name of PID 55977<\/p>\n<p># ps -p 55977 -o comm=<\/p>\n<p>Total Memory Usage Calculation<\/p>\n<p>Print Total sum of actual Memory Usages<\/p>\n<p>ps aux | awk &#8216;{sum +=$4}; END {print sum}&#8217;<\/p>\n<p>Check Actual Memory Consumption<\/p>\n<p>free -m<\/p>\n<p>Check total usage of One particular process<\/p>\n<p>ps aux | awk &#8216;{print $4,$11}&#8217; | sort |tail -n 23 |grep -w &#8216;someprocess&#8217; |wc -l<\/p>\n<p>All most consuming process<\/p>\n<p>ps aux | awk &#8216;{print $4,$11}&#8217; | sort |tail -n 20<\/p>\n<p>Create many Files Sequentially<br \/># seq -w 1 30 | xargs -i -t zcat in_Feb2011\/in_Files-{}May2011.gz | grep -E &#8216;name.html?secsid=3304847|name.html?secsid=30780899&#8217;<\/p>\n<p>Delete Empty Directories<br \/># find folder\/ -type d -empty | xargs -i -t rm -rf {}<br \/>or<br \/># find folder\/ -type d -empty -delete<\/p>\n<p>Real Time Monitoring on Linux<br \/># watch -n1 &#8211;difference &#8220;echo &#8220;Uptime&#8221;; uptime; echo \\n ; ps -eo pcpu,pid,args | sort -k 1 -r |grep -v watch | head -10; echo &#8220;\\n&#8221; ; tail \/var\/log\/cron | grep &#8220;check_load&#8221; &#8220;<\/p>\n<p>DISK SPACE COMMAND<\/p>\n<p>Find files based and sorted on Size<br \/># find \/ -type f -size +20000k -exec ls -lh {} \\; 2&gt; \/dev\/null | awk &#8216;{ print $NF &#8220;: &#8221; $5 }&#8217;\u00a0 | sort -nrk 2,2<\/p>\n<p>Killing processes in one Line<br \/># kill -9 `ps -ef | grep rsync| grep -v grep| awk &#8216;{print $2}&#8217;`<\/p>\n<p>Check Memory Fault<br \/># dd if=\/dev\/urandom bs=768304 of=\/tmp\/memtest count=1050<br \/># md5sum \/tmp\/memtest; md5sum \/tmp\/memtest; md5sum \/tmp\/memtest<\/p>\n<p>Repair Mysql MYISAM File<br \/># myisamchk &#8211;force &#8211;sort_buffer_size=64M &#8211;key_buffer_size=16M &#8211;read_buffer_size=8M &#8211;write_buffer_size=8M ..\/data\/phplists\/phplist_linktrack.MYI<\/p>\n<p>Command to make Services off in Defined Level on Linux<br \/># chkconfig &#8211;list | awk &#8216;{print $1}&#8217; | cut -d: -f1 | grep -vE &#8216;^crond|^network|^sshd|^syslog|^iptables&#8217; | awk &#8216;{print $1}&#8217; | while read line; do chkconfig\u00a0 &#8211;level 3 $line off; count=`expr $count + 1`; echo $count $line;done<\/p>\n<p>or Use this&#8230; One lineer \u00a0<br \/># chkconfig &#8211;list | awk &#8216;{print $1}&#8217;| grep -vE &#8216;^crond|^network|^sshd|^syslog|^iptables&#8217; | xargs -i chkconfig &#8211;level 3 {} off<\/p>\n<p>Counting Hits from Web Server Access log<br \/># awk &#8216;{print $1}&#8217; \/opt\/indian.com\/access_log | grep -vE &#8216;^:|^common|^-&#8216; | sort | uniq -c | sort -nr &gt; \/var\/www\/reports\/ips\/indian.txt<\/p>\n<p>or\u00a0 # awk &#8216;$1&gt;10000 {print $1}&#8217; \/opt\/indian.com\/access_log | uniq -c | sort -nr\u00a0 &gt; \/var\/www\/reports\/ips\/indian.txt<\/p>\n<p>PERL Search and Replace Text Pattern using Perl On linux Platform<br \/># find . -type f -name &#8220;*.html&#8221; | xargs perl -pi~ -e &#8216;s\/\\\/js\\\/active18\\\/\/\\\/read\\\/js\\\/active18\\\/\/&#8217;<\/p>\n<p>BACKUP SCRIPTS<\/p>\n<p>15 2 * * * root \/usr\/bin\/mysqldump -u root -pPASSWORD &#8211;all-databases | gzip &gt; \/mnt\/disk2\/database_`data &#8216; %m-%d-%Y&#8217;`.sql.gz \u00a0<\/p>\n<p>Details about SUID, SGID and Sticky bit permission on linux os<br \/>* SUID or setuid: change user ID on execution. If setuid bit is set, when the file will be executed by a user, the process will have the same rights as the owner of the file being executed.<br \/>* SGID or setgid: change group ID on execution. Same as above, but inherits rights of the group of the owner of the file on execution. For directories it also may mean that when a new file is created in the directory it will inherit the group of the directory (and not of the user who created the file).<br \/>* Sticky bit. It was used to trigger process to &#8220;stick&#8221; in memory after it is finished, now this usage is obsolete. Currently its use is system dependant and it is mostly used to suppress deletion of the files that belong to other users in the folder where you have &#8220;write&#8221; access to.<\/p>\n<p>Numeric representation :<\/p>\n<p>Octal digit Binary value Meaning<br \/>0 000 setuid, setgid, sticky bits are cleared<br \/>1 001 sticky bit is set<br \/>2 010 setgid bit is set<br \/>3 011 setgid and sticky bits are set<br \/>4 100 setuid bit is set<br \/>5 101 setuid and sticky bits are set<br \/>6 110 setuid and setgid bits are set<br \/>7 111 setuid, setgid, sticky bits are set<\/p>\n<p>file : 2644<br \/>dir : 2755<\/p>\n<p>Textual representation :<\/p>\n<p>SUID If set, then replaces &#8220;x&#8221; in the owner permissions to &#8220;s&#8221;, if owner has execute permissions, or to &#8220;S&#8221; otherwise. Examples:<br \/>-rws&#8212;&#8212; both owner execute and SUID are set<br \/>-r-S&#8212;&#8212; SUID is set, but owner execute is not set<\/p>\n<p>SGID If set, then replaces &#8220;x&#8221; in the group permissions to &#8220;s&#8221;, if group has execute permissions, or to &#8220;S&#8221; otherwise. Examples:<br \/>-rwxrws&#8212; both group execute and SGID are set<br \/>-rwxr-S&#8212; SGID is set, but group execute is not set<\/p>\n<p>Sticky If set, then replaces &#8220;x&#8221; in the others permissions to &#8220;t&#8221;, if others have execute permissions, or to &#8220;T&#8221; otherwise. Examples:<br \/>-rwxrwxrwt both others execute and sticky bit are set<br \/>-rwxrwxr-T sticky bit is set, but others execute is not set<\/p>\n<p>drwxrwxrwt &#8211; Sticky Bits &#8211; chmod 1777<br \/>drwsrwxrwx &#8211; SUID set &#8211; chmod 4777<br \/>drwxrwsrwx &#8211; SGID set &#8211; chmod 2777<\/p>\n<p>Procedure to add a swap file<\/p>\n<p>You need to use dd command to create swapfile. Next you need to use mkswap command to set up a Linux swap area on a device or in a file.<\/p>\n<p>a) Login as the root user<\/p>\n<p>b) Type following command to create 512MB swap file (1024 * 512MB = 524288 block size):<\/p>\n<p># dd if=\/dev\/zero of=\/swapfile1 bs=1024 count=524288<\/p>\n<p>c) Set up a Linux swap area:<\/p>\n<p># mkswap \/swapfile1<\/p>\n<p>d) Activate \/swapfile1 swap space immediately:<\/p>\n<p># swapon \/swapfile1<\/p>\n<p>e) To activate \/swapfile1 after Linux system reboot, add entry to \/etc\/fstab file. Open this file using text editor such as vi:<\/p>\n<p># vi \/etc\/fstab<\/p>\n<p>Append following line:<\/p>\n<p>\/swapfile1 swap swap defaults 0 0<\/p>\n<p>So next time Linux comes up after reboot, it enables the new swap file for you automatically.<\/p>\n<p>g) How do I verify swap is activated or not?<\/p>\n<p>Simply use free command:<\/p>\n<p>$ free -m<\/p>\n<p>strace<br \/>October 23rd, 2008 No comments<\/p>\n<p>strace -q -f -c -p<\/p>\n<p>strace -f verbose=all -e write=all -o \/tmp\/strace.log -p [pid]<\/p>\n<p>strace -T -t -q -f -e trace=file,open,close,read -o \/tmp\/prod.strace.log -p &lt;oms or ui pid&gt;<\/p>\n<p>-f traces all child processes as they are created byt he currently traced process as a result of the fork() system call.<\/p>\n<p>-e is a qualifying expression which modifies which events to trace or how to race them<\/p>\n<p>verbose=all dereferences structures for all system calls<\/p>\n<p>write=all performs a full hexadecimal and ASCII dump of all the data written to all file descriptors<\/p>\n<p>-o output file<\/p>\n<p>-p process id to trace<\/p>\n<p>Replace -p [pid] with [command] to trace a specific command.<\/p>\n<p>Drop Caches<br \/>October 21st, 2008 No comments<\/p>\n<p>Kernels 2.6.16 and newer provide a mechanism to have the kernel drop the page cache and\/or inode and dentry caches on command, which can help free up a lot of memory. Now you can throw away that script that allocated a ton of memory just to get rid of the cache\u2026<\/p>\n<p>To use \/proc\/sys\/vm\/drop_caches, just echo a number to it.<\/p>\n<p>To free pagecache:<\/p>\n<p># echo 1 &gt; \/proc\/sys\/vm\/drop_caches<\/p>\n<p>To free dentries and inodes:<\/p>\n<p># echo 2 &gt; \/proc\/sys\/vm\/drop_caches<\/p>\n<p>To free pagecache, dentries and inodes:<\/p>\n<p>echo 3 &gt; \/proc\/sys\/vm\/drop_caches<\/p>\n<p>As this is a non-destructive operation and dirty objects are not freeable, the user should run \u201csync\u201d first!<\/p>\n<p>How to copy data in VI editor<br \/>January 20th, 2009 No comments<\/p>\n<p>vi first\u00a0 edit source file<br \/>then move your cursor to start of selection<br \/>ma\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 mark current position with letter a<br \/>then move your cursor to end of selection<br \/>y&#8217;a\u00a0\u00a0\u00a0\u00a0 yank to buffer x from current position to mark a<br \/>:e other\u00a0 edit target file<br \/>move cursor to where you want the data<br \/>p\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 put from buffer x<\/p>\n<p>Copying a block of text from one file to another in Vi<br \/>December 29th, 2008 No comments<\/p>\n<p>To copy a block of text between files execute the commands:<br \/>Command \u00a0\u00a0 \u00a0Explaination<br \/>1. \u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Edit the file containing the text you want to copy.<br \/>2. \u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Go to the top line to be copied.<br \/>3. \u00a0\u00a0 \u00a0ma \u00a0\u00a0 \u00a0Mark this line as mark \u201ca\u201d.<br \/>4. \u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Go to the bottom line to be copied<br \/>5. \u00a0\u00a0 \u00a0y&#8217;a \u00a0\u00a0 \u00a0Yank (y) the text from the current cursor location to the mark \u201ca\u201d (&#8216;a)<br \/>6. \u00a0\u00a0 \u00a0:split second-file \u00a0\u00a0 \u00a0Open another window containing the second file. (This the file in which the text is to be inserted.)<br \/>7. \u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Go to the line where the insert is to occur. The text will be place after this line.<br \/>8. \u00a0\u00a0 \u00a0p \u00a0\u00a0 \u00a0Put the text after the cursor.<\/p>\n<p>For Vi Editor<br \/>Do you like www.linuxnix.com ? Please consider supporting us by becoming a subscriber and get a Linux basics e-book for free.<br \/>unix_vi_editor<\/p>\n<p>VI editor is the default file editor in most of the Linux\/Nix machines. It is having great capabilities to edit a file with in few key strokes.<\/p>\n<p>Lets start with some general information and then move on to some good things what vi editor can do for you while editing a file.<br \/>1. Vi stands for visual.<br \/>2. Vi have its variants like vim which stands for Vi IMproved, VimX11 for gui and winvi for MS windows.<br \/>3. Vi is the most popular editor and next most popular editor is gedit.<br \/>4. Do you know there is a book on VI editor from orally which is of 600+ pages.<br \/>5. Some other editors which will do the work of editing files are neno, pico, gedit, emacs, joe, nedit, ed etc.<\/p>\n<p>Learning vi editor and remembering them is a very a easy task if you learn it in a systematic way.<br \/>a. Modes of VI<br \/>b. Navigational commands<br \/>c. Editing commands.<br \/>d. Search and Replace<br \/>e. Save and quiting a file.<\/p>\n<p>a. Modes of VI :<br \/>Vi have two mode of operation.<br \/>1. Command mode<br \/>2. Inserting mode<\/p>\n<p>Command mode :<br \/>Vi editor begins in command mode, where cursor movement(navigation in the file) and editing occur. To enter in to command mode from Inserting mode press esc button.<\/p>\n<p>Inserting mode :<br \/>Used for entering text, this is similar to notepad in Windows. To enter in to inserting mode you can use any of the following.<br \/>i or I =&gt; present line<br \/>o =&gt; one line down the present line<br \/>O =&gt; one line above<\/p>\n<p>Note : All comments will work in command mode only.<\/p>\n<p>b. Navigational commands :<br \/>1. Character navigation k, h, l and j<br \/>h =&gt; To move one character left.<br \/>j =&gt; To move one line down.<br \/>k =&gt; To move one line up.<br \/>l =&gt; To move one character right.<\/p>\n<p>How to use above commands in clever way?<br \/>Examples :<br \/>6j =&gt; to move 6 lines down from the present courser.<br \/>7k =&gt; to move 7 lines above from the present courser.<\/p>\n<p>2. Word Navigation<br \/>w =&gt; word forward.<br \/>e =&gt;word forward, but end of the word.<br \/>b =&gt; one word backward.<\/p>\n<p>Examples :<br \/>32w =&gt; To move 32 words forward<br \/>6b =&gt; To move 6 words back.<\/p>\n<p>3. Setting (nu) mbering to lines<br \/>:set nu<br \/>Removing of (nonu)mbering to lines<br \/>:set nonu<\/p>\n<p>4. Moving paragraphs<br \/>move one paragraph up =&gt; {{<br \/>move one paragraph down =&gt; }}<\/p>\n<p>5. Moving page up\/down<br \/>For up =&gt; ctrl+b<br \/>For down =&gt; ctrl+f<\/p>\n<p>6. Moving start\/end of the file<br \/>Starting of the file(first line =&gt; [[<br \/>End of the file(last line) =&gt; ]]<\/p>\n<p>7. Going to any line :<br \/>:lineno<\/p>\n<p>Example :<br \/>If we want to go to 56 line then type<br \/>:56<\/p>\n<p>c. Editing commands<\/p>\n<p>8. Replace one letter<br \/>Replace one letter =&gt; r<br \/>Delete one letter =&gt; x<\/p>\n<p>&gt;9. Editing one word<br \/>Edit one word =&gt; cw<br \/>Delete one word =&gt; dw<\/p>\n<p>10. Editing one line<br \/>Editing a line from courser to the end of that line =&gt; d$<\/p>\n<p>11. Cutting<br \/>deleting(cutting) one line =&gt; dd<\/p>\n<p>Examples :<br \/>2dd(deleting\/cutting two lines)<\/p>\n<p>12. Pasting<br \/>Pasting a line below the courser =&gt; p<br \/>Pasting a line above the courser =&gt; P<\/p>\n<p>13. Coping<br \/>Copying one line =&gt; yy<br \/>Copying n lines =&gt; nyy<\/p>\n<p>14. Special commands<br \/>joining lines =&gt; J<br \/>undoing things =&gt; u<br \/>repeating previous command =&gt; .<\/p>\n<p>d. Search and replace<\/p>\n<p>15. Search for a term \/term<\/p>\n<p>Example : If you want to search for suresh then press \/suresh enter<br \/>\/suresh<br \/>Moving to next occurrence, press \u201cn\u201d with out quotes moving to previous occurrence, press \u201cN\u201d with out quotes.<\/p>\n<p>16. Searching and replacing a term(here separator is \/ )<br \/>:%s\/searchterm\/replaceterm\/g<br \/>change default separator<br \/>:%s_\/home\/surya\/grade_\/home\/testing\/dest_g<\/p>\n<p>To search and replace particular term from given line to other given line.<br \/>:%s294,304\/sahana\/xyz\/g<\/p>\n<p>e)Save and quiting a file<br \/>:w =&gt; save the file<br \/>:q =&gt; quit the file<br \/>:wq =&gt; save and quit<br \/>:w! =&gt; force save the file<br \/>:q! =&gt; force quit with out save<br \/>:wq =&gt; save and quit forcefully<\/p>\n<p>==========================================================================================================================================================<br \/>Disk Space issues<\/p>\n<p>mpstat -P ALL<\/p>\n<p>cat \/proc\/interrupts<\/p>\n<p>iostat -kd 1<br \/>iostat -c -t<br \/>iostat -c -t 2 10 <br \/>iostat -c 3 <br \/>iostat -d 5<br \/>iostat -x 1<\/p>\n<p>sar -b<\/p>\n<p>vmstat -S M 2 5<\/p>\n<p>Find process causing high iowait<\/p>\n<p>netstat -autpn | grep :80<br \/>netstat -autpn | grep :3306<br \/>netstat -na<br \/>netstat -an|grep :80|sort|more<br \/>netstat -an|grep ESTABLISHED<br \/>netstat -ntu | awk &#8216;{print $5}&#8217; | cut -d: -f1 | sort | uniq -c | sort -nr | more<br \/>ps ax | awk &#8216;$3 ~ \/^D\/ { print $0 }&#8217;<br \/>netstat -an | grep :80 | wc -l<\/p>\n<p>DO NOT DO THIS until you are certain what is going on.<\/p>\n<p>echo 100 &gt; \/proc\/sys\/vm\/inactive_clean_percent<br \/>echo 2 10 20 &gt; \/proc\/sys\/vm\/pagecache<\/p>\n<p>==========================================================================================================================================================<\/p>\n<p>To View Or List Only Directories In Linux?<br \/>Do you like www.linuxnix.com ? Please consider supporting us by becoming a subscriber and get a Linux basics e-book for free.<\/p>\n<p>How to view\/list only directories in Linux?<br \/>Ans : This can be achieved in two ways<br \/>1. Through ls command<br \/>2. Through find command<\/p>\n<p>With ls we have to use grep to get the directory listings.<br \/>Ls \u2013l grep ^d<\/p>\n<p>Example :<br \/>[root@test surendra_a]# ls -l grep ^d<br \/>d\u2014\u2014\u2014 2 surendra_a surendra_a 4096 Sep 8 09:54 HTWFAIP<br \/>drwxrwxr-x 2 surendra_a root 4096 Nov 27 12:30 LinuxCBT \u2013 RHEL5<br \/>drwxrwxr-x 2 surendra_a root 4096 Oct 12 16:40 Software<br \/>[root@test surendra_a]#<\/p>\n<p>With find we can have more controle on how to display only directories.<\/p>\n<p>A. To display all the directories and sub-directories in present directory<br \/>#find . -type d<\/p>\n<p>B. Displaying only directories in present directory<br \/>#find \/root\/ -type d \u2013maxdepth 1<\/p>\n<p>C. Displaying just directories in present directry and its sub-directories<br \/>#find \/root\/ -type d \u2013maxdepth 2<\/p>\n<p>&nbsp;<\/p>\n<p>How TO kill to Process<\/p>\n<p>a) pgrep httpd | xargs kill -9<\/p>\n<p>b) kill -9 `ps -ef | grep \u2018httpd\u2019 | grep -v grep | awk \u2018{print $2}\u2019`<\/p>\n<p>Top to Cpu Usage commands<\/p>\n<p>ps auxf | sort -nr -k 3 | head -10<\/p>\n<p>Top processes consume RAM Memory<\/p>\n<p>ps auxf | sort -nr -k 4 | head -10<\/p>\n<p>Find and delete \/ remove files older 7 days \/ 1 week on Linux<\/p>\n<p>find \/root\/server -type f -mtime +7 -exec rm -rf {} \\;<\/p>\n<p>Remove files older 90 days \/ 3 months \/ 1 quarter<\/p>\n<p>find \/var\/ -type f -mtime +90 -exec rm -rf {} \\;<\/p>\n<p>Remove files older 365<\/p>\n<p>find \/var\/ -type f -mtime +365 -exec rm -rf {} \\;<\/p>\n<p>check your public IP<\/p>\n<p>n Linux, we have some best ways to check Public IP for that server.<\/p>\n<p>1. curl ifconfig.me<\/p>\n<p>2. wget -qO- ipecho.net\/plain<\/p>\n<p>3. wget -qO- ifconfig.me\/ip<\/p>\n<p>check your public IP<\/p>\n","protected":false},"excerpt":{"rendered":"<p>find . -type f -exec grep &#8216;NMX_FXNG_AND_CONTRACT_DBF&#8217; {} \\;<\/p>\n<p>history<\/p>\n<p> history|awk &#8216;{print $2}&#8217;|awk &#8216;BEGIN {FS=&#8221;|&#8221;} {print $1}&#8217;|sort|uniq -c|sort -r<\/p>\n<p> history|tail -1000|awk &#8216;{print $2}&#8217;|awk &#8216;BEGIN {FS=&#8221;|&#8221;} {print $1}&#8217;|sort|uniq -c|sort -r<\/p>\n<p>-daystart This flag starts at the beginning of the day.-atime The time the file was last accessed &#8212; in number of days.-ctime The time [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/1884"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1884"}],"version-history":[{"count":6,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/1884\/revisions"}],"predecessor-version":[{"id":1887,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/1884\/revisions\/1887"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1884"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1884"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}