Tips and tricks on Ubuntu

Hello everyone!

I'm back, so soon! Because I'm a Windows guys so those daily tips on Ubuntu will be very handy to me and I have to search for them back and forth so I think that I should put them here for someone like me


PS: I will update on this post as long as I found a new useful command

Packaging

  • dpkg-query -L <package_name>: see all files a package installed into your machine
  • dpkg-deb -c <package.deb>: see all files will be installed from a deb package
  • apt-file list <package_name>: see all files in a package NOT installed on your machine (have to have apt-file installed)
  • dpkg -L <package_name>: list all installed files in a package and the location
  • apt-cache policy <package_name>: check if a package is installed into your machine and the candidate sources
  • apt-cache madison <pacakge_name>: query the candidate package from sources
  • dpkg --print-architechture: return the repository package architecture of the current OS (for now, mostly amd64 for 64-bit)
  • dpkg --print-foreign-architectures: will list the support architecture of the repository package (if it supports 2-bit then will return i386)
  • dpkg --add-architecture <architecture>: add the support architecture to the repository package (then run apt-get update)
  • Check python module is installed: python -c "import <module>" then echo $?
There is a good source to find more from this thread.

Add new repository into sources:
  • Add repo key into apt-key: curl <key_url> | sudo apt-key add - # note the hyphen at the end
  • Add the repo into sources.list or add a new list file under sources.list.d or use: sudo add-apt-repository <repo_info>
  • sudo apt-get update: update the sources list before you can install the package from new repo

File

  • touch <filename>: to create a file or you can use vi <filename> then directly edit that file
  • cat <filename> or more <filename> to view file content. To view last number of line using tail -n <filename> or tail -f <filename> to view on the fly update
  • search file with find, here is a specific example: find fine which was modified more than 7 days: find <search_path> -type f -mtime +7 -name <pattern>.
  • search file contains string with grep: grep -rwn <search_path> -e <pattern>
  • understand Linux directory structure to build your setup properly
  • search file then copy them with find <search_path> -name "<pattern>" -exec cp {} <target_path> \;
  • search file exclude multiple folders: find <search_path> -path <exclude_path> -prune -o -path <exclude_path> -prune -o -print
  • When file can't be delete, check the open file processes: fsof +D <file_path> then kill -9 <process_id> to release the file

System

  • Check system mounted partitions: df -Th
  • Check size of folder: du -sh <folder> or with tool ncdu
  • Find where your java is located: readlink -f $(which java) or update-alternatives --list java
  • Control systemd service with systemctl command (need sudo to start/stop service): systemctl start <service_name> or systemctl status <service_name>
  • Check service log with jounralctl -u <service_name>
  • Kill all process by name: kill -9 $(ps -ax | grep 'proc_name' | fgrep -v grep | awk '{ print $1 }')

Network

  • Configure/change IP with this guide
  • Remove host name from ssh known_hosts: ssh-keygen -R <host_name>

Usage


  • Sound setting is 'Dummy Speaker' can't play sound (only with Dell XPS model): restart laptop, enter CMOS bios then disable (uncheck) Audio Enable, save and exit then launch Ubuntu. Restart and do the same to enable Audio again and launch Ubuntu (you might unplug the headphone until Ubuntu fully boots up to let it detects the headphone plug and play)
  • Terminals are not display on screen via external monitors (use extra monitors): by some reasons, when you see your terminal applications are running, but you can select them to display on active screen. Open System Settings => Appearance, select Behaviour tab, check then uncheck Enable workspaces. The terminal screen will move up into your active screen.



Comments