Skip to main content

Command Palette

Search for a command to run...

Basic Linux Commands

Updated
7 min read
Basic Linux Commands
J

I am continue learning variety of DevOps technologies, including AWS, Linux, Python, Shell Scripting, Docker, Terraform, Jenkins, Git/GitHub, and Computer Networking. I have a strong ability to troubleshoot and resolve issues, and are consistently motivated to expand the knowledge and skills through exploration of new technologies.

I welcome you to this blog. We'll cover all the required Linux for getting started with DevOps or who wanna learns Linux in a very understandable manner.

Here, We cover the INTRODUCTION TO LINUX.

So let's start

INTRODUCTION TO LINUX Firstly, we have to know what is open source because Linux is Open Source.

What is Open Source? Open source: The software and source code are available to all. Anyone can inspect, modify, and enhance it. Many open-source licenses exist, each different in particular.

Linux Origins

1984: The GNU Project and the Free Software Foundation Creates an open-source version of UNIX utilities.

• Creates an open-source version of UNIX utilities.

• Creates the General Public License (GPL) Software license enforcing open source principles.

1991: Linus Torvalds

• Creates open-source, UNIX-like kernel, released under the GPL.

Today:

• Linux kernel + GNU utilities= complete, open-source, UNIX-like operating system.

• Packaged for targeted audiences as distributions.

Why Linux? • Open Source.

• Community support.

• Most Servers run on Linux.

• DevOps most of the tools are implemented on Linux only.

• Automation.

• Secure.

Architecture of Linux architecture of linux diagram hashnode.png

Linux Kernel receives a command from hardware and passes the signal from hardware to shell, from the shell we can execute commands and expect the return.

Different Linux distros ➔ Popular Desktop Linux OS(operating system)

• Ubuntu Linux

• Linux Mint

• Arch Linux

• Fedora

• Debian

• OpenSuse

➔ Popular Server Linux OS

• Red Hat Enterprise Linux

• Ubuntu Server

• Centos

• SUSE Enterprise Linux

Most used Linux distros currently in IT industry.

• RPM based:- RHEL & Centos.

• Debian based:- Ubuntu Server.

right rpm.pngDifference between RPM based and Debian based:

From the user’s point of view, there isn’t much difference between these tools. The RPM and DEB formats are both just archive files, with some metadata attached to them. DEB files are installation files for Debian-based distributions. RPM files are installation files for Red Hat based distributions. Ubuntu is based on Debian’s package management based on APT and DPKG. Red Hat, CentOS, and Fedora are based on the old Red Hat Linux package management system, RPM.

DEB or .deb (Debian based softwares)

DEB is the extension of the Debian software package format and the most often used name for such binary packages. DEB was developed by Bedian.

Example: Google chrome software

Package name: google-chrome-stable_current_amd64.deb

Installation: dpkg -i google-chrome-stable_current_amd64.deb

RPM or .rpm (Red Hat based software)

It is a package management system. The name RPM variously refers to the .rpm file format, files in this format, software packaged in such files, and the package manager itself. RPM was intended primarily for Linux distributions; the file format is the baseline package format of the Linux Standard Base. RPM was developed by Community & Red Hat.

Example: Google chrome software

Package Name: google-chrome-stable-57.0.2987.133-1.x86_64.rpm

Installation: rpm -ivh google-chrome-stable-57.0.2987.133-1.x86_64.rpm

NOTE: You will also encounter different commands, packages, and service names while using both kinds of distros.

Linux Commands: whoami - shows the current user

compgen -u - lists all the users

man (command name) - it's a guide with information about any command

echo "Hello world" - prints Hello world

echo "Hello" > filename → adds the text into the mentioned file

history - list of all commands we used till now

history 10 - list of the last 10 commands used

df -h - shows the disk-free space

Working with directories: ln -s /mnt/c/Users/Chris/Desktop - changes the directory to desktop

pwd - shows the current directory

cd - changes to the home directory

cd Desktop - changes to sub-directory

cd .. - changes to the parent directory

cd ~ - to go back to the default directory

Working with Files: touch demo.txt - creates a text file named demo in the current directory

touch (file1 name) (file2name)....(fileN name) - creates N number of files at a time in the current directory

ls - lists out all the files and folders in the current directory

cat demo.txt - opens a file

nano demo1.txt - creates a new file and also can modify the existing file.

vi demo.txt - modifies the contents of a file

less - opens a file in another window(same as nano)

Advanced Commands(Files): lsof - lists out all the open files

lsof -u [username] - lists out all the open files in a specific user

ls -a → list of all the files and folders including all hidden files and folders

cat -b (filename) - gives line numbers to lines only with text

cat -n (filename) - gives line numbers to even empty lines and to all lines with text in it

grep cH [file name] - returns the whole word containing that string or character mentioned in the command but case sensitive

grep -i cH [file name] - returns case insensitive

sed 's/chris/cgirs/' [file name] - replaces a word with another word within the command line only

sort (file name) - returns all the lines in alphabetical order of starting letters

sort -r (file name) - returns in reverse order

sort -n (file name) - returns in numerical order

cut -c1-2 (file name) - returns only the first 2 columns in a file

tar -cvf (filename) (source-folder) - to zip a .tar format file

tar -xvf (tar-file) - to unzip a .tar format file

To know more about ls commands, please click here

To know more about cat commands, please click here

Modifying file's/folder's directories: cp demo.txt Desktop - copies the mentioned file into the mentioned directory

mv demo.txt Desktop - moves the mentioned file to the mentioned directory and deletes it from the current directory

rm demo.txt - removes a file from the current directory(deletes)

Note: once used an rm command to remove a file, you can't undo it because there's no recycle bin in Linux. So please use the rm command carefully

mkdir (folder name) - creates a new directory(folder)

rmdir (folder name) - removes a directory(folder) only which is empty

rm -r (folder name) - removes a non-empty directory with its contents

Files and their Permissions: There are three types of permission:

read (r)

write (w)

execute (x)

These are generally represented as in rwx

These permissions affect three groups of owners:

user/owner (u)

group (g) → an entity among a user group

guests or other people (o)

ls -l - shows the file permissions for that mentioned file

chmod (+x or +w or +r) [file name] - changes any one of the permissions of a file

chmod u=rwx,g=rx,o=rw [filename] - changes all the permissions of a file at a time

chmod 777 [file name] - changes all permissions of a file to rwx

chown (username) [file name] - changes the owner of the file to that mentioned username.

Commands related to User-Id's and stuff:

id - will return the UserId, GroupId, groups belonging to the user etc

id -u (username) - will return only the UserId of that user

id -g (username) - will return only the GroupId of that user

Some of the commands with sudo: We use sudo when we are accessing root permissions

sudo bash - changes the author to root

su (user name) - gets back to the user as author

sudo apt-get install package - installs a package

sudo useradd (username) - adds a new user

sudo passwd (username) - adds a password to the user

sudo userdel (username) - deletes a user

sudo groupadd (group name) - adds a group of users

sudo groupdel (group name) - deletes a group of users

Networking commands: ifconfig - shows the ip address and some of the addresses of our device

ifconfig -a - shows all the interfaces in our device

ifconfig (port which we want to see) - shows the information only about port mentioned

ping google.com - checks whether google.com can communicate with us or not

ping (Ip addrs) - checks whether our system can communicate with ip addrs mentioned of another device connected to the same network as ours

wget (url) - will download the file in the url to the current directory only from http and https

curl (url) - to transfer data to or from a server, using any of the supported protocols (HTTP, FTP, IMAP, POP3, SCP, SFTP, SMTP, TFTP, TELNET, LDAP, or FILE).

nslookup google.com - will show the ip address and server address of google.com

I followed Udemy Course - Linux Administration Course and also followed Zero to Hero from trainwithshubham (Devops)

That's it for the blog guys. I hope you loved the reading!

Thank you 😊