Sharing some programming knowledge.

0%

Ubuntu (Linux) Tips on Creating a Convenient Environment

  • Usage (Example): sudo ln -s ~/Downloads/eigen/Eigen /usr/local/include/Eigen
  • After doing this, /usr/local/include/Eigen will link to the original folder ~/Downloads/eigen/Eigen, so the compiler will be able to detect Eigen library.
  • Note: -s means the link is permanant ( will not disappear after rebooting ); must provide full paths rather than relative paths.

Delete all the files that has the same name under a directory

  • Usage: find . -name 'introduction.md' -type f | xargs rm -rf

Open a directory in command line by creating alias

  • It’s convenient to create an alias in ~/.bashrc.
  • Usage: gedit ~/.bashrc, then add alias od='nautilus --browser ~/OneDrive' in the file, just replace od by your name and ~/OneDrive by your own directory, then save it and run source ~/.bashrc to update.
  • Next time you just need to open the terminal and simpy run od (for example) to open ~/OneDrive directory, without having to type in the whole path.

Make read-only file system writable

  • My NTFS file system on my ubuntu suddenly become read-only, this is perhaps because it is not properly unmounted, thus they are marked as locked.
  • Use sudo ntfsfix /dev/sda3 to fix it. ( /dev/sda3 is the device you want to mount )
  • However, when I then try to run sudo mount /dev/sda3 /media/east in terminal, it says “Windows is hibernated, refused to mount.“. So why it can be mounted in GUI interface but cannot do the same thing in command line ?

Switch back to the last working directory in terminal

  • Usage: cd -

Copy and paste in Linux terminal

  • It depends on Linux distributions and terminal applications, but in Ubuntu (eoan), it can be achieved by :
  1. Just simply select the text in terminal for copying.
  2. And middle click for paste.
  • Or use keyboard shortcut: Ctrl+Shift+C for copy and Ctrl+Shift+V for paste.

Use systemctl to manage systemd services

  • Usage:

    • Create a service: (recommend in /etc/systemd/system; below is a common template to use )
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      [Unit]
      Description=start ss-local
      After=network.target
      StartLimitIntervalSec=0

      [Service]
      Type=simple
      Restart=always
      RestartSec=1
      User=username
      ExecStart=/usr/bin/ss-local -c /home/ss.json

      [Install]
      WantedBy=multi-user.target
    • Start a service: sudo systemctl start application for root user ( systemctl --user start application for current user ). application is the service name, namely, application.service.
    • Stop a service: sudo systemctl stop application
    • Restart a service: sudo systemctl restart application
    • To tell systemd to start services automatically at boot, you must enable them: sudo systemctl enable application.service; If you don’t want it afterwards, just replace enable by disable and run the command.
  • Note:

    • sudo systemctl enable application.service command is quite convenient if you want to let a software start at boot.
    • After you modify services, run sudo systemctl daemon-reload to reload them.
    • Usually, in Ubuntu, the package provided unit files go in /lib/systemd/system/ directory, whereas the user provided or any modification to the package provided unit files go in /etc/systemd/system/ directory. But I’m not sure where this application.service is: systemctl --user start application.

Find and Replace in Ubuntu (fd, sd, rg)

  • fd – “A simple, fast and user-friendly alternative to ‘find’”

  • sd – “Intuitive find & replace CLI (sed alternative)”

  • ripgrep(rg) – “ripgrep recursively searches directories for a regex pattern”

  • Find all files then pass them to sd to replace:

    • command: fd -HI -e conf -0 --type file | xargs -0 sd --string-mode 'find_str' 'replace_str'
    • Explanation: fd command finds all the files with .conf suffix and pass them to sd command to replace. -H let fd additionally searches hidden directories and files; -I let fd searches patterns from .gitignore files; -e to specify a certain suffix of files (if don’t use -e then it will find all the files).
    • Note: It’s convenient to use fd 'file_name' to find all files with the given file name.
  • Find all files containing the given string then pass them to sd to replace:

    • command: rg --files-with-matches --fixed-strings 'find_str' | xargs sd --string-mode 'find_str' 'replace_str'
    • Explanation: rg command finds all the files (in the current directory) with ‘find_str’ and pass them to sd command; sd command will replace ‘find_str’ with ‘replace_str’.

Create ~/bin directory and add it to PATH

You can put your own bash files in ~/bin directory, which is easier to manage these bash files than putting them in /usr/bin or /usr/local/bin. Of course, you should add PATH="$PATH:~/bin" to your ~/.bashrc file. After that, if you have a file in ~/bin called fn, then you could simply run fn in the terminal from any directory to execute it.

Grant your bash (shell) file with executable permission

If you create a bash file or just a text file, and you want to execute the comands in it, you have to grant it with executable permission. Simply use chmod 700 file_name to change its permission (700 means owner can read(4), write(2) and execute(1)) .


As I am just a learner, please comment if you find any mistakes or have any advice. :)