Stripping Paths from Files in TAR Archives

There is no way to get tar to ignore directory paths of files that it is archiving. So, for example, if you have a large number of files scattered about in subdirectories, there is no way to tell tar to archive all the files while ignoring their subdirectories, such that when unpacking the archive you extract all the files to the same location. You can, however, tell tar to strip a fixed number of elements from the full (relative) path to the file when extracting using the “--strip-components” option.

Read more

Piping Output Over a Secure Shell (SSH) Connection

We all know about using scp to transfer files over a secure shell connection. It works fine, but there are many cases where alternate modalities of usage are required, for example, when dealing when you want to transfer the output of one program directly to be stored on a remote machine. Here are some ways of going about doing this. Let “$PROG” be a program that writes data to the standard output stream.

Read more

Locally Mounting a Remote Directory Through a Firewall Gateway on OS X

Download and install MacFUSE. Download the sshfs binary, renaming/moving to, for example, “/usr/local/bin/sshfs”. Create a wrapper tunneling script and save it to somewhere on your system path (e.g., “/usr/local/bin/ssh-tunnel-gateway.sh”), making sure to set the executable bit (”chmod a+x”): #! /bin/bash ssh -t GATEWAY.HOST.IP.ADDRESS ssh $@ Create the following script, and save it to somewhere on your system path (e.g., “/usr/local/bin/mount-remote.sh”), making sure to set the executable bit (”chmod a+x”):

Read more

`gcd` - A Git-aware `cd` Relative to the Repository Root with Auto-Completion

The following will enable you to have a Git-aware “cd” command with directory path expansion/auto-completion relative to the repository root. You will have to source it into your “~/.bashrc” file, after which invoking “gcd” from the shell will allow you specify directory paths relative to the root of your Git repository no matter where you are within the working tree. gcd() _gcd() " prev="$$2" dirnames=$(cd $TARGET; compgen -o dirnames $2) opts=$(for i in $dirnames; do if [[ $i !

Read more

ilter for Unique Lines Adjacent or Otherwise While Preserving Original Order

There are two BASH utilities that help you filter input for unique lines: ‘uniq’ and ‘sort‘: One gotcha with ‘uniq’ is that it only filters out duplicate adjacent lines. So if your input looks like: apple apple apple chicory chicory chicory banana banana Then running ‘uniq’ on it will yield: apple chicory banana But if the input has non-adjacent duplicate lines: apple banana banana chicory apple banana chicory banana banana apple apple apple banana chicory Then the results are:

Read more

Filesystem Management with the Full Power of Vim

Just discovered “vidir“, a way to manipulate filenames inside your favorite text editor (better known as Vim). Previously, I would use complex and cumbersome BASH constructs using “for;do;done”, “sed”, “awk” etc., coupled with the operation itself: $ for f in *.txt; do mv $f $(echo $f | sed -e 's/foo\(\d\+\)_\(.*\)\.txt/bar_\1_blah_\2.txt/'); done Which almost always involved a “pre-flight” dummy run to make sure the reg-ex’s were correct: $ for f in *.

Read more

Editing Remote Files With Your Local Vim Using the SCP Protocol

I love Vim! It is so easy enough to edit a remote file with my local Vim through the Secure Copy protocol: $ vi scp://user@remote.host.com/projects/foo/bar.py However, I often find myself wishing that bash completion was available to expand/complete paths on the remote system. Furthermore, when editing files outside of my home directory hierarchy, I have to remember to add an extra slash after the host name, e.g.: $ vi scp://user@remote.host.com//var/www/html/index.htm A solution is to write a custom wrapper function that takes scp-style remote file paths, converts them into Vim-style remote file paths, and then invokes Vim on them.

Read more

Easily Create Clean Compressed Tarballs of Your Git Repository

Ideally, you could refer the whole world — or at least, the significant portion thereof that want your code — to your (public mirror) Git repository. But unfortunately, the whole world does not (yet) use Git (“I know it was you Fredo, I know it was you, and it breaks my heart.“). Sad. Sooooo sad. But true. So the only recourse is for you to send these tortured souls an archived snapshot of your code via e-mail.

Read more

Boosting Interactive Bash Efficiency Through History Search Completion Editing

Most of us know about using the bang operator (!) to recall an entry from our bash history: $ ! # repeat last command $ !22 # repeat command 22 You can use “!:” followed by a number to substitute in arguments from previous commands. So, for example, to run the command “dosomething” on the first argument of the previous command: $ dosomething !:1 The fc command is also very useful, opening up the default editor to let you edit previous commands.

Read more