🚀 Mastering Linux Commands Part 1

Photo by Alex Chumak on Unsplash

🚀 Mastering Linux Commands Part 1

Introduction🌐

In the vast realm of Linux, the command line stands as a powerful gateway to interact with the operating system. At its core lies the shell, a program that orchestrates user commands, file management, and interactions with other programs. Let's embark on a journey to unveil the intricacies of the Linux command line, demystifying concepts and commands that empower users in this dynamic environment.

Understanding the Shell 🐚:

A shell is a program that provides a user interface for interacting with the Linux operating system. It allows users to execute commands, manage files and directories, and interact with other programs. There are many different shells available for Linux, but some of the most popular ones include Bash, Zsh, and Tcsh.

for eg if i run python3 command it will open the python interpreter in cli but how did it do it? if we want to see we can use this command whereis python3 it will give some paths like usr/bin/python3 or something like that so this path is where the python3 executable command is there so if i run this usr/bin/python3 than it will open the python interpreter only how cool is this

So whatever command we run that runs in the particular folder only that basically means youre in this folder

So if i want to do something inside the download folder than i need to step inside it to do something

Environment Variables 🌱:

Environment variables store crucial information about a user's environment. From home directories to executable paths, these variables influence how commands are executed across processes, each running instance being a process.

Environment variables are variables that store information about the user's environment, such as their home directory, the current working directory, and the path to executable programs. Environment variables are accessible to all processes running on the system, and they can be used to configure programs and scripts.

there are some variables that we need to set it are just the named values that are used to change how the commands and process are executed So what is process here any instance of the running command is known as process

Path Environment Variable 🛤️:

Path variables: Path variables are a type of environment variable that specifies the directories where the shell should search for executable programs. When a user enters a command without specifying a full path, the shell will search the directories listed in the PATH variable to find the program. This makes it possible to execute programs from any directory without having to type in the full path to the program. So shell is a command line interface that interprets are commands and tells OS what to do basically. and what are this commands this commands are the are the executable files

when we type a certain command for eg python3 or git or any command it first checks the $PATH in that it have certain different paths which are separated by : and it one by one checks all the paths if this command you entered is available in this particular $PATH if not checks the other Paths which are separated by : and if its finds then it run this command and if i do some random command like "lsjdnff" this ain't a command it will check the $PATH and it will not find it so it will give message as command not found

Environment Variables Set:

  • PATH: Directories for executable programs.

  • HOME: User's home directory.

  • USER: User's login name.

  • EDITOR: Default text editor.

  • PAGER: Default pager.

Linux File System📁:

The Linux file system used to resemble an unorganized town where individuals constructed their houses wherever they pleased. However, in 1994, the Filesystem Hierarchy Standard (FHS) was introduced to bring order to the Linux file system.

By implementing a standard like the FHS, software can ensure a consistent layout across various Linux distributions. Nonetheless, not all Linux distributions strictly adhere to this standard. They often incorporate their own unique elements or cater to specific requirements.

Bash Profile Files 📜:

Bash profile files are scripts that are executed by the Bash shell when it starts up. They are used to configure the shell environment, such as setting environment variables, defining aliases, and loading functions. There are two main types of Bash profile files: user-specific and system-wide.

Bash Profile Files Unveiled 🌐:

Bash profile files are the architects of your Linux environment. Let's delve into the intricacies of user-specific and system-wide Bash profile files:

Bash files include: .profile, .bash_history, .bash_logout, .bashrc.

User-Specific Bash Profile Files:

User-specific Bash profile files: These files are located in the user's home directory and are executed every time the user logs in. The most common user-specific Bash profile files are:

  • .profile: Executed for non-interactive shells.

  • .bashrc: Executed for interactive shells.

  • .bash_login: Executed if .bash_profile is absent.

System-Wide Bash Profile Files:

System-wide Bash profile files: These files are located in the /etc directory and are executed for all users. The most common system-wide Bash profile files are:

  • /etc/profile: Executed for non-interactive shells.

  • /etc/bash_bashrc: Executed for interactive shells.

Execution Order:

The order in which Bash profile files are executed is as follows:

System-wide profile files User-specific profile files This means that the settings in the user-specific profile files will override the settings in the system-wide profile files.

Bash profile files are typically used to set the following types of environment variables

Lets start with the commands

  • cd /mnt = cd here means change directory and /mnt means mount the drives then we can do the following

  • ls = this will list all the files that are available

  • pwd = this commands tell in which working directory we are in it will give the path in which we are working currently

  • tags with ls = so we can add specific tags with the ls command to see different types of files such as we can use

  • . .. = here (. ) means current directory and (.. ) means previous directory

  • ls -a = this will show the hidden files available

  • ls -l = this will give the information about the files

  • we can also combine different tags together also such as

  • ls -al = here it will show the hidden files and also the long long information about it -cd .. = this goes one folder up like from current to the parent -cd .. /pictures = suppose youre in the downloads directory and you want to go to the pictures directory so .. Means go the parent directory and /pictures means there is a folder known as pictures go inside that directory simple

  • ls -r = here this means show the files which in the subfolders , like folders inside the other folders

  • cat filename. Extension = this command is used to open the files and see its content in the terminal this is also used to concatenate multiple files

  • cat > filename. Txt = tis command actually creates a files if not exist and allows you to write the content inside it

  • ctrl +c = tis command here is used to exit out from anything

  • cat file1. Txt file2. Txt >final. Txt = so here what happening is this > this funnel thing here is called as redirection , it is concatenating the data from the multiple files and put it into the third while which is final. Txt so cat is multipurpose command we can also concatenate multiple files as we want cat > something create a file cat filename will open it and cat file1 file2 and remember to give the extension

  • echo = this is used to print for example echo "hello world" you can also do this like below

  • echo "hello world "> new. Txt so it will save the hello world in that file new. Txt and you can see this file using the cat new. Txt and it will open and it will have hello world written inside it

  • man = so this command gives the information about the commands what they do and its associate and more information about how can we use the following command for eg man cat or man echo this will display the information about the commands

  • | = pipe or tunnel this works like the output of the first command will act as input for the second command for eg if we have this file cat new. Txt which has content like hello world and if we want to changes its letters to the upper command we can do the following cat new. Txt | tr a-z a-z > upper. Txt so what we are doing here is instead of opening the new. Txt take the o/p of it and do (tr) means translate it from a-z lowercase to a-z uppercase and (>) redirect it the new file upper. Txt so when i do cat upper. Txt it will go hello world

  • tr =this command in Unix-like operating systems is a versatile tool for translating or deleting characters from standard input and writing the result to standard output. It can be used for a wide variety of tasks, including:

Case conversion: convert lowercase characters to uppercase or vice versa. Character deletion: remove specific characters from a text stream. Character replacement: replace certain characters with others. Character squeezing: remove duplicate consecutive characters. The tr command has a simple syntax and a variety of options, making it a powerful and flexible tool for text manipulation.

Syntax:

bash tr [options] set1 [set2] use code with caution. Learn more options:

-c: complement set1, translating characters not in the set. -d: delete characters in set1 from the output. -s: squeeze repeated characters in set1 to single occurrences. -t: truncate set1 to the length of set2.

  • mkdir random = so this command mkdir is used to make directory so basically it helps in creating the folders we can also make subfolders

  • mkdir random/hello = this will create a folder inside the existing folder random

  • mkdir -p random/middle/hello = so this command -p means parent will create a folder between this two folders random and hello

  • touch filename. Extension = this is used to create files such as txt or just files for eg touch random/names. Txt

  • cp = this command is used to create the copy of the existing file cp new. Txt copy_new. Txt this is copy from one to another

  • mv = this command is used to move files from one dir to another for eg mv names. Txt random so we are moving the names. Txt to the random directory/folder

  • mv names.Txt myname.Txt = so here what is happening that we are not providing new folder or we are not moving it in this case it will rename the names.Txt to myname.Txt

  • mv names. Txt .. /newnames. Txt = this will move the file from the current directory to the parent directory and also it will rename it to newnames. Txt how cool is this

  • cp -r = this will copy the dir and all its subdirectory and will saves it copy in the destination dir for eg cp -r random demo so here we are copying with flag -r and it will cop the random and all its subfolder and save its copy in the other folder demo

  • rm = this command will remove the file and notice something it will not go to the bin but remove entirely so be cautious using this

  • mv file1. Txt fileone. Txt = this is how you rename the file

  • mv random demo = this move the random folder to the demo folder

  • mv demo/random . = this move random folder which is inside the demo folder to the parent directory

  • rm random = this will give error while removing the random which is a folder to remove the folder and all its subfolders and files use the following command

  • rm -r random = this will not give the error while removing the folder instead it will now remove the folder smoothly

  • rm -rf filename or folder = this command is used sometime it may happen if the file is open it will be difficult to remove or delete it so use rm and -rf flag this will work fine

  • sudo = this command means super user do it is used at administrative level which will learn more while learning the kubernets

  • df = this command shows the disk free space available we can by default it shows in percentage and kb's if we want to see it in the mb then use flag -m if want to see it in more readable format than we can use df -h it will be better to read

  • du = this command shows the disk usage statistics so basically it shows the usage of all files and folders in the current directory -head = this command shows the first 10 things related to the file the initial default is 10 items it will list it out but we can also change it using the following command -head -4 filename. Extension = here the flag is set to 4 -4 but we can change it accordingly -tail = similar to the head but in the tail command it will show the 10 items or details from the tail of the document we can also change the default value of 10 from other tail -2 filename. Extension = this will show the last two items or the details of the document

  • diff - this command is used to compare files and it identifies and display the differences line by line good tool while having the to check multiple source code files

  • find = command is a versatile tool in unix-like operating systems for searching for files and directories based on various criteria. It can scan the entire file system and locate files based on their names, permissions, owners, file types, creation dates, modification dates, and other attributes.

Syntax: find [options]

options: -user: search for files owned by a specific user. -group: search for files owned by a specific group. -name: search for files with matching names or patterns. -type: search for specific file types, such as regular files, directories, symbolic links, etc. -perm: search for files with specific permissions. -size: search for files within a specified size range. -mtime: search for files modified within a specified time range. -ctime: search for files created within a specified time range. -exec: execute a command on each matching file.

Examples: find all files named "config. Txt" in the current directory and its subdirectories: -find . -name "config. Txt"

find all files owned by the user "root": -find . -user root

find all files that are executable: -find . -perm +x

find all files that are larger than 1mb: -find . -size +1000000c

find all files that were modified in the last week: -find . -mtime -7

find all files that are empty directories: -find . -type d -empty

find all files that are symbolic links: -find . -type l

find all files and directories that are named starting with "temp": -find . -name "temp*"

Conclusion 🚀:

In this Blog we Got familiar with few of basic Linux Commands which are like the fundamentals and we understood the different types of profiles available.. Armed with these commands, you've now wielded the magic of the Linux command line. As you delve deeper, the command line unveils its richness, offering you the keys to efficiently manage and explore your Linux realm. May your commands be swift, and your directories ever organized!

This Blog is the Part 1 of my Mastering Linux Commands Stay tuned for Part 2 till then happy Learning 🌐💻