Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Monday, February 3, 2020

Git project branch and directory info

To get the Git top level, or root, of the repo:

git rev-parse --show-toplevel

Get the current current branch name:

git rev-parse --abbrev-ref HEAD

Get the current working directory relative to project root:

echo "${PWD##$(git rev-parse --show-toplevel)}"

Get the current working directory relative to project root and the branch name in one line:

echo "${PWD##$(git rev-parse --show-toplevel)} [$(git rev-parse --abbrev-ref HEAD)]"

Wednesday, May 17, 2017

Drive images with progress indication

# make a drive image
sudo bash -c "pv -EE /dev/sdX >my_disk.img"

# make a compressed drive image
sudo bash -c "pv -EE /dev/sdX | gzip >my_disk.img.gz"

Monday, June 13, 2016

Resizing a VirtualBox disk image

This should go without saying, but make sure you have a good backup of everything before you do any of this.. If in doubt, just backup the whole VirtualBox folder. You have been warned.

Sunday, May 1, 2016

Configuring Gnome-Shell from the command line

Here's some useful tricks to automate the configuration of the gnome shell

Ubuntu-Gnome 16.04 on Thinkpad x230

This is mostly for myself, but perhaps someone else might get some ideas from it..

Tuesday, April 26, 2016

Edit gnome settings with your favorite editor

Using the vipe command, we can read from stdin, edit the contents in the default editor and send it down the pipe.

Saturday, March 26, 2016

find files from a certain date range in linux

When you need to find a file from a specific range of dates, like a reciept that you know you saved but can't remember where.. You can use the find command along with some parameters to find files newer than a specific date, older or between two dates.

Bash one-liner to fix "Failed to fetch" error for chrome in ubuntu

See this page for details on how an why, but here's the error:

W: Failed to fetch http://dl.google.com/linux/chrome/deb/dists/stable/Release:
    Unable to find expected entry 'main/binary-i386/Packages'
    in Release file (Wrong sources.list entry or malformed file)

And this will fix it in one shot:

sudo sed -i 's?deb http://dl.google.com/linux/chrome/deb/ stable main?deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main?g' /etc/apt/sources.list.d/google-chrome.list

Sunday, March 13, 2016

bash functions to view markdown documents

If you're writing markdown documents from vim or emacs, here's a couple handy ways to view them as rendered HTML.

Friday, March 11, 2016

Reverse SSH Forward with special user

We will create a user on our machine that the remote machine can connect as and initiate a reverse port forward. For this example, we'll allow the remote machine to connect with the user name "ssh-limited" and forward port 62222 back to themselves.

Tuesday, January 26, 2016

Make better bash scripts for yourself (and others)

When you write a bash script, there's a few things you can do to make your life (and anyone else who uses it) a lot better. Here's a few of the things that I (try) to do when I write scripts to make it easier to use and less error prone.

Sunday, January 17, 2016

Ditch capslock for escape in xwindows

Run this and your capslock will be changed to the escape key for the current session:
xmodmap -e 'clear Lock' -e 'keycode 0x42 = Escape'
Taken from this post on the vim wiki

Tuesday, March 17, 2015

Print the current tag OR commit in git

Here's the command to print the tag name of the current commit, or just the commit if there is no tag. It's useful for a script that builds or exports something and you always want to have some version reference..
# prints tag name or short revision
git name-rev --tags --name-only --no-undefined --always $(git rev-parse HEAD)

Friday, November 14, 2014

git lsd – For when you don't want to use gitk --all

If all you want is a quick look at your commit history with all the branches nicely laid out with their merges shown with pretty colored lines... Try this instead of one of the gui viewers:
git log --graph --boundary '--format=%C(yellow)%h%Creset%C(bold cyan)%d%Creset %s %C(dim normal)(%ar)%Creset' --all
This will output something like this (depending on your terminal color settings):
* 4b22a90 (HEAD, master) erases chip before programming (2 days ago)
*   ee107af Merge branch 'sysclk' (2 days ago)
|\  
| * e9df6f6 removes clk config to use defaults (2 days ago)
| * 66a1ea3 fixes clkout pin timing (2 days ago)
| * bcdda54 enables PLL @ 16MHz (internal 2MHz x 8) (2 days ago)
| * cd22bab enables the 32KHz rc for system clock (2 days ago)
| * cee8efd enables the internal 32MHz rc for sysclk (2 days ago)
| * aa10b4d enables 2MHz internal rc osc for sysclk (2 days ago)
| * 80cbade re-defines F_CPU (2 days ago)
|/  
*   0644c9d Merge branch 'diagnostics' (3 days ago)
|\  
| * b9e155d adds SYSCLK/10000 on pin A0 (3 days ago)
| * 57807ab adds blinky led (3 days ago)
|/  
* 5cfc996 basic skeleton (3 days ago)
Of course you can mess with the format and colors, but this is how I like it. Once you have it set the way you like it, alias it for later use!
git config --global alias.lsd "log --graph --boundary '--format=%C(yellow)%h%Creset%C(bold cyan)%d%Creset %s %C(dim normal)(%ar)%Creset' --all"
Now you can just type git lsd and have a quick overview of your commits.

Monday, October 13, 2014

Adjusting the system time in a VirtualBox Guest

This is only useful in a couple situations, but very handy if you need it. The guest OS must be unlocked, so you'll need to shut it down before you do this.
#/bin/bash

# First get the name of the vm you need to adjust:
VBoxManage list vms

# Then, adjust the time in milliseconds (neg nums go back in time):
VBoxManage modifyvm $MY_VM --biossystemtimeoffset -32000000000  # about a year

# To get it back on current time, just set it to zero:
VBoxManage modifyvm $MY_VM --biossystemtimeoffset 0

Thursday, September 25, 2008

Remote File Transfer, after Logout with rsync, ssh and disown

rsync -PhaC ./folder/ remote.host:~/folder/ > rsync.log

Enter Your Password

Hit ctrl-z to pause then type 'bg' to send process to background.

Find the pid of the rsync process with:
$ ps | grep rsync | cut -d' ' -f 1 > rsync.pid; cat rsync.pid;

disown `cat rsync.pid`

Now you can logout and the transfer will continue on. To kill the process, simply enter the following:
kill `cat rsync.pid`

To monitor progress, just type:
tail -f rsync.log

Tuesday, July 8, 2008

Local Files > Remote tar.gz with GnuTar and SSH

How to go from local files to remote compressed tarbal with no intermediate temp files in one command with tar and ssh.

So I'm getting ready to do a fresh upgrade/install to Ubuntu 8.04, the Hardy Heron, and I decide to back up my home folder to my file server. A quick ssh server df -h tells me that I have 443GB left on a 2.3TB array. It's barely enough, I need more HDs.

Ok, so I need to tar and feather my home before sending it. df -h on my computer shows that I'm using 85% of my /home patition. So there's probably not enough room to tar it localy and transfer to my server. I could just transfer all the files and folders to the server without taring, but hey, that's no fun :)

The problem is that although rsync and scp both support compression during the transfer, everything comes out just like it went in. I did some googling to see if I could just pipe tar to rsync or scp, but didn't find anything helpful. However, I was reminded that you can pipe output from a local program to a remote program using ssh a la echo "Hello world" | ssh anotherhost.com 'cat > /tmp/1' (taken from here)

So all we need to do now is get tar to pipe to ssh which then cats to a file on the remote server. Here's the basic process:
  1. Tar the files, output to stdout:
    tar cjv /home

  2. Use ssh to connect to remote server:
    ssh remotehost

  3. Write the stream to a file with timestamp:
    cat > home-`date +%Y.%m.%d-%H:%M:%S`.tar.gz

If we put all that together, we get the following:

tar cjv /home | ssh remotehost 'cat > home-`date +%Y.%m.%d-%H:%M:%S`.tar.gz'

I hope this helps anybody unfortunate enough to stumble upon this blog.