CARTO Solutions Technical Workshops
Jorge Sanz · May 2019
http://bit.ly/1905-cli-workshop
This workshop is intended to show why using a command line interface can be useful on many of our everyday tasks, and more convenient and effective than other graphical interfaces.
_ | CLI | GUI |
---|---|---|
Eease | ❌ | ✅ |
Functionality | ✅ | ❌ |
Speed | ✅ | ❌ |
Multitasking | ❌ | ✅ |
Automation | ✅ | ❌ |
# pipe the result of a command into another
cat /etc/hosts | grep localhost
# write the contents of a command results into a file (overwriting)
cat /etc/hosts > /tmp/myhosts
HISTSIZE
environment variable defines the size of your history.Ctrl + r
allows you to search on your history!!
executes the previous commandCTL + L
clean the screen (same as executing clear
command)CTL + A
, CTL + E
: go to the begin/end of the lineCTL + W
, CTL + U
: remove the previous word or the everything before the cursorfc
will open an editor with the last command, useful for editing long ones!$
will replace by the last parameter of your last command# Show system info
uname -a
# Show the current user
whoami
# Show the current directory
pwd
# Show the current date
date +%F\ %T
# Print a variable
echo $HISTSIZE
# Print disk status
df -h | grep -v loop
# Print usage
du ~/media/carto --max-depth=1 --human-readable
# Find processes
ps aux | grep unclutter
# Send a kill signal to the unclutter processs
kill -9 2162
ps aux | grep unclutter
You can write pretty complex scripts using bash that include functions, control structures, and so on.
# You can do for loops
for i in {1..5}
do
echo "Welcome $i"
done
# If structures... and much more
if [ $HISTSIZE -gt 500 ]
then
echo "Big History!"
else
echo "Short History"
fi
# Counting lines, words, and characters
wc /etc/hosts
# Usually we only want the lines
wc -l /etc/hosts
# Print the beginning of a file
head -n5 /etc/hosts
# Same with the end
tail -n5 /etc/hosts
# Combining pipes with wc to count how many files are in a folder
ls /var/log/*.log* \
| wc -l
# Finding all files inside a folder based in their names and types
# we redirect errors to the black hole at /dev/null
find /var/log -type f -name "*log*" 2> /dev/null \
| wc -l
# Finding and deleting
find /tmp/ -name ".DS_Store" -type f -delete 2> /dev/null
# Finding and executing a command (count lines in gitignore files)
find ~/src/carto -name ".gitignore" -type f -exec wc -l {} \;
# Print comments in the hosts file
sed -n -e '/\#.*/p' /etc/hosts
# Remove comments and then remove blank lines
sed -e '/\#.*/d' -e '/^\s*$/d' /etc/hosts
The Silver Searcher, quick find anything on your code
ag betis ~/src/carto/help
# accessing a simple URL
curl http://numbersapi.com/8/26
# making a POST request with a Content Type header
curl \
--data '{"q":"select user"}' \
--header "content-type:application/json" \
https://jsanz.carto.com/api/v2/sql
# making a HEAD request
curl \
--head \
https://jsanz.carto.com/api/v2/sql?q=select+user
# formatting the result using python3
curl -s \
--data '{"q":"select user"}' \
--header "content-type:application/json" \
https://jsanz.carto.com/api/v2/sql \
| python3 -m json.tool
# just formatting the output
curl -s \
--data '{"q":"select cartodb_id, scalerank, featurecla, name, worldcity from populated_places limit 5"}' \
--header "content-type:application/json" \
https://jsanz.carto.com/api/v2/sql > /tmp/data.json
cat /tmp/data.json | jq .
# processing the results to show only the names
cat /tmp/data.json \
| jq ".rows[].name"
# generate a new JSON with custom properties
cat /tmp/data.json \
| jq ".rows[] | {id: .cartodb_id, scale: .scalerank, name: .name}"
# combining with other tools
curl -sk -G "${CARTO_API_URL}api/v1/map/named/?api_key=${CARTO_API_KEY}" \
| jq ".template_ids[]" \
| tr -d '"' \
| sort \
| head
cat /tmp/data.json | jq ".rows[]" \
| json2csv > /tmp/data.csv
cat /tmp/data.csv
https://csvkit.readthedocs.io/en/latest/
csvclean
: fix common CSV problemscsvcut
: remove columnscsvgrep
: filtering datacsvjoin
: join two CSVs by a given columncsvjson
: generate json or GeoJSONcsvlook
: pretty print CSVcsvstat
: basic statistics# convert to CSV and pretty print the table
cat /tmp/data.csv \
| csvlook
cat /tmp/data.csv \
| csvgrep -c 4 -m tree \
| csvlook
cat /tmp/data.csv | csvstat
# ogrinfo presents an overview of a layer from a data source: type, extent, SRS, schema, etc
ogrinfo -summary ~/media/carto/flights.gpkg cartodb-query
# ogr2ogr processes and converts a datasource between different formats
# on this example we generate a new Geopackage only with one geometry from the original dataset
# this is useful to upload to carto only a dataset definition (after importing you delete that single row)
ogr2ogr \
-f GPKG \
-overwrite \
-limit 1 \
-nln flights_first \
/tmp/flights_first.gpkg ~/media/carto/flights.gpkg
ogrinfo -summary /tmp/flights_first.gpkg flights_first \
| head -n7
Jump around your file system without traversing folders
https://github.com/wting/autojump
I have aliased the command autojump
to the key j
because I'm so lazy
pigz
is a parallized version of gzip
, it uses all your cores to compress/decompress your files
htop
is a convenient way to inspect your running process, filter, kill, etc
duc
is a cached du command, that is, a tool that builds an index of how your hard disk is being used and helps you understand where your gigabytes are. It comes with cli and graphical interfaces.
duc ls ~/media/carto | head
# Translator using several providers
# https://github.com/soimort/translate-shell
trans -brief es:en 'cuando se despertó, el dinosaurio aún estaba allí'
trans -brief es:zh+ja 'cuando se despertó, el dinosaurio aún estaba allí'
# Pretty print source code
# http://pygments.org/docs/cmdline/
pygmentize -g /home/jsanz/src/sdks/carto-python/carto/fields.py
# Get the top ten commands from your history
# (you may have to change the column index in my case was $3)
history \
| awk '{CMD[$3]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' \
| grep -v "./" \
| column -c3 -s " " -t \
| sort -nr \
| nl \
| head -n10