Linux tips

Here is the list of command lines which often used in ZTI operations.

  1. Disk
    • Check size of disk
      df -h
    • Check disk usage
      du -sh <directory>
      Ex.
      du -sh /opt
      du -sh * | sort -n -r
  2. Directory
    • List directory
      ls -lsh
      ls -lsha
    • Create directory
      mkdir -p /opt/user/usr1/docs
    • Remove directory
      rm -rf /opt/user/usr1/
    • Change direcotry mode
      chmod -R 777
  3. System
    • Manage service
      systemctl restart <service_name>
      systemctl status <service_name>
      systemctl start/stop <service_name>

      Ex.
      sytemctl restart tomcat
    • Find running service by name
      ps -ef | grep <name>
      Ex.
      ps -ef | grep java
    • Find running service on port
      lsof -i -P -n | grep '<port>'
      Ex.
      lsof -i -P -n | grep ':5000'
    • Kill service
      kill -9 <pid>
      kill -9 pgrep -f 'java -jar'
    • Find & Kill process
      lsof -i -P -n
      lsof -i -P -n | grep ':5000'
      lsof -i -P -n | grep ':'
      kill -9 $(ps aux | grep | awk '{print $2}')
      kill -9 $(lsof -i -P -n | grep ':3013')
    • List all services
      systemctl list-units --type service --all
  4. JAVA
    • Select and set default JAVA version
      alternatives --config java
  5. VI command
    • vi ใช้สำหรับเขียนไฟล์ หรือแก้ไขไฟล์
      vi <file_name>
    • i แก้ไขไฟล์
    • /<search_massage> ค้นหาข้อความในไฟล์
:w  save file
:wq  save file และออกจาก vi
:q  ออกจาก vi ถ้าไม่มีการแก้ไขข้อมูล
:q!  ออกจาก vi โดยไม่สนว่าข้อมูลมีการแก้ไขหรือไม่
:e name  เปลี่ยนชื่อ file
:e!  เอา save file version ล่าสุดกลับมา
:e + name  โหลด file เพื่อแก้ไข และเลือน cursor ไปบรรทัดสุดท้าย
:e +n name  โหลด file เพื่อแก้ไข และเลือน cursor ไปบรรทัด n
:e #  แก้ไข file อันใหม่
^^  เหมือน  :e #
:w name  save ข้อมูลลง file ใหม่
:w! name  save ข้อมูลทับลง file เดิม
คำสั่งที่ใช้บ่อย

คำสั่งอื่นๆ ดูได้ที่ https://saixiii.com/vi-linux-command/