Skip to main content

CLI - Linux

Command Line Interface

Shell

Shell list

cat /etc/shells

Change shell

chsh -s <shell> [<user>]

or

usermod -s <shell> <user>

Open file manager from CLI

xdg-open <path>

Indicate command type

Type: function, builtin, or file

# -a: display all locations
type [-a] <command>

Preserve file timestamp after edit (bash)

vi-preserve-time () {
for file in "$@"; do
local mtime=$(stat -c %y "$file")
vi "$file"
touch -d "$mtime" "$file"
done
}

Hardware

CommandDescription
lsusbList USB devices
lspciList PCI devices
lspci -nnList PCI devices and devices code
lspci -kList PCI devices and kernel driver
cat /proc/cpuinfoShow CPU info
cat /proc/meminfoShow memory info

Mount image

mount -o loop <image.iso> <directory>

File system

ACL

Ref: Red Hat - Setting Access ACLs

File permission

Only change directory permission

chmod <permission> $(find -type d)

Only change file permission

chmod <permission> $(find -type f)

Change file owner and group

chown { <owner>[:<group>] | --reference <file> } <file>

Change file permission to 644, directory to 755

OptionDescription
-RRecursive
-h | --no-dereferenceChange symbolic links instead of referenced file
chmod [-R] u=rw,go=r,a+X <path>

or

chmod [-R] a=r+X,u+w <path>

File attribute

Ref: chattr manual

AttributeDescription
aAppend only
ANo atime updates
cCompressed
iImmutable
sSecure deletion
SSynchronous updates
uUndeletable

List attribute

lsattr

Change attribute

chattr {+|-}<attribute> <file>

List open files

lsof <path>

Manual / Help

Show manual

man [<page>]

Search manual

man -k <keyword>
apropos <keyword>

DNS

vi /etc/resolv.conf

Sharing desktop

Disable sharing desktop encryption (Ubuntu)

Ref: Ask Ubuntu

dconf-editor
# org > gnome > desktop > remote-accessand > require-encryption
gsettings set org.gnome.Vino require-encryption false

File

Copy

cp [<option>] <source> <destination>
OptionDescription
-a | --archiveArchive, same as -dR --preserve=all
-p | --preserve[=<attributes>]Preserve mode, ownership, timestamps (Default: mode,ownership,timestamps)
-R | -r | --recursiveCopy directories recursively

OpenJDK (Java)

List installed JVM

update-java-alternatives --list

Switch JVM

update-java-alternatives --set <JVM name>

Show Linux distribution / version

cat /etc/*-release
cat /proc/version
uname -a

# For systemd
hostnamectl

# For Debian
lsb_release -a

Last login

Ref: Enable/Disable Last Login Message

Suppress Last Login message

touch ~/.hushlogin

Clear last login log

> /var/log/lastlog

Disable logging last login

chattr +i /var/log/lastlog

Identify processes using files or sockets

fuser <option> <file>
OptionDescription
-v, --verboseVerbose
-k, --killKill processes accessing the file
{ -c | -m | --mount } { <mount point> | <device> }Mount point or device

Watch

Execute command periodically

# Default: 2 seconds
watch [-n <seconds>] <command>

FHS (File system Hierarchy Standard) / File system Structure

Ref: The FHS 3.0 Specification

man hier

Using dash "-" as path argument

Ref: TLDP Advanced Bash-Scripting Guide

使用 "-" 做為檔案路徑參數時,一般有以下用途

  • Redirection from stdin / to stdout
  • cd - change to previous working directory

true

:

is equivalent to

true