Skip to main content

Command Palette

Search for a command to run...

Linux Basics

Getting Started

Updated
•6 min read•View as Markdown
Linux Basics

In 1991 Linus Torvald started developing Linus Kernel.

The Kernel is the most importand piece in the OS which allows hardware to taslk to the software.

Popolar Linux Distributions.

  • Debian - Very stable core

  • Red hat Enterprise(RHEL) - Greate for enterprise applications in corporate

  • Ubuntu - Debian based OS

  • Fedora - Uses Red Hat in the backend

  • Linux Mint - It’s Ubuntu based

  • Gentoo - This is for advanced users

  • Arch Linux - Lightweight and community driven

  • openSUSE - Great for new Linux user with easy to use graphical installer.

Basic Commands

echo: This command just print out the text command and display.

echo Hello World

pwd(Print Working Directory): As the name suggest, it will print the directory path

  • pwd -L : Logical path

  • pwd -P : Physical Path

labex:project/ $ pwd
/home/labex/project
labex:project/ $ cd symlink_dir 
labex:symlink_dir/ $ pwd -L
/home/labex/project/symlink_dir
labex:symlink_dir/ $ pwd -P
/home/labex/project/real_dir

cd(Change Directory):

Absolute path: This is the path from the root directory. The root is the head honcho. The root directory is commonly shown as a slash (/). Every time your path starts with /, it means you are starting from the root directory. For example, /home/asit/Desktop.

Relative path: This is the path from where you are currently in the filesystem. If I was in location /home/asit/Documents and wanted to get to a directory inside Documents called learnings, I don’t have to specify the whole path from root like /home/asit/Documents/learnings; I can just go to learnings/ instead.

labex:~/ $ cd /home     
labex:/home/ $ cd labex/asit/Documents/
labex:Documents/ $ learnings
  1. cd . : (current directory): This is the directory you are currently in.

  2. cd .. : Takes you to the directory above your current one.

  3. cd ~ : This directory defaults to your “home directory,” such as ~/.

  4. cd - : This will take you to the previous directory you were just at. Ex - It takes from home directory(~/ to the previously accessed directory which is → ~/asit/Documents)

cd /etc: This is absolute path. /etc contains the configuration files.

labex:~/ $ cd /etc 
labex:/etc/ $ pwd
/etc
labex:~/ $ /home 
labex:/home/ $ cd labex/asit/Documents/London 
labex:London/ $ cd .
labex:London/ $ cd ..
labex:Documents/ $ cd ~
labex:~/ $ cd -
~/asit/Documents
labex:Documents/ $

ls(List Directories): This is to list the directories and files. In the below example when we put the command ls for our Document/ folder, the 2 folders inside were listed. i.e → learnings and London

labex:Documents/ $ ls
learnings  London

Note: We can give specific path for ls and it will show the content such as folders and files inside that path. See the below example.

labex:asit/ $ ls Documents 
learnings  London
  • ls -a : a stands for all. This will list out all files and folders including the hidden ones.

  • ls -l : l stands for long. This shows the detail list in long format.

  • ls - la/ ls - al : This will show all in long format depends upon the in the order in which the the command is written.

labex:project/ $ ls -a
.  ..  data_file.txt  test  test_file.txt
labex:project/ $ ls -l
total 8
-rw-rw-r-- 1 labex labex 12 Sep 24 02:27 data_file.txt
drwxrwxr-x 2 labex labex  6 Sep 24 02:27 test
-rw-rw-r-- 1 labex labex 27 Sep 24 02:27 test_file.txt
  1. File Permissions: The first column (e.g., -rw-rw-r--) shows the file permissions.

    • The first character indicates the file type (- for regular file, d for directory).

    • The next three characters show owner permissions.

    • The next three show group permissions.

    • The last three show permissions for others.

    • r means read, w means write, and x means execute.

  2. Number of Links: The number right after the permissions (1 for files, 2 for the directory in this example).

  3. Owner Name: The username of the file owner (labex in this case).

  4. Group Name: The group that has access to the file (also labex here).

  5. File Size: Size in bytes (12 for data_file.txt, 6 for the test directory, and 27 for test_file.txt).

  6. Last Modification Date and Time: When the file was last changed (Aug 7 11:23 for all files here).

  7. File or Directory Name: The name of the file or directory.

ls -lh : -h stands for human readable.

labex:project/ $ ls -lh
total 8.0K
-rw-rw-r-- 1 labex labex 12 Sep 24 02:27 data_file.txt
drwxrwxr-x 2 labex labex  6 Sep 24 02:27 test
-rw-rw-r-- 1 labex labex 27 Sep 24 02:27 test_file.txt

ls - lt/ ls -ltr: -t is for sorting and -tr is for reverse sorting. It is sorted by modification time.

labex:project/ $ ls -lt
total 8
-rw-rw-r-- 1 labex labex 12 Sep 24 02:27 data_file.txt
drwxrwxr-x 2 labex labex  6 Sep 24 02:27 test
-rw-rw-r-- 1 labex labex 27 Sep 24 02:27 test_file.txt
labex:project/ $ ls -ltr
total 8
-rw-rw-r-- 1 labex labex 27 Sep 24 02:27 test_file.txt
drwxrwxr-x 2 labex labex  6 Sep 24 02:27 test
-rw-rw-r-- 1 labex labex 12 Sep 24 02:27 data_file.txt

ls -l test: It lists the content. if the file is empty then it is show like the below

labex:project/ $ ls -l test
total 0

ls | cowsay: Show output in a fun way :)

labex:project/ $ ls | cowsay
 __________________________________
< data_file.txt test test_file.txt >
 ----------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

Note: There are many more ls options to explore. Here are a few more you might find useful:

  • -R: List subdirectories recursively (shows contents of all subdirectories)

  • -S: Sort by file size (largest first)

  • -X: Sort alphabetically by entry extension

  • -1: List one file per line (useful for scripts)

Installing and starting wsl - Windows Subsystem For Linux in Windows.

wsl --install

wsl ~

touch : This is to use create new empty file

touch mysuperduperfile
touch banana.jpg

file : This is to find out what kind of file the file is

labex:Code/ $ file banana.jpg
banana.jpg: empty

cat : This is for viewing the content of the file. This is also for concatenate multilple files. This is only helpful for small files. It combines multiple files and show their output.

cat dogfile birdfile

Note: Linux is Case Sensitive

mkdir: This means make directory

labex:~/ $ cd ~
labex:~/ $ mkdir -p maze/twisty/little/passages
labex:~/ $ cd maze/twisty/little/passages
labex:passages/ $ pwd
/home/labex/maze/twisty/little/passages
labex:project/ $ mkdir ~/project/digital_garden/notes
labex:project/ $ mkdir ~/project/digital_garden/projects
mkdir ~/project/digital_garden/resources
labex:digital_garden/ $ ls ~/project/digital_garden 
notes  projects  resources

mkdir -p: It allows you to make a parent directory

labex:digital_garden/ $ mkdir -p ~/project/digital_garden/projects/web_app/src/components

This command does a lot at once:

  • It creates a web_app folder inside projects

  • Inside web_app, it creates an src folder

  • Finally, inside src, it creates a components folder

la -R: This is to display the content recursively

labex:digital_garden/ $ ls -R ~/project/digital_garden/projects/web_app
/home/labex/project/digital_garden/projects/web_app:
src

/home/labex/project/digital_garden/projects/web_app/src:
components

/home/labex/project/digital_garden/projects/web_app/src/components: