Elevate Your Linux Skills: Beginner to Intermediate

Elevate Your Linux Skills: Beginner to Intermediate

Mastering Linux Command Line

Linux isn't just a free operating system. It's a super helpful tool for developers. With its powerful command line, it can make a developer's life easier. For instance, you can automate lots of tasks, combine several commands into one shortcut, automate almost anything, and much more. Knowing how to use Linux the right way can save you tons of time. And you can also impress your colleagues by showing off your Linux skills and expertise.

Harnessing the Power of man Command

In Linux, the 'man' command is really helpful. It gives you info about commands and topics, whether you're just starting or exploring advanced stuff. It's great for both beginners and experienced users. 'man' is your guide to using Linux well, no matter your skill level.

Another good example can be 'find' command. find has a lot of arguments to choose from. It is not always possible to remember all of those. Suppose, you need to find a file 'config.js' in your project. You do not know whether the file name is all lowercase. You can use the man command to check the options available.

find command has -iname option to search case insensitive. So run the command find {project_path} -iname config.js to find the file. If you scroll a bit further in man output, you can see -type option that lets you mention what you want to find.

So, man command is your best friend while working on the Linux command line.

Input Output Redirection

Redirection is the feature in Linux that allows changing standard input and standard output when executing a command. By default, output is printed on a terminal and input is received from the user through the terminal. Suppose, you want to store the output in a file or take input from a file. Input Output Redirection is the way of doing it. There are three I/O streams in Linux for redirection.

  1. Standard input (stdin): By default the keyboard is the standard input stream. Using input redirection it can be changed to a file. It is numbered as 0.

  2. Standard Output (stdout): The terminal is the default output medium but the file can be used as the output stream using output redirection. It is numbered as 1.

  3. Standard Error (stderr): Same as stdout but instead of outputting normal info, this stream handles errors. For example, if a command is miswritten or lists a directory that does not exist.

Input Redirection

It is denoted by < symbol.

Output Redirection

Output redirection is denoted by both > and >> symbols. > means overwrite and >> means append. Output redirection does not handle error.

A good example of using output redirection is adding Environment variables and aliases in the system.

Error Redirection

Output redirection is denoted by 2> symbol. Error redirection only stores error output.

If you want to store both output and error in the same file use 2>&1 symbol.

Aliases

Some Linux commands are long so remember those can be an issue. To achieve some output you may need to run multiple commands. Aliases are a great feature of Linux that let's you shorten a long command or merge multiple commands into one simple command.

I am using cls as the abbreviation of clear. I am using zsh shell so I have written my aliases in ~/.zshrc file. If you are using bash you have to add alias to ~/.bashrc file. One simple way is to use output redirection.

echo 'alias cls="clear"' >> ~/.zshrc

It is also possible to group multiple aliases.

alias cls="clear"
alias ll="ls -al"
alias cl="cls; ll"

Monitoring Output

tail command by default prints the last 10 lines of a file. But with the option -f it monitors changes in a file and outputs an appended line.

watch command executes a command periodically. The two most used options are -d and -n. -n represents interval time. The default is 2 seconds. -d represents difference. Terminal highlights the changes. If the highlight needs to be persisted then -d=cumulative option is to be used.

watch -n 1 -d=cumulative "less first.txt"

Wildcards

Wildcards represent a pattern in Linux systems. Wildcards substitute characters in a string. There are mainly three wild cards *, ? and [].

  • Asterix (\):* Represents one or many characters. *.txt means any file that has .txt extension. hello.* means any file that has the name hello.

  • Question (?): It represents only one character. ?.txt means files that have one character in the name.

  • Square Brackets ([]): It specifies both options or ranges. hell[o,p,q].txt can represent hello.txt, hellp.txt, hellq.txt. [o-q] also represents the same strings.