Linux General Commands - yenbohuang/techNotes GitHub Wiki

Check Ubuntu version

lsb_release -a

Environment variables

Preferred

  • /etc/profile.d

System

  • /etc/profile
  • /etc/default/locale
  • /etc/bash.bashrc
  • /etc/sudoers
  • /etc/environment

See details on https://help.ubuntu.com/community/EnvironmentVariables

/etc/profile.d/*.sh

Files with the .sh extension in the /etc/profile.d directory get executed whenever a bash login shell is entered (e.g. when logging in from the console or over ssh), as well as by the DisplayManager when the desktop session loads.

You can for instance create the file /etc/profile.d/myenvvars.sh and set variables like this:

export JAVA_HOME=/usr/lib/jvm/jdk1.7.0
export PATH=$PATH:$JAVA_HOME/bin

See details on https://help.ubuntu.com/community/EnvironmentVariables#A.2Fetc.2Fenvironment

Upgrading from Ubuntu 14.04 LTS

To upgrade on a desktop system:

  • Open the "Software & Updates" Setting in Systemsettings.
  • Select the 3rd Tab called "Updates".
  • Set the "Notify me of a new Ubuntu version" dropdown menu to "For any new version".
  • Press Alt+F2 and type in "update-manager" (without the quotes) into the command box.
  • Update Manager should open up and tell you: New distribution release '14.10' is available.
  • Click Upgrade and follow the on-screen instructions.

To upgrade on a server system:

  • Install the update-manager-core package if it is not already installed.
  • Make sure the /etc/update-manager/release-upgrades is set to normal.
  • Launch the upgrade tool with the command sudo do-release-upgrade.
  • Follow the on-screen instructions.

Note that the server upgrade will use GNU screen and automatically re-attach in case of dropped connection problems. There are no offline upgrade options for Ubuntu Desktop and Ubuntu Server. Please ensure you have network connectivity to one of the official mirrors or to a locally accessible mirror and follow the instructions above.

See details on https://wiki.ubuntu.com/UtopicUnicorn/ReleaseNotes

Decompress files

tar -vxjf /mnt/mydownloads/archive.tar.bz2
tar -vxzf /mnt/mydownloads/archive.tar.gz
unzip archive.zip -d /home/yenbo.huang/folder

See details on:

Enable Time Synchronization

sudo apt install chrony

https://help.ubuntu.com/lts/serverguide/NTP.html

Set Timezone

sudo timedatectl set-timezone your_time_zone

https://linuxize.com/post/how-to-set-or-change-timezone-on-ubuntu-18-04/

Generate UUID

uuidgen

Calculate MD5

md5sum <filename>
echo <string> | md5sum

Finding files

locate <filename>
find <path> -name <filename>
which <filename>
whereis <filename>

See details on https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/4/html/Step_by_Step_Guide/s1-managing-locating.html

Delete files with extension recursively

find <path> -type f -name '*.log' -delete

See details on http://unix.stackexchange.com/questions/116389/recursively-delete-all-files-with-a-given-extension

Copy files with specific extension and retain folder structure

cp --parents `find -name \*.properties` /e/temp/props/

See details on https://stackoverflow.com/questions/15617016/copy-all-files-with-a-certain-extension-from-all-subdirectories

Running sudo with environment variables

sudo <key1>=<value1> <key2>=<value2> .... <command>

See details on http://askubuntu.com/questions/57915/environment-variables-when-run-with-sudo

Using systemd

This is an example for docker daemon in /etc/systemd/system/docker.service.d/override.conf:

[Service]
Environment="CONSUL_HTTP_TOKEN=<ACL token>"
ExecStart=
ExecStart=/usr/bin/docker daemon --insecure-registry <url:port> -H fd:// -s overlay

Run the following commands after changes:

#!/bin/sh

sudo systemctl daemon-reload
sudo systemctl enable docker
sudo systemctl start docker

See details on https://www.freedesktop.org/software/systemd/man/systemd.service.html

Check Disk Usages

  • To list top 10 directories eating disk space.
du -a ~/ | sort -n -r | head -n 10
  • Display the amount of disk space used by the specified files and for each sub-directory.
du

See details on:

Output message on both console and log file

some-command | tee -a file

See details on http://stackoverflow.com/questions/418896/how-to-redirect-output-to-a-file-and-stdout

Redirect stderr to stdout

some-command 2>&1

See details on http://superuser.com/questions/71428/what-does-21-do-in-command-line

Check owner of a process

ps -o user= -p <pid>

See details on https://unix.stackexchange.com/questions/284934/return-owner-of-process-given-pid/284938

⚠️ **GitHub.com Fallback** ⚠️