Thursday, 9 August 2012

Useful Linux commands

Some Useful Linux Commands
1) Monitoring
  • To see running processes:  ps -f
  • To see processes by application name:  ps ax | grep <appname>
  • Kill process:  kill -9 <pid>
  • Signal to process (for example to restart nginx): kill -s HUP <pid of master nginx>
  • Used for monitoring system input/output: iostat
  • System-monitor process-view: htop or top
  • Used for monitoring network connections: netstat
  • sar -P ALL - statitiscs by processor for <cpu, swap, etc> last 24 hours
  • free - memory statistics 
2) Files
  • Ensure that all directories are 775 
        find /var/www/test -type d | xargs chmod 775 
or
       chmod 0755 $(find . -type d)
  • Make all files group and owner readwrite 
      find /var/www/test -type f | xargs chmod o+w,o+r,g+w,g+r
  • make sh script executable:
      add to beginning of the file #!/bin/bash
      chmod +x <name.sh>
  • To see changes in file in real time: tail -f <file name>
  • Create symbolic link ln -s <real file location>
  • Find file: find ./ -name <searchterm>
  • Find in file content find <path> -name "*.php" -print | xargs grep "function testA("
  • Text viewer/editor : nano <filename>
  • Text viewer/editor : vim <filename>
  • need to press a - to enter in the editor mode, then press esc - to exit from editor mode, and :qw command to quit and write changes
  • File manage: mc
  • grep <options> <what to search> <where to search>
        Recursive search grep -r
        Show context grep -C 3
        Show string number grep -n 

  •      Sort for example tab delimeter file

       sort -t"$TAB" -k1.4 -nr
       where -t delimeter, -k1.4 -first column from 4th sybmol, -n - numeric sort, -r reverse order

  • difference between files
      diff <file1> <file2> -b -B -i -u
      where -b Ignore changes in amount of white space.
     -B Ignore changes that just insert or delete blank lines.
     -i ignore case
     -u unified format

  •    To get the lines in one file that are not in another   
         grep -F -x -v -f fileB fileA
This works by using each line in fileB as a pattern (-f fileB) and treating it as a plain string to match (not a regular regex) (-F). You force the match to happen on the whole line (-x) and print out only the lines that don't match (-v). Therefore you are printing out the lines in fileA that don't contain the same data as any line in fileB.
       or

      comm <(sort a) <(sort b) -3
-3 - only that are in file b
-2 - in both files
-1 -only that are in file a

  • Compare and find where is first byte and line where difference exists
cmp <file1> <file2>


  • extracting gz
gzip -d your_file.gz
or
tar -xvzf your_file.tar

 3) Version Control System
  • SVN 
       Copy remote repository  svn checkout https://test.com:8443/svn/Project/trunk ./ --username <name> --password <pass>
       Commit svn commit -m "added howto section."
       Commit with auth svn commit \local\path\to\your\repository --message "release" --no-auth-cache --non-interactive --username user11 --password your_mega_passwd
  • GIT
      Copy remote repository git clone https://test.com:8443/svn/Project/trunk
      Commit:
      git commit -a -m "bug fixed"
      git push
or
    git add <changed file>
    git commit -m  "bug fixed" 
    git push

Regex:
match all \r\n - [\r\n]+
https://regexr.com/

4) Docker
docker exec -ti  <image_name> /bin/bash
docker ps
docker restart/stop/start
docker run -it <image_name> /bin/bash


Good posts:
Some Useful Linux Commands
The 10 most useful Linux commands
Five Linux performance commands every admin should know
The Linux Directory Structure, Explained

No comments:

Post a Comment