Basic Linux Commands
The following file people.txt is used throughout this tutorial:
firstName lastName age city ID department
Thomas Shelby 30 Rio 400 IT
Omega Night 45 Ontario 600 Design
Wood Tinker 54 Lisbon N/A IT
Giorgos Georgiou 35 London 300 Data
Timmy Turner 32 Berlin N/A Engineering
man
This command shows manual of other commands. Simply run man <command> to show the manual.
sudo
The sudo command allows you to run programs with the security previleges of another user (by default, as the superuser).
pwd (Print Working Directory)
This command prints an absolute path of the current working directory.
ls (List current directory)
This command lists all files and folders in the current directory. This command also accepts flags that may show different results.
-
List all files including hidden files.
ls -a -
List all files and their info
ls -l -
You can even combine flags
`ls -la
cd (Change Directory)
This command is used for changing the current working directory.
mv (Move)
This command moves files from a directory to another. This command can also be used to change a file or directory's name
cp (Copy)
This command copies files from a directory to another.
rm (Remove)
This command removes files or directories. Please note that removed objects are not stored in recycle bin and gone for good.
deleting a file
rm file.txt
recursive deletion (used for deleting a directory)
rm -r file.txt
touch (Create new file)
This command creates a new file.
touch file.txt
mkdir (Make Directory)
This command creates a new directory.
mkdir dir
echo
This command displays a line of text.
echo 'hello world'
cat (Print file content)
This command concatenate files and prints to stdout.
cat people.txt
head & tail
This command outputs the first and last part of the file respectively.
Print first 2 lines of the file
cat people.txt | head 2
Print last 2 lines of the file
cat people.txt | tail 2
Piping (|)
The Unix/Linux systems allow stdout of a command to be connected to stdin of another command. You can make it do so by using the pipe character |.
cat people.txt | head 2
grep
This command prints lines that match the given pattern.
Prints lines that contain 'IT'.
cat people.txt | grep IT
find
This command searches for files in the directory hierarchy.
-
Find files that matches the given pattern
find -name PATTERN
which
WHICH is used for locating the executable file associated wit the given command.
which python
alias
ALIAS allows you to create shortcuts for repetitive long commands.
Syntax
alias [option] [name]='[value]'
Example: Make an alias
alias c='clear'
Example: List all aliases
$alias
-='cd -'
...=../..
....=../../..
.....=../../../..
......=../../../../..
1='cd -1'
2='cd -2'
3='cd -3'
4='cd -4'
5='cd -5'
6='cd -6'
7='cd -7'
8='cd -8'
9='cd -9'
_='sudo '
afind='ack -il'
diff='diff --color'
egrep='egrep --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn,.idea,.tox}'
fgrep='fgrep --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn,.idea,.tox}'
g=git
ga='git add'
gaa='git add --all'
gam='git am'
gama='git am --abort'
...
Make alias permanent
Alias lives in the active shell session. When the shell is closed, the alias is also gone.
To make aliases permanent, you can add them to .bashrc.
export
This command is shell dependent. This may not work on other shells rather than Bash.
Export is Bash shell built-in commands. It marks an environment variables to be exported to child-processes.
Example: View all export variables
export
Example: Set PATH environment variable
export PATH=$PATH:/your/path
cut
push & pop
chmod (Change mode)
CHMOD controls who can access files, search directories, and run scripts.
When listing files in Linux, you may see the following output.
drwxrwxr-x 4 siraphob siraphob 4096 Jun 18 22:22 backend
drwxrwxr-x 6 siraphob siraphob 4096 Jun 18 22:22 developers-handbook
drwxrwxr-x 3 siraphob siraphob 4096 Jun 18 22:22 frontend
drwxrwxr-x 9 siraphob siraphob 4096 Jun 18 22:22 infra
drwxrwxr-x 2 siraphob siraphob 4096 Jun 18 22:22 internship-2022
-rw-rw-r-- 1 siraphob siraphob 2164 Jun 18 22:22 README.md
drwxrwxr-x 2 siraphob siraphob 4096 Jun 18 22:22 soilcrete
drwxrwxr-x 4 siraphob siraphob 4096 Jun 18 22:22 training
drwxrwxr-x 4 siraphob siraphob 4096 Jun 18 22:22 uploads
drwxrwxr-x 3 siraphob siraphob 4096 Jun 18 22:22 uxui
The 10 characters of each file indicates its type and permissions.
The first characters indentifies the file type. If it's a dash (-) then it's a file. If it is the letter d then it's a directory.
The next 9 characters represents the permissions.
- The first 3 characters show the permissions for the user who own the file. (user permissions).
- The middle 3 characters show the permissions for the members of the file group. (group permissions).
- The last 3 characters show the permissions for anyone not in the first two categories (other permissions).
There are 3 letters that represent permissions.
ris for Read permissionwis for Write permissionxis for Execute permission
df (Disk Free)
DF is used to display the amount of available disk space for file systems.
$ df -h
Filesystem Size Used Avail Use% Mounted on
udev 16G 0 16G 0% /dev
tmpfs 3.1G 2.4M 3.1G 1% /run
/dev/nvme0n1p5 48G 26G 21G 56% /
tmpfs 16G 13M 16G 1% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 16G 0 16G 0% /sys/fs/cgroup
/dev/loop0 128K 128K 0 100% /snap/bare/5
/dev/loop1 56M 56M 0 100% /snap/core18/2409
/dev/loop2 249M 249M 0 100% /snap/gnome-3-38-2004/99
/dev/loop3 255M 255M 0 100% /snap/gnome-3-38-2004/106
/dev/loop4 66M 66M 0 100% /snap/gtk-common-themes/1519
/dev/loop5 62M 62M 0 100% /snap/core20/1518
/dev/loop6 82M 82M 0 100% /snap/gtk-common-themes/1534
/dev/loop7 62M 62M 0 100% /snap/core20/1328
/dev/loop8 321M 321M 0 100% /snap/telegram-desktop/3975
/dev/nvme0n1p1 96M 46M 51M 48% /boot/efi
/dev/loop9 296M 296M 0 100% /snap/vlc/2344
/dev/loop10 55M 55M 0 100% /snap/snap-store/558
/dev/loop11 44M 44M 0 100% /snap/snapd/14978
/dev/loop12 47M 47M 0 100% /snap/snapd/16010
tmpfs 3.1G 72K 3.1G 1% /run/user/1000
du (Disk Usage)
How much disk space are being used by these files.
$ du -hs
13G /home/siraphob
tar
TAR is used to create a .tar.gz or .tgz archive files, also called tarballs.
- Compress an entrie directory into an archive
tar -czvf archive.tar.gz /path/to/dir-or-file
# c - create an archive
# z - compress an archive with gzip
# v - display progress in the terminal
# f - allows you to specify the file name of the archive
- Extract a
.tar.gzarchive.
tar -xvzf archive.tar.gz
# x - extract files
# v - verbose, print the file names as they are extracted
# z - the file is a `gzipped` file
# f - use the following `tar` archive for the operation
history
HISTORY shows a list of commands used in the terminal session.
kill
KILL is used for terminating processes manually. KILL sends a signal to a process which terminates it.
- Show all available signals
$ kill -l
HUP INT QUIT ILL TRAP ABRT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM STKFLT CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH POLL PWR SYS
- kill a process
kill -s <signal> <pid>
To see PID of processes, use
pidof <program-name>.
ping
PING is used to ping the target server.
$ ping google.com
PING google.com(kul08s10-in-x0e.1e100.net (2404:6800:4001:80e::200e)) 56 data bytes
64 bytes from kul08s10-in-x0e.1e100.net (2404:6800:4001:80e::200e): icmp_seq=1 ttl=115 time=51.8 ms
64 bytes from kul08s10-in-x0e.1e100.net (2404:6800:4001:80e::200e): icmp_seq=2 ttl=115 time=39.8 ms
64 bytes from kul08s10-in-x0e.1e100.net (2404:6800:4001:80e::200e): icmp_seq=3 ttl=115 time=39.6 ms
64 bytes from kul08s10-in-x0e.1e100.net (2404:6800:4001:80e::200e): icmp_seq=4 ttl=115 time=39.3 ms
^C
--- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 39.279/42.634/51.835/5.315 ms
fg (Foreground)
FG is a commadn that moves a background process on the current shell to the foreground.
To simulate and sending a job to the background, we'll invoke the PING command.
ping google.com
After that, press Ctrl+Z to suspend the job and send it to the background.
To list all jobs, use the following command.
$ jobs -l
[1] + 9850 suspended ping google.com
You can invoke the background process back to the foreground by referencing them using the following command.
fg %<job-id>
In this case, it'll be
fg %1
[1] + 9850 continued ping google.com
64 bytes from kul08s10-in-x0e.1e100.net (2404:6800:4001:80e::200e): icmp_seq=3 ttl=115 time=75.4 ms
64 bytes from kul08s10-in-x0e.1e100.net (2404:6800:4001:80e::200e): icmp_seq=4 ttl=115 time=41.8 ms
64 bytes from kul08s10-in-x0e.1e100.net (2404:6800:4001:80e::200e): icmp_seq=5 ttl=115 time=40.8 ms
bg (Background)
BG runs jobs in the background. Continues a suspended job in the background.
Example
ping google.com- Send it to background using
Ctrl+Z. - Continue the job in the background using
bg
curl
CURL is a command line tool that allows to transfer data across the network.
It supports lots of protocols out of the box, including HTTP, HTTPS, FTP, FTPS, SFTP, IMAP, SMTP, POP3, and many more.
- Perform a HTTP GET request
curl https://httpbin.org/get
TIPS: use
json_ppto pretty print the json output.
- Perfom a HTTP POST request
curl -d '{"hello": "world"}' \
-H 'Content-Type: application/json' \
-X POST https://httpbin.org/post
rsync
RSYNC stands for remote sync. It's a local and remote file synchronization tool. It's used for sending files from a machine to another machine.
Example 1: Sync directories
Create two directories
mkdir dir1 dir2
Create files in dir1
touch dir1/file{1..100}
Sync files from dir1 to dir2
rsync -av dir1/ dir2
# trailing slash on dir1 indicates that to sync all contents inside dir1
# a - sync in the archive mode, this will sync files recursively and preserve symbolic links
# v - verbose mode, prints files as they're syncing
Example 2: Sync files to remote system
rsync -a ~/dir1 username@remote_host:destination_directory
rsync -a ~/dir1 [email protected]:/home/siraphob/dir1
xargs
XARGS builds and executes command provided through the standard input. It takes the input and converts it into a command argument for another command.
Example 1
seq 5 | echo
1
2
3
4
5
You'll see no output because ECHO doesn't take stdin as input but command line arguments.
$ seq 5 | xargs echo
1 2 3 4 5
You can use XARGS to feed the output as the argument of the ECHO command.
Example 2: Recursive LS
ls | xargs ls
Difference between XARGS and PIPE(
|)XARGS can be used when you need to take the output from one command and use it as an argument to another. On the other hand, piping is taking output from a command and feed into the stdin of another.
Example 3: Placeholder argument
ls | xargs -I {} echo "hello {}"
Example 4: Execute arguments separatedly
$ ls | xargs -t echo
echo Desktop Documents Downloads Music Pictures Programs Public qmk_firmware Scripts snap Templates Videos Workspace
Desktop Documents Downloads Music Pictures Programs Public qmk_firmware Scripts snap Templates Videos Workspace
The above command runs the ECHO only once. If we want to run ECHO separately for each directory, we could use -n <number-of-max-args> to fix the maximum number of arguments to run by XARGS.
$ ls | xargs -t -n 1 echo
echo Desktop
Desktop
echo Documents
Documents
echo Downloads
Downloads
echo Music
Music
echo Pictures
Pictures
echo Programs
Programs
echo Public
Public
echo qmk_firmware
qmk_firmware
echo Scripts
Scripts
echo snap
snap
echo Templates
Templates
echo Videos
Videos
echo Workspace
Workspace
The above command uses -I flag to specify a placeholder. In this case, we use {} as the placeholder.
awk
AWK is a command line text manipulation scripting language.
AWK works on programs that contain rules comprised of patterns and actions. Patterns are enclosed in curly braces {}. Together, a pattern and an action form a rule. The entrie AWK program is enclosed in single quotes ''.
Example 1: Print the first column
cat people.txt | awk '{ print $1 }'
Example 2: Print 2 columns
cat people.txt | awk '{ print $1, $2 }'
Example 3: Skip the first line
cat people.txt | awk '{ if (NR > 1) { print $0 } }'
Example 4: Find people working in IT
AWK allows you to use Regex by adding a /pattern/ at the start of the rules.
cat people.txt | awk '/IT/'
sed
This command is a stream editor for filtering and transforming text. Most common use of SED command is for substitution or for find and replace.
-
Replace
powerwithtechecho "apiplus power" | sed "s/power/tech/" -
Delete a particular line
sed '5d' file.txt # remove line 5
Bash shortcuts
Ctrl+c
Sends SIGINT signal and kills the current process.
Ctrl+z
Send SIGTSTP to suspend the current process.
Ctrl+d
Send an EOF marker. This will close the current shell.
Ctrl+r
Recall the last command including the specified character.
References
- [1] https://httpbin.org/
- [2] https://www.youtube.com/watch?v=rp7jLi_kgPg&t=1s
- [3] https://www.freecodecamp.org/news/the-linux-awk-command-linux-and-unix-usage-syntax-examples/
- [4] https://www.digitalocean.com/community/tutorials/how-to-use-rsync-to-sync-local-and-remote-directories
- [5] https://ss64.com/bash/syntax-keyboard.html