Command Line Chronicles (Part 1): Unleashing the Power of Linux
Practical Tips, Tricks, and Examples for Mastering Linux Commands
Welcome to my blog, where we explore the world of Linux commands. Join me as we dive into practical tips, tricks, and examples for all Linux users. Enhance your command line skills and unlock the full potential of Linux. Let's navigate the terminal together and elevate our Linux expertise in just a few short reads. Get ready to command your way to success!
What is Shell in Linux?
It is a command line interface that takes all the commands as input and interprets them and tells the operating system what to do. It is a command line interface that accepts and interprets user commands, facilitating communication with the underlying operating system. Essentially acting as a translator.
Commands:
ls
It is used to list all the files in the current directory.
ls -a
It is used to list all the hidden files. Anything that starts with a '.' is hidden in Linux-based systems.
ls -l
The "ls -l" command is used to list the files in a long format and provides detailed information about the file such as Permissions, last modified, File size, Owner, etc.
ls -R
The "ls -R" command is used to list all the files and directories recursively. It displays the contents of the current directory and its subdirectories in a hierarchical manner.
You can concatenate two or more flags in a single command like so.
ls -la
Here in the 'ls' command, the functions of both the '-l' and '-a' flags are executed.
mkdir folder
This command is used to make a directory called "folder" in the current directory think of it as creating a new folder in Windows.
mkdir head_folder/sub_folder
The above command is used to make a directory inside an existing directory here, "head_folder" is already an existing directory and the "sub_folder" directory is created new.
Let's say you wanna create a folder in between "head_folder" and "sub_folder" This can be done by the following command.
mkdir -p head_folder/middle_folder/sub_folder
Here "middle_folder" is created in between "head_folder" and "sub_folder" Also the flag -p represents parent
cd
cd - change directory
The 'cd' command changes the working directory to the specified directory. Here are some examples of how you can use the 'cd' command:
- To navigate to a directory within the current directory:
cd directory_name
- To navigate to a specific directory path:
cd /path/to/directory
- To go up one directory level:
cd ..
- To go directly to your home directory:
cd ~
- To go to the previous directory you were in:
cd -
What are Environment variables?
In simple terms, Environment variables affect how commands and processes are executed. More technically Environment variables provide a way to store and retrieve information that can be used by various programs and processes. They are typically key-value pairs, where the variable name (key) is associated with a specific value.
Any instance of a running command is called a process.
echo $PATH
In the Linux terminal, the command "echo PATH" is used to display the value of the environment variable called "PATH."
The PATH variable in Linux contains a list of directories where the system looks for executable files when a command is entered in the terminal. Each directory in the PATH is separated by a colon (":"). When you execute a command in the terminal, the system searches for the corresponding executable file in these directories in the order they are listed.
The above image shows what a typical PATH looks like.
You can set up temporary environment variables within a session that will expire once you exit the terminal this can be done by the following command
export TEMP_VAR="Hello World"
Setting one-time Environment variables has various use cases including Customizing the Environment, Scripting and Automation, Testing and debugging, One-time specific sessions, Collaboration and shared Environments, etc.
pwd
The above command prints the full path of the current working directory. 'pwd' stands for "print working directory".
cat
The "cat" command stands for concatenate. It is used to list all the contents that are available inside a file in a Standard output.
cat adresses.txt
The above command shows what is written inside the file named addresses.
cat > catalog
The above command creates a file name catalog. The symbol '>' represents redirection of output since there isn't a folder named "catalog" It enables the user to create a file named "catalog" Then the user can edit in the subsequent command lines.
cat one.txt two.txt > three.txt
The "cat" command can also be used to merge the contents of two or more files into one as seen in the above command. Here the contents of files one.txt and two.txt are merged into three.txt
The "cat" command can be used to alter the contents of the file. Let's say you have to change all the characters in a particular file from lowercase to uppercase and save it in a new file this can be done by -->
cat lowercase.txt | tr a-z A-Z > uppercase.text
In the above code, the contents of a file named lowercase.txt are converted into uppercase and saved into uppercase.txt for this we use the operator '|' which is called the pipe operator. The pipe operator allows you to redirect the output of one command to serve as the input for another command in a sequential manner. It enables you to chain multiple commands together, creating a pipeline of data processing. Also 'tr' stands for translate (in this particular case this translates from lowercase to uppercase).
cat file.txt \
cat file2.txt
To add a new command before executing the current command hit backslash then press enter in the above commands cat file.txt is not executed immediately due to the backslash.
echo "Hello world"
The "echo" command prints whatever you say it to print.
You can also make the "echo" command to print a certain text into a file let's see how we can do it.
echo "Hello World" > file.txt
In the above command the text "Hello world" is printed into file.txt
Note that whatever was previously inside that file.txt (if it is a pre-existing file) is now overridden with "Hello World".
man echo
The "man" command helps to know information about a particular command. For example the above command outputs -->
touch new_file.txt
The Touch command is used to create a new file.
That is the end of this blog let's look into more awesome commands like moving files, renaming and deleting them and much more in the next part of Command Line Chronicles.