TRÄW🤟
TRÄW🤟

@thatstraw

27 Tweets 8 reads Nov 27, 2021
LINUX COMMAND LINE CHAINING⛓️:
In this thread I will you how to use the Linux command line in a more productive, professional and creative way.
🧵A thread ↓
#Linux #infosec #CyberSecurity #Security
WHAT IS COMMAND LINE CHAINING?
First things first, let's define what Linux command line chaining is?
Linux command chaining is a technique of merging (or joining) several commands such that each of them can execute in sequence depending on the operator that separates them and these operators decide how the commands will get executed.
It allows us to run multiple commands in succession (one after the other) or simultaneously (at the same time). It’s like short one line shell scripts that can be executed through the terminal directly.
They're several operators or simples we use in chaining commands, here is a list of them:
&, &&, ||, |, ; {}, (), \, >>, >, and !. Now you know what command line chaining is? Let's dive into some practical examples.
1. & (Ampersand)
This send a command, script, or process to the background. In other words, it makes a command run in the background.
2. && (Logical AND)
The `&&` operator will only execute the second command if the first command SUCCEEDS!, in other words if the first command exists with a zero status that's when the second command gets executed or else it will not get executed.
So if we try a domain which is down or doesn't exist the second command will not get executed as shown below.
3. || (Logical OR)
It's much more like an else statement in programming. The `||` execute the second command if the first command fails. In other words if the first command exits with a none zero status code.
4. ; (Semicolon)
The command following this operator will execute even if the command preceding this operator is not successfully executed or terminated with errors if you will.
5. ! (Logic NOT)
NOT (!) operator: It is used to negate an expression in command. We can use it to delete all files except one in a directory. Let's create a folder and name it xtremepentest , cd into it and then create a couple of files in there using the touch command as shown
Now let's delete all the files except for the `file.mp3`:
6. &&-|| (AND-OR)
It's a combination of the `AND OR` operator. It's much like the if-else statement in programming.
Say we want to check if a domain is online or exists. If it's online or exists download the `index.html` page else print `the domain is down`.
Now let's make the first command fail and see what happens:
7. | (Pipe)
The output of the command preceding this operator will act as an input of the command succeeding this operator. In other words the output of the another command will be given to the input of the other command.
Some commands don't support piping like the ping command as show below:
But fortunately, there's a way to solve this problem. We can use the `xargs` (xargs: build and execute command lines from standard input) command together with the `ping` command as shown below:
8. >,>>, < (Input-Output Redirection)
I'm pretty sure you are familiar with these ones? They simply redirects the output of a command or a group of commands to a file or stream. Here are some examples
The first command `echo "command line chaining is awesome" > xtremepentest.txt` creates a file with the name `xtremepentest.txt` (The redirection operator `> `allows us to give the output of the `echo` command as an input to `xtremepentest.txt` file, if the file doesn't exists
it will be created else if it does all the contents of the file will be replaced with text `command line chaining is awesome`.) while the second `wc -c -l < xtremepentest.txt` command will print the character count and line count in xtremepentest.txt.
Sometimes we don't want the contents of the file to replaced, we just want to append the new text at the end of the text file. This is where the redirection operator `>>` comes in hand. The image below clearly shows that:
9. \ (Concatenation)
Used to concatenate large commands over several lines in a shell. This improves the readability for the users. It is also use to join a filename with spaces between the words as show below:
10.() (Precedence/Parenthesis)
This operator is used to set precedent value so that multiple commands can execute in a given order.
In the first case, if the first command is successful then the second will get executed but the third command will not execute. But in the second case, the third command will get executed as the precedence is set using the `()` operator.
11. {} (Combination Operator):
The execution of the command succeeding this operator depends on the execution of the first command. The set of commands combined using `{}` operator executes when the command preceding it has successfully executed.
In the first case, hello will always get printed. If the file exists then the command will get executed as it is preceding the && operator. If we want to execute both second and third commands only if the file exists, then we use {} operators to combine the commands.
Congrats🥳🥂 you made it to the end. That's it for today's thread! Thanks for reading🥂 If you enjoyed this thread , RT the first tweet and follow me @xtremepentest for more future Linux, Networking and Cybersecurity content.

Loading suggestions...