Learning Linux: Week 1 – Mastering the Basics


Here is a comprehensive guide to the basics of Linux that you need to learn
What is OS?
An OS is a software that acts as a middleman or a bridge between computer hardware and the computer user.
It provides a user interface and controls the computer hardware so that software can function
What is Shell?
a program that takes your input and sends it to the OS to perform
Terminal / Console → programs that launch a shell for you
The default shell for almost all Linux distributions is bash (AL 2022 or AL 2023, etc.)
Basic Commands
pwd
→ used to see where you are currently ( i.e., print working directory)cd
→ used to change directoryeg:
cd /home/pete/Pictures
→ Change your working directory to/home/pete/Pictures
Few shortcuts:
.
→ Current Directory..
→ Parent Directory~
→ Home Directory-
→ This will take you toprevious directory
You were just at
ls
→ used to list or display the content of a directory-a
→ lets you view all files (even the hidden ones)-l
→ long format with detailed info ( file permissions, links , owner , owner group , file size , timestamp of last modification )
touch
→ allows you to create new empty files- Syntax:
touch <filename>
- Syntax:
cat
→ It is used to display the content of a file on the terminal itself, but also allows you to combine multiple files and show you the output of them- Syntax:
cat <filename>
- Syntax:
file
→ to display the kind or type of file- Syntax :
file <filename>
- Syntax :
whoami
→ tells you the username of the current userclear
→ clears the terminalecho
→ print anything on the terminal- Syntax:
echo “HEllo World!”
- Syntax:
history
→ used to display command history, it stores the history of all commands used in the current sessionexit
→ exit the terminal with the current userwhatis
→ displays a one-line description of the commandtype
→ displays the type of every Linux command that you wantw
⇒ to see who is logged into the system and who is using it-s
⇒ short notation (remove LOGGING, JCPU,, PCPU-V
⇒ version-o
⇒ old-style output<user>
⇒ for a particular user-h
⇒ no-header
last
⇒ listing of last logged-in users-x
⇒ to display system shutdown information and run level changes-a
⇒ display host names in the last columns-w
⇒ display full user name and domain names
diff
→ compares files line by line-y
→ display output in two columns side by side-q
→ report only if the file differs-c
→ to display the output and contents of the file-
→ to delete!
→ change
There are multiple options you can use with each command, to check use
man <command>
or<command> —help
Directory Tree
The directory tree in Linux refers to the hierarchical structure used to organize files and directories within the file system. It is a fundamental concept in Linux and Unix-like operating systems.
All directories are installed under
/
→ Root Directory or ROOT file system (parent of all directories)/bin
→ contains executable files of many basic shell commands/opt
→ used for storing or installing the files of 3rd party applications not available from the distribution’s repo- command practice → store software code in
/opt
and binary files in/bin
- command practice → store software code in
/home
→ each user has a home directory where they store user-specific config files and user data- One user doesn’t have access to another user’s home directory
/usr
→ stores user bin and all executable files, libraries./usr/bin
→ contains basic user commands/usr/sbin
→ contains additional commands for the admin/usr/share
→ contains documentation common to all libraries
/root
→ home directory for root user (separate from /home directory)/lib
→ codes that can be used by executable binaries/dev
→ details about system devices or hardware ( virtual files not physically on disk )/dev/null
→ path to which anything sent will be destroyed- Use: When you want to discard output or prevent clutter.
/dev/zero
→ contains an infinite sequence of 0- Use: When you need to create empty files or initialize storage with zero bytes.
/dev/random
→ contains an infinite sequence of random numbers- Use: When you need secure random numbers, like for encryption keys.
/tmp
→ stores temporary files, which get removed when the system reboots/etc
→ contains config files for scripts, programs,etc; contains core config files for the system/boot
→ has boot files, config, and info about boot files and details of the kernel of Linux distribution/sbin
→ system binaries, similar to/bin
Except it contains binaries that are run by root or the sudo user/var/
→ contains data and info that changes / updates most of the time- contains directors for
log
,mail
,spok
- contains directors for
/media
→ mount point for removable media like USB, DVD, and SD Card. When connected, a directory is automatically created under this/mnt
→ Mount directory, similar to/media
But not automatically, system admins use this to manually mount a filesystem to view data/srv
→ Service data , contains data for services provided by the system
rm -rf /
cleans the entire linux system but won’t work unless you provide-no-preserve-root
with it
Absolute vs Relative Paths
Aspect | Absolute Path | Relative Path |
Starts from | Root directory (/) | Current working directory |
Begins with | / (slash) | Does not begin with / |
Uniqueness | Always points to the same location, regardless of the current directory | Depends on the current directory you are in |
Example | /home/user/Documents/file.txt | Documents/file.txt (if in /home/user/ directory) |
Usage | Used when the full, exact path is needed | Used when referring to files/folders relative to the current location |
Navigation | No need to change directories; path is always the same | Changes based on where you currently are in the filesystem |
Searching
Using Find
a built-in command in Linux (use
man find
for details)Finding By Name
using
-name
option we can find by the filenameSyntax:
find -name “query”
for ignoring case-sensitivity in filenames using
-i
(Eg:find -iname “query”
)Invert the search using
!
→find \\! -name “query_to_avoid”
use
-not
→ to match with not equal
Find by Type
using
-type
we can search by type of filef
→ regular filed
→ directoryl
→ symbolic linkc
→ character deviceb
→ block device
Syntax:
find <path> -type c
Find by Time & Size
using parameter
-size
to filter by the size of filecommonly used size suffix
c
→ bytesk
→ kilobytesM
→ megabytesG
→ gigabytesb
→ 512-byte blocks
find /usr -size 50c # less than 50 bytes
find /usr -size +700M # more than 700 egabytes
Find by Owner and Permissions
search via username using
-user
optionfind /var -user syslog
search via groupname using
-group
optionfind /etc -group shadow
Find by specific permission
using
-perm
optionfind / -perm 644
search for files with at least these permissions using
-
before permissionfind / -perm -644
Find by depth
There can be multiple recursive folders or files in the specified file, so you can set the max and min depth to search up to using
-maxdepth
and-mindepth
optionsfind -maxdepth 2 -name file1 find -mindepth num -name query
Using Locate
A third-party package
mlocate
It is used to search for files throughout your system- Install via
yum install mlocate
orapt-get install mlocate
- Install via
This is considerably faster than
find
because it uses a database that lists all files on the filesystemThis database needs to be updated either once everyday with a cron or manually via
updatedb
commandthe locate database must always be up-to-date to find new files
locate query locate -b query # basename should be equal to the query locate -e query # only return results that still exist in the filesystem locate -S # statistics about the information
Wildcards ( *, ? , ^ , [ ] )
Asterisk ( * )
matches any no. of characters, including zero characters
(In Linux, the term "zero characters" primarily refers to null characters (ASCII NUL, 0x00). These are control characters that do not have a visual representation and are often used in programming contexts)
used to match any file or directory that fit a certain pattern
Eg :
*.txt
orfile*
Question Mark ( ? )
matches exactly one character
used to match only a specific number of characters
Eg:
file?.txt
→ will match for file1.txt, file2.txt
Caret ( ^ )
used in regular expressions, not as wildcards in simple file matching
Eg:
^hello
→ would match lines that start with the word “hello” in a text file
Square bracket ( [ ] )
used to match a single character from a set or range of characters
Usage → characters inside the square brackets define a set or range that the character must match
Eg:
file[a-z].txt
→ match for files like filea.txt, fileb.txt, etc.
Hard Links vs Soft Links
Feature | Hard Link | Soft Link (Symbolic Link) |
Reference | Points directly to the inode (data) of the file. | Points to the path of the file. |
Target | Cannot be used to link to directories (except special cases). | Can link to directories and files. |
Inode | Has the same inode as the original file. | Has a different inode from the original file. |
Cross-filesystem | Cannot link across different filesystems. | Can link across different filesystems. |
Broken Link | Cannot become broken unless the filesystem is damaged. | Can become broken if the target is deleted or moved. |
Visibility | Not distinguishable from a regular file. | Identifiable by ls -l (shows the path it points to). |
Subscribe to my newsletter
Read articles from MRIDUL TIWARI directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

MRIDUL TIWARI
MRIDUL TIWARI
Software Engineer | Freelancer | Content Creator | Open Source Enthusiast | I Build Websites and Web Applications for Remote Clients.