Hacker News new | past | comments | ask | show | jobs | submit login
Ask HN: What are your go to shell one-liners?
14 points by nbbaier 12 days ago | hide | past | favorite | 31 comments
What are the commands/pipelines you keep coming back to again and again?





The champion is definitely this:

    alias s='cd ..'
This was enabled by default in some distro, I think it was Mandrake Linux. I learned to use it and now I can't live without it. 's' is always ready to be immediately pressed, and it can even be spammed.

    export EDITOR=vim

    function highlight() {
        egrep --color "$1|\$"
    }

    function mkcd() {
        mkdir $1
        cd $1
    }

    function git-cite() {
        git log -1 $1 "--pretty=format:%H (\"%s\")%n" | sed -e 's/\([0-f]\{12\}\)[0-f]*/\1/'
    }

    alias gd='git diff'
    alias gpr='git pull --rebase'
(and a bunch of other similar aliases)

    function gcmr() {
        git checkout origin/merge-requests/$1
    }

  alias ..='cd ..'
  alias ...='cd ../..'
  alias ....='cd ../../..'
They read more intuitively and 'cd -' works better.

He he. Nice.

Novell Netware had that built in.

cd .. did what you expect.

cd ... went up 2 levels.

cd .... up 3. And so on.


pushd and popd are very handy too.

Off-topic, but useful key combinations for bash:

    C-r      search history and recall (C-R for another match)
    C-o      run and recall next (C-O next, C-O next, ...)
    C-M-/    undo last edit
    M-d      delete next word
    M-backsp delete prev word
    C-y      paste text deleted by the two above (combined), Emacs's "yank"
M means Meta key, substitute Alt key on a PC.

Not really off-topic, these combos facilitate writing fabulous one-liners.


One super-useful key combo which is often overlooked is C-x-e, which edits the current command in $EDITOR. Very useful when dealing with history and long commands that need parameter editing.

wget --convert-links --page-requisites --adjust-extension https://www.website-name.com

For downloading a single page from a website for offline storage capabilities. A private, quick, and simple alternative to saving information using archive.org.


Here are some simple but most used ones.

  # go back to the last directory
  cd -

  # run last command with sudo
  sudo !!

  # truncate file
  > /tmp/file-to-truncate.txt

  # show directory size
  du -sh my-dir

  # refresh output of command every 2 seconds (needs watch command)
  watch ls /tmp

  # check how long a command takes
  time du -sh mydir
Then I also have a neat function for cd ..:

  function ..() { 
    for i in $(seq 1 $1); do cd ..; done 
  }

  # cd ..
  .. 

  # cd ../../../../
  .. 4

> cd -

  git checkout -
This was life changing when I first found out about it.

For MacOS:

    #!/bin/bash
    # install in /usr/local/bin/nap
    if [ -z $1 ]; then
        echo "No time provided. Usage: nap [-cl] [minutes]"
        exit 2
    fi
    if [[ $1 == *c ]]; then
        echo Cancelling sleep. PIDs \(if any\): `pgrep -f "nap"`
        pkill -f "nap"
        exit 0
    fi
    if [[ $1 == *l ]]; then
        ps -Af | grep nap
        exit 0
    fi
    time=$(($1 * 60))
    echo Sleeping in $1 minutes...
    sleep $time && pmset sleepnow&
I wrote this 10+ years ago, still use it every day.

Assuming that `pmset sleepnow` puts laptop to standby, why exactly do you want it to happen in 3, 15, or 600 minutes? And every day?

A really useful one for vi / vim users is:

  $ set -o vi
Now you can edit the shell command lines (including the history) using vi / vim commands.

By default you will be in append mode.

You can press Esc to go back to command mode.

Then you can edit the current command using vi commands, and also go upward in the history using the k command, to edit any previous history line.

When done editing, hit Enter, and the edited command will be executed by the shell.

You can put the above set command in your .profile so that it always works.


  $ vi ’grep -l pattern files' 
lets you edit with vi, all the files matching that grep pattern.

For example, this is useful for editing all files of a project, which contain a particular variable name, in order to change that name to something else everywhere it is used.

The single quotes in the command should actually be backward single quotes, which I don't have on my mobile keyboard right now.


To copy one directory into another:

tar fc . | ( cd destination ; tar fx -)

You can use almost the same recipe to copy between host using ssh:

tar fc . | ssh user@host 'cd destination ; tar fx - )'


<some slow command>; say complete

Tells me when my build or whatever is done so I can so something else while I wait.


Alternatively, with pushbullet to get notification on all devices:

<cmd> ; pu done


!! these double exclamation marks always come in handy when forgetting to add that sudo

For macOS (and Fish shell):

    caffeinate -d -i -t (math "((7*60)+30)*60")
to prevent my laptop from sleeing for a specific period of time. I usually run it at the beginning of every day.

Always reaching for this munging strings in Github Actions (bash):

  MY_STR=$(awk -F'/' '{print $2}' <<< "$FROM")
Where -F could be a comma or other delimiter, printing 1-based field index.

find /somewhere -name 'something*' | xargs -I {} some -command on {}

I love this one and use it all the time, especially using the special case when your command can take multiple filename arguments and they come last. xargs will glom as many as it can into a single command before overflowing the max command line size, which can be faster than starting many processes. Grep is the best example:

      find . -name something | xargs grep -C5 search_string
(added the -C5 for context lines)

      du -s * | sort -nr | head
to find the largest subdirectories. (Although 'duc ls' is faster if I have it installed and up to date on that machine.)

   emacs $(git grep <whatever> | cut -d: -f1 | uniq)

this one is very neat


+1 I do this, too, but with a function:

  function cheat () {
    curl https://cheat.sh/$1
  }
Example:

  % cheat jq

I use this all the time, specially when it's a long line.

    $ ^sometihng^something

One is not enough, I want a list of code snippets. I manage them using Alfred.

What's in your toolbox?

md is “make debug” which runs e.g. nodemon or cargo watch, rerunning tests when I save a file

    win



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

Search: