Hacker News new | past | comments | ask | show | jobs | submit login
Ask HN: What are some small scripts you use daily?
68 points by poiuz on June 27, 2017 | hide | past | favorite | 56 comments
What are some small scripts you use daily?



I wind up making lots of interim files when importing/exporting/munging/analyzing data–to help keep track of these things (CSVs, scripts and miscellany that I may or may not want to revisit in the future) I have a function I call today to auto-create daily scratch directories:

    TODAY_DIR="$HOME/today/"
    DATE_DIR=$(date +'%Y-%m-%d')
    
    if [ ! -d  $TODAY_DIR$DATE_DIR ];
    then
        mkdir -p $TODAY_DIR$DATE_DIR
    fi;
    
    echo $TODAY_DIR$DATE_DIR
So you can do stuff like this with less thinking/typing:

    cp somefile.csv $(today)
I've been using this for a few years and continually find it handy, both at the command line and in keeping files clustered when I want to dig something up later. It is slightly less helpful if you regularly work past midnight, though!


Not exactly a script, but I work at home and sometimes I need to drown out the kids (or the wife!). I have this in my .bashrc:

alias whitenoise='play -q -c 2 -n synth brownnoise band -n 1600 1500 tremolo .1 30'

It plays some fuzzy whitenoise, which drowns them out and lets me keep focused (music is often distracting to me).


I call this blingle. I call it after any long running operation that I want to be notified of. It pops a desktop notification and sends a push message to my phone.

eg: make deploy; blingle

#!/bin/bash

MSG=${@:-"Job complete"}

notify-send "$MSG"

curl -s \

  --form-string "token=TOKEN" \

  --form-string "user=UID" \

  --form-string "message=$MSG" \

  https://api.pushover.net/1/messages.json


For a free alternative, check instapush.im or pushbullet


I have letters like r and b aliased in my bash profile to check for and run a bash script, if it exists, in each project directory (r = ./run.sh, b = ./build.sh).

In each of those scripts, I typically have a one liner depending on what the project requires. A simple build one is:

    #!/usr/bin/env bash

    make build
And run:

    #!/usr/bin/env bash

    docker run foo/bar
Or maybe:

    #!/usr/bin/env bash

    python manage.py runserver
I might also add (source) environment variable settings, etc. Sort of like my own personal decentralized makefile.

Then I add each script to my .git/info/exclude for each project. It saves so much time switching between projects to not have to remember any particular one's build or run commands.


This is brilliant; I think I'll start doing this.

One slight modification: name the build and run scripts something that you will never expect to be in that repo (maybe like run-xyz.sh where xyz are my initials, 10 random characters, etc.).

Then, the filename can be excluded in a global gitignore file.


Yeah, good points. Maybe putting the script(s) inside a .whatever directory inside the project root like some other dev tools do is worth consideration. What do you think?


Really to do this right you should make it function like direnv and create a whitelist of scripts you trust (bonus points for including hashes of the script). On first run it'll ask you to review and trust the script, and then just work on subsequent runs.

I'd love to have something like that as a utility


This is awk which emits the stream of unique things, as they are seen. it doesn't require sorted input. It runs at the cost of building the obvious hash in memory so can drive you to swap over large inputs, but its portable, does not require post-install s/w typically not on small systems and it delivers outcomes fast.

I use it all the time when I have some UNIX pipe emitting things and I want to "see" the uniques before I do sort | uniq -c type things.

#!/bin/sh awk '{ if (!h[$0]) { print $0; h[$0]=1 } }'


I do something similar with Perl, since I know the syntax a bit better. It allows me to (from memory) scrub out non-unique things like timestamps.

So, without the scrubbing:

tail -f somefile | perl -ne '!$SEEN{$_}++ and print'

Scrubbing off leading timestamps:

tail -f somefile | perl -ne 's/^[0-9:]//;!$SEEN{$_}++ and print'


I wrote https://github.com/hboon/genie which lets me run create directory-specific aliases such as:

push -> git push origin develop; git push origin master; git push origin --tags; terminal-notifier -title "Pushed to Git" -message "Project X"

In another directory, push may push different branches. Ditto for pull, release, keep, etc

Use them daily.


I run an AHK script with a little over 2000 abbreviations (e.g. typing 'abbn' expands to 'abbreviation'). It helps me type 100+ WPM without too much strain.


That's a cool idea. Do you have your file posted anywhere? If not I may cook something up on my own, I'm curious to try it out.


AHK has been working great for me over the years. My addresses, snippets of emails, expansions are all stored in an ahk file. I use various email addresses for different sites; so a@a would become aj(at)ajonit(dot)com or a@l would become admin(at)learnqtp(dot)com. The possibilities are endless. Few year's back I created a video for my blog readers and published an AHK template. You may download it here http://www.learnqtp.com/get-productive-with-automation-autoh...


I created a Perl script called anyconnector which allows me to jump around between different Cisco Anyconnect VPN's using details stored in KeePass entries.

e.g.

  To connect: anyconnector -c env-name
  Disconnect: anyconnector -d
  Get status: anyconnector -s
It gets used by my team all day, every day.


Looks pretty handy! How do you interface with the KeePass database? Is there a library? with Perl bindings?


There's quite a neat little library with an impressive amount of functionality for interacting with Keepass, called "File::KeePass;".

Here's a copy of the script itself: https://gist.github.com/kjbweb/38508fd92669101ec1fca4bea62bc...

It works on Mac's too, so I've been told.


My thinkpad running Linux is a bit temperamental when changing displays, often enumerating an existing display port as a new one.

I use the following script to switch to dual external monitors at a standard resolution, and a counterpart script to switch back to the internal hidef monitor.

If only I could reliably fix xfce4's panel placement all of the time...and not have to restart chrome and pycharm/intelliJ on each display change!

  #!/bin/bash
  
  EXT1=`xrandr --current | sed 's/^\(.*\) connected.*$/\1/p;d' | grep -v ^eDP | head -n 1`
  EXT2=`xrandr --current | sed 's/^\(.*\) connected.*$/\1/p;d' | grep HDMI | head -n 1`
  INT=`xrandr --current | sed 's/^\(.*\) connected.*$/\1/p;d' | grep -v ^DP | head -n 1`
  
  
  xfconf-query -c xsettings -p /Xft/DPI -s 96
  xfconf-query -c xfce4-panel -p /panels/panel-1/size -s 28
  
  xrandr \
  --output VIRTUAL1 --off \
  --output ${INT} --off \
  --output ${EXT2} --mode 1680x1050 --pos 0x150 --rotate normal \
  --output ${EXT1} --mode 1920x1200 --pos 1680x0 --rotate normal --primary


Something that I added to my bash profile is this simple `cd` override that triggers `workon` from `virtualenvwrapper` if the folder I'm accessing has a `virtualenv` with the same name.

    cd()
    {
      command cd "$@"
      [[ "$OLDPWD" == "$HOME"/Work/* && ! -z "$VIRTUAL_ENV" && "$OLDPWD" == *"${VIRTUAL_ENV##*/}"* && "$PWD" != *"${VIRTUAL_ENV##*/}"* ]] && deactivate
      [[ -z "$VIRTUAL_ENV" && "$PWD" == "$HOME"/Work/* ]] && next="$(sed -e "s/^.*Work\///" -e "s/\/.*$//" <<< "$PWD")" && [[ -d "$WORKON_HOME"/"$next" ]] && workon "$next" && unset next
    }
Yes, too many one-liners and short-circuits are usually a no-no, but it's not like anyone else is ever going to use this and I like these shortcuts.


Syncing current dev directory to my webserver using rsync multiple times a day:

rsync -rlptvDC --progress --rsh="ssh -l username" * myserver.com


I created a git-repository of the scripts I use often:

https://github.com/skx/sysadmin-util

That seems to be somewhat popular on github, but I rarely receive feedback so I'm not sure if people star because they use them too, or just because they suspect they might.


I have a lil thing I call "did" that just greps my shell history for a term, sorting and de-duplicating the result:

    $ cat `which did`
    grep $1 ~/.bash_history | sort | uniq

I also set HISTSIZE=10000000

I don't mind having a large history file laying around because it's so useful.


Thanks for this one. I hadn't stumbled on a good way to search my history before.


Cheers. :)


This Bash function rebases and pushes all my feature branches on the upstream "develop" branch:

  rebase-all () 
  { 
      old=`git rev-parse --abbrev-ref HEAD`;
      stashed=`git stash`;
      for b in $(git branch --format '%(authorname) %(refname:short)' | sed -ne "s/^`git config --get user.name` //p" | grep -- -);
      do
          git checkout $b && git rebase origin/develop && git push --force || ( git rebase --abort && echo Could not rebase $b );
          echo;
      done;
      git checkout $old;
      if [ "$stashed" != "No local changes to save" ]; then
          git stash pop;
      fi
  }


I'm in sales for a surveying/feedback SaaS co. Before demos I brand the survey/account with the prospect's company's colors & logo. During the demo I send custom branded, inline email surveys to a test gmail account to help my prospect understand the survey respondent POV. I also have to delete that test-survey email every hour so that the next prospect I demo isn't privy to who is exploring us. I often have to do 4-8 demos a day so as you can imagine that got old fast.

I wipe the inbox for my test gmail account every hour at the :45 minute mark using a ruby script. I don't automate the sending because it's part of the education of the prospect.


https://github.com/david-cako/flac-phobic

Biggest timesaver in the world for someone who listens to as much music as I do and doesn't want to deal with manually transcoding FLAC files.

iTunes and Apple Music will soon support FLAC natively, though -- previously it was out of necessity, space, and not wanting to use VLC and lose my main/single use of my Apple Watch. It's still about the latter two.

I recently also set it to output a manifest that I can feed into rsync on my work computer to pull my library from my backup server.


I have a small perl script I wrote called helpme

by default it shows a list of topics. Then if you run it with the topic, it displays the details about the topic. I use it to remember how to do less frequent stuff at my day job.


I was using a blog for this, but I hate maintaining a blog.

I'm going to use this instead!


How does it work?


Its super simple 11 lines of code then lines for the topics and description.

I have a topics hash/dictionary.

If the helpme command is run without a topic argument, it prints out all the topic keys to the dictionary one per line.

If it is run with a topic argument, it just prints out the value for that topic.


I wrote a Docker helper script, which resets it to a baseline state when I need to clear out cruft post-restart. I call it harpoon, so I can harpoon the whale when I go to restart my dev machine.


I'm interested. Care to share?


bash functions:

    # Helper function for running command in each subdirectory under current one.
    function each {
      if [ -z $1 ]; then
        : # If no command is given, then this is a no-op.
      else
        find . -maxdepth 1 -mindepth 1 -not -path '*/\.*' -type d -exec sh -c "(echo {} && cd {} && $* && echo)" \;
      fi
    }
zsh functions:

    # Helper function for navigating tmux sessions
    export WORKSPACE_ROOT=$HOME/workspace
    ws() {
      if [ -z $1 ]; then
        if [ -z $TMUX ]; then
          tmux attach-session
        else
          cd $WORKSPACE_ROOT
        fi
      elif [[ $1 == 'ls' ]]; then
        tmux list-sessions
      else
        tmux attach -t $1 || cd $WORKSPACE_ROOT/$1/src; tmux new-session -s $1
      fi
    }

    # Helper function for running command in each subdirectory under current one.
    each() {
      if [ -z $1 ]; then
        # If no command is given, just exit
      else
        find . -maxdepth 1 -mindepth 1 -not -path '*/\.*' -type d -exec sh -c "(echo {} && cd {} && $* && echo)" \;
      fi
    }


I have a deployment script that is part of my one-click deployment to the customer's webserver. It's kind of a legacy to having really crap interwebs though, as my upload used to be in the 700-900 kbit/sec range, so it strips out anything from the deploy package that isn't absolutely essential to create sort of a delta of changes.

Makes deployments blistering fast now that I've got 30 Megabit/sec up though... :)


I wrote a docker-wrapped script to create keys and certificates for use with HTTPS for development. These are configured with algorithms which modern browsers won't complain about. It also allows alternate domains.

https://hub.docker.com/r/qubyte/cert-creator/


- a bat to launch some programs as admin on windows on startup

ahk scripts:

- Easy Window Dragging (KDE style)

- Rocker (click left-right mouse button to go forward in explorer etc, right-left to go back)

- hidedesk (hides desktop icons unless I click on the desktop, sadly a bit buggy)

- some custom script to unify hotkeys for the brightness of my external dell monitor and my laptop screen.

browser fixes:

- some drag to scroll js for greasmonkey

- shortcut for bookmarking to pocket with keyboard only (with tags)


Over the years I’ve taught some of my (macOS user) co-workers the magic of the Automator.app

It’s transformed the office. It’s the little things ya know.


Can you give some use case examples?


examples would be nice


`watch-reload [watch dir] [reload domain]`, which watches for changes in a directory and triggers a reload for tabs matching a certain domain using a Chrome extension.

`watch-copy [copy to dir]`, which watches for changes in the current directory and builds and copies to another directory.

`set-lights [group name] [on/off/high/low]`, which posts commands to a Hue bridge.


I start projects very often and find myself using my scaffolding [tool](https://github.com/vutran/zel) for minimal dotfiles (which are downloaded from a repo) very often.

Also use it a lot for syncing dotfiles in my user directory between my work and home computers.


I like using a lot of aliases and chaining them together if I can and it makes sense.

The most common one I use all the time is one that changes the command prompt to show what git branch I'm on when in that directory. Just google it if you'd like.

If you repeat some command a lot or a chain of commands and it takes a lot of tedious typing, then script it.


I have a simple script called ~/bin/shipit

    web [master=] $ cat $(type -P shipit)
    #!/bin/bash
    exec ./shipit
    web [master=] $
This allows me to tweak every projects deploy script and also maintain it in version control.


A very unsafe python script to unescape strings.

    #!/usr/bin/env python
    import sys

    def unescape(string):
        print eval(string)

    if __name__ == '__main__':
        unescape(sys.stdin.read())


I have a shell alias that creates a temp directory labeled with the current time stamp and suffixed tag then changes to it. Makes it very quick get a new scratch area for shell fu.

Plus all the scratch dirs are in one place so cleanup is a breeze to free up space.


Sharing is caring? :)


Ask and ye shall receive!

    make-scratch-dir () {
      local name="$1"
      local pattern='^[a-z0-9\\-]+$'
      if [[ "$#" != 1 ]]; then
        echo "Usage: make-scratch-dir <name>" 1>&2
        return 1
      elif [[ ! "$name" =~ $pattern ]]; then
        echo "Invalid name: ${name}"
      fi
      local full_path="$HOME/tmp/scratch/$(date +%Y%m%d-%H%M%S)-${name}"
      mkdir -p "${full_path}"
      pushd "${full_path}"
      echo "Now in temp dir: ${full_path}"
    }
The directory change is done via pushd so you can hop back via popd. Also, it restricts the suffixes to ensure the directories are all "simple" names.


I didn't know of pushd, very interesting, may be useful for directory traversal in general.


I typically have a script that prints out my local IP, instead of if/ipconfig or some OS UI. If I'm hosting a static page with an HTML5 game, it's then pretty easy to access it on another computer or my phone.


Classify digital photographs that I need to review by its content. Import/export data from/to a DB, analyze it statistically and show graphically the info. Pretty standard.


I use this to do a `git pull` on all of my folders inside of my "sites" folder:

#!/bin/bash

for i in /sites/*/.git; do ( echo $i; cd $i/..; git pull; ); done


Take a look at `mr` which allows you to script updating/checking out/etc on a large number of git, mercurial, and other repositories:

https://myrepos.branchable.com/

For example I store all my repos beneath ~/Repos/ so I can update them all, checking out any missing ones with:

    cd ~/Repos/
    mr checkout
    mr update


There's a gui app on top of the script, so not sure if it counts, but I find SelfControl (selfcontrolapp.com) indispensible for staying focused each day.


I've installed it recently but some sites are hard. For example, I'd block YouTube in the blink of an eye, but what if I really need to look up a video tutorial or something in the middle of a work session?


my screens are somewhat too bright if i want to do work at night so

   #!/bin/bash                                                                       
   xrandr --output DVI-I-1 --brightness $1                                      
   xrandr --output HDMI-0 --brightness $1  
   xrandr --output DVI-D-0 --brightness $1                      
~




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: