Wednesday, June 5, 2013

Open all files where grep condition is matched

I wanted to open all of the files in an editor where a specific grep pattern was matched.  This was made easy with the use of grep's recursive search, the cut field (to trim out just the file name), and the open command.

For example, to recursively search all files from the current directory, find all files that contain "implements Serializable", and open them, use the following command:

grep -r "implements Serializable" . | cut -d ":" -f1 | xargs open

This will search all of the files for the text, strip off just the file name (what appears before the colon, ":"), and open using the default editor.

Wednesday, December 12, 2012

Reboot into specific grub option

The command grub-reboot <#> is useful to allow rebooting the machine without needing to be physically present to select a specific grub menu option.  Additional details are here:

http://www.cyberciti.biz/tips/force-grub-to-reboots-into-the-specified-os.html

Monday, April 16, 2012

I needed to find a file in a list of jar files, but did not know which one.

The following command will list all jar files in the current directory, and pass them into the "jar tf" command to display the table of contents. The results will then be grep'd for the file_name

find -name "*.jar" | xargs -n 1 jar tf | grep -i

Restarting specific process id

Several times a day gnome-shell will crash and need to be restarted, normally I go into a virtual console and type:
ps -ef | grep gnome-shell
and see results like:
mdeimel 23496 2008 14 09:43 ? 00:00:01 /usr/bin/gnome-shell
mdeimel 23509 1 0 09:43 ? 00:00:00 /usr/libexec/gnome-shell-calendar-server
mdeimel 23522 20953 0 09:43 pts/1 00:00:00 grep --color=auto -i gnome-shell
then I type:
kill -1 23496
in order to kill and restart the service (the '-1' parameter calls a restart of the same process)

In an attempt to speed-up this process I created an alias to handle everything at once. Instead of using the "ps" command I started using "pidof" to get the exact process id right away, and pass that into the kill command:
kill -1 `pidof gnome-shell`
and to save as an alias in ~/.bashrc I have
#Restart gnome
alias gnome="kill -1 `pidof gnome-shell`"

Restarting audio

After having some problems with the quality of my audio, I needed to restart the service, here are the commands:

To kill the existing audio process run:
pulseaudio -k

To start a new daemon thread for audio run:
pulseaudio -D