Pipes, aliases and scripts make Linux so much easier to use. Credit: Gorodenkoff / Shutterstock Some of the most versatile commands you can run on the Linux command line are those that enable you to pass the output of one command to another by using something called a “pipe”. You can also set up complex commands as “aliases” so that you only need to type a simple string to run them. Using pipes One of the features that I’ve always loved about working on the command line is the ability to use pipes expressed as vertical bars (i.e., |) to pass the output of one command to another command. For example, you can count the number of people logged into a Linux server with a command like this: $ who | wc -l 3 The who command shows you who is logged in. The wc -l command counts the lines of output sent by the who command. You can look at recently run commands with a command like this: $ history | tail -3 1014 ls bin 1015 uptime 1016 history | tail -3 You can look at the most recently updated files in the current directory with a command like this: $ ls -ltr | tail -3 -rw-r--r--. 1 justme justme 2459 Nov 17 13:59 AllTables -rwx--x--x. 1 justme justme 701 Nov 17 14:05 mkTable -rwxr-x---. 1 justme justme 86 Nov 20 11:25 myscript Some of the aliases in the sections below also make use of pipes. Creating aliases One of the most time-saving and clever things you can do as you warm up to Linux is to turn complex commands into aliases. In other words, you associate a word or abbreviation like “ll” with a command like “ls -l” or a single word like “recent” with a command like “history | tail -10” to view the most recent ten commands you’ve used. Aliases can save you time by reducing how much you have to type and can avoid typing errors by supplying all of command’s details – especially if you use them often (less typing) or infrequently (less brain power required to remember how to use them). To save aliases, insert them into your .bashrc file and they will be available every time you open a terminal window. alias myprocs="ps -ef | grep `whoami`" # show my processes alias c="clear" # clear the screen alias rec="ls -ltr | tail -3" # show recent file updates Understanding your search path $PATH Your search path on a Linux system is a sequence of file system locations separated by colons (:) that is used to find a command when you type it. If you type “date”, for example, your shell will look at each location on your search path until it finds it – finds an executable by that name. When it finds the command, it runs it. Your PATH setting should include directories like “/usr/bin” and “/usr/local/bin”. You can examine your search path with a command like this: $ echo $PATH /home/justme/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin Editing text files Before you can start creating text files – whether notes, lists or scripts – you will need to become familiar with one of the Linux text editors. The default on many Linux systems today is called “vim”. It makes it easy to add content, move around the lines in the files, replace words or lines, etc. Like aliases, scripts allow you to run a series of commands simply by typing the just their name. Here’s an example of a very simple script: #!/bin/bash echo This script is running in $SHELL echo The user running it is $USER The first line identifies the shell that will be used to run the script. Without this, the shell that is currently in use will run the script. The other two lines will display the script being used and the name of the user running it. To create a simple script like this, you would start vim with a command like “vim myscript”. Press “i” to get into input mode and then enter the text shown. When you’re done entering the text, press the enter key to exit input mode and then enter “:wq” to save the file and exit vim. Note that scripts have to be given execute permission with a command like that shown below to be run when you type the script name. $ chmod 750 myscript $ ./myscript This script is running in bash The user running it is justme In the example above, the script name is preceded with “./” since the current file system location is likely not in your search path. Writing scripts will make many of your tasks easier to run – especially when they include complex commands (e.g., loops that allow you to run commands with a series of values or case statements that run differently depending on some variable). Wrap-up Using pipes, aliases and scripts can make the Linux command line easier to use and more powerful. Related content how-to How to examine files on Linux Linux provides very useful options for viewing file attributes, such as owners and permissions, as well as file content. By Sandra Henry Stocker Oct 24, 2024 6 mins Linux how-to 8 easy ways to reuse commands on Linux Typing the same command again and again can become tiresome. Here are a number of ways you can make repeating commands – or repeating commands but with some changes – a lot easier than you might expect. By Sandra Henry-Stocker Oct 15, 2024 5 mins Linux news SUSE Edge upgrade targets Kubernetes and Linux at the edge SUSE Edge 3.1 includes a new stack validation framework and an image builder tool that are aimed at improving the scalability and manageability of complex Kubernetes and Linux edge-computing deployments. By Sean Michael Kerner Oct 15, 2024 6 mins Edge Computing Linux Network Management Software how-to Lesser-known xargs command is a versatile time saver Boost your Linux command line options and simplify your work with xargs, a handy tool for a number of data manipulation tasks. By Sandra Henry Stocker Oct 11, 2024 6 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe