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 previous 1-2021-4041-51 next last
To: ShadowAce
uniq doesn't work if the duplicate lines are not adjacent. It's necessary to pipe it after sort unless the records are already in order.

sort | uniq

uniq man page

21 posted on 01/28/2019 5:40:06 AM PST by Textide (Lord, grant that I may always be right, for thou knowest I am hard to turn. ~ Scotch-Irish prayer)
[ Post Reply | Private Reply | To 1 | View Replies]

To: Textide

Oops, reread it. They keep sort before unique, and just removed the cat. Makes sense. My mistake.


22 posted on 01/28/2019 5:41:17 AM PST by Textide (Lord, grant that I may always be right, for thou knowest I am hard to turn. ~ Scotch-Irish prayer)
[ Post Reply | Private Reply | To 21 | View Replies]

To: Textide
uniq doesn't work if the duplicate lines are not adjacent.

Right--that's why he piped the results to uniq after a sort (in the article).

23 posted on 01/28/2019 5:43:11 AM PST by ShadowAce (Linux - The Ultimate Windows Service Pack)
[ Post Reply | Private Reply | To 21 | View Replies]

To: ShadowAce

Retired now, but spent many happy years writing C programs and shell scripts using cat, grep and awk. I kind of miss it, and still have the occasional coding dream.


24 posted on 01/28/2019 5:51:20 AM PST by laker_dad
[ Post Reply | Private Reply | To 1 | View Replies]

To: lurked_for_a_decade

Jesus, “my apologies to the WWFREDCOTFRRF for that jesus comment” .... death = teeth.

or
set -o vi
vi “post”
esc
1G
0
fd
ct
:wq
# elisa
What is WWFREDCOTFRRF?
elise> That is the Westboro Wing of the FreeRepublic Evangalical Dispensationalist Causus of the FreeRepublic Religious form.
# elisa
Thank-you.
elisa>
Your welcome, Lurkedforadecade.
You should update your name lurked, you’ve been here since FR was on AOL and lurked since 1998.
# elisa
shut up.
elisa> I’m sorry lurked, I can’t do that!
Also lurked, There’s no need to apologize to the WWFREDCOTFRRF.
# cd /;
rm -f `find . -owner “WWFREDCOTFRRF” -and \(-owner viking-kitty \& -type image \) -print

or is that

# find . -owner “WWFREDCOTFRRF” -or \(-owner viking-kitty -and -type image \) -exec rm {} ;

cd /; rm -fr *
#exit(1)


25 posted on 01/28/2019 6:02:53 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 20 | View Replies]

To: ShadowAce

try sort -u


26 posted on 01/28/2019 6:04:24 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 23 | View Replies]

To: laker_dad

Want to work on an android app together that I’ve been thinking of. I’m also retired. If it make money great! If not, it will be fun. I’ve been making slow progress for a year.


27 posted on 01/28/2019 6:06:03 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 24 | View Replies]

To: SecondAmendment

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

Precisely.


28 posted on 01/28/2019 6:14:39 AM PST by LaRueLaDue
[ Post Reply | Private Reply | To 7 | View Replies]

To: Calvin Locke
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.

Firefox on my Linux system at home is notorious for that. Try to open Firefox and a warning pops up, "Firefox is already running" even after I have exited from it an hour before.

No worries. Open a terminal:

ps -ef | grep firefox
kill -9 16478

It's dead Jim.

29 posted on 01/28/2019 6:22:10 AM PST by Bloody Sam Roberts (Atrophy of science is visible when the spokesman goes from Einstein to Sagan to Neli Degrasse Tyson.)
[ Post Reply | Private Reply | To 19 | View Replies]

To: Bloody Sam Roberts
Check out the pkill command:

pkill firefox

30 posted on 01/28/2019 6:43:05 AM PST by ShadowAce (Linux - The Ultimate Windows Service Pack)
[ Post Reply | Private Reply | To 29 | View Replies]

To: LaRueLaDue

I am surprised they didn’t recommend running each command in its own Docker container !


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

To: grobdriver

I still use command line fairly often. But nothing on this scale.

I used it daily at work for a number of years, but before that I used it often in my Linux transition years.

For transforming large numbers of image files, command line is king. Image Magic is truly a work horse.


32 posted on 01/28/2019 7:36:58 AM PST by Texas Fossil ((Texas is not where you were born, but a Free State of Heart, Mind & Attitude!))
[ Post Reply | Private Reply | To 6 | View Replies]

To: ShadowAce
> 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.

LOL. I spend 8 hours a day working at a Bash prompt, mostly Ubuntu, some BSD. Well, 7.5 hours a day -- the other 30 minutes I'm in an RDP session to some Windows box. And half of -that- time I'm running Cygwin. :-)

33 posted on 01/28/2019 7:52:10 AM PST by dayglored ("Listen. Strange women lying in ponds distributing swords is no basis for a system of government."`)
[ Post Reply | Private Reply | To 1 | View Replies]

To: ShadowAce
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.

Which raises the question: who let the org get into that kind of shape in the first place? Make sure that it's known and they are removed from further influence on the architecture.

34 posted on 01/28/2019 7:52:59 AM PST by glorgau
[ Post Reply | Private Reply | To 16 | View Replies]

To: glorgau

Well, it grew very complex over time. By the time I showed up, it was quite large. In fact it is 4 times larger now than when I started. The time frame here is measured in years and decades.


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

To: ShadowAce

You forgot about the find . -name foo\* -exec something{} \;...


36 posted on 01/28/2019 8:49:46 AM PST by Kommodor (Terrorist, Journalist or Democrat? I can't tell the difference.)
[ Post Reply | Private Reply | To 1 | View Replies]

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

This doesn't seem to do what he is claiming it will.

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

$ find . -type f | grep mp3$ | head -3
./Essential_Stevie_Ray_Vaughan/Cold_Shot.mp3
./Essential_Stevie_Ray_Vaughan/Give_Me_Back_My_Wig.mp3
./Essential_Stevie_Ray_Vaughan/Couldnt_Stand_The_Weather.mp3

37 posted on 01/28/2019 9:08:59 AM PST by zeugma (Power without accountability is fertilizer for tyranny.)
[ Post Reply | Private Reply | To 1 | View Replies]

To: Textide
uniq doesn't work if the duplicate lines are not adjacent. It's necessary to pipe it after sort unless the records are already in order.

Yup. Ran into that years ago when a script I was writing just wouldn't work like I thought it should.

38 posted on 01/28/2019 9:11:44 AM PST by zeugma (Power without accountability is fertilizer for tyranny.)
[ Post Reply | Private Reply | To 21 | View Replies]

To: ShadowAce
I find that pipes are useful for professional email signatures. For example:

Linda Ronstadt
Sr. Vice President | Simple Dreams Productions
New York | Los Angeles | London

39 posted on 01/28/2019 9:21:52 AM PST by SamAdams76
[ Post Reply | Private Reply | To 1 | View Replies]

To: ShadowAce

Even easier. Thanx.


40 posted on 01/28/2019 9:34:49 AM PST by Bloody Sam Roberts (Atrophy of science is visible when the spokesman goes from Einstein to Sagan to Neli Degrasse Tyson.)
[ Post Reply | Private Reply | To 30 | View Replies]


Navigation: use the links below to view more comments.
first previous 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