avatar khanh than

SOFTWARE ENGINEER

To keep tracking my skills & accomplisments

05 Nov 2024

● Operating System

Linux Glimpse

Ubuntu (based on Linux) is an operating system that is popular for strong programmability and cost-free availability. It has many variants such as manjaro, ubuntu,… for users to select.

Author: the Linux kernel v0.01 was created by Linus Torvalds in 1991, about 6 years after the first version of window was launched by Microsoft in 1985. Since then, various Linux distributions have emerged, leading to some confusion about the concepts of the Linux operating system:

Thank goodness! Bash (Bourne Again Shell) is the default shell for many Linux distributions, providing users with a regular way to interact with the operating system using text-based commands.

This post is to show some regular Bash commands:

  1. ls : list of files in current dir
    • ls -l : more details about authorization and ownership.

  2. cd Dirname: change current directory.
    • cd .. : back to the mother directory.
  3. pwd : print the current directory path.

  4. which appName : print the application directory path.

  5. touch newfileName : create an empty file.

  6. mkdir newDirName : create an empty folder.

  7. rm fileName : remove a file.
    • rm -rf dirName : delete a folder recursively (warning: cannot be undone).

  8. find : search for a file or directory.
    • find . -name “*.txt” : search all files .txt” in current dir.
    • find . -iname “*.TXt” : search files “.txt” in current dir, ignoring case-senstive.
    • find . -maxdepth 1 -name “*.txt” : search all files .txt” in current dir but only one layer (excluding sub-folders).
    • find ./folderName -iname “*.TXt” : search files “.txt” in a folder.

  9. grep : search lines within a file content.
    • grep -i “=” file.txt : search any lines has ‘=’ in a file.
    • grep -in “=” file.txt : any lines has ‘=’ and show line numbers.
    • grep -i “category” ./_dirName/* : any lines has ‘category’ in all files of a folder.
  10. clear : wipe out the screen.

  11. cat fileName : show content of a file.

  12. nano fileName : open a file (create the file if it is not existing).

  13. top : displays real-time system processes, including memory and CPU usage…
    • top | grep -in “sql” : only running filenames that has ‘sql’ at its name.
    • ps aux : show a snapshot of all current running process, including PID, CPU and memory usage, user…
    • ps aux | grep -in “sql” : only running filenames that has ‘sql’ at its name.
  14. kill processID : stops a process by its PID (process ID).

  15. tail : show some specific content of a file from the end.
    • tail -n 3 fileName : show the last 3 lines.
    • tail -f fileName : show the last appended data and keep tracking new updating.

  16. chmod : change modes or permissions of a file.

    • chmod +x file.sh : make a script file executable:

    • chmod 755 filename : read & execute for everyone and plus write access for the owner. This mode is seen very commonly.
    • chmod 744 filename : read for everyone and plus write & execute permission for the owner.
    • Binary Rule : it’s better to read the permission pattern from right to left (ex: drwxr-xr-x = 755).
      • rwx = “read-write-execute” = 111 = 2^2 + 2^1 + 2^0 = 7 (grant all permissions).
      • r–x = “read-0-execute” = 101 = 2^2 + 0^1 + 2^0 = 5 (execute & read).
      • rw– = “read-write-0” = 110 = 2^2 + 2^1 + 0^0 = 6.
      • r– – = “read-0-0” = 100 = 2^2 + 0^1 + 0^0 = 4.

      "755" refers to 3 types of user: owner, group and others.

    • Common types of linux users:
      1. Admin user (with “sudo”): uses through “sudo” commands as root when necessary.
      2. Root user: a superuser has the highest level of access on the system: install software, manage user accounts.
      3. Owners: typically the creator of a file.
      4. Group: a collection of users, each shares similar permissions to a file.
      5. Regular users: like an individual user, who can install programs in their own home space. But they are restricted from accessing system files and performing administrative tasks.
      6. System users: users created for intalled specific programs like mysql, postgres…

      (Hint: right-click on a file, select "properties", we can check some user types in tab "permission".)

  17. chown : changes file owner and group.
    • chown userName:groupName filename : assigns file ownership to new user and new group.

  18. wget url.com/file.zip : downloads file from an url (GET http method).
    • curl -O url.com/file.zip : downloads file and save it with its original name.
    • tar -xzvf file.tar.gz : extract a compressed .tar.gz file.

  19. printenv : print out all global variables.
    • printenv | grep -in user : print out all global variables including the text “user” in their names.

  20. echo : print out, over-write or append lines into a file.
    • echo HOME : print out a global variable “HOME”.
    • echo “(pwd)” : print out a result of a command “pwd”.
    • echo “line for overwriting” > filename.txt : creating or overwriting the file.
    • echo “line for appending” >> filename.txt : appending a line into the file.
  21. man cd : show manual of command “cd”.

  22. who : show current logged-in user.

  23. sudo pacman -Syu (for manjaro OS): update the system and upgrade all installed packages using the default package manager pacman, excluding personal applications (chrome, code,…).


Afterword: (Lời bạt)

Share on: