Xargs, Printenv, Nano and Pipe
Roshansahurk
Posted on February 2, 2023
Xargs
- Xargs stands for extended arguments.
- The function of Xargs is to build and execute command lines from standard input.
- xargs is used in combination with
rm
,cp
,mkdir
, and other similar commands. - The synopsis for using Xargs command is:
xargs [options] [command [initial-arguments]]
Xargs executes the command one or more times with any initial-arguments followed by items read from standard input. The default command used by xarg is /bin/echo. Below are the some basic examples for a better understanding of xargs.
Combine Xargs
-
Let us consider the below two examples:
find [location] -name "[search-term]" -type f | xargs [command] find . -name '[search-term]' | xargs grep '[string-to-find-in-files]'
The first example above demonstrates using the find
command to find all files. The list of files is then piped to xargs
, which uses the reaspective command.
The last example above searched for all the files and piped them to xargs
, which then executes the grep
command on them.
Printenv
- Printenv stands for print environment.
- The function of the printenv is to print the values of the specified environment variables.
- If no variable is specified, it prints out the name and value pairs.
-
The synopsis for using printenv command is:
printenv [OPTION]... [VARIABLE]...
Let's display the values of specific environment variables.
$ printenv HOME
/root
$ printenv HOME SHELL
/root
/bin/bash
Nano
- Nano is a text editor loaded with several features such as opening multiple files, scrolling per line, undo/redo, syntax coloring, line numbering, and soft-wrapping overlong lines.
-
The synopsis for using nano command is:
nano '[Complete-name-of-the-file-with-its-extesion]'
Use tab for autocompletion of file name
- Commands are entered by using the Control (^) and the Alt or Meta (M-) keys.
Basic Commands :
Command | Explanation |
---|---|
CTRL + V | Scrolls page up. |
CTRL + Y | Scrolls page down. |
CTRL + A | Lets you jump to the beginning of the line. |
CTRL + E | Lets you to jump to the end of the line. |
CTRL + G | A Help window will pop out and show you all the available commands. |
CTRL + O | To save the file. Nano will ask you to edit or verify the desired file name. |
CTRL + K | It cuts the entire selected line to the cut buffer (similar to clipboard). |
CTRL + U | To paste the text from the cut buffer into the selected line. |
CTRL + C | Shows the current cursor position in the text (line/column/character). |
CTRL + X | To exit Nano text editor. It prompts a save request if you made any changes to the file. |
ALT + A | To select text. You can combine this command with CTRL + K to cut a specific part of the text to the cut buffer. |
Pipe
- Pipes are used for interprocess commmunication, i.e. it is used to combine two or more commands.
- The output generated by the output acts as an input for another command.
- Pipes are denoted by symbol
|
. -
Below are few examples where pipes are used.
cat [name-of-the-file] | less cat [name-of-the-file] | grep -v a | sort - r
-
Lets look at the output
- The cat command is used to open the contents of file.
- The less command shows the contents of file into a readable format.
- The grep command is used to find strings and values in a document.
- Sort command sorts out the contents of a file.
- In the first example the contents of a file are piped into the less command, which in further opens the file into a more readable format.
- In the last example the contents of the file are piped into grep. The output produced by grep is piped into sort. The output generated by grep will be specified words. The sort command will sort the output generated by grep in reverse order.
On success, zero is returned.
On error, -1 is returned.
You will see more about in above sections where pipes have been used with xargs.
Posted on February 2, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.