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

Find and copy scattered mp3 files into one directory
No problem with word splitting. That should works on many Unix likes.

Stop Flash from tracking everything you do.
Brute force way to block all LSO cookies on a Linux system with the non-free Flash browser plugin. Works just fine for my needs. Enjoy.

Enter parameter if empty (script becomes interactive when parameters are missing)
Can be used for command line parameters too. If you have a more complicated way of entering values (validation, GUI, ...), then write a function i.e. EnterValue() that echoes the value and then you can write: $ param=${param:-$(EnterValue)}

Change user within ssh session retaining the current MIT cookie for X-forwarding
When you remotely log in like "ssh -X userA:host" and become a different user with "su UserB", X-forwarding will not work anymore since /home/UserB/.Xauthority does not exist. This will use UserA's information stored in .Xauthority for UserB to enable X-forwarding. Watch http://prefetch.net/blog/index.php/2008/04/05/respect-my-xauthority/ for details.

Cut out a piece of film from a file. Choose an arbitrary length and starting time.
With: -vcodec, you choose what video codec the new file should be encoded with. Run ffmpeg -formats E to list all available video and audio encoders and file formats. copy, you choose the video encoder that just copies the file. -acodec, you choose what audio codec the new file should be encoded with. copy, you choose the audio encoder that just copies the file. -i originalfile, you provide the filename of the original file to ffmpeg -ss 00:01:30, you choose the starting time on the original file in this case 1 min and 30 seconds into the film -t 0:0:20, you choose the length of the new film newfile, you choose the name of the file created. Here is more information of how to use ffmpeg: http://www.ffmpeg.org/ffmpeg-doc.html

Validating a file with checksum
Makes sure the contents of "myfile" are the same contents that the author intended given the author's md5 hash of that file ("c84fa6b830e38ee8a551df61172d53d7").

find files ignoring .svn and its decendents

Converts multiple youtube links to mp3 files
Usage: ytmp3 "YTurl" "YTurl2" "YTurl3" "YTurlN" Uses the shift command to let you extract the .mp3 from as many youtube urls as you like (or wherever else youtube-dl is supported) *Requires youtube-dl Orginal chunk of code: youtube-dl -q -t --extract-audio --audio-format mp3 URL taken from here http://www.commandlinefu.com/commands/view/9701/convert-youtube-videos-to-mp3

list files recursively by size

Run a script in parrallel over ssh
Runs a local script over ssh assuming ssh keys are in place. -P argument prints results to stdout. # Uses - https://code.google.com/p/parallel-ssh/


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: