Thursday, November 1, 2012

Useful Unix commands

Copy files between 2 Linux systems:

          scp root@source_sys1:/home/odpt/download/abdc-file-rhel30.zip .

Search and replace for some text in all matching files:

          find /fs4/install/ -name "*.properties" | xargs sed -i 's|ServerName=abc.string.com|efg.string1219.com|g'

Find the application running on particular port

      netstat -atnp|grep -i 23914

useful vi editor command

  • Find and replace string in a vi file: ":%s/original_string/replace_with/gc"
  • to see line numbers in file: se nu
  • Delete: [make sure you pressed Esc key before running below commands]
    • Delete a line: dd
    • Delete a word: dw
    • Delete all the characters in current line after current cursor position: d$

Search for some text in many files:

          find /fs4/123/B105/install/ -name "*.properties" | xargs grep -iR "serverPort=8080"

Create Directories hierarchically.

    For example we want to create a directory "c" in "/a/b" directory, where are "a" and "b" don't exist.
          mkdir -p /a/b/c #This will create directories "a", "b" and "c"

Share a folder between various systems/nodes


      Login to system1, Create a folder "/sts/lib/data/" and give full acces(777)
chkconfig --add nfs
    service nfs start
  vi /etc/exports and add "/sts/lib/data 222.222.11.22 (rw,no_root_squash,async)" (IP of 2nd machine)
       su -
  service nfs restart
  showmount -e
  Login to system2 where you want to access sharing
  mount -t nfs system1_ip:/sts/lib/data /sts/lib/data (mount -t nfs <IP:PATH> <mount-location>)
  Now we can access folders back and forth.

CUT command


    Cut command in unix (or linux) is used to select sections of text from each line of files or from the
    output text in console. You can use this command to select fields or columns from a line by specifying a
   delimiter or you can select a portion of text by specifying the range or characters. Basically the cut
   command slices a line and extracts the text.

    Important options in cut command:
      -c, --characters=LIST   select only these characters
      -d, --delimiter=DELIM   use DELIM instead of TAB for field delimiter
      -f, --fields=LIST       select only these fields;  also print any line that contains no delimiter character,
      unless the -s option is specified.

    Examples:
     1. ifconfig | grep "inet addr" | cut -d':' -f2
       f2 returns 2nd filed.
       d':' used to cut delimiter ':'
    2. ifconfig | grep "inet addr" | cut -c-40
       -c-40 return 40 character from each line.

File related
  find


  Syntax:
  Example:



  grep


  Syntax:
  Example:




  touch


  Syntax:
  Example:



  tail

  Syntax:
  Example:


  ls

  ll

  rm
  Usage: Delete a file or directory. For folder we have to give -r option.
  Syntax:
  Example:


  mv:
  Usage: move file or folder from one place to another. This command is also used to rename a file.
  Note: For folder we have to give -r option
  Ex: mv /home/abc/file1.txt . #Moves a file named "file1.txt" from "/home/abc" to current directory.
        mv /home/abc/file1.txt /home/def/ #moves a file "file1.txt" from "/home/abc" to "/home/def" directory.
        mv file1.txt file2.txt #renames file name from file1.txt to file2.txt


  chmod:


  Syntax:
  Example:



  • chmod a+x file_name
  • chmod a-x file_name
  • chmod a+w file_name
  • chmod a+r file_name


  chown


  Syntax:
  Example:




  xargs

  locate

  which

http related
  wget
  curl
  telnet

Memory related:
  du
  df
  free

process related info
  ps
  kill
  top

system related.
  uname

Compress files
  zip
  unzip
  tar
  gzip
  gunzip

User related:
  whoami
  last
  passwd
  chfn ............... change your "Real Name" as seen on finger
  chsh ............... change the shell you log into

netstat
crontab
tcpdump