MHN

Ask HN: Best things in your bash_profile/aliases?

345 points by yesenadam·2877 days ago·Show in HN

What are the best, most useful aliases or functions etc for bash that you've collected or written? Or bash scripts. Please give explanation/usage if necessary. Ones you actually use often. Thanks!

Add a comment on Hacker News →
2 of 277 comments
  1.     function convert-decimaltobinary(){
            n="$1"
            bit=""
            while [ "$n" -gt 0 ]; do
                bit="$(( n&1 ))$bit";
                : $(( n >>= 1 ))
            done
            printf "%s\n" "$bit"
        }
        function convert-binarytodecimal(){
            echo "$((2#$1))"
        }
        function convert-hextodecimal(){
            echo $(( 16#$1 ))
        }
  2. I have the below for cases when I have to make a quick screencast. It needs ffmpeg and I am not sure how well the yt flag works as I don't really use Youtube any longer. Anyway.. first I do 'ffsc sc' and if I want that video to be smaller I do 'ffsc yt <path_to_file>'.

    ## Make screencast or convert to yt

    ffsc () {

    vid_name="$HOME/Videos/Screencasts/screencast_$(date +'%y%m%d-%H%M%S')"

            case $1 in
                    sc) ffmpeg -f x11grab -s 1920x1080 -i $DISPLAY -r 30 -f alsa -i default -preset ultrafast \
                        -c:v ffvhuff -c:a flac ${vid_name}.mkv
                        printf "${vid_name}.mkv" | xsel -i
    
                        echo
                        echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
                        echo "Output file name: ${vid_name}.mkv"
                        echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
                        echo
    
                        [ -e /tmp/ffsc.tmp ] && { rm /tmp/ffsc.tmp }
                        printf "${vid_name}.mkv" > /tmp/ffsc.tmp
                    ;;
                    yt) ffmpeg -i $2 -c:v libx264 -crf 18 -preset slow -pix_fmt rgb24 -c:a copy ${2/scr/yt_scr}
                        yt_name=$(cat /tmp/ffsc.tmp)
                        printf "${yt_name/scr/yt_scr}" | xsel -i
    
                        echo
                        echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
                        echo "Output file name: ${yt_name/scr/yt_scr}"
                        echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
                        echo
                    ;;
    
                    mini) ffmpeg -i $2 -strict -2 -s 1680x1050  -r 60 -c:v libx264 -b:v 164 -crf 22 -preset slow \
                          -pix_fmt yuv420p -c:a copy -an ${2/scr/mini_scr}
                          mini_name=${2/scr/mini_scr}
                          ffmpeg -i ${mini_name} -filter:v "setpts=0.5*PTS" -an ${mini_name/.mkv/.mp4}
                          rm ${mini_name}
                        printf "${mini_name/.mkv/.mp4}" | xsel -i
    
                        echo
                        echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
                        echo "Output file name: ${mini_name/.mkv/.mp4}"
                        echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
                        echo
                    ;;
            esac
    }

    And another nice one I didn't see here..

    # Python as a calculator

    pc() { python -c "print($*)"; }