working on it ...

Filters

Explore Public Snippets

Sort by

Found 1,127 snippets

    public by gwarah modified Jun 23, 2018  881  1  7  0

    improved cd: <directory pattern> as parameter

    An improvement of cd Linux command. From a given directory pattern, the snippet finds directories matching it and output them as a menu. # Requisites: 1. SHELL=bash 2. locatedb available and up to date. It's necessary to select the directories (see: man (locate | locatedb | updatedb) for more information) # How to use: 1. put this
    #!/bin/bash
    # /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    # * File       : rexcd.sh
    # * Goal       : improve cd linux command
    # * Requisites :
    # *     1. SHELL=bash
    # *     2. Locate service available and up to date. It's necessary to search directories
    # *        (see locate, locatedb and updatedb manual for more information)
    # * History    :
    # *   #version;date;description
    # *   0.1.1; 02/05/2018; tested in ubuntu 16.04/bash and minor code improvements
    # *   0.1.0; 30/04/2018; first release (tested in cygwin 2.10.x)
    # */
    
    # How to use
    #
    # 1. put this script into a directory (Ex: ~/scripts)
    #
    # 2. insert this code in your ~/.bashrc:
    # . ~/scripts/rexcd.sh
    #
    # 3. open a new shell session and test it:
    # cd ~
    # rexcd scripts
    #
    # 4. Example of output (cygwin 2.10.x)
    # Choose one directory
    # 
    # 1) /home/prpe/scripts
    # 2) /home/prpe/sintaxe/scripts_plone
    # 3) /home/prpe/sintaxe/scripts_plone/scripts
    # 4) /usr/share/doc/xorg-scripts-1.0.1
    # 5) /usr/x86_64-pc-cygwin/lib/ldscripts
    # Option [1] 2
    # 
    #  
    # prpe@DESK-00-090117 ~/sintaxe/scripts_plone
    # $
    
    
    #
    # ToDo:
    #
    # 1. switch:-i (ignore case)
    # 2. switch:-l limits of directories in menu (from m-th to n-th search matches)  (m,n|m,|,n default 1 to number of matches)
    # 3. switch:-d root directory -d <dir> (default /)
    # 4. switch:-u perform an update of db table
    #
    
    # syntax: rexcd_exit <msg>
    function rexcd_exit {
        local msg=$1
        echo "${msg}" >&2
        return 1
    }
    
    # syntax: 
    #  export rexcd_opt=""
    #  rexcd_menu opt1 opt2 ... optn
    #  echo $rexcd_opt
    function rexcd_menu {
        # return code + option
        local FN_RESULT=1
        rexcd_opt=""
    
        # variáveis de controle
        local num_options=$#
        local arr_options
        local num_choose=1
        local FN_DEFAULT=""
        local v_aux1 v_aux2
        local fl ind
    
        # menu message
        local FN_MSG="Choose one directory "
        local PS3="#"
    
        # checking number of options and creating list of them
        if [ $num_options -lt 2 ]; then
            rexcd_exit "At least 2 options required"
            return $?
        fi
    
        let ind=1
        for fl in $*; do
            arr_options[${ind}]="${fl}"
            let ind++
        done
    
        # menu
        FN_RESULT=1
        ind=1
        num_choose=""
        FN_DEFAULT=$ind
        PS3="Option [${FN_DEFAULT}] "
        
        echo "" ; echo "${FN_MSG}" ; echo ""
        select num_choose in `echo ${arr_options[@]}`; do
            case "${num_choose}" in
                *)
                    if [ -z "${num_choose}" ]; then
                        read -t 2 -p "Invalid option"
                        echo "" ; echo "${FN_MSG}" ; echo ""
                        continue
                    fi
                    FN_RESULT=0
                    break
                    ;;
            esac
        done
        echo ""
    
        rexcd_opt="${num_choose}"
        return ${FN_RESULT}
    }
    
    function rexcd {
        local contador fl
        local padrao=""
        local max_opc=15
        local lst_opc=""
        local retorno=0
        local v_aux=0
    
        # its necessary declare as export
        export rexcd_opt=""
    
        if [ $# -ne 1 ]; then
            rexcd_exit "Number of arguments invalid: $#. Must be exactly one."
            return $?
        fi
    
        padrao=$1
        contador=0
        cd /
        for fl in `locate -i "${padrao}"`; do
            # filter not directories elements
            [ ! -d "${fl}" ] && continue
    
            # search regards
            basename "${fl}" | grep -q -i "${padrao}"
            [ $? -ne 0 ] && continue
    
    
            # update array e var count
            let v_aux=contador+1
    
            if [ $contador -eq 1 ]; then
                lst_opc="${fl}"
            else
                lst_opc="${lst_opc} ${fl}"
            fi
    
            let contador++
            # verifica o máximo de entrada
            if [ $contador -ge $max_opc ]; then
                echo "WARNING: Maximum number of options $max_opc reached" >&2
                break
            fi
        done
    
        if [ ${contador} -eq 1 ]; then
            rexcd_opt="${lst_opc}"
        elif [ ${contador} -gt 1 ]; then
            export rexcd_opt=""
            rexcd_menu ${lst_opc}
            retorno=$?
        else
            rexcd_exit "No directory matches with ${padrao}."
            retorno=$?
        fi
    
        [ $retorno -eq 0 ] && cd ${rexcd_opt}
        return ${retorno}
    }
    																														

    public by kerid18 modified May 9, 2018  863  3  5  2

    Random 8 Character Password Generator Script

    #!/bin/bash
    #
    #
    #  Random password generator for Bash 2.x +
    #+ by Antek Sawicki <tenox@tenox.tc>,
    #+ who generously gave usage permission to the ABS Guide author.
    #
    # ==> Comments added by document author ==>
    
    
    MATRIX="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
    # ==> Password will consist of alphanumeric characters.
    LENGTH="8"
    # ==> May change 'LENGTH' for longer password.
    
    
    while [ "${n:=1}" -le "$LENGTH" ]
    # ==> Recall that := is "default substitution" operator.
    # ==> So, if 'n' has not been initialized, set it to 1.
    do
    	PASS="$PASS${MATRIX:$(($RANDOM%${#MATRIX})):1}"
    	# ==> Very clever, but tricky.
    
    	# ==> Starting from the innermost nesting...
    	# ==> ${#MATRIX} returns length of array MATRIX.
    
    	# ==> $RANDOM%${#MATRIX} returns random number between 1
    	# ==> and [length of MATRIX] - 1.
    
    	# ==> ${MATRIX:$(($RANDOM%${#MATRIX})):1}
    	# ==> returns expansion of MATRIX at random position, by length 1. 
    	# ==> See {var:pos:len} parameter substitution in Chapter 9.
    	# ==> and the associated examples.
    
    	# ==> PASS=... simply pastes this result onto previous PASS (concatenation).
    
    	# ==> To visualize this more clearly, uncomment the following line
    	#                 echo "$PASS"
    	# ==> to see PASS being built up,
    	# ==> one character at a time, each iteration of the loop.
    
    	let n+=1
    	# ==> Increment 'n' for next pass.
    done
    
    echo "$PASS"      # ==> Or, redirect to a file, as desired.
    
    exit 0
    +

    public by gwarah modified May 2, 2018  2310  2  6  0

    List (and optionally run) snapped apps

    Show all installed snaped apps in a zenity menu. The user can choose one and run it. just run this script: ./mysnaps.sh
    #!/bin/bash
    # program     : mysnaps.sh
    # description : choose one snapped app and run it
    # author      : lp (Luís Pessoa)
    # version     : 0.1.0b
    # dependences : 
    #   1) shell: bash 
    #   2) packages awk, snap and zenity installed
    #   3) at least one snap app instaled
    # history     :
    #   lp; 07/02/2018; begin of development
    #   lp; 09/02/2018; first release
    
    ###############
    # functions
    ###############
    
    
    ###############
    # end functions
    ###############
    
    # building snap list header 
    snap_cols="$(snap list | \
    awk '{ if (NR == 1) { for (i = 1; i <= NF; i++)  printf "--column " $i " " }} ')"
    snap_cols="--column check ${snap_cols}"
    
    # building snap list options
    snap_ops=$(snap list | \
    awk '
    BEGIN { }
    {
    if (NR >= 2 ) {
        for (i = 1; i <= NF; i++) {
            
            if ( i == 1 ) {
                if ( $i == "core"  ) break;
                if (NR == 2) {printf "TRUE ";} 
                else {printf "FALSE ";}
            }
            printf $i;
            if ( i == NF ) { print "";}
            else { printf " "; }
       }
    }}')
    
    # snap app selection
    opc=$(zenity  --list  --text "Apps snaped " --radiolist  ${snap_cols} ${snap_ops})
    case $? in
             1)
                    echo "No snap app selected"
                    exit 1
                    ;;
            -1)
                    echo "Error!"
                    exit 1
                    ;;
    esac
    
    # run app selected
    app_opc=`echo $opc | awk '{print $1}'`
    ${app_opc}
    exit $?
    												

    public by yourfriendcaspian modified Sep 2, 2017  126  0  3  0

    Copy file remotely using scp in Linux

    The scp command copies from a location to a remote path. In this example, the remote server is Linux based and the command will use 'root' as username and will copy into the /tmp directory. Root password will be requested before transferring file.
    >scp myFile.txt root@1.2.3.4:/tmp/													

    public by charlwillia6 modified Aug 14, 2017  272  1  3  0

    Uninstall package.json

    Uninstall package.json: npm_uninstall.sh
    # /bin/bash 
    
    npm uninstall `ls -1 node_modules | tr '/\n' ' '`
    
    																								

    public by adam2015 modified May 21, 2017  282  8  4  -1

    Install docker-machine and docker-compose

    install_dc() {
    curl -L https://github.com/docker/compose/releases/download/1.13.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
    chmod +x /usr/local/bin/docker-compose
    }
    
    install_dm() {
    curl -L https://github.com/docker/machine/releases/download/v0.11.0/docker-machine-`uname -s`-`uname -m` >/usr/local/bin/docker-machine
    chmod +x /usr/local/bin/docker-machine
    }

    public by johnny13 modified Dec 15, 2016  161  1  2  0

    MacOS bash aliases for File, List and Disk functions

    MacOS bash aliases for File, List and Disk functions: .bash_alias_osx
    #
    ## Shortcuts
    alias d="cd ~/Documents/Dropbox"
    alias dl="cd ~/Downloads"
    alias dt="cd ~/Desktop"
    alias p="cd ~/projects"
    alias g="git"
    alias h="history"
    alias j="jobs"
    
    
    #
    ## Detect which `ls` flavor is in use
    if ls --color > /dev/null 2>&1; then # GNU `ls`
    	colorflag="--color"
    	export LS_COLORS='no=00:fi=00:di=01;31:ln=01;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.ogg=01;35:*.mp3=01;35:*.wav=01;35:'
    else # macOS `ls`
    	colorflag="-G"
    	export LSCOLORS='BxBxhxDxfxhxhxhxhxcxcx'
    fi
    
    #
    # #	List Aliases
    alias ls="gls -AXhsg1N --group-directories-first --color=auto"                                     # Nice list
    alias lsl="ls -lhAC	| less"									   # less list
    alias ll="ls -xhks"										   # little list
    alias lx="ls -XhkGgos1"										   # extension list
    alias la="ls -Cap1hk"										   # list all
    alias lm="ls -Cp1hk"										   # mini list
    alias lr="ls -CgAR"									           # recursive list
    alias lsd='ls -CapXokdh'									   # directory list
    alias lspath="ls -ACGghskop1 $PWD/**"						                   # nice full path list
    
    
    # Print each PATH entry on a separate line
    alias path='echo -e ${PATH//:/\\n}'
    
    
    #
    ## Disk Aliases
    alias fhere="find . -name "
    alias df="df -Tha --total"
    alias free="free -mt"
    alias du="du -ach | sort -h"
    alias filelarge="du -h | grep '^\([0-9.]*G\|[0-9]\{3,\}M\)' | sort -n"
    
    #
    ## System Status
    alias psg="ps aux | grep -v grep | grep -i -e VSZ -e"
    
    #
    ## get rid of command not found ##
    alias cd..='cd ..' 
     
    #
    ## directories
    alias ..='cd ..'
    alias ...='cd ../../../' 
    alias ....='cd ../../../../' 
    alias .....='cd ../../../../' 
    alias .4='cd ../../../../' 
    alias .5='cd ../../../../..'
    
    #
    ## Stats
    alias path='echo -e ${PATH//:/\\n}'
    alias now='date +"%T"'
    alias nowtime=now
    alias nowdate='date +"%d-%m-%Y"'
    
    ## pass options to free ## 
    alias meminfo='free -m -l -t'
     
    ## get top process eating memory
    alias psmem='ps auxf | sort -nr -k 4'
    alias psmem10='ps auxf | sort -nr -k 4 | head -10'
     
    ## get top process eating cpu ##
    alias pscpu='ps auxf | sort -nr -k 3'
    alias pscpu10='ps auxf | sort -nr -k 3 | head -10'
     
    ## Get server cpu info ##
    alias cpuinfo='lscpu'
     
    ## older system use /proc/cpuinfo ##
    ##alias cpuinfo='less /proc/cpuinfo' ##
     
    ## get GPU ram on desktop / laptop## 
    alias gpumeminfo='grep -i --color memory /var/log/Xorg.0.log'
    
    ## this one saved by butt so many times ##
    alias wget='wget -c'
    
    ## Enable aliases to be sudo’ed
    alias sudo='sudo '
    
    ## Updates
    alias update='brew update; brew upgrade; brew cleanup; npm install npm -g; npm update -g; sudo gem update --system; sudo gem update; sudo gem cleanup'
    
    
    # IP addresses
    alias ip="dig +short myip.opendns.com @resolver1.opendns.com"
    alias localip="ipconfig getifaddr en0"
    alias ips="ifconfig -a | grep -o 'inet6\? \(addr:\)\?\s\?\(\(\([0-9]\+\.\)\{3\}[0-9]\+\)\|[a-fA-F0-9:]\+\)' | awk '{ sub(/inet6? (addr:)? ?/, \"\"); print }'"
    
    
    # Clean up LaunchServices to remove duplicates in the “Open With” menu
    alias clean_menu="/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user && killall Finder"
    # Recursively delete `.DS_Store` files
    
    alias clean_ds="find . -type f -name '*.DS_Store' -ls -delete"
    
    # Empty the Trash on all mounted volumes and the main HDD.
    # Also, clear Apple’s System Logs to improve shell startup speed.
    # Finally, clear download history from quarantine. https://mths.be/bum
    alias clean_trash="sudo rm -rfv /Volumes/*/.Trashes; sudo rm -rfv ~/.Trash; sudo rm -rfv /private/var/log/asl/*.asl; sqlite3 ~/Library/Preferences/com.apple.LaunchServices.QuarantineEventsV* 'delete from LSQuarantineEvent'"
    
    
    # macOS has no `md5sum`, so use `md5` as a fallback
    command -v md5sum > /dev/null || alias md5sum="md5"
    
    # macOS has no `sha1sum`, so use `shasum` as a fallback
    command -v sha1sum > /dev/null || alias sha1sum="shasum"
    
    
    # Trim new lines and copy to clipboard
    alias c="tr -d '\n' | pbcopy"
    
    
    # Hide/show all desktop icons (useful when presenting)
    alias hidedesktop="defaults write com.apple.finder CreateDesktop -bool false && killall Finder"
    alias showdesktop="defaults write com.apple.finder CreateDesktop -bool true && killall Finder"
    
    
    # URL-encode strings
    alias urlencode='python -c "import sys, urllib as ul; print ul.quote_plus(sys.argv[1]);"'
    
    
    # Ring the terminal bell, and put a badge on Terminal.app’s Dock icon
    # (useful when executing time-consuming commands)
    
    
    # Kill all the tabs in Chrome to free up memory
    # [C] explained: http://www.commandlinefu.com/commands/view/402/exclude-grep-from-your-grepped-output-of-ps-alias-included-in-description
    alias chromekill="ps ux | grep '[C]hrome Helper --type=renderer' | grep -v extension-process | tr -s ' ' | cut -d ' ' -f2 | xargs kill"
    
    alias badge="tput bel"
    
    

    public by route66 modified Dec 1, 2016  7555  8  4  0

    Renaming multiple files using find

    find . -type f -name 'file*' -execdir mv {} {}_renamed ';'            

    public by TripleM modified Oct 19, 2016  1721  1  5  0

    Ubuntu check public ip

    Simple bash command to ckeck public ip of server
    wget http://ipinfo.io/ip -qO -

    public by shebin512 modified Oct 4, 2016  2895  1  5  0

    Bash Script to clean magento 1.x Cache

    Magento 1.x cache clean Bash script. 1. Save the code in "/path/of/the/file/.sh" 2. Give executable permission for the file "chmod a+x /path/of/the/file/.sh" 3. Run the script from your magento document root like $ /path/of/the/file/.sh
    #!/bin/bash 
    # author Shebin Sheikh
    
    # function to remove the magento cache.
    function mageCacheClear {
    
    	local current_dir=$(pwd);
    	#echo $current_dir
    	#exit
    
    	if [[ -f "$current_dir/app/Mage.php" ]]; then
    		#echo "Inside Mage Doc root"
    		rm -vrf var/{cache,full_page_cache}/ media/{js,css}/
    	else
    		echo -e "Not inside A magento 1.X's document root!"
    		exit $?
    	fi
    
    }
    
    mageCacheClear # call the function
    • Public Snippets
    • Channels Snippets