Learning Linux: Week 1 – Mastering the Basics

MRIDUL TIWARIMRIDUL TIWARI
9 min read

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 directory

    • eg: cd /home/pete/Pictures → Change your working directory to /home/pete/Pictures

    • Few shortcuts:

      • . → Current Directory

      • .. → Parent Directory

      • ~ → Home Directory

      • - → This will take you to previous 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>
  • 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>
  • file → to display the kind or type of file

    • Syntax : file <filename>
  • whoami → tells you the username of the current user

  • clear → clears the terminal

  • echo → print anything on the terminal

    • Syntax: echo “HEllo World!”
  • history → used to display command history, it stores the history of all commands used in the current session

  • exit → exit the terminal with the current user

  • whatis → displays a one-line description of the command

  • type → displays the type of every Linux command that you want

  • w ⇒ 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
  • /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
  • /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

AspectAbsolute PathRelative Path
Starts fromRoot directory (/)Current working directory
Begins with/ (slash)Does not begin with /
UniquenessAlways points to the same location, regardless of the current directoryDepends on the current directory you are in
Example/home/user/Documents/file.txtDocuments/file.txt (if in /home/user/ directory)
UsageUsed when the full, exact path is neededUsed when referring to files/folders relative to the current location
NavigationNo need to change directories; path is always the sameChanges 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 filename

      • Syntax: 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 file

        • f → regular file

        • d → directory

        • l → symbolic link

        • c → character device

        • b → block device

      • Syntax: find <path> -type c

    • Find by Time & Size

      • using parameter -size to filter by the size of file

      • commonly used size suffix

        • c → bytes

        • k → kilobytes

        • M → megabytes

        • G → gigabytes

        • b → 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 option

      • find /var -user syslog
    • search via groupname using -group option

      • find /etc -group shadow
  • Find by specific permission

    • using -perm option

      • find / -perm 644
    • search for files with at least these permissions using - before permission

      • find / -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 options

      find -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 or apt-get install mlocate
    • This is considerably faster than find because it uses a database that lists all files on the filesystem

    • This database needs to be updated either once everyday with a cron or manually via updatedb command

    • the 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 or file*

  • 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.

FeatureHard LinkSoft Link (Symbolic Link)
ReferencePoints directly to the inode (data) of the file.Points to the path of the file.
TargetCannot be used to link to directories (except special cases).Can link to directories and files.
InodeHas the same inode as the original file.Has a different inode from the original file.
Cross-filesystemCannot link across different filesystems.Can link across different filesystems.
Broken LinkCannot become broken unless the filesystem is damaged.Can become broken if the target is deleted or moved.
VisibilityNot distinguishable from a regular file.Identifiable by ls -l (shows the path it points to).
0
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.