Commands tagged shell function (6)

  • Uses the shell builtin `declare` with the '-f' flag to output only functions to grep out only the function names. You can use it as an alias or function like so: alias shfunctions="builtin declare -f | command grep --color=never -E '^[a-zA-Z_]+\ \(\)'" shfunctions () { builtin declare -f | command grep --color=never -E '^[a-zA-Z_]+\ \(\)'; } Show Sample Output


    2
    builtin declare -f | command grep --color=never -E '^[a-zA-Z_]+\ \(\)'
    sciro · 2018-07-23 05:24:04 1005
  • shell generate random strong password Show Sample Output


    2
    len=20; tr -dc A-Za-z0-9_ < /dev/urandom | head -c ${len} | xargs
    aysadk · 2019-04-16 20:20:46 38
  • Thanks to this user: https://stackoverflow.com/a/35636373/2394635 Show Sample Output


    1
    s='Test "checkin_resumestorevisit \"- "Online_V2.mt" Run'; s=${s#*'"'}; s=${s%'"'*}; echo "$s"
    bugmenot · 2022-01-08 16:16:18 364
  • 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
  • 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

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

Run a command only when load average is below a certain threshold
Good for one off jobs that you want to run at a quiet time. The default threshold is a load average of 0.8 but this can be set using atrun.

Convert spaces in file names to underscores

Which processes are listening on a specific port (e.g. port 80)
swap out "80" for your port of interest. Can use port number or named ports e.g. "http"

list folders containing less than 2 MB of data
This command will search all subfolders of the current directory and list the names of the folders which contain less than 2 MB of data. I use it to clean up my mp3 archive and to delete the found folders pipe the output to a textfile & run: $ while read -r line; do rm -Rv "$line"; done < textfile

Burn CD/DVD from an iso, eject disc when finished.
cdrecord -scanbus will tell you the (x,y,z) value of your cdr (for example, mine is 3,0,0)

Run a command for blocks of output of another command
The given example collects output of the tail command: Whenever a line is emitted, further lines are collected, until no more output comes for one second. This group of lines is then sent as notification to the user. You can test the example with $ logger "First group"; sleep 1; logger "Second"; logger "group"

batch convert Nikon RAW (nef) images to JPG
converts RAW files from a Nikon DSLR to jpg for easy viewing etc. requires ufraw package

a function to create a box of '=' characters around a given string.
First argument: string to put a box around. Second argument: character to use for box (default is '=') Same as command #4948, but shorter, and without the utility function.

Backup all mysql databases to individual files on a remote server
It grabs all the database names granted for the $MYSQLUSER and gzip them to a remote host via SSH.

Most used commands from history (without perl)
I copied this (let's be honest) somewhere on internet and I just made it as a function ready to be used as alias. It shows the 10 most used commands from history. This seems to be just another "most used commands from history", but hey.. this is a function!!! :D


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: