Basic Linux Commands

Jyoti Sachdeva
4 min readJun 3, 2022
Linux

Before jumping to linux commands, let’s get a quick overview of linux.

Linux is an operating system’s kernel.

Linux is free and open-source which means that you can simply change anything in Linux and redistribute it in your own name!

Linux Distributions are commonly called “distros”. Some of the distros are:

  • Ubuntu Linux
  • Red Hat Enterprise Linux
  • Linux Mint
  • Debian
  • Fedora

Linux is actually the name of the Kernel and not the whole operating system. The whole operating system is called the Gnu/Linux operating system. Linux treats everything as a file and its file system has a tree based structure.

Linux File Structure

/ is the root directory. root is home for superuser and is different from /.

1. man

man provides information for all the commands. If you need information for any command, just use man command_name.

man [section optional] command

-k searches through all sections

man -k ls

dane_query_tlsa(3) — API function

dane_query_to_raw_tlsa(3) — API function

(3) is the section number.

man 1 ls or man ls

Since section number is optional we can ignore it.

2. ls

ls lists the contents. By default each directory has two hidden folders which are . and ..

. is current directory and .. is parent directory.

jsachdev@C02FX9J2MD6M linuxtutorial % lsdirectory1 file1.txt

-F is used to differentiate between file and folder.

jsachdev@C02FX9J2MD6M linuxtutorial % ls -Fdirectory1/ file1.txt

-a shows all contents including hidden files and folders

jsachdev@C02FX9J2MD6M linuxtutorial % ls -a.  ..  directory1 file1.txt

-l shows long detailed information and h shows size information in human readable form

jsachdev@C02FX9J2MD6M linuxtutorial % ls -lhtotal 0drwxr-xr-x  2 jsachdev  staff    64B Jun  3 14:38 directory1-rw-r--r--  1 jsachdev  staff     0B Jun  3 14:37 file1.txt
Permissions
drwxr-xr-x means it is directory. User has read, write and execute permissions.
group and others has only read and execute permissions.
2 is the number of hard links.jsachdev is usernamestaff is group name.64B is file sizenext is the date file was modified

3. cal

cal shows the calendar of current month and year.

cal      June 2022Su Mo Tu We Th Fr Sa1  2  3  45  6  7  8  9 10 1112 13 14 15 16 17 1819 20 21 22 23 24 2526 27 28 29 30

We can add month and year or just year

cal 3 2011
March 2011
Su Mo Tu We Th Fr Sa1 2 3 4 56 7 8 9 10 11 1213 14 15 16 17 18 1920 21 22 23 24 25 2627 28 29 30 31

We can show after and before a date as well along with current date

after -A

before -B

cal -A 1 12 2017   December 2017          January 2018Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa1  2      1  2  3  4  5  63  4  5  6  7  8  9   7  8  9 10 11 12 1310 11 12 13 14 15 16  14 15 16 17 18 19 2017 18 19 20 21 22 23  21 22 23 24 25 26 2724 25 26 27 28 29 30  28 29 30 3131

4. pwd

pwd shows path to present working directory.

jsachdev@C02FX9J2MD6M linuxtutorial % pwd/Users/jsachdev/linuxtutorial

5. cd

cd is used to change the current directory.

cd [optional directory name/path]

cd takes you to home directory for your user.

jsachdev@C02FX9J2MD6M ~ % cdjsachdev@C02FX9J2MD6M ~ % pwd/Users/jsachdev

you can mention relative directory name from current directory or if the directory does not fall under where your current directory, you would need to mention the path.

jsachdev@C02FX9J2MD6M ~ % cd Desktopjsachdev@C02FX9J2MD6M Desktop %jsachdev@C02FX9J2MD6M Desktop % cd ~/Downloads/sqldeveloperjsachdev@C02FX9J2MD6M sqldeveloper %

6. mkdir

mkdir is used to create a directory.

jsachdev@C02FX9J2MD6M linuxtutorial % mkdir directory2jsachdev@C02FX9J2MD6M linuxtutorial % lsdirectory1 directory2 file1.txt

to create recursive directories use -p

jsachdev@C02FX9J2MD6M linuxtutorial % mkdir  directory3/subdirectorymkdir: directory3: No such file or directoryjsachdev@C02FX9J2MD6M linuxtutorial % mkdir -p directory3/subdirectoryjsachdev@C02FX9J2MD6M linuxtutorial % tree.├── directory1├── directory2├── directory3│   └── subdirectory├── file1.txt└── file2.txt4 directories, 2 files

There could be scenario that you want to create folders for few months or all months from year 2020 to 2022. There is a short end notation:

jsachdev@C02FX9J2MD6M directory1 % mkdir {jan,feb}_{2020..2022}jsachdev@C02FX9J2MD6M directory1 % lsfeb_2020 feb_2021 feb_2022 jan_2020 jan_2021 jan_2022

7. touch

touch is used to create an empty file.

jsachdev@C02FX9J2MD6M linuxtutorial % touch file2.txtjsachdev@C02FX9J2MD6M linuxtutorial % lsdirectory1 directory2 file1.txt file2.txt

To create multiple empty files at once:

jsachdev@C02FX9J2MD6M directory2 % touch file_{1..10}.txtjsachdev@C02FX9J2MD6M directory2 % lsfile_1.txt file_2.txt file_4.txt file_6.txt file_8.txtfile_10.txt file_3.txt file_5.txt file_7.txt file_9.txt

8. rm

rm is used to remove files/directories. -r is used to remove recursively

jsachdev@C02FX9J2MD6M linuxtutorial % rm file1.txtjsachdev@C02FX9J2MD6M linuxtutorial % rm d1rm: d1: is a directoryjsachdev@C02FX9J2MD6M linuxtutorial % rm -r d1/

9. rmdir

rmdir is used to remove directories.

jsachdev@C02FX9J2MD6M linuxtutorial % mkdir -p d1/d2jsachdev@C02FX9J2MD6M linuxtutorial % rmdir d1rmdir: d1: Directory not emptyjsachdev@C02FX9J2MD6M linuxtutorial % rmdir d1/*

10. !!

!! runs the most recent command.

jsachdev@C02FX9J2MD6M linuxtutorial % lsd1  file2.txt test1jsachdev@C02FX9J2MD6M linuxtutorial % !!lsd1  file2.txt test1

Thanks for reading!

--

--