GithubHelp home page GithubHelp logo

byte-cook / bash-alias-and-function Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 17 KB

Use these shortcuts to make bash even simpler

License: GNU General Public License v3.0

bash bash-alias bash-aliases bash-function bash-functions bashalias bashrc

bash-alias-and-function's Introduction

Bash Alias and Function

This project contains useful bash aliases and functions. Bash aliases are essentially shortcuts that save you from having to remember long commands and save you a lot of typing when working on the command line.

To make the alias or function persistent you need to declare it in the ~/.bashrc file. If you want to make your ~/.bashrc more modular, you can store your aliases in a separate file. Some distributions, such as Ubuntu and Debian, include a ~/.bash_aliases file that is referenced in ~/.bashrc.

Pick the shortcuts you are interested in and copy the code into one of the files mentioned above.

Run alias commands as root

Alias definition:

sudo='sudo '

Usage:

sudo <youralias>

How it works: If the last character of the alias value is a blank, then the next command word following the alias is also checked for alias expansion. See https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html?ref=linuxhandbook.com#Aliases.

Mount iso file

Alias definition:

function mount.iso() {
    # $1: the iso file
    if [ $# -eq 1 ]; then
        sudo mkdir -p /media/iso 
        sudo mount -o loop -t iso9660 "$1" /media/iso
    else
        echo Usage: mount.iso \<iso-file\>
    fi
}
function umount.iso() {
    sudo umount /media/iso
    sudo rmdir /media/iso
}

Convert video files

Convert MP4 files to MP3 files:

alias convert.mp42mp3='for f in *.mp4; do ffmpeg -i "$f" "${f%.mp4}.mp3"; done'

Convert MOV files to MP4 files

alias convert.mov2mp4='for i in *.mov; do ffmpeg -i "$i" -vcodec h264 -crf 30 -acodec aac "${i%.mov}_new.mp4"; done'

Convert high quality MP4 files to MP4 files with lower quality:

alias convert.mp42mp4='for i in *.mp4; do ffmpeg -i "$i" -vcodec h264 -crf 30 -acodec aac "${i%.mp4}_new.mp4"; done'

Or if the conversion is to be done for individual files:

function convert.mp42mp4.file() {
    # $1: input file name
    if [ "$#" == 0 ]; then
        echo Usage: convert.mp42mp4.file \<file\> [\<file\> ...]
        return 1
    else
        for n in "$@"
        do
            if [ "${n##*.}" == "mp4" ]; then
                outputFile="${n%.*}"_new.mp4
                ffmpeg -i "$n" -vcodec h264 -crf 30 -acodec aac "$outputFile"
            fi
        done
    fi
}

Download m3u8 file

function download.m3u8 {
    # $1: m3u8 url
    # $2: output filename
    if [ $# -eq 2 ]; then
        cd ~/Downloads
        ffmpeg -protocol_whitelist file,http,https,tcp,tls,crypto -i "$1" -c copy "$2"
    else
        echo Usage: download.m3u8 \<m3u8-url\> \<output-file\>
    fi
}

Create folders for MP3 CD

function mkdir.mp3() {
    # $1: start index
    if [ "$#" != 1 ]; then
        echo Usage: mkdir.mp3 \<start-index\>
        return 1
    else
        # check input
        if [ $(($1 % 6)) != 1 ]; then
            echo Error: Input is not a valid start index
            return 1
        fi
        
        lastIndex=$(($1+5))
        folder=MP3_$1-$lastIndex
        for i in $(seq $1 $lastIndex);
        do
            mkdir -p $folder/MP3_$i
        done
    fi
}

Encrypt/decrypt tar archives

function tar.encrypt() {
    # $1...: files or folders to encrypt
    if [ "$#" == 0 ]; then
        echo Usage: tar.encrypt \<file\> [\<file\> ...]
        return 1
    else
        tar czvpf - "$@" | gpg --symmetric --cipher-algo aes256 -o archive.tar.gz.gpg
    fi
}

function tar.decrypt() {
    # $1: archive to decrypt
    if [ "$#" != 1 ]; then
        echo Usage: tar.decrypt \<archive\>
        return 1
    else
        gpg -d $1 | tar xzvf -
    fi
}

Find files containing search text

function grep.findfiles() {
    # $1: search text
    # $2: directory
    if [ "$#" != 2 ]; then
        echo Usage: grep.findfiles \<search-text\> \<directory\>
        return 1
    else
        # -R: recursive
        # -i: ignore case
        # -l: print the name of the files
        grep -Ril "$1" "$2"
    fi
}

Appendix: Bash programming reference

https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html

${param#pattern} : pattern must match the beginning of param, the rest is returned
${param%pattern} : pattern must match the ending of param, the rest is returned

bash-alias-and-function's People

Contributors

byte-cook avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.