My Bash Tutorial

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

My Bash Tutorial

fschmidt
Administrator
Reply | Threaded
Open this post in threaded view
|

Re: My Bash Tutorial

OmegaKV
I think this covers most of the bash I use on a day to day basis. A couple of commands not mentioned which I use every day are the "grep" and "more" commands. This is less commonly used and not essential, but I also find the "-" symbol convenient, which resolves to the previous directory, so that I can do "cd -" and it brings me to the previous directory I was in. I also find "realpath" is convenient, because if I want to get the absolute path of a file I can simply type "realpath ../filename" instead of manually constructing it by "cd ..; echo $(pwd)/filename; cd -"
Reply | Threaded
Open this post in threaded view
|

Re: My Bash Tutorial

fschmidt
Administrator
Thanks for the feedback.  I use grep very rarely since I prefer using GUI search where I can easily pick the files I want to edit from the result.  When is grep preferable to GUI search?  I never use "more" since I always prefer using my text editor.  I also never use "-" which seems like a hack specific to "cd".  I mean "cd -" works but "ls -" doesn't.  And "realpath" isn't available on the Mac.
Reply | Threaded
Open this post in threaded view
|

Re: My Bash Tutorial

OmegaKV
A command's output can be filtered by grep if it is piped to grep.

For example:

"ps aux | grep firefox" to look for instances of firefox running. I'm not sure if "ps aux" works on mac, but it prints a table containing the names of all the running processes and their process IDs, username, etc.

At work we have a command that monitors a certain reading and prints a bunch of usually unnecessary diagnostic info. The command runs indefinitely. Every time the reading value changes, it prints the value (e.g. "value: 5.4") plus all the diagnostic info all over again. The diagnostic info is a distraction that makes it difficult to see how the reading value is changing. So I can do 'monitor | grep "value:"', so that when the reading value changes it prints only the one line containing the value, and this makes it easier for a human to see how the value is changing.

If the reading value still changes too rapidly for me to follow what is happening, I might want to be able to control when it prints each output line. To do this I modify the command to the following: 'monitor | grep "value:" | more', and now it only prints each new output line when I press enter.
Reply | Threaded
Open this post in threaded view
|

Re: My Bash Tutorial

fschmidt
Administrator
On my machine, I mostly use a GUI activity monitor instead of "ps".  I mostly use things like "ps", "grep", and "more" when I ssh into a remote machine.  But this is advanced, so I think I can leave it out of the tutorial.