Free Republic
Browse · Search
General/Chat
Topics · Post Article

Skip to comments.

Command-Line Tip: Put Down the Pipe
Linux Journal ^ | 22 January 2019 | Kyle Rankin

Posted on 01/28/2019 5:06:01 AM PST by ShadowAce

Learn a few techniques for avoiding the pipe and making your command-line commands more efficient.

Anyone who uses the command line would acknowledge how powerful the pipe is. Because of the pipe, you can take the output from one command and feed it to another command as input. What's more, you can chain one command after another until you have exactly the output you want.

Pipes are powerful, but people also tend to overuse them. Although it's not necessarily wrong to do so, and it may not even be less efficient, it does make your commands more complicated. More important though, it also wastes keystrokes! Here I highlight a few examples where pipes are commonly used but aren't necessary.

Stop Putting Your Cat in Your Pipe

One of the most common overuses of the pipe is in conjunction with cat. The cat command concatenates multiple files from input into a single output, but it has become the overworked workhorse for piped commands. You often will find people using cat just to output the contents of a single file so they can feed it into a pipe. Here's the most common example:


cat file | grep "foo"

Far too often, if people want to find out whether a file contains a particular pattern, they'll cat the file piped into a grep command. This works, but grep can take a filename as an argument directly, so you can replace the above command with:


grep "foo" file

The next most common overuse of cat is when you want to sort the output from one or more files:


cat file1 file2 | sort | uniq

Like with grep, sort supports multiple files as arguments, so you can replace the above with:


sort file1 file2 | uniq

In general, every time you find yourself catting a file into a pipe, re-examine the piped command and see whether it can accept files directly as input first either as direct arguments or as STDIN redirection. For instance, both sort and grep can accept files as arguments as you saw earlier, but if they couldn't, you could achieve the same thing with redirection:


sort < file1 file2 | uniq
grep "foo" < file

Remove Files without xargs

The xargs command is very powerful on the command line—in particular, when piped to from the find command. Often you'll use the find command to pick out files that have a certain criteria. Once you have identified those files, you naturally want to pipe that output to some command to operate on them. What you'll eventually discover is that commands often have upper limits on the number of arguments they can accept.

So for instance, if you wanted to perform the somewhat dangerous operation of finding and removing all of the files under a directory that match a certain pattern (say, all mp3s), you might be tempted to do something like this:


find ./ -name "*.mp3" -type f -print0 | rm -f

Of course, you should never directly pipe a find command to remove. First, you should always pipe to echo to ensure that the files you are about to delete are the ones you want to delete:


find ./ -name "*.mp3" -type f -print0 | echo

If you have a lot of files that match the pattern, you'll probably get an error about the number of arguments on the command line, and this is where xargs normally comes in:


find ./ -name "*.mp3" -type f -print0 | xargs echo
find ./ -name "*.mp3" -type f -print0 | xargs rm -f

This is better, but if you want to delete files, you don't need to use a pipe at all. Instead, first just use the find command without a piped command to see what files would be deleted:


find ./ -name '*.mp3" -type f

Then take advantage of find's -delete argument to delete them without piping to another command:


find ./ -name '*.mp3" -type f -delete

So next time you find your pinky finger stretching for the pipe key, pause for a second and think about whether you can combine two commands into one. Your efficiency and poor overworked pinky finger (whoever thought it made sense for the pinky to have the heaviest workload on a keyboard?) will thank you.


TOPICS: Computers/Internet
KEYWORDS: linux
Navigation: use the links below to view more comments.
first 1-2021-4041-51 next last
A few tips for some command-line stuff. I realize not everyone is into the command line. Just think of it as an optional extension that can make your work more efficient and powerful.

It's not required (much) anymore.

1 posted on 01/28/2019 5:06:01 AM PST by ShadowAce
[ Post Reply | Private Reply | View Replies]

To: ShadowAce

I just knew legalization was a bad idea. I think we should all put down the pipe.


2 posted on 01/28/2019 5:07:32 AM PST by ClearCase_guy (If White Privilege is real, why did Elizabeth Warren lie about being an Indian?)
[ Post Reply | Private Reply | To 1 | View Replies]

To: rdb3; Calvinist_Dark_Lord; JosephW; Only1choice____Freedom; Ernest_at_the_Beach; martin_fierro; ...

3 posted on 01/28/2019 5:07:47 AM PST by ShadowAce (Linux - The Ultimate Windows Service Pack)
[ Post Reply | Private Reply | To 1 | View Replies]

To: ClearCase_guy

4 posted on 01/28/2019 5:15:42 AM PST by Vaquero (Don't pick a fight with an old guy. If he is too old to fight, he'll just kill you .)
[ Post Reply | Private Reply | To 2 | View Replies]

To: ShadowAce

Thank you for putting in the disclaimer, Because this even scared me. Way over my pay grade and capabilities. I won’t be piping my cats anywhere. lol


5 posted on 01/28/2019 5:16:28 AM PST by Openurmind
[ Post Reply | Private Reply | To 1 | View Replies]

To: ShadowAce
I realize not everyone is into the command line.

LOL! Ya think??

You and me and about a dozen others still alive - everyone else is GUI-bound.

6 posted on 01/28/2019 5:18:01 AM PST by grobdriver (BUILD KATE'S WALL!)
[ Post Reply | Private Reply | To 1 | View Replies]

To: ShadowAce

Dumb article that misses the whole point of the “Unix way”, which IS to stitch various simple commands together to accomplish a task
...


7 posted on 01/28/2019 5:21:04 AM PST by SecondAmendment (This just proves my latest theory ... LIBERALS RUIN EVERYTHING!)
[ Post Reply | Private Reply | To 1 | View Replies]

To: ShadowAce
What ever can be type on a command line can be placed in a shell script and run as a command itself by changing the permission of the text file to include the execute permission.

# vi hellfrworld.sh
vi> echo "Hello Freeper World."
vi> exit(0)
vi> :wq
# make hellofrworld
make> cp hellofrworld.sh hellofrworld
make> chmod +x hellofrworld
#
#./hellofrworld
Hello Freeper World.
#

man bash

If used the bourne, korn, csh, tcsh, wksh, and of courcs bash.

The ultimate shell however is the emacs editor.

The command line rules!

The command line language itself is a powerful programming tool and is frequently use used to automate administrative tasks and failrly complex programs and is frequently the glue invoked behind the scenes of a Graphical User Interface (GUI) that i merely passed the parameters that were selected on the GUI.

When I was a grad student most MS thesis were on the design of Graphical User Interfaces from someone getting their MS CSA with a Human Computer Interaction concentration.

We affectionately called GUI's the idiot interface back in the day!

antediluvian - of course!

But I degress!

8 posted on 01/28/2019 5:24:56 AM PST by lurked_for_a_decade (Imagination is more important than knowledge! ( e_uid == 0 ) != ( e_uid = 0 ). I Read kernel code.)
[ Post Reply | Private Reply | To 1 | View Replies]

To: SecondAmendment
I think you missed the point.

While the "Unix way" is to stitch various command together to get a job done, as you say, there can be more efficient ways of writing those commands.

No point in chaining commands together when one command will do the whole job.

9 posted on 01/28/2019 5:25:10 AM PST by ShadowAce (Linux - The Ultimate Windows Service Pack)
[ Post Reply | Private Reply | To 7 | View Replies]

To: Openurmind
I won’t be piping my cats anywhere

That's a pity.


10 posted on 01/28/2019 5:26:50 AM PST by Lazamataz (McCain's passing ended up being + 2 net Republican Senators. Him, and Lindsey Graham.)
[ Post Reply | Private Reply | To 5 | View Replies]

To: lurked_for_a_decade

If I could spell and type at the same time I’d rule the world!


11 posted on 01/28/2019 5:27:02 AM PST by lurked_for_a_decade (Imagination is more important than knowledge! ( e_uid == 0 ) != ( e_uid = 0 ). I Read kernel code.)
[ Post Reply | Private Reply | To 8 | View Replies]

To: ShadowAce

12 posted on 01/28/2019 5:27:05 AM PST by Larry Lucido
[ Post Reply | Private Reply | To 1 | View Replies]

To: lurked_for_a_decade

I also hate cats! Sorry viking kitty. I just do. Almost as much as I hate Democrats. (LOL)


13 posted on 01/28/2019 5:28:29 AM PST by lurked_for_a_decade (Imagination is more important than knowledge! ( e_uid == 0 ) != ( e_uid = 0 ). I Read kernel code.)
[ Post Reply | Private Reply | To 11 | View Replies]

To: Vaquero

I see what you did there.


14 posted on 01/28/2019 5:30:58 AM PST by Quality_Not_Quantity (Even my cat voted Republican)
[ Post Reply | Private Reply | To 4 | View Replies]

To: lurked_for_a_decade
Viking Kitty is not amused

15 posted on 01/28/2019 5:34:05 AM PST by Waverunner (I'd like to welcome our new overlords, say hello to my little friend)
[ Post Reply | Private Reply | To 13 | View Replies]

To: lurked_for_a_decade
The command line language itself is a powerful programming tool and is frequently use used to automate administrative tasks and fairly complex programs ...

Yup. I frequently write script-writing scripts, with enough intelligence in them to parse out information from the input file so that it can do many different things on a per-server basis.

With scripts, I can verify SAN connections, check and reset SNMP, perform multiple-password logins, configure network bonding/teaming, make configuration changes across 1500+ servers in less time than most people can do 10 servers, check ssh connections to any number of servers, and, of course, perform our monthly scheduled updates on all of our servers, determing whether or not to reboot them afterwards, and to notify the application owner of the update/reboot beforehand.

With scripts, I have singlehandedly lowered the effort of our team from a month-long task to something a single person can do in less than haf an hour.

I love the command line.

16 posted on 01/28/2019 5:35:12 AM PST by ShadowAce (Linux - The Ultimate Windows Service Pack)
[ Post Reply | Private Reply | To 8 | View Replies]

To: lurked_for_a_decade
I also hate cats! Sorry viking kitty. I just do. Almost as much as I hate Democrats. (LOL)

Or Democats, for that matter...


17 posted on 01/28/2019 5:36:37 AM PST by COBOL2Java (Marxism: Trendy theory, wrong species)
[ Post Reply | Private Reply | To 13 | View Replies]

To: Lazamataz

One thing cool about it though, at least with Linux you can do anything you want to do with it if you like.


18 posted on 01/28/2019 5:39:24 AM PST by Openurmind
[ Post Reply | Private Reply | To 10 | View Replies]

To: grobdriver

I pity those that are unaware of the command line powers, particularly for bypassing recalcitrant GUI functions that don’t do what they are supposed to do.


19 posted on 01/28/2019 5:39:44 AM PST by Calvin Locke
[ Post Reply | Private Reply | To 6 | View Replies]

To: Waverunner

I don’t have my photos on a server so that I can’t post one here but my response would be a military K-9 dog with really wide open mouth with exposed death, growling and salivating and barking with a caption that says, “Go ahead and run! He likes fast food!”


20 posted on 01/28/2019 5:40:05 AM PST by lurked_for_a_decade (Imagination is more important than knowledge! ( e_uid == 0 ) != ( e_uid = 0 ). I Read kernel code.)
[ Post Reply | Private Reply | To 15 | View Replies]


Navigation: use the links below to view more comments.
first 1-2021-4041-51 next last

Disclaimer: Opinions posted on Free Republic are those of the individual posters and do not necessarily represent the opinion of Free Republic or its management. All materials posted herein are protected by copyright law and the exemption for fair use of copyrighted works.

Free Republic
Browse · Search
General/Chat
Topics · Post Article

FreeRepublic, LLC, PO BOX 9771, FRESNO, CA 93794
FreeRepublic.com is powered by software copyright 2000-2008 John Robinson