Top 53 useful Unix commands

Allen Kim
5 min readOct 4, 2023
Photo by Gabriel Heinzer on Unsplash

It’s better to know basic Unix commands. It allows you to navigate your Unix or Linux system, confirm current system status and manage files or directories.

whoami : who am I? display effective user id

$ whoami
allenkim

man : manual, show manual

$ man whoami

clear : clear the terminal screen

$ clear

pwd : print working directory

$ pwd
/Users/allenkim

ls : list directory contents

$ ls -al
total 9632
drwxr-xr-x+ 75 allenkim staff 2400 4 Oct 16:18 .
drwxr-xr-x 5 root admin 160 3 Aug 13:58 ..

cd : q change directory

$ cd projects
$ pwd
/Users/allenkim/projects

mkdir : make directories

$ mkdir foo
$ cd foo

rmdir : remove directories

$ rmdir foo

touch : change file access and modification times

$ touch foo.txt
$ ls foo.txt
foo.txt

open : open files and directories

$ open https://www.google.com # will open a web browser

mv : move files

 $ mv -f foo bar

cp : coy files

$ cp *.txt /tmp

head : display first lines of a file

$ head -n 500 foo # To display the first 500 lines of the file foo:

tail : display the last part of a file

$ tail -n 500 foo # To display the last 500 lines of the file foo:

date : display or set date and time

$ date
Wed 4 Oct 2023 16:30:41 EDT
$ date "+DATE: %Y-%m-%d"
DATE: 2023-10-04

cat : concatenate and print files

$ cat file1

less : opposite of more

$ less foo.txt

echo : write arguments to the standard output

$ echo "Welcome to Linux"
Welcome to Linux
$ echo $HOME
/Users/allenkim

wc : word(line, character, and byte) count

$ wc foo.txt
55 221 1420 foo.txt

diff : differential file and directory comparator

$ diff foo.txt bar.txt
1c1
< Hello
---
> World

find : walk a file hierarchy

$ $ find . -name "*.txt" -print

grep : file pattern searcher

$ grep command foo.txt
Useful 50 Unix command

du : display disk usage statistics

$ du -hs ~/projects
29G projects

df : display free disk space

$ df -h
Filesystem Size Used Avail Use% Mounted on
udev 47G 0 47G 0% /dev
tmpfs 9.4G 170M 9.2G 2% /run
/dev/sda2 1.7T 690G 963G 42% /
tmpfs 47G 0 47G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 47G 0 47G 0% /sys/fs/cgroup
/dev/sda1 511M 3.6M 508M 1% /boot/efi

history : list your previous comman list

$ history
1125 grep
1126 man grep
1127 man grep
1128 grep command foo.txt
1129 du
1130 man du
1131 du -ah
1132 man du
1133 du -hs ~
1134 du -hs projects
1135 man history

ps : process status

$ ps aux | grep -i firefox
allenkim 14307 0.0 0.0 408636160 1456 s010 S+ 4:49pm 0:00.00 grep -i firefox

top : display sorted information about processes

$ top
$ top -stats pid,command,cpu,th,pstate,time

kill : terminate or signal a process

$ kill 142 157
$ kill -9 142

killall : kill processes by name

$ killall firefox

gzip : compression/decompression tool using Lempel-Ziv coding (LZ77)

$ gzip foo.txt # compress
$ ls foo.*
foo.txt.gz
$ gzip -d foo.txt.gz # decompress
$ ls foo.*
foo.txt

jobs : list the jobs that you are running in the background and in the foreground.

$ top # then press Ctrl-Z
zsh: suspended top
$ jobs
[1] + suspended top

fg : moves a background job in the current shell environment into the foreground.

$ jobs
[1] + suspended man gzip
$ fg %1
[1] + continued man gzip

tar : manipulate tape archives

$ tar -cvzf file.tar.gz /path/to/dir/ # compress an entire directory
$ tar -cvzf file.tar.gz dir1 dir2 dir3 # compress multiple directories
$ tar -cvzf file.tar.gz /path/to/filename

xargs : construct argument list(s) and execute utility

$ echo 'one two three' | xargs mkdir
$ ls
one two three

ln : link files

$ mkdir bar baz
$ ln -s bar foo
$ la -al
drwxr-xr-x 2 allenkim staff 64 4 Oct 17:09 bar
drwxr-xr-x 2 allenkim staff 64 4 Oct 17:09 baz
lrwxr-xr-x 1 allenkim staff 3 4 Oct 17:09 foo -> bar

who : display who is on the system

$ who
allenkim console 3 Aug 14:10
allenkim ttys010 4 Oct 12:23
allenkim ttys011 4 Oct 16:44

su : substitute user identity

$ su jane
Password:
$ whoami
jane

sudo : execute a command as another user

$ cat /etc/sudoers
visudo: /etc/sudoers: Permission denied
$ sudo cat /etc/sudoers
#
# Sample /etc/sudoers file.
#
...

passwd : change password

$ passwd
Changing password for allenkim.
Old Password:
New Password:
Retype New Password:

chown : change owner of a file and group

$ chown allenkim foo.txt
$ ls -l foo.txt
-rw-r--r-- 1 allenkim staff 810 4 Oct 17:02 foo.txt

chmod : change file modes or Access Control Lists

$ ls -l foo.txt
-rw-r--r-- 1 allenkim staff 810 4 Oct 17:02 foo.txt
$ chmod 600 foo.txt
$ ls -l foo.txt
-rw------- 1 allenkim staff 810 4 Oct 17:02 foo.txt

wget : The non-interactive network downloader.

 $ wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.17.2.tar.xz
--2023-10-04 17:27:02-- https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.17.2.tar.xz
Resolving cdn.kernel.org (cdn.kernel.org)... 151.101.137.176
Connecting to cdn.kernel.org (cdn.kernel.org)|151.101.137.176|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 102167060 (97M) [application/x-xz]
Saving to: ‘linux-4.17.2.tar.xz’

linux-4.17.2.tar.xz 100%[=====================================>] 97.43M 50.4MB/s in 1.9s

2023-10-04 17:27:04 (50.4 MB/s) - ‘linux-4.17.2.tar.xz’ saved [102167060/102167060]

$ ls *.tar.xz
linux-4.17.2.tar.xz

scp : OpenSSH secure file copy

$ scp ~/rebels.txt dvader@deathstar.com:~/revenge
$ scp -r dvader@deathstar.com:~/revenge ~/revenge

ssh : OpenSSH remote login client

$ ssh allen@my.remote.server.com
admin@my.remote.server.com's password: xxxyyyzzz
Activate the web console with: systemctl enable --now cockpit.socket
...
Last login: Wed Feb 15 19:49:51 2023 from 10.251.21.20

sed : stream editor

$sed 's/unix/linux/' foo.txt
$sed 's/unix/linux/g' foo.txt

awk : pattern-directed scanning and processing language

$cat > employee.txt 
ajay manager account 45000
sunil clerk account 25000
varun manager sales 50000
amit manager account 47000
tarun peon sales 15000
deepak clerk sales 23000
sunil peon sales 13000
satvik director purchase 80000
$ awk '/manager/ {print}' employee.txt
ajay manager account 45000
varun manager sales 50000
amit manager account 47000
$ awk '{print $1,$4}' employee.txt
ajay 45000
sunil 25000
varun 50000
amit 47000
tarun 15000
deepak 23000
sunil 13000
satvik 80000

unzip : list, test and extract compressed files in a ZIP archive

$ # To unzip master.zip using to a /tmp/data/ directory:
$ unzip -d /tmp/data/ master.zip

ifconfig : configure network interface parameters

$ ifconfig
$ ifconfig -a
$ ifconfig -a eth0
$ sudo ifconfig eth1 up
$ sudo ifconfig eth1 down

uname : display information about the system

$ uname
Darwin

which : locate a program file in the user’s path

$ which perl
/usr/bin/perl

ping : send ICMP ECHO_REQUEST packets to network hosts

$ ping www.google.com
PING www.google.com (172.217.13.100): 56 data bytes
64 bytes from 172.217.13.100: icmp_seq=0 ttl=117 time=18.818 ms
64 bytes from 172.217.13.100: icmp_seq=1 ttl=117 time=18.708 ms
^C
--- www.google.com ping statistics ---
2 packets transmitted, 2 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 18.708/18.763/18.818/0.055 ms

telnet : connect to remote server over a TCP/IP network port 23

$ telnet www.example.com 1723

lsof : list open files

$ sudo lsof -i :8000

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 24922 cody 79u IPv4 0x4d990156241107c9 0t0 TCP localhost:irdmi->localhost:60453 (ESTABLISHED)

--

--