Docker remove ALL volumes not attached

Docker can be tricky to debug. There’s instances where after upgrading a container the volume that was previously attached fails to work.

In this case it was a dev image and the quickest, and dirtiest, option was to clear out and rebuild. I could go one by one to delete the specific volumes.

docker volume ls
docker volume rm {specific_docker_container}

Instead I chose to delete all volumes not currently in use. This worked for my use case as I only have two dockers setups, left the one I wanted to keep running while executing the following commands.

docker volume rm `docker volume ls -q -f dangling=true`

grep without filenames

I needed to grep a directory, and sub directories, and not displaying the filenames in the output. This is when the man page came to the rescue.

man grep | grep filename

Output

       -H, --with-filename
       -h, --no-filename

There we go, -h, –no-filename, is what I needed.

Example with output to a file:

grep -r "searching-for-this" . --no-filename > /tmp/test.txt

How to check cp progress after cp has started

First, the easier way to do this is to use rsync in the first place.

rsync -avh –progress sourceDirectory destinationDirectory

However, sometime you think cp will be quick and you already kicked it off. Here is a quick way to check the progress of a copy command after you’ve already started.

watch lsof -p`pgrep -x cp`

This will let you know what it is transferring and how much it has left to do. In a way it provides a way to check the progress of the cp, copy comand.

To find out more check out these two SO links:

Check progress

CP Command Help

Ubuntu Command line Display Driver Change

Recently upgraded to 15.10 and that looked okay at first. However, the displa driver that it installed, NVIDIA, was causing issues. So I defaulted back to the open source driver and reboot. MISTAKE! Ended up causing the system to crash and couldn’t do anything about it.

I rebooted into root terminal. Then ran a command to display the available drivers.

ubuntu-drivers devices

The spit out a list of drivers. Then I reinstalled the latest available nvidia driver.

apt-get install nvidia-352-updates

Bash test if file exists

Using bash I needed to check that a file exists, then do something. This one turns out to be super simple. I wanted to conditionally load a db file if it existed in this case.

What I needed was, a check for the file in bash. Then execute the command. In this instance it was to load a postgres restore file.

db_dump="/home/vagrant/files/db.dump"
[ -f $db_dump ] && printf "Loading DB...\n";sudo su postgres -c "pg_restore -d db_name_here $db_dump;" || printf "No db dump to load\n"

Another way to write this is to use a full if/else conditional. Admittedly this is a little cleaner.

db_dump="/home/vagrant/files/db.dump"
if [ -f "$db_dump" ]
then
	printf "Loading DB...\n";
        sudo su postgres -c "pg_restore -d db_name_here $db_dump;"
else
	printf "No DB file found.\n";
fi