0

This is the exact question: Append to a file in your home directory called 'water.txt' a list of all processes that have the string 'er' at the end of their name.

I know the command to list running process are ps -A, or top but the hard part is the appending only certain processes to a new file based on pattern match

The two commands that come to mind are cut and grep but I don't know exactly how to combine them together especially because the list of processes are not stored in a file/ or are they?

Percy Shey
  • 27
  • 4
  • 2
    Can you show your attempt? – 123 May 16 '17 at 20:45
  • Didn't try that, but here what could help you to start: `ps -aux | grep "*er*" >> ~/water.txt` – grundic May 16 '17 at 20:48
  • @grundic thanks but why are they 2 asterisks around the word 'er'. when i try the command with and without the asterisks, it output different results – Percy Shey May 16 '17 at 20:52
  • I don't remember exactly the nature of grep. It might be that you have to use `.*` as grep uses regexes, not globs. You can read more here: http://stackoverflow.com/questions/1069302/using-the-star-sign-in-grep – grundic May 16 '17 at 20:57

2 Answers2

0

If you're looking to append all processes ending with the string er, you should use a mixture of ps and grep:

ps -aux | grep 'er$' >> ~/water.txt

The $ at the end of the er string is to make sure the process finishes with those 2 characters.

Girrafish
  • 2,320
  • 1
  • 19
  • 32
0

A command that is the combination of ps and grep is called pgrep.

With that command, you can do this to list all files that end in er:

pgrep -fa 'er$'

The option '-f' is to use the "full" name of the commands, and '-a' is to list the full name of the command with the PID number.

And to redirect the output to a file, just use '>':

pgrep -f 'er$' > ~/water.txt

The ~ means to use your home directory.