Unix Commands


Editing on Vi
-----------------------
vi  <filename>
:0     -> to goto first line
:<number> goto line number
/oracle-> searh for a word "oracle"
    use arrow key -> then
    n -> to search Next occurance
:q!     -> Exit without save
:wq     -> Save and Quit
Esc i     -> Insert
Esc a    -> Append
Esc dd  -> Delete a Line
    <number>dd -> Delete number of lines.
Esc $     -> Go to end of current line
Esc 0     -> Go to first character of the current line
Esc 2yy -> Copy the 2 lines from the current line
Esc p   -> Paste copied lines   

Find matching filenames
--------------------------------
Find filenames with extension .jar
---------------------------------
find . -name '*.jar'

Find filenames with Case insensitive (uppercase/lowercase) with extension .jar
-----------------------------------------------------------------------------
find . -iname '*.jar'

Find all filenames with extension .xml modified time greater than yesterday.
--------------------------------------------------------------
find . -name '*.xml' -mtime -1

Unzip all zip files in a directory
--------------------------------
for z in *.zip; do unzip $z;done

find . -name '*.zip' -print0 | xargs -I{} -0 unzip {}


List files in a zip file without extracting
-------------------------------------------
unzip -l <filename>

Extract a gzipped tar archive ( *.tar.gz ) using option xvzf
Use the option z for uncompressing a gzip tar archive.
-------------------------------------------------------
tar xvfz archive_name.tar.gz

Create tar file
-------------------------
tar -cvf trading.tar *

Compressed tar file
-------------------------------
tar -zcvf trading.tgz *

List the zipped files contains file names
----------------------------------------
search all the jar files and print the jar files if it contains the class Service2PipelineMapper
find . -iname '*.jar' -exec bash -c 'unzip -l $1 | grep -q -e 'Service2PipelineMapper' && echo $1 $2' {} {} \;


Create ZIP file
----------------
Create trading.zip with all the files in the current directory

zip trading.zip *


Copy multiple files
--------------------------
lists the zip files in current directory, including sub-directories, and copy the files to
the directory temp2 in the current directory
find . -name '*.xml' | xargs  cp -i -t ./temp2/


Move multiple files
------------------
lists the zip files in current directory, including sub-directories, and copy the files to
the directory temp2 in the current directory

find . -name '*.xml' | xargs mv  -t ../../temp2/


Search in the contents of the File(s)
----------------------------------------------
grep command searches for occurance of pattern in file(s).

Search for a pattern in a File prints lines matched
--------------------------------------

grep 'javascript' index.xml
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript"> </script>
<script type="text/javascript"> _uacct = "UA-1149868-3"; urchinTracker(); </script>



Search for the pattern LP_SL_NO in the file and prints the occurances only.

grep -o 'LP_SL_NO' <filename>  

Search for the pattern LP_SL_NO in current directory, and it prints the occurances only.
grep -o 'LP_SL_NO' ./ -R


Change the time stamp of the file. 
--------------------------------------------- 
touch -t 08092013 <filename>

Search and Replace the content in file(s)
-----------------------------------------------------
sed -i 's/<patterntosearch>/<patterntoreplace>/g' <filename>
/g -> replace all occurances.

sed -i 's/docTimestamp="2013-07-26T11:07:13"/docTimestamp="2014-07-26T11:07:13"/g' <filename>

sed -i 's/<Modified\/>/<Modified>2013-03-03<\/Modified>/g' <filename>

If escape characters are included in the pattern use \ before the character 

Search for a pattern in xml files and Replace with a String within the current directory 

ls *.xml | xargs sed -i  's/docTimestamp="[0-9][0-9][0-9][0-9][-][0-9][0-9][-][0-9][0-9]/docTimestamp="2013-08-02/g'

Process status details
----------------------
List the java processes with started and elapsed time and the command used to start the process is below.


etime - elapsed time
cmd - command line
stime - start time


ps -eo pid,user,etime,stime,cmd | grep java


No comments: