Bash

I needed to copy all pictures from a folder to another place, while saving the paths and folder names. (from/one/two/three.jpg should become to/one/two/three.jpg)

The images were inside a number of different directories and doing it manually would’ve been hell.

Therefore, first I found all images and wrote their full path to a file: find . -type f -exec file {} \; | awk -F: '{ if ($2 ~/[Ii]mage|EPS/) print $1}' >> ~/allm (I’m not pretending I understand the above line).

Then I wrote this basic script to create the folders and to copy the files inside them. This was much harder than expected, especially because of the spaces inside the filenames, which created problems at every step.

The final code was:

counter=0; # To count how many files have we copied
IFS=$'\n' #makes newline the only separator, not just spaces, in the next line.
for o in $(cat allm) # for every line inside the file allm
do
    echo =========== $counter ==========
    counter=$((counter+1)) #increase counter

    # Everything inside quotes because apparently that's how you do spaces. Also all the names are inside variables, because again apparently that's how you do spaces.

    directory=pictures/$(dirname from_white_210220019/"$o")
    from="from_white_210220019/$o"
    to="pictures/from_white_210220019/$o"

    mkdir -p $directory

    echo "copying (from to)" $from $to
    cp ${from} ${to}
done 

The IFS thing decides how should the elements of the file be split. By default it’s spaces and newlines, we needed to tell it to split only by newlines, explicitly.

The entire solution is quick and dirty (the “from_white_xxx” thing would be the first thing to refactor) but it works, no need for overengineering a quick one-time hack which I’ll prolly never need again.

Keynav scrolling

This is a nice discovery!

Q: Can I use keynav to scroll?
A: Yes! X11 represents mouse scrolling as key presses, so you just add the relevant stanza to your keynavrc. Mouse buttons are 1=left, 2=middle, 3=right, 4=scroll-up, 5=scroll-down, 6=scroll-left, 7=scroll-right. So for example to scroll up with i and down with e:

i click 4,end
e click 5,end

Stack

  • Learn find, awk and other holy shell commands which I usually copypaste without actually taking time to learn the proper syntax
  • Learn bash – again, formally.