Commands tagged function (131)

  • # Usage: ftagmarks TAG BOOKMARKS.JSON ftagmarks Bash ~/.mozilla/firefox/*.default/bookmarkbackups/bookmarks-*.json Tag can be partial matching, e.g. input 'Bas' or 'ash' will match 'Bash' tag. # Exact tag matching: ftagmark(){ jq -r --arg t "$1" '.children[] as $i|if $i.root == "tagsFolder" then ([$i.children[] as $j|{title: ($j.title), urls: [$j.children[].uri]}]) else empty end|.[] as $k|if $k.title == $t then $k.urls else empty end|.[]?' "$2"; } Usage: ftagmark TAG BOOKMARKS.JSON # List all tags: ftagmarkl(){ jq -r '.children[] as $i | if $i.root == "tagsFolder" then $i.children[].title else empty end' "$1"; } Usage: ftagmarkl BOOKMARKS.JSON # Requires: `jq` - must have CLI JSON processor http://stedolan.github.io/jq Show Sample Output


    0
    ftagmarks(){ jq -r --arg t "$1" '.children[] as $i|if $i.root == "tagsFolder" then ([$i.children[] as $j|{title: ($j.title), urls: [$j.children[].uri]}]) else empty end|.[] as $k|if ($k.title|contains($t)) then $k.urls else empty end|.[]?' "$2"; }
    qwertyroot · 2016-12-24 15:12:04 15

  • 0
    take dirname
    prabhakaran9397 · 2017-09-26 16:12:46 21
  • Use this function with bash version 4+ to convert arbitrary hexadecimal sequences to binary. If you don't have bash 4+ then modify the lowercase to uppercase demangling statement s=${@^^} to set s equal to the uppercase hex input or the bc command throws an input parser error. Show Sample Output


    0
    hex2bin () { s=${@^^}; for i in $(seq 0 1 $((${#s}-1))); do printf "%04s" `printf "ibase=16; obase=2; ${s:$i:1};\n" | bc` ; done; printf "\n"; }
    RiskNerd · 2018-10-02 22:02:33 336
  • The Windows Subsystem for Linux (WSL) is a compatibility layer for running binary Linux executables natively in Windows. A folder such as "C:\Program Files (x86)\Common Files" is represented as "/mnt/c/Program Files (x86)/Common Files". This function allows you to change the current directory to a Windows folder. Show Sample Output


    0
    function _cd() { local dir; dir="$(sed -e 's~\([a-z]\):~/mnt/\L\1~gi' <<< "${*//'\'/"/"}" )"; if [ -d "$dir" ]; then cd "$dir" || exit; fi; }
    mikhail · 2019-06-06 17:53:28 41
  • MacOS Solution due to lack of pidof command and procfs on MacOS Kernel. Show Sample Output


    0
    function _xterm_cwds() { for pid in $(pgrep -x zsh); do reply+=$(lsof -p $pid | grep cwd | awk '{print $9}') done }; function xcd() { cd $1 }; compctl -K _xterm_cwds xcd
    cdiehl · 2019-06-15 02:26:11 35
  • Same thing just a different way to get there. You will need lynx


    -1
    lynx --dump --source http://www.xkcd.com | grep `lynx --dump http://www.xkcd.com | egrep '(png|jpg)'` | grep title | cut -d = -f2,3 | cut -d '"' -f2,4 | sed -e 's/"/|/g' | awk -F"|" ' { system("display " $1);system("echo "$2); } '
    solarislackware · 2009-12-03 18:53:57 3
  • The shell has perfectly adequate pattern matching for simple expressions. Show Sample Output


    -1
    function ends_in_y() { case $(date +%A) in *y ) true ;; * ) false ;; esac } ; ends_in_y && echo ok
    unixmonkey9199 · 2010-04-06 22:18:52 3
  • Output the html from xkcd's index.html, filter out the html tags, and then view it in gwenview. Show Sample Output


    -1
    gwenview `wget -O - http://xkcd.com/ | grep 'png' | grep '<img src="http://imgs.xkcd.com/comics/' | sed s/title=\".*//g | sed 's/.png\"/.png/g' | sed 's/<img src=\"//g'`
    hunterm · 2010-08-24 22:21:51 4
  • Shorter version with curl and awk


    -1
    eog `curl 'http://xkcd.com/' | awk -F "ng): |</h" '/embedding/{print $2}'`
    dog · 2010-08-25 14:04:30 3
  • Just added view with the eog viewer.


    -1
    wget -O xkcd_$(date +%y-%m-%d).png `lynx --dump http://xkcd.com/|grep png`; eog xkcd_$(date +%y-%m-%d).png
    theanalyst · 2010-10-27 13:42:55 3
  • (Changed to "bartonskis" suggestion.)


    -1
    flipf(){ if [ -f "$1" -a -f "$2" ]; then mv "$1" "$1.$$" && mv "$2" "$1" && mv "$1.$$" "$2" || echo "$!"; else echo "Missing a file: $!"; fi; }
    Void42 · 2010-12-16 10:38:28 7
  • This function is used to set environmental variables from a list of alternatives depending on what's installed on the system. It returns the first program found in the list. Example usage: export BROWSER=$(find_alternatives chromium-browser google-chrome opera firefox firefox-bin iceweasel konqueror w3m lynx) . export EDITOR=$(find_alternatives vim nano pico emacs kate) . export PAGER=$(find_alternatives vimpager less most more pg)


    -1
    find_alternatives(){ for i;do which "$i" >/dev/null && { echo "$i"; return 0;};done;return 1;}
    eightmillion · 2011-01-06 19:53:46 3
  • Create a secure tunnelled connection for access to a remote MySQL database. For example, connect with MySQL Workbench to root@127.0.0.1:13306. Show Sample Output


    -1
    sshmysql() { ssh -L 13306:127.0.0.1:3306 -N $* & }
    peterRepeater · 2011-09-01 10:21:55 8
  • This is an "argument calculator" funktion. The precision is set to 4 and you can use dot (.) or comma (,) as decimal mark (which is great for german users with a comma on the numpad).


    -1
    calc() { echo "scale=4; ${*//,/.}" | bc -l; }
    fpunktk · 2011-10-24 19:58:20 4
  • Group membership in OS X is a mish-mash of standards that end up meaning there's almost a half-dozen of ways to belong to a group, what with group inheritance and automatic assignment. This means there's no easy command to find out all groups a user belongs to. The only sensible way then is to list all users and then query each user for membership. NOTE: This is a function. Once input you can execute it by calling with a groupname. Show Sample Output


    -1
    members () { dscl . -list /Users | while read user; do printf "$user "; dsmemberutil checkmembership -U "$user" -G "$*"; done | grep "is a member" | cut -d " " -f 1; };
    eduo · 2012-05-20 11:34:33 7
  • Quickly remove the conflicting line (key) from current users known_hosts file when there is an SSH host conflict. Very nice when you get tired of writing out full commands. Ideally you would place this into your .bash_profile Usage: rhost [n] Example: rhost 33 (removes line 33 from ~/.ssh/known_hosts) Function assumes the $HOME exists, you could alternatively use "~/.ssh/known_hosts" Mac OSX likes a space for sed -i "$1" d


    -1
    rhost() { if [[ $1 =~ ^[0-9]+$ ]]; then sed -i "$1"d ${HOME}/.ssh/known_hosts; else echo "rhost [n]"; fi }
    lowjax · 2013-08-01 21:10:34 13
  • Function that searchs a process by its name and shows in the terminal. * Shows the Header for reference * Hides the process 'grep' from the list * Case sensitive Show Sample Output


    -1
    psg(){ ps aux | grep -v grep | egrep -e "$1|USER"; }
    ivanalejandro0 · 2014-12-31 22:27:27 9
  • This is useful for paging through long directories, mulitple directories, etc. I put this in my ~/.bash_aliases file and alias 'lsl' to it.


    -2
    function lsless() { ls "$@" | less; }
    argherna · 2009-11-13 17:28:06 7
  • hb(){ sed "s/\($*\)/`tput setaf 2;tput setab 0;tput blink`\1`tput sgr0`/gI"; } hb blinks, hc does a reverse color with background.. both very nice. hc(){ sed "s/\($*\)/`tput setaf 0;tput setab 6`\1`tput sgr0`/gI"; } Run this: command ps -Hacl -F S -A f | hc ".*$PPID.*" | hb ".*$$.*" Your welcome ;) From my bash profile - http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html Show Sample Output


    -2
    hb(){ sed "s/\($*\)/`tput setaf 2;tput setab 0;tput blink`\1`tput sgr0`/gI"; }
    AskApache · 2010-04-07 08:45:26 4
  • you can use xmlstarlet to parse output instead of perl


    -2
    curl -s http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query=${@:-<YOURZIPORLOCATION>}|xmlstarlet sel -E utf-8 -t -m //forecast/txt_forecast/forecastday -v fcttext -n
    fanfani · 2010-04-13 22:14:48 3
  • Function that searchs for process by its name: * Shows the Header for reference * Hides the process 'grep' from the list * Case sensitive The typical problem with using "ps | grep" is that the grep process shows up the in the output. The usual solution is to search for "[p]attern" instead of "pattern". This function turns the parameter into just such a [p]attern. ${1:0:1} is the first character of $1 . ${1:1} is characters 2-end of $1 Show Sample Output


    -2
    psg(){ ps aux | grep -E "[${1:0:1}]${1:1}|^USER"; }
    flatcap · 2015-01-01 00:12:45 9
  • Should run anywhere that Bash works.. Your mileage may vary. Show Sample Output


    -2
    alias functions='shopt -s extdebug;declare -F | grep -v "declare -f _" | declare -F $(awk "{print $3}") | column -t;shopt -u extdebug'
    goeltzec · 2021-11-22 06:49:36 408
  • For those days when you need to know if something is happening because the day ends in "y". Show Sample Output


    -3
    function ends_in_y() { if [ `date +%A | sed -e 's/\(^.*\)\(.$\)/\2/'` == "y" ]; then echo 1; else echo 0; fi }
    allrightname · 2010-04-06 20:14:34 7
  • This backup function preserve the file suffix allowing zsh suffix aliases and desktop default actions to work with the backup file too. Show Sample Output


    -3
    buf () {oldname=$1; if [ "$oldname" != "" ]; then datepart=$(date +%Y-%m-%d); firstpart=`echo $oldname | cut -d "." -f 1`; newname=`echo $oldname | sed s/$firstpart/$firstpart.$datepart/`; cp -i ${oldname} ${newname}; fi }
    Seebi · 2010-12-14 19:58:34 4

  • -4
    function mkdcd () { mkdir "$1" && cd "$1" }
    shinokada · 2013-08-31 12:48:13 26
  • ‹ First  < 3 4 5 6 > 

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands


Check These Out

Transfer SSH public key to another machine in one step
This command sequence allows simple setup of (gasp!) password-less SSH logins. Be careful, as if you already have an SSH keypair in your ~/.ssh directory on the local machine, there is a possibility ssh-keygen may overwrite them. ssh-copy-id copies the public key to the remote host and appends it to the remote account's ~/.ssh/authorized_keys file. When trying ssh, if you used no passphrase for your key, the remote shell appears soon after invoking ssh user@host.

Write comments to your history.
A null operation with the name 'comment', allowing comments to be written to HISTFILE. Prepending '#' to a command will *not* write the command to the history file, although it will be available for the current session, thus '#' is not useful for keeping track of comments past the current session.

Read aloud a text file in Mac OS X

Count number of files in a directory
Just want to post a Perl alternative. Does not count hidden files ('.' ones).

Download all PDFs from an authenificated website
Replace *** with the appropiate values

Multi-thread any command
For instance: $ find . -type f -name '*.wav' -print0 |xargs -0 -P 3 -n 1 flac -V8 will encode all .wav files into FLAC in parallel. Explanation of xargs flags: -P [max-procs]: Max number of invocations to run at once. Set to 0 to run all at once [potentially dangerous re: excessive RAM usage]. -n [max-args]: Max number of arguments from the list to send to each invocation. -0: Stdin is a null-terminated list. I use xargs to build parallel-processing frameworks into my scripts like the one here: http://pastebin.com/1GvcifYa

disable caps lock
a quick one-line way to disable caps lock while running X.

Image to color palette generator
Extract a color palette from a image useful for designers. Example usage: $extract-palette myawesomeimage.jpg 4 Where the first argument is the image you want to extract a palette from. The second argument is the number of colors you want. It may be the case where you want to change the search space. In that case, change the -resize argument to a bigger or smaller result. See the ImageMagick documentation for the -resize argument.

Setting reserved blocks percentage to 1%
According to tune2fs manual, reserved blocks are designed to keep your system from failing when you run out of space. Its reserves space for privileged processes such as daemons (like syslogd, for ex.) and other root level processes; also the reserved space can prevent the filesystem from fragmenting as it fills up. By default this is 5% regardless of the size of the partition. http://www.ducea.com/2008/03/04/ext3-reserved-blocks-percentage/

Show apps that use internet connection at the moment.
show only the name of the apps that are using internet


Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: