Find out how to take your Linux command fu to the next level with pipes.
There are many reasons why the Linux command line interface holds such an inherent flexibility. One reason is that, with ease, you can make two or more commands work together seamlessly. One such example of this is the pipe.
What is the pipe?
The pipe is that vertical bar on your keyboard that usually lives on the same key as the backslash. But what does the pipe do?
Simply put, it takes the output of whatever command is on the left and uses it as the input of the command on the right. To make this simple, I'm going to show you how to create a new file, add text to that file, and search for a string in that file, all from a single command using two pipes.
SEE: How to find files in Linux with grep: 10 examples (free PDF) (TechRepublic)
Individually these commands would be touch test, echo "this is my file" > test, and grep file test. Those three commands create a file named test, add the string "this is my test" to test, and then searches test for the string "file."
How do you run the commands with pipes?
That single command would be:
touch test | echo "this is my file" > test | grep file test
What happens with this is, the first pipe takes the output of touch test and sends it to the second command as input, which means the second command now has the necessary file available to echo text into. The second pipe then takes the output of the second command and uses it as input for the third command, which means the necessary string of text is available to search.
So the output of the first command is used as the input of the second command and the output of the second command is used as the input of the third command.
Using the pipe in Linux commands is a very simple way to make the CLI incredibly versatile, as well as efficient. Get used to using the pipe and take your Linux command fu to the next level.
- How to become a software engineer: A cheat sheet (TechRepublic)
- 10 free alternatives to Microsoft Word and Excel (TechRepublic download)
- Choosing your Windows 7 exit strategy: Four options (TechRepublic Premium)
- How to back up a local Linux directory to a remote Linux host with rsync (TechRepublic)
- How to set a Linux hostname without rebooting (TechRepublic)
- How to locate and close an open port in Linux (TechRepublic)
- Libarchive vulnerability can lead to code execution on Linux, FreeBSD, NetBSD (ZDNet)
- Must-read coverage: Programming languages and developer career resources (TechRepublic on Flipboard)