• Bash tips and tricks (mostly for the command line)


    Hi again!

    Here’s a kind of small enhancement for the ‘type’ command. Something like: “always wanted to know what functions I have but never dared to ask”. ;)
    This could be put to ~/.bashrc. Then try command:

    def
    

    And here’s the code:

    # def = type + show all functions and aliases
    # def [items]
    #
    def()
    {
        if [ "$1" != "" ] ; then
            type "[email protected]"
        else
            # Show all function names and aliases.
            # Show function names using the 'ls' command!
    
            local function_names=$(declare -F | sed 's|declare -[^ ]*||')
    
            if [ -n "$function_names" ] ; then
                echo "Function names:"
                local tmpdir=/tmp/tmp-$$.dir
                mkdir -p $tmpdir || return 1
                pushd $tmpdir >/dev/null
                touch $function_names
                ls $function_names
                popd >/dev/null
                rm -rf $tmpdir
            else
                echo "No functions."
            fi
            alias
        fi
    }
    
  • And noticed that post #2 had a bug, here’s a new version:

    __common_sum()
    {
        local opts xx
        for xx in "[email protected]"
        do
            case "$xx" in
                -c) opts="--ignore-missing" ;;
            esac
        done
        command ${FUNCNAME[1]} $opts "[email protected]"
    }
    
  • Hello again bash friends!

    Here’s a handy function for helping you to work a bit differently on the command line!

    If you wish to open a file, say mydoc.odt, and then another, like mytext.txt, you’d normally do that like this:

    libreoffice mydoc.odt
    gedit mytext.txt
    

    But now, with the trick below, you can open them easily by just writing the file names on the command line:

    mydoc.odt mytext.txt
    

    instead of remembering the names of programs that open them.

    The only thing it takes is: put the underlying bash function into your ~/.bashrc and enjoy!

    # This bash function enables opening files without specifying
    # the command that should open them. Just give the name(s) of file(s)
    # on the terminal command line.
    #
    # The implementation is based on two main factors:
    # 1. given file(s) are handled by the unknown command handler of bash
    # 2. mime types of files must be set up correctly
    #
    # Example: use just the names of the files:
    #    testfile.txt second-testfile.odt
    # instead of more tedious
    #    notepadqq testfile.txt ; libreoffice second-testfile.odt
    #
    # Put this function to your ~/.bashrc in order to use it automatically.
    #
    # Note: either command exo-open or xdg-open must be installed.
    #
    command_not_found_handle()
    {
        local good    # list of files found => they will be opened
        local bad     # list of unrecognized words => give error
        local opener  # command to open any file
        local tmp
    
        for tmp in /usr/bin/exo-open /usr/bin/xdg-open
        do
            if [ -x $tmp ] ; then
                opener=$tmp
                break
            fi
        done
    
        for tmp in "[email protected]"
        do
            if [ -f "$tmp" ] ; then
                good+=("$tmp")
            else
                bad+=("$tmp")
            fi
        done
    
        if [ "$good" != "" ] ; then
            $opener "${good[@]}" >/dev/null 2>&1 &
        fi
        if [ "$bad" != "" ] ; then
            echo "Error: '${bad[@]}' not found."
        fi
    }
    
  • @manuel

    That’s a neat function. Thanks for sharing! :)

  • A small continuation to the command_not_found_handle tip above, here’s an supplementary tip to make it a tad more useful:

    You can use filename completion of the bash shell with the default key binding M-/ (boils down to ‘Esc’ and ‘/’ keys) to complete a filename.

    And if that M-/ feels a bit clumsy, you can put a key binding to your ~/.bashrc file, for example:

    bind '"§":complete-filename'
    

    would make the § key to complete a filename.

    Choose your preferred key sequence instead of §, depending on your keyboard layout.

    Now, if you have a file mylongfilename.odt, you can press key § after writing the beginning of the file name e.g.

    mylo
    

    and then press § and the full file name should appear (unless you have other files with the same prefix).

    See

    man bash
    

    for more.

  • Here is a simple script to make a back up copy of your “home” directory.

    Why backup your home directory? because most of your configuration files are in your home directory as , files (dot files). Your Bookmarks and preferences for Firefox are in your home dir. If you are using ssh on your lan, your ssh keys are in your home directory. So if I’m playing around and stupidly do something that trashes Antergos, I simply re-install Antergos. Then i unzip my back up home directory to a thumb drive, a NAS, or my Antergos based file server. Then copy everything from the back up home dir to the new home dir and tell it to overwrite all existing files with the same name. Bingo, you are pretty much back to where you were at the time of your last backup.

    There are two things I know of that cannot be salvaged this way. Your E-mails, contacts, etc if your are using Evolution as a mail client. Also your Chromium bookmarks. To solve this, before I make a backup in Evolution I use the “Back up Evolution data…” function and save it somewhere in my home directory. In Chromium, I use the “Export bookmarks” function and save to my home directory. Then after the backup is unzipped, use the restore function in Evolution and Chromium to restore from these files.

    Of course, the whole idea is for this home directory back up file to exist somewhere on another device besides the SSD or Hard Drive containing your Antergos OS.

    START FILE
    #!/bin/bash

    #This file creates and gzips a backup of your home directotory in a tarball

    #The following variable represents the pathname where the
    #backup .tar.gz files will be placed
    #DO NOT put a / on the end of PATHNAME i.e. /home/don/ is incorrect
    #In the case below, the backup file will be placed on my Antergos server in a #directory named "HomeBkup. /home/don/Server is a mount point for my #Antergos LAN server. Change this to suit your needs.

    PATHNAME="/home/don/Server/HomeBkup"

    #create new backup filename as HomeBkup-MoDaYear
    FILENAME=“HomeBkup-”date +%m%d%Y

    #create new backup file

    tar --create --verbose --exclude=/home/don/Server --exclude=/home/don/OdroidServer --gzip --file=$PATHNAME/$FILENAME.tar.gz /home/don
    END FILE

    Have fun experimenting.
    The “/home/don” at the end of the tar command is the directory to be saved (target directory) in this case my home directory /home/don.

    You can customize how much data is being saved and therefore how big your back up file will be by using the --exclude option. In my case, /home/don/Server and /home/don/OdroidServer are mount points for two servers on my lan. I back them up by other means so no need to back them up as part of my home directory. So, I exclude them.

    If you have other means for backing up your Documents directory, Music directory, Pictures directory, etc. you can exclude them from your backup. You can use as many excludes as you like. i.e.
    –exclude /home/don/Music --exclude /home/don/Pictures

  • @pudge
    sorry, for some reason the comments before the PATHNAME= command got the # shifted,

    Also in the very last line of the post, I forgot the = sign in my exclude examples. S/B
    –exclude=/home/don/Music --exclude=/home/don/Pictures

  • @pudge Thanks! Backups are always a great idea, and a script for automating the creation is another great idea.

    Further ideas could be making backup occur automatically and regularly, e.g. daily. For that one can use e.g. systemd or a cron job.

  • @manuel said in Bash tips and tricks (mostly for the command line):

    @pudge Thanks! Backups are always a great idea, and a script for automating the creation is another great idea.

    Further ideas could be making backup occur automatically and regularly, e.g. daily. For that one can use e.g. systemd or a cron job.

    If you are going to make daily backups, change the FILENAME assignment from
    FILENAME=HomeBkup-date +%m%d%Y which yields a unique filename of HomeBkup-08122018 for Aug 12 2018
    To
    FILENAME=date +%A which yields a filename that equals the day of the week such as SUNDAY for Sunday.

    That way, Tar will create 7 backups named Sun thru Sat. Then on the next Sunday tar will overwrite the old SUNDAY file with the new Sunday file. Same for the rest of the week days.

    This way you will always have the last seven backups but never more than seven. With unique filenames such as HomeBkup-08122108 you would have to manually clean up old backups to keep the storage space usage reasonable.

  • Any of you guys willing to make me a bash script for this: https://forum.antergos.com/topic/10447/deepin-wallpaper-blur-fix

    if you need anymore info let me know, thanks ;)
    Ant

bash7 tips6 mostly2 tricks1 Posts 30Views 3138
Bloom Email Optin Plugin

Looks like your connection to Antergos Community Forum was lost, please wait while we try to reconnect.