The main part of Linux is its terminal. Whenever you interact with the terminal, you do that by passing some input, which is often referred to as a standard stream or I/O stream. It provides a way to interact with input-output operations.
What is a shell?
- It is a command-line interface that takes commands as input and converts them into instructions. For example, when you give a command to change the directory, you write "python3." But how does the computer know what "python3" is? This is where the shell comes in and informs the computer that "python3" is an executable file stored in the /usr/local/bin directory. The part of the terminal where you write commands is known as the command prompt.
So, for example, when you type "python3," the computer needs to know where to look for the corresponding files. All these files are present in the environment variable named "PATH." You can view all the directories included in the PATH variable by using the command "echo $PATH." The "echo" command is typically used to display output on the terminal. By executing this command, it will show you a list of directories specified in the PATH variable.
Standard Stream:
The standard stream is divided into three parts.
In the Linux shell, the standard streams have specific names that are commonly used in shell scripting and command-line operations:
Standard Input (stdin): In the Linux shell, the standard input is represented by the file descriptor number 0 or the symbol "<". It is typically associated with the keyboard or a file, and you can redirect input from a file to stdin using the "<" symbol. For example, "command < input.txt" redirects the contents of input.txt to the standard input of the command.
Standard Output (stdout): In the Linux shell, the standard output is represented by the file descriptor number 1 or the symbol ">". It is typically associated with the screen or console, and you can redirect the output to a file using the ">" symbol. For example, "command > output.txt" redirects the standard output of the command to the file output.txt.
Standard Error (stderr): In the Linux shell, the standard error is represented by the file descriptor number 2 or the symbol "2>". It is also associated with the screen or console, but it is used specifically for error messages and diagnostics. You can redirect the standard error to a file using the "2>" symbol. For example, "command 2> error.txt" redirects the standard error output of the command to the file error.txt. If you want to remove the error messages shown in the console, you can use "2> /dev/null".
Commands list:
Working with directories:
ls or ls -a
: This command is used to see the files present in the current directory. If you want to see hidden files, use "ls -a" (any file whose name starts with a dot is hidden). You can also use "ls -l" to see more information about the current directory, and "ls -r" to see all the files present in the subdirectory.open "directory name"
: This command is used to open the given directory.mkdir "folder name"
: This command is used to create a directory/folder. It will create a folder where you are running this command.mkdir -p directory1/parent/directory2
: The "-p" flag will create the directory under directory1. If directory1 is not present, it will create it.cd "directory name"
: This command is used to change the directory to the specified directory. Use "cd .." to go to the parent directory and "cd -" to go to the previously opened directory.
Working with files:
cat "filename"
: This command shows all the contents of the file on the terminal.filename > "filename"
: This command creates a file with the specified name. For example, "cat file.txt fileTwo.txt > fileThree.txt" copies the contents from two files and pastes them into the third file.man "command name"
: This command provides information about what a command does. For example, "man tr" explains the "tr" command.cat filename.txt | tr a-z A-Z
> filename2.txt: This command converts all the contents in "filename.txt" to uppercase and pastes them into "filename2.txt". Here, the pipe "|" is used for redirection.touch "filename.txt"
: This command is used to create a new file.cp initialFile.txt finalFile.txt
: This command copies the contents of the initialFile.txt and pastes them into finalFile.txt.mv initial-path final-path
: This command is used to move a file from the initial path to the final path.less filename
: This command opens a new window and shows the content of the file. You can quit the window by pressing "q".
Pipe (|):
- It redirects stdout as stdin. For example, "ls -la /etc | less" opens a new window and shows all the files present in the /etc folder. Here, the output is treated as input, and the pipe acts as a feeder for the "less" command.
Head:
- It prints the first ten lines of the file. For example, "head /var/log/syslog" prints the first ten lines. If you want to print the first 15 lines, you can use "head -n 15 /var/log/syslog".
Tail:
- It gives you the last ten lines of the file. For example, "tail /var/log/syslog" shows the last ten lines.
Sort:
- It sorts the list present in the file. For ascending sort, use "sort texting.txt". For descending sort, use "sort -r texting.txt".
tr (Translate):
- It converts lowercase characters into uppercase. For example, use "cat testing.txt | tr a-z A-Z".
uniq (Unique):
- This command gives all the unique values present in the file. For example, "uniq testing.txt" shows all the unique values. Use "-c" to show the number of occurrences, "-u" to show only unique values, and "-d" to show values that appear more than once. Note that it works only when two duplicate values are adjacent to each other.
grep:
- This command is used to extract a specific expression from the output. For example, "ls | grep desktop" extracts the word "desktop" from the output of the "ls" command.
Environment variables:
These are variables whose values keep changing and are used by processes to get things working. For example, using
echo $PWD
shows the current directory. If you change the directory to "desktop" and execute the command again, it will show a different output. Commands like "pwd" use environment variables to work."PATH" is the most important environment variable, which stores all the directories where the system searches when you write a command into the shell.
Users can set environment variables using the format
ENVIRONMENT_VARIABLE=VALUE
. However, these environment variables will only persist until the terminal is active. Once the terminal is closed, the process and jobs associated with that terminal will also end.Environment variables for specific users are present in their home directory (~/.bashrc).
To check the value of an environment variable, you can use the "echo" command. For example,
echo $LOGNAME
displays the value of the "LOGNAME" environment variable."printenv" is a command used to see all the environment variables.
This is the end of our blog, but not the journey! We will be covering more fascinating topics in future blogs. Stay tuned to extract all the juice from our blogs! ๐๐ Stay connected for exciting updates! ๐โจ #TechBlogs