Shell

How to initialize your KiCAD 8 project on the command line

TL;DR:

Inside the directory where you want to create the project, run

wget -qO- https://raw.githubusercontent.com/ulikoehler/KiCAD-ProcessAutomation/master/InitializeKiCad8Project.sh | bash /dev/stdin MyProject

You should replace MyProject (at the end of the command) with your project name.

Note: This will initialize an empty KiCAD project without any libraries. This is equivalent to creating a new project in KiCAD itself (using the GUI).

Continue reading →

Posted by Uli Köhler in Electronics, KiCAD, Shell

How to initialize your KiCAD 6 project on the command line

For the KiCAD 5 version of this script see How to initialize your KiCAD 5 project on the command line

TL;DR:

Inside the directory where you want to create the project, run

wget -qO- https://techoverflow.net/scripts/kicad6-init.sh | bash /dev/stdin MyProject

You should replace MyProject (at the end of the command) with your project name.

Note: This will initialize an empty KiCAD project without any libraries. This is equivalent to creating a new project in KiCAD itself (using the GUI).

Continue reading →

Posted by Uli Köhler in Electronics, KiCAD, Shell

How to test if MongoDB database exists on command line (bash)

Use this command to test if a given MongoDB database exists:

mongo --quiet --eval 'db.getMongo().getDBNames().indexOf("mydb")'

This will return an index such as 0 or 241 if the database is found. On the other hand, it will return -1 if the database does not exist.

docker-compose version:

docker-compose exec mongodb mongo --quiet --eval 'db.getMongo().getDBNames().indexOf("mydb")'

where mongodb is the name of your container.

Now we can put it together in a bash script to test if the database exists:

# Query if DB exists in MongoDB
mongo_indexof_db=$(mongo --quiet --eval 'db.getMongo().getDBNames().indexOf("mydb")')
if [ $mongo_indexof_db -ne "-1" ]; then
    echo "MongoDB database exists"
else
    echo "MongoDB database does not exist"
fi

 

docker-compose variant:

# Query if DB exists in MongoDB
mongo_indexof_db=$(docker-compose -f inspect.yml exec -T mongodb mongo --quiet --eval 'db.getMongo().getDBNames().indexOf("mydb")')
if [ $mongo_indexof_db -ne "-1" ]; then
    echo "MongoDB database exists"
else
    echo "MongoDB database does not exist"
fi

 

Posted by Uli Köhler in MongoDB, Shell

How to run jpegoptim recursively for each file in a directory

This command will run jpegoptim in lossless mode for each .jpg file in a folder recursively. This is how I reduce the size of my photo & image collection.

find . \( -iname \*.jpg -or -iname \*.jpeg \) -print0 | xargs -P 4 -n 1 -0 jpegoptim

See How to use xargs in parallel for details on  the -P 4 -n 1 syntax which runs 4 jpegoptim processes in parallel.

Note that the output may be scrambled due to 4 processes running in parallel, but it’s typically 4 times faster using this approach. You can also use

find . \( -iname \*.jpg -or -iname \*.jpeg \) -print0 | xargs -0 jpegoptim

to run just one jpegoptim process in parallel.

Posted by Uli Köhler in Shell

How to remove every .plist file on the command line

Use this command to remove every .plist file in the current folder recursively:

find . -name "*.plist" -exec rm -v {} \;

Since .plist files are metadata from MacOS applications, it’s often fine to remove them from backups, NAS storage etc.

Posted by Uli Köhler in Shell

How to just show HTTP headers using wget

Use --server-response -qO /dev/null:

wget --server-response -qO /dev/null [URL]

Example:

wget --server-response -qO /dev/null https://techoverflow.net

Example output:

HTTP/1.1 200 OK
Server: nginx/1.14.0 (Ubuntu)
Date: Mon, 18 Nov 2019 02:15:19 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Set-Cookie: cookielawinfo-checkbox-necessary=yes; expires=Mon, 18-Nov-2019 03:15:18 GMT; Max-Age=3600; path=/
Set-Cookie: cookielawinfo-checkbox-non-necessary=yes; expires=Mon, 18-Nov-2019 03:15:18 GMT; Max-Age=3600; path=/
Link: <https://techoverflow.net/wp-json/>; rel="https://api.w.org/"

 

Posted by Uli Köhler in Shell

How to initialize your KiCAD 5 project on the command line

In case you are using KiCAD 6 (released in 2021), this is not the right post for you ! See How to initialize your KiCAD 6 project on the command line

Note: This script initializes a KiCAD project in the recommended configuration (i.e. with project-specific footprint and symbol libraries). In case you want to initialize an empty project, see How to initialize an empty KiCAD project on the command line

TL;DR:

Inside the directory where you want to create the project, run

wget -qO- https://techoverflow.net/scripts/kicad-init.sh | bash /dev/stdin MyProject

You should replace MyProject (at the end of the command) with your project name.

Note: This will initialize an empty KiCAD project without any libraries. This is equivalent to creating a new project in KiCAD itself (using the GUI).

Continue reading →

Posted by Uli Köhler in Electronics, KiCAD, Shell

bash: Pass arguments but read script from stdin

You can use /dev/stdin to read the bash script from stdin but still pass command line arguments:

cat myscript.sh | bash /dev/stdin arg1 arg2 # ...

This is especially useful when directly piping scripts from wget or curl. Example:

wget https://example.com/script.sh | bash /dev/stdin arg

 

 

Posted by Uli Köhler in Shell

How to initialize an empty KiCAD project on the command line

TL;DR:

Note: We recommend to use our new script to initialize a project with project-specific footprint and symbol libraries, see How to initialize your KiCAD project on the command line. The script on this page initializes an empty project without any libraries.

Inside the directory where you want to create the project, run

wget -qO- https://techoverflow.net/scripts/kicad-initialize.sh | bash /dev/stdin MyProject

You should replace MyProject (at the end of the command) with your project name.

Note: This will initialize an empty KiCAD project without any libraries. This is equivalent to creating a new project in KiCAD itself (using the GUI).

Continue reading →

Posted by Uli Köhler in Electronics, KiCAD, Shell

bash template: Script with one argument

Use this template to create a bash / shell script with one argument that prints a usage message if used with the wrong number of arguments:

#!/bin/bash
if [ $# -ne 1 ]
then
    echo "Usage: $0 <filename>"
    exit 1
fi
# Note: The argument is $1
# TODO: Place your code here

 

Posted by Uli Köhler in Shell

How to pad string with zeroes & right-justify in bash using sed

Pad any string using this sed command:

sed -e :a -e 's/^.\{1,3\}$/0&/;ta'

This pads every string to length 4, usage example:

$ echo x | sed -e :a -e 's/^.\{1,3\}$/0&/;ta'
000x

In case you want to pad to a different length, replace 3 in the script by (your desired length - 1).

You can also use a bash function like this:

# Zero pad to length 4, right-justified
function zero_pad4 { echo $1 | sed -e :a -e 's/^.\{1,3\}$/0&/;ta' ; }

Usage example:

$ zero_pad4 1x
001x

Original source, modified: The Unix School

 

Posted by Uli Köhler in Shell

How to convert a DVI file to SVG on the command line

If you want to convert my.dvi to my.svg, use this command

dvi2ps my.dvi | ps2eps - > my.eps && eps2svg my.eps

This produces my.svg – note that if my.svg already exists, eps2svg will create my_1.svg, my_2.svg and so on and will not overwrite my.svg!

You can also use this shell function:

function dviToSVG { dvi2ps "$1" | ps2eps - > "${1%.*}.eps" && eps2svg "${1%.*}.eps" "${1%.*}.svg" ; }

Usage example:

dviToSVG my.dvi # Produces my.svg

 

Posted by Uli Köhler in LaTeX, Linux, Shell

How to recompress gzipped .gz files to .xz files on Linux

Copy and paste this shell function into your bash or zsh:

function gzToXz { zcat "$1" | xz -c - -ev9 > "${1%.*}.xz" ; }

This will decompress the file using zcat and pipe it into xz with the highest compression setting (-ev9). If you prefer less compression but more speed, use e.g. -5 instead of -ev9.

Use like this:

gzToXz my.gz

which will recompress my.gz to my.xz.

In case you want to recompress every .gz file in the current directory, use:

for i in *.gz ; do echo "$i" ; gzToXz "$i" ; done

It might be best to make a backup of the data even though I have used this script many times for my own data.

Posted by Uli Köhler in Linux, Shell

Listing devices supported by OpenWRT

Problem:

You need a list of devices supported by OpenWRT.

Continue reading →

Posted by Uli Köhler in Embedded, Shell

git svn: Clone latest revision only

Problem:

You want to use git-svn to clone a SVN repository, but you don’t want to clone the entire history (which can be quite slow) but only the latest revision.

Continue reading →

Posted by Uli Köhler in git, Shell, Subversion, Version management

SVN: Find last revision number without cloning

Problem:

You want to find out what the last revision number of a remote subversion repository is without cloning it (e.g. because cloning takes a looong time with subversion).

Continue reading →

Posted by Uli Köhler in Shell, Subversion, Version management

Enable Menu-Style Autocompletion in ZSH

Problem:

You have installed zsh but you don’t see the menu-style autocompletion (where you can navigate the suggestions using the arrow keys on the keyboard)

Continue reading →

Posted by Uli Köhler in Shell

Shell: Strip directory from path

Problem:

In the Linux shell, you have  a file path and you want to strip everything but the filename, for example  you have the path ../images/photo.jpg  and want only photo.jpg

Continue reading →

Posted by Uli Köhler in Shell

Convert ZIP to TAR using Linux shell

Problem

You have ZIP archive and want to convert it to TAR automatically conserving the original directory structure.

Continue reading →

Posted by Uli Köhler in Shell

Strip extension from filename in bash/zsh

Problem:

You have a filename in the Linux shell and want to strip/remove the filename extension from it – e.g. if you have myarchive.zip, you want to get only myarchive as output.

Continue reading →

Posted by Uli Köhler in Shell