[Linux][System] Learn Linux Command Operations

Check kernal version of Linux

  • uname -a
zn@ubuntu:~$ uname -a
Linux ubuntu 5.4.0-105-generic #119~18.04.1-Ubuntu SMP Tue Mar 8 11:21:24 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
zn@ubuntu:~$ uname --help
Usage: uname [OPTION]...
Print certain system information. With no OPTION, same as -s.
-a, --all print all information, in the following order,
except omit -p and -i if unknown:
-s, --kernel-name print the kernel name
-n, --nodename print the network node hostname
-r, --kernel-release print the kernel release
-v, --kernel-version print the kernel version
-m, --machine print the machine hardware name
-p, --processor print the processor type (non-portable)
-i, --hardware-platform print the hardware platform (non-portable)
-o, --operating-system print the operating system
--help display this help and exit
--version output version information and exit

Check IP address

  • ip addr
  • ifconfig (apt install net-tool)
zn@ubuntu:~$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:32:c3:a7 brd ff:ff:ff:ff:ff:ff
inet brd scope global dynamic noprefixroute ens33
valid_lft 1506sec preferred_lft 1506sec
inet6 fe80::a506:7e6f:bf8d:e1d8/64 scope link noprefixroute
valid_lft forever preferred_lft forever

Check free disk space / Usage

  • df -ah
zn@ubuntu:~$ df -ah
Filesystem Size Used Avail Use% Mounted on
sysfs 0 0 0 - /sys
proc 0 0 0 - /proc
udev 1.9G 0 1.9G 0% /dev
devpts 0 0 0 - /dev/pts
tmpfs 391M 2.0M 389M 1% /run
/dev/sda1 20G 5.3G 14G 29% /
securityfs 0 0 0 - /sys/kernel/security
tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup

Check folder/file size

  • du -sh <targetfolderorfile>
zn@ubuntu:~/Desktop/work/20220329$ du -sh tmp
4.0K tmp

Manage service on system / start / stop / restart

  • systemctl status <servicename>
  • service <servicename> status
zn@ubuntu:~$ service --help
Usage: service < option > | --status-all | [ service_name [ command | --full-restart ] ]
zn@ubuntu:~$ systemctl --help
systemctl [OPTIONS...] {COMMAND} ...
Query or send control commands to the systemd manager.-h --help Show this help
--version Show package version
--system Connect to system manager
--user Connect to user service manager
-H --host=[USER@]HOST
Operate on remote host
-M --machine=CONTAINER
Operate on local container
-t --type=TYPE List units of a particular type
--state=STATE List units with particular LOAD or SUB or ACTIVE state
-p --property=NAME Show only properties by this name
-a --all Show all properties/all units currently in memory,
including dead/empty ones. To list all units installed on
the system, use the 'list-unit-files' command instead.
--failed Same as --state=failed
-l --full Don't ellipsize unit names on output
-r --recursive Show unit list of host and local containers
--reverse Show reverse dependencies with 'list-dependencies'
--job-mode=MODE Specify how to deal with already queued jobs, when
queueing a new job
--show-types When showing sockets, explicitly show their type
--value When showing properties, only print the value
-i --ignore-inhibitors
zn@ubuntu:~/Desktop/work/20220329$ service --status-all
[ + ] acpid
[ + ] alsa-utils
[ - ] anacron
[ + ] apparmor
[ + ] apport
[ + ] atd
[ + ] avahi-daemon
[ - ] bluetooth
[ - ] console-setup.sh
[ + ] cron
[ + ] cups
[ + ] cups-browsed

Check open ports on machine

  • ss -tupln (sudo show more content)
  • netstat -tupln (sudo show more content) (apt install net-tools)
zn@ubuntu:~$ ss -tupln
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
udp UNCONN 0 0*
udp UNCONN 0 0*
udp UNCONN 0 0*
udp UNCONN 0 0*
udp UNCONN 0 0*
udp UNCONN 0 0 [::]:39352 [::]:*
udp UNCONN 0 0 [::]:5353 [::]:*
tcp LISTEN 0 128*
tcp LISTEN 0 5*
tcp LISTEN 0 5 [::1]:631 [::]:*
zn@ubuntu:~$ ss --help
Usage: ss [ OPTIONS ]
-h, --help this message
-V, --version output version information
-n, --numeric don't resolve service names
-r, --resolve resolve host names
-a, --all display all sockets
-l, --listening display listening sockets
-o, --options show timer information
-e, --extended show detailed socket information
-m, --memory show socket memory usage
-p, --processes show process using socket
-i, --info show internal TCP information
-s, --summary show socket usage summary
-b, --bpf show bpf filter socket information
-E, --events continually display sockets as they are destroyed
-Z, --context display process SELinux security contexts
-z, --contexts display process and socket SELinux security contexts
-N, --net switch to the specified network namespace name
-4, --ipv4 display only IP version 4 sockets
-6, --ipv6 display only IP version 6 sockets
-0, --packet display PACKET sockets
-t, --tcp display only TCP sockets
-S, --sctp display only SCTP sockets
-u, --udp display only UDP sockets
-d, --dccp display only DCCP sockets
-w, --raw display only RAW sockets
-x, --unix display only Unix domain sockets
--vsock display only vsock sockets
-f, --family=FAMILY display sockets of type FAMILY
FAMILY := {inet|inet6|link|unix|netlink|vsock|help}
-K, --kill forcibly close sockets, display what was closed
-H, --no-header Suppress header line

Check CPU usage for a process

  • ps aux | grep <processname>
  • top (simular to task manager in Windows)
  • htop (sudo apt install htop)
zn@ubuntu:~$ ps --help sUsage:
ps [options]
Basic options:
-A, -e all processes
-a all with tty, except session leaders
a all with tty, including other users
-d all except session leaders
-N, --deselect negate selection
r only running processes
T all processes on this terminal
x processes without controlling ttys
For more details see ps(1).
zn@ubuntu:~$ toptop - 04:30:42 up  1:36,  1 user,  load average: 0.01, 0.01, 0.00
Tasks: 308 total, 1 running, 237 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.3 us, 0.7 sy, 0.0 ni, 99.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 3994684 total, 1000840 free, 1266408 used, 1727436 buff/cache
KiB Swap: 969960 total, 969960 free, 0 used. 2454372 avail Mem
4413 zn 20 0 443292 64440 38380 S 1.0 1.6 0:05.67 Xorg
4569 zn 20 0 3401748 186048 81712 S 1.0 4.7 0:12.64 gnome-shell
4887 zn 20 0 796952 38004 28068 S 1.0 1.0 0:03.39 gnome-terminal-
5347 zn 20 0 44216 4028 3284 R 0.3 0.1 0:00.11 top
1 root 20 0 159972 9264 6712 S 0.0 0.2 0:02.20 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_gp

Mount a USB

  • mount <target> /mnt
zn@ubuntu:~$ mount -hUsage:
mount [-lhV]
mount -a [options]
mount [options] [--source] <source> | [--target] <directory>
mount [options] <source> <directory>
mount <operation> <mountpoint> [<target>]
Mount a filesystem.

Auto mount on boot

  • nano /etc/fstab (config for mounting drives while boot)

Infos for command / on-line reference manuals

  • man <commandtocheck>
zn@ubuntu:~$ man man
MAN(1) Manual pager utils MAN(1)
man - an interface to the on-line reference manuals
man [-C file] [-d] [-D] [--warnings[=warnings]] [-R encod‐
ing] [-L locale] [-m system[,...]] [-M path] [-S list] [-e
extension] [-i|-I] [--regex|--wildcard] [--names-only] [-a]
[-u] [--no-subpages] [-P pager] [-r prompt] [-7] [-E encod‐
ing] [--no-hyphenation] [--no-justification] [-p string]
[-t] [-T[device]] [-H[browser]] [-X[dpi]] [-Z] [[section]
page[.section] ...] ...
man -k [apropos options] regexp ...

Check free memory on machine

  • free -h
  • vmstat
  • top
zn@ubuntu:~/Desktop/work/20220329$ free -h
total used free shared buff/cache available
Mem: 3.8G 1.3G 743M 16M 1.8G 2.2G
Swap: 947M 0B 947M
zn@ubuntu:~$ vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 0 911392 167532 1567788 0 0 107 46 65 119 1 0 99 0 0

Change permission for file

  • chmod 777 <target>
  • chmod +rwx <target>

Linux Directory Commands

  • pwd
  • ls <targetDir?>
  • cd <targetDir>
  • mkdir <targetDir>
  • rmdir <targetDir>

Redirection operator / Write output to file

  • echo “hi-01” > tmp.txt
  • echo “hi-02” >> tmp.txt
zn@ubuntu:~/Desktop/work/20220329$ echo "hi-01" > tmp.txt
zn@ubuntu:~/Desktop/work/20220329$ echo "hi-02" >> tmp.txt
zn@ubuntu:~/Desktop/work/20220329$ cat tmp.txt
  • tar -cvzf filename.tar.gz
  • (c: create archive, x: extract, v: verbose, f: file)

Copy a file

  • cp <source> <destination>

Rename / Move a file in Linux

  • mv <oldname> <newname>

Remove file/ directory

  • rm -rf <target>
  • rmdir <directory name>

Terminate a running process

  • kill <PID>

Find the difference in two files

  • diff <file1> <file2>
zn@ubuntu:~/Desktop/work/20220329$ echo "hi-02" >> tmp2.txt
zn@ubuntu:~/Desktop/work/20220329$ diff tmp.txt tmp2.txt
< hi-01
> hi

Echo all files in here with names including “tmp”

  • $ for i in *tmp*; do echo $i; done
zn@ubuntu:~/Desktop/work/20220329$ for i in *tmp*; do echo $i; done

Create a text file without opening it

  • touch <filename>

Schedule a task in Linux

  • cron
  • at (sudo apt install at)
CRON(8)              System Manager's Manual              CRON(8)NAME
cron - daemon to execute scheduled commands (Vixie Cron)
cron [-f] [-l] [-L loglevel]
cron is started automatically from /etc/init.d on entering
multi-user runlevels.
-f Stay in foreground mode, don't daemonize.
-l Enable LSB compliant names for /etc/cron.d files.
This setting, however, does not affect the parsing
of files under /etc/cron.hourly, /etc/cron.daily,
/etc/cron.weekly or /etc/cron.monthly.
zn@ubuntu:~/Desktop/work/20220329$ echo "hi now" > tmp3.txt | at -m 05:17
warning: commands will be executed using /bin/sh
job 2 at Tue Mar 29 05:17:00 2022
zn@ubuntu:~/Desktop/work/20220329$ cat tmp3.txt
hi now

Get current time

  • date
zn@ubuntu:~/Desktop/work/20220329$ date
Tue Mar 29 05:13:59 PDT 2022

Identify which shell you are using

  • echo $SHELL
zn@ubuntu:~/Desktop/work/20220329$ echo $SHELL

Login to another system in your network

  • ssh <username>@<ip address>
zn@ubuntu:~/Desktop/work/20220329$ ssh
usage: ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
[-D [bind_address:]port] [-E log_file] [-e escape_char]
[-F configfile] [-I pkcs11] [-i identity_file]
[-J [user@]host[:port]] [-L address] [-l login_name] [-m mac_spec]
[-O ctl_cmd] [-o option] [-p port] [-Q query_option] [-R address]
[-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]]
[user@]hostname [command]

Show sort lines in a file

  • sort <filename>
zn@ubuntu:~/Desktop/work/20220329$ echo "hi-00" >> tmp.txt
zn@ubuntu:~/Desktop/work/20220329$ sort tmp.txt
zn@ubuntu:~/Desktop/work/20220329$ cat tmp.txt

Check username / Check usergroup

  • whoami
  • id
zn@ubuntu:~/Desktop/work/20220329$ whoami
zn@ubuntu:~/Desktop/work/20220329$ id
uid=1000(zn) gid=1000(zn) groups=1000(zn),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),116(lpadmin),126(sambashare)

Run 1.sh, if success then run 2.sh

  • 1.sh && 2.sh

Cut command & Pipeline example

  • cat tmp4.txt | cut -d: -f2
zn@ubuntu:~/Desktop/work/20220329$ nano tmp4.txt
zn@ubuntu:~/Desktop/work/20220329$ cat tmp4.txt
zn@ubuntu:~/Desktop/work/20220329$ cat tmp4.txt | cut -d: -f2

Print Uniq lines

  • cat <targetname> | uniq

Count file charaters, lines

  • wc <targetfile>
zn@ubuntu:~/Desktop/work/20220329$ wc tmp4.txt
3 3 34 tmp4.txt

Grep command example

  • cat <filename> | grep <target>
  • GREP = Global Regular Expression Print
zn@ubuntu:~/Desktop/work/20220329$ cat tmp4.txt | grep name

Start bash as root

  • sudo -i
zn@ubuntu:~$ ls -l
total 32
drwxr-xr-x 3 zn zn 4096 Mar 29 04:20 Desktop
drwxr-xr-x 2 zn zn 4096 Mar 29 03:08 Documents
drwxr-xr-x 2 zn zn 4096 Mar 29 03:08 Downloads
drwxr-xr-x 2 zn zn 4096 Mar 29 03:08 Music
drwxr-xr-x 2 zn zn 4096 Mar 29 03:08 Pictures
drwxr-xr-x 2 zn zn 4096 Mar 29 03:08 Public
drwxr-xr-x 2 zn zn 4096 Mar 29 03:08 Templates
drwxr-xr-x 2 zn zn 4096 Mar 29 03:08 Videos
zn@ubuntu:~$ sudo -i
root@ubuntu:~# ls -l
total 4
drwx------ 6 root root 4096 Mar 29 05:36 snap




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store