zigford.org

About | Links | Scripts
Sharing linux/windows scripts and tips

Speed up your shell game

April 21, 2019 — Jesse Harris

Part of good use of the shell is doing repetitive things fast. A GUI can often be faster than using the command line because you can almost select things with your eyes ( eg, shift click files in a list to copy selectively. ) You can't always use a gui, however.


Perhaps your forced to move files around through the terminal. Today I was faced with such a task and to make matters worse, the files were quite large. So copying them individually would take a long time, and running them in parallel by using the & trick would have lead to huge io issues.

By defining a few quick and dirty functions, I was able to make my job a lot easier:

  1. add - This functions will take a single argument to add a file to a list of files to process later

    function add() { echo "${1}" >> ~/list.txt; }
    
  2. l - This one is a quick shortcut to list files starting with a particular letter. I used this to make shorter lists to scan through as I had quite a few files to look at.

    function l() { ls | grep -i "^$1.*"; }
    
  3. upload - This one will read the list and upload files that haven't been copied yet and are available in the current relative directory.

    function upload() {
    cat ~/list.txt | while read p
    do 
        [[ -f "${p}" ]] &&
        [[ ! -f /media/extHDD/"${p}" ]] &&
        echo "Copying : ${p}" &&
        cp "${p}" /media/extHDD/
    done
    }
    

Using these functions together, I would use l a to list all the files starting with a, then add a_file_.mkv to add it to the list to be processed.

Finally, use upload to have files pushed to the destination.

As you can see, none of these function are special or tricky, just simple little hacks to reduce keystrokes and make a job easier. I would say the main learning is, whatever you are doing use the tools to make your life easier.

Tags: bash, shell