---
# System prepended metadata

title: bash magic
tags: [bash, ' magic']

---

---
title: bash magic
tags: bash, magic
---

# Bash magic

## Image Magick

### Annotate and gif (from python)
```python
import os
import sys
import glob
import subprocess


imgdir = sys.argv[1]
idname = os.path.split(imgdir)[-1]
filelist = []
for file in glob.glob(os.path.join(imgdir,'*'+ idname + '.png')):
    date = os.path.split(file)[-1].split('_')[1]
    newfilename = os.path.splitext(file)[0] + '_date.png'
    command = " convert " + file + " -pointsize 54 -gravity Center label:'" + str(date) + "' -append " + newfilename
    #subprocess.call(command, shell=True)
    
    filelist.append(newfilename)


command2 = "convert -delay 50 " + ' '.join(filelist) + " " + idname + ".gif"
subprocess.call(command2,shell=True)
```

Crop image:
```convert -crop 879x861+390+450 gfw.png gfw_crop.png```

## read xml 
```
xgrep -x '///Cloud_Coverage_Assessment/text()' MTD_MSIL2A.xml
```

## comparing 'float' values
in bash everything is string, but bc can be used like this:
```if (( $(bc <<< "$test > $k") ))```

## wget subdirectory only
```
wget -r -l1 --no-parent http://www.domain.com/subdirectory/
```
where:

-r: recursive retrieving
-l1: sets the maximum recursion depth to be 1
--no-parent: does not ascend to the parent; only downloads from the specified subdirectory and downwards hierarchy

## unzip and delete zip

`find . -depth -name '*.zip' -execdir unzip -n {} \; -delete`