Category Archives: YASDUT

./desc – quickly describe your files

Everytime I work on scientific projects, I deal with a massive amount of measurement data, lists and fragments of them etc. The problem is, that after several days or weeks, my “descriptive” filenames aren’t that descriptive anymore and I simply don’t have a clue what I did and what’s in the file. I thought it would be nice, if there was a database which holds files descriptions.

I came up with a tiny script which I placed in ~/bin (part of my $PATH) and called it “desc”. If you run it when you’re in a specific directory where you want to store description data for some files, simple type:

desc yourFileToDescribe

…and you will be promted to enter a description. The description is then stored in ./.DESCRIPTION.db and can be accessed via the “desc”-script anytime you’re in the working directory.

Here is the script, just try it out:

#!/bin/bash
# Tamas Gal - http://tamasgal.com
# desc v0.1
# this script creates a file-description-entry in ./.DESCRIPTION.db

FILE_TO_DESCRIBE=$1
FILE_DESCRIPTION=
DESCRIPTION_FILE="`pwd`/.DESCRIPTION.db"

if (( ${#FILE_TO_DESCRIBE} == "0" )) ; then
if [ -e $DESCRIPTION_FILE ] ; then
cat $DESCRIPTION_FILE
exit
fi
echo "No description(s) available."
exit
fi

if [ -e $DESCRIPTION_FILE ] ; then
if (( `grep "$FILE_TO_DESCRIBE: " $DESCRIPTION_FILE | wc -l` > "0" )) ; then
echo "`grep "$FILE_TO_DESCRIBE: " $DESCRIPTION_FILE`"
read -p "Edit (y/n)? "
[ "$REPLY" == "y" ] || exit
fi
fi

echo "Enter description for '$FILE_TO_DESCRIBE':"
read FILE_DESCRIPTION

if (( ${#FILE_DESCRIPTION} != "0" )) ; then
grep -v "$FILE_TO_DESCRIBE: " $DESCRIPTION_FILE > "$DESCRIPTION_FILE.tmp"
mv "$DESCRIPTION_FILE.tmp" $DESCRIPTION_FILE
echo "$FILE_TO_DESCRIBE: $FILE_DESCRIPTION" >> $DESCRIPTION_FILE
echo "done"
fi

YASDUT #4 (Line Numbering)

YASDUT #4

Line Numbering

To add line numbering to an existing file, you can simply use ‘nl’ which adds line number to a given input text and redirect the output to a new file:

nl foo.bar > foo.bar.num

Read the man pages for more options!

YASDUT #3 (List Only Directories)

YASDUT #3

List Only Directories

There are many ways to list only directories of a given parent directory. Here is one of them:

ls -l | grep "^d"

You can define a fancy alias like ‘lsd’ to avoid typing a lot every time ;-)

alias lsd 'ls -l | grep "^d"'

YASDUT #2 (Hotkeys in Bash)

YASDUT #2

Hotkeys in Bash

Most o the bash users only uses the Ctrl+C and Ctrl+D command to stop a process or quickly kill the shell, but there are more useful hotkeys in a bash shell:

  • ctrl-c kill the current command/process
  • ctrl-d kill the shell
  • ctrl-l clear the screen
  • ctrl-r search in the previously given commands
  • esc-p like ctrl-r lets you search through the previously given commands
  • ctrl-u clears the typing before the hotkey
  • ctrl-a jump to the begining of the command you are currently typing
  • ctrl-e jump to the end of the command you are currently typing in
  • esc-b take you back by one word while typing a command
  • ctrl-h delete one letter at a time from the command you are typing in
  • ctrl-z pus the currently running process in background, the process can be brought back to run state by using ‘fg’.
  • esc-. the last command you typed

Yet Another Short Daily Unix Thought #0 (Next UID)

OK so here we are, the 0. short daily unix thought. The title of this series is kind of weird but it is what it is. Since it’s pretty looooong and so daily, I’ll use YASDUT #x instead where x is an increasing natural number.

YASDUT #0 (Next UID)

To find the next user ID available on your system, you can simply use this bash script:

#!/bin/bash
TMP_UID_LIST=/tmp/_UIDList
awk -F":" '{ print $3 }' /etc/passwd > TMP_UID_LIST
awk -F":" '{ print $3 }' /etc/group >> TMP_UID_LIST
LAST_UID=`sort -g TMP_UID_LIST | tail -1`
NEXT_UID=`expr $LAST_UID + 1`
echo "The next UID is $NEXT_UID"