Commands by Xk2c (10)


  • 2
    bashrc-reload() { builtin exec bash ; }
    Xk2c · 2016-04-30 10:37:38 11

  • 9
    command systemctl --no-page --no-legend --plain -t service --state=running
    Xk2c · 2016-04-30 10:35:05 18
  • some people on the net already use a cd(), but most of them break 'cd -' functionality, that is "go back where you have been previosly", or 'cd' which is "go back home". This cd() copes with that. Also when given a file name, go to the directory where this file is in. cd() { if [[ -n ${*} ]] then if [[ s${*}e == s-e ]] then builtin cd - elif [[ ! -d ${*} ]] then builtin cd "${*%/*}" else builtin cd "${*}" fi else builtin cd ~ fi ls -la }


    -6
    cd(), do a ls (or whatever you can imagine) after a cd, func to long please refer to description
    Xk2c · 2015-01-01 20:50:19 9
  • many have aliases like: alias ...="cd ../../" alias ....="cd ../../../" and so furth. ..() mitigates to need for those aliases, see sample output for an example # .. -> go up 1 directory # .. 4 -> go up 4 directories ..() { local DIR='' declare -i NUM=0 if [[ ${1} =~ ^[1-9][0-9]*$ ]] then while (( ${NUM} < ${1:-1} )) do DIR="${DIR}../" NUM=$(( ${NUM} + 1 )) done else DIR=.. fi cd "${DIR}" } Show Sample Output


    -4
    [ ~/temp/foo/bar/baz ] $ .. 3
    Xk2c · 2015-01-01 20:41:17 10
  • Thanks to the great grml team for this func! You really should look at their shell configs for further usefull things! http://git.grml.org/?p=grml-etc-core.git;a=blob_plain;f=etc/grml/script-functions;h=4d6bcea8f9beae83abd08f44155d299ea54a4a9f;hb=HEAD # {{{ check for availability of program(s) # usage example: # check4progs [-s,-q,--quiet,--silent] arg [arg .... argn] # # with option given either of: # -s,-q,--quiet,--silent # # check for available progs but produce no output check4progs() { [ -n "${ZSH_VERSION}" ] && emulate -L sh local RTN=0 local oldifs="${IFS}" local ARG d found local VERBOSE=1 case ${1} in -q | -s | --quiet | --silent) VERBOSE=0 shift 1 ;; *) ;; esac while [ $# -gt 0 ] do ARG="$1" shift found=0 IFS=: for d in $PATH do if [ -x "${d}/${ARG}" ] then found=1 break fi done IFS="${oldifs}" # check for availability if [ ${found} -eq 0 ] then if [ ${VERBOSE} -eq 1 ] then printf "%s: binary not found\n" "${ARG}" >&2 fi RTN=1 fi done # return non zero, if at least one prog is missing! return $RTN } # }}} Show Sample Output


    -6
    $ if check4progs cp foo mv bar rsync; then echo "needed progs avail, lets do funky stuff"; else echo "oh oh better abort now"; fi
    Xk2c · 2015-01-01 16:16:00 8
  • shopt-set() { declare -i RTN=0 local ARG='' while (( ${#} > 0 )) do ARG="${1}" shift 1 if ! builtin shopt -s "${ARG}" 1>/dev/null 2>&1 then RTN=1 fi done return ${RTN} } Show Sample Output


    -6
    shopt-set() ... func to long, please refer to description
    Xk2c · 2015-01-01 03:20:52 10
  • Actually your func will find both files and directorys that contain ${1}. This one only find files. ..and to look only for dirs: finddir() { find . -type d -iname "*${*}*" ; }


    -4
    findfile() { find . -type f -iname "*${*}*" ; }
    Xk2c · 2015-01-01 03:15:51 8
  • David thanks for that grep inside! here is mine version: psgrep() { case ${1} in ( -E | -e ) local EXTENDED_REGEXP=1 shift 1 ;; *) local EXTENDED_REGEXP=0 ;; esac if [[ -z ${*} ]] then echo "psgrep - grep for process(es) by keyword" >&2 echo "Usage: psgrep [-E|-e] ... " >&2 echo "" >&2 echo "option [-E|-e] enables full extended regexp support" >&2 echo "without [-E|-e] plain strings are looked for" >&2 return 1 fi \ps -eo 'user,pid,pcpu,command' w | head -n1 local ARG='' if (( ${EXTENDED_REGEXP} == 0 )) then while (( ${#} > 0 )) do ARG="${1}" shift 1 local STRING=${ARG} local LENGTH=$(expr length ${STRING}) local FIRSCHAR=$(echo $(expr substr ${STRING} 1 1)) local REST=$(echo $(expr substr ${STRING} 2 ${LENGTH})) \ps -eo 'user,pid,pcpu,command' w | grep "[${FIRSCHAR}]${REST}" done else \ps -eo 'user,pid,pcpu,command' w | grep -iE "(${*})" fi }


    -10
    psgrep() ... func to long, please look under "description"
    Xk2c · 2015-01-01 02:58:48 8
  • hgrep() { if [[ ${#} -eq 0 ]] then printf "usage:\nhgrep [--nonum | -N | -n | --all-nonum | -an | -na] STRING\n" return 1 fi while [[ ${#} -gt 0 ]] do case ${1} in --nonum | -N | -n | --all-nonum | -an | -na) builtin history | sed 's/^[[:blank:]]\+[[:digit:]]\{1,5\}[[:blank:]]\{2\}//' | grep -iE "(${*:2})" break ;; *) builtin history | grep -iE "(${*})" break ;; esac done } 'hgrep -n' helps in using full grep support, e.g. search for _beginning_ of specific commands, see example output Show Sample Output


    -1
    hgrep() { ... } longer then 255 characters, see below
    Xk2c · 2014-04-02 16:40:36 10
  • Simply sourcing .bashrc does not function correctly when you edit it and change an alias for a function or the other way round with the *same name*. I therefor use this function. Prior to re-sourcing .bashrc it unsets all aliases and functions.


    4
    bashrc-reload() { builtin unalias -a; builtin unset -f $(builtin declare -F | sed 's/^.*declare[[:blank:]]\+-f[[:blank:]]\+//'); . ~/.bashrc; }
    Xk2c · 2014-03-02 14:24:18 9

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

Reset terminal that has been buggered by binary input or similar

Check for Firewall Blockage.
This is just one method of checking to see if an IP is blocked via IP tables or CSF. Simple and to the point. Replace xx.xx.xx.xx with the IP you wish to check.

List all authors of a particular git project
This should work even if the output format changes.

list all file extensions in a directory
Just a little simplification.

Find status of all symlinks
The symlinks command can show status of all symbolic links, including which links are dangling, which symlinks point to files on other file systems, which symlinks use ../ more than necessary, which symlinks are messy (e.g. having too many slashes or dots), etc. Other useful things it can do include removing all dangling links (-d) and converting absolute links to relative links (-c). The path given must be an absolute path (which is why I used $(pwd) in the example command).

Get the Volume labels all bitlocker volumes had before being encrypted
Get information of volume labels of bitlocker volumes, even if they are encrypted and locked (no access to filesystem, no password provided). Note that the volume labels can have spaces, but only if you name then before encryption. Renaming a bitlocker partition after being encrypted does not have the same effect as doing it before.

Randomize lines in a file
Works in sort (GNU coreutils) 7.4, don't know when it was implemented but sometime the last 6 years.

show all key and mouse events
for mousevents, move the mouse over the window and click/move etc. usefull for getting mouseKeys, or keyKeys. also usefull for checking if X gets those mouse-events.

Check a directory of PNG files for errors
Useful for checking if a large number of PNG files was downloaded successfully by verifying the built-in CRC checksum. For incomplete files, the command will print: "00002309.png EOF while reading IDAT data ERROR: 00002309.png" The process is very fast; checking 21,000 files of 5MB in size took only five minutes on a 2011 Intel mobile dual-core.

Add page numbers to a PDF
Put this code in a bash script. The script expects the PDF file as its only parameter. It will add a header to the PDF containing the page numbers and output it to a file with the suffix "-header.pdf" Requires enscript, ps2pdf and pdftk.


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: