Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7681427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:20:21+00:00 2026-05-31T18:20:21+00:00

Background: sometimes I use ps aux | grep process_name to figure out some statistics

  • 0

Background: sometimes I use ps aux | grep process_name to figure out some statistics of a process. But I can’t remember the headers of ps output. So I need to do:

ps aux | tee 1.txt | grep process_name > 2.txt
cat 1.txt | head -1 | cat - 2.txt

So my questions is: is there a way to achieve this without the two temporary files, and preferably use one line of commands instead of two lines?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:20:22+00:00Added an answer on May 31, 2026 at 6:20 pm

    The simplest way is just to use something a bit smarter than grep. For example:

    ps aux | perl -ne 'print if $. == 1 || m/process_name/'
    

    will print the first line of ps aux‘s output, plus any line that matches process_name (which is a Perl regular expression — more powerful than POSIX BRE’s, and therefore more complicated, but I don’t think you’ll find any surprises if you’re just searching for a process name).

    Edited to add: For that matter, since ps aux‘s headers are unlikely to change between runs, you could write:

    ps aux | head -1 ; ps aux | grep process_name
    

    though I don’t know if that still counts as “one line of commands”. 🙂

    More options: Since you don’t like the above, here are some more. This one will read and print the first line directly, and then leave the rest for grep to read and process:

    ps aux | ( ( read -r LINE ; echo "$LINE" ) ; grep process_name )
    

    This one will cause every line to be written both to standard output (file descriptor 1) and to a custom file descriptor (3); then head gets standard output, while grep gets what was written to the custom file descriptor:

    ( ( ps aux | while read -r LINE ; do echo "$LINE" ; echo "$LINE" 1>&3 ; done | head -1 1>&4 ) 3>&1 | grep process_name ) 4>&1
    

    That one is the most general way that I know of to do what you’re asking for — at least, using only Bash builtins and features — but as you can see, it’s quite unwieldy, which is why I recommend the simpler ways when you don’t need the full power of this approach. Also, that last one no longer guarantees that headers come first; on my system, the output of head seems to consistently end up after the output of grep. (I suppose that could be addressed by swapping the positions of the head and grep commands, but that still wouldn’t be reliable so far as I know, and it seemed to have weird effects when I tried it just now.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

the site is resizable, so I can not use px, and sometimes it's a
First some background. I have a batch-type java process run from a DOS batch
I've heard that list comprehensions can be slow sometimes, but I'm not sure why?
Sometimes I get a broken background in Chrome. I do not get this error
Background: Some time ago, I built a system for recording and categorizing application crashes
Background: There are a lot of great tutorials and tricks pages for Vim, but
I use performSelectorInBackground to do calculations on a background thread to keep the main
I'm trying to use shadowbox in multiple occasions: sometimes I happen to need more
I was wondering if somebody has some insight on this issue. A little background
Did some search online, found simple 'tutorials' to use named pipes. However when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.