Ubuntu Technical

Ubuntu technical problems and solutions reference, a modern cookbook.

Category Archives: shell script

Customized GPG

Gpg encryption is cool. It’s so cool, that I want to keep all my important files (that means back-up files) encrypted on my external storage.

Using gpg is fairly straight forward:

1) Generate a private key.

gpg --gen-key

After answering some standard questions, the key is ready.
Note: You better not forget the password you choose, or else your encrypted files are lost forever.

2) Check you key:

gpg --list-keys

This will display a list of all available keys.

3) Encrypt a file

gpg --encrypt --recipient 'key name' foo.txt

This will generate the encrypted file: foo.txt.gpg

4) Decrypt a file

gpg --output foo2.txt --decrypt foo.txt.gpg

foo2.txt file will be created.

So, until now I presented a quick guide to encrypt/decrypt a file. However, this wasn’t enough for me. I wanted to go a little further. I wanted to be able to encrypt folders as well, and the possibility to delete the original file, and keep only the encrypted one. So, I though I write my own function.
And so, tec was born.

In short tec stands for: tar, encrypt, clean. Long description: Tarballs and encrypts the TARGET using gpg (GnuPG) encrypton. Optionally it deletes the TARGET.

I have created a repository for my function on Gitorious, feel free to check the Projects page and download it if you like.

Just copy tec.sh into /usr/bin, and you’re good to go.

cd <download directory>
sudo cp tec.sh /usr/bin

For general help, type:

tec.sh --help

Basic usage:

# with delete option, to delete the original file, and keep the encrypted one
tec.sh -dr <key> <file>
# without delete option
tec.sh -r <key> <file>

The project is in it’s early phases. Currently it only encrypts. For decryption the standard gpg commands have to be used. I plan to maintain the function, and try to add as much functionality as I can.

cheers.



Flattr this

Advertisements

Compress and Extract files with tar.gz

Or simply building a “tarball” as it is sometimes called. Even though this is a trivial task, I found myself quite often looking for the command, better yet for the options, because I tend to forget. So why not look for the command on my own blog.

Creating a tar archive

cd location_of_files_to_be_archived
tar cvzf name_of_archive.tar.gz *

Parameters explained:
– c – create
– v – verbose output
– z – –gzip, –gunzip, –ungzip filter the archive through gzip
– f – use the following file for archive

Note: The f option needs to be the last in the list of parameters. Whatever characters are following f, will be the name of the tarball.

Extract a tar archive

cd location_of_files_to_be_archived
tar xvzf name_of_archive.tar.gz *

Parameters explained:
– x – extract

Cheers



Flattr this

Using bash to count number of lines of code in a project

As the title suggests, here are 2 ways of counting the lines of code you just wrote, in whatever language. My examples will look into .java files.

To make the result include commented line, in other words, every line in the files.

cd {project location}
find . -type f -name '*.java' -exec cat {} \; | sed '/^\s*$/d' | wc -l

The second command excludes commented out lines. It only counts code, pure code.

cd {project location}
find . -type f -name '*.java' -exec cat {} \; | sed '/^\s*#/d;/^\s*$/d;/^\s*\/\//d' | wc -l

Cheers



Flattr this

Sync directories excluding file types

I have recently needed to find a solution to moving the content of a directory from one folder into another, but excluding certain file types. After digging through the documentation I have found the rsync command.
Short extract from it’s man pages:

Rsync is a fast and extraordinarily versatile file copying tool. It can copy locally, to/from another host over any remote shell, or to/from a remote rsync daemon. It offers a large number of options that control every aspect of its behavior and permit very flexible specification of the set of files to be copied. It is famous for its delta-transfer algorithm, which reduces the amount of data sent over the network by sending only the differences between the source files and the existing files in the destination. Rsync is widely used for backups and mirroring and as an improved copy command for everyday use.

So, here is the script to copy files from one place to another ignoring certain file types (in this example I exclude .avi files):

rsync /source/folder -av --exclude='*.avi' /destination/folder

The -v option is for verbose, so that we can see in the output what’s being synced.
Enjoy.



Flattr this

Calling shell script from Unity Launcher

I have already written in the past a post regarding how to create a custom unity launcher.
Yesterday, I’ve come across an issue. I’ve downloaded Pentaho’s Data Integration tool Kettle, and the executable is a shell script. Meaning that it needs to be called from a terminal in order to run. Now, even though I’ve added the shell script into a Unity Launcher, the software didn’t start after pressing the icon in the dash.
So, I figured out that I need to create another shell script which calls the gnome-terminal and passes the call to the kettle shell script.
In order to achieve this, I used expect (install)

So, I’ve created a script called call_tool.sh:

#!/usr/bin/expect -f

# Get a Bash shell
spawn -noecho bash

# Wait for a prompt
expect "$ "

# Send the dir to kettle and then send the command for spoon.
send "cd /path to script\n"
send "./spoon.sh\n"

# Hand over control to the user
interact

The \n character is needed for the enter.

After that I’ve made the script executable:

sudo chmod +x call_tool.sh

Then pointed my Unity launcher to call_tool.sh, and that’s it. It works.



Flattr this

%d bloggers like this: