Create Multiple Nested Directories in Linux
Did you know that you can create a complete directory tree with one command?
Let’s start with a simple example:
mkdir -p a/b/c
will create this tree:
`-- a
`-- b
`-- c
And any of these:
mkdir -p a/{b1,b2}/c
or
mkdir -p a/b{1,2}/c
Will create this tree:
› Continue reading
If you enjoyed this post, make sure you subscribe to my RSS feed!
Brainfuck String Generator Generator
Here is a small C program, which takes a string as its input, and generates a (not so trivial) brainfuck code which generates this string.
For a tool to convert brainfuck code into C, see this post
To run the program, first compile it with gcc:
gcc -o bf_generator bf_generator.c
No, run it:
./bf_generator “a string to convert into brainfuck code”
You can also pass a text file as its input through a pipe:
cat testfile.txt | ./bf_generator
Download the source code here: 
Enjoy
If you enjoyed this post, make sure you subscribe to my RSS feed!
A Bash Script to Convert brainfuck Code Into C
Brainfuck is a minimalist Turing Complete language. You can read about it here.
The following bash script takes a file with brainfuck code (legal characters are >< ,.+-[] any other characters are ignored), and generates its C equivalent code, which can then be compiled with a C compiler and executed.
The output of this script lacks indentation. If you insist on having the C code indented you can achieve this by passing the output through a pipe to “indent”, “astyle” or similar programs.
The script should be called in the following way:
./bf.sh code.bf > code.c
Where code.bf is the file containing the brainfuck code.
If you enjoyed this post, make sure you subscribe to my RSS feed!
Share Code Snippets Easily From the Command Line
This is a great tip I found at commandlinefu to easily share code snippets write from the command line.
First, you may want to add this to your aliases file:
1 | alias share='curl -F "sprunge=<-" http://sprunge.us | xclip' |
(If you don’t have the xclip program, you can easily install it from your package manager.)
Now, say that you have a c program named “hello.c”, to share it, type this (after reloading your aliases file of course):
cat hello.c | share
Now, the code is already in the internet, and the URL is in your clipboard. Go to firefox and click the middle mouse button in the URI area. You’ll see a URL similar to this:
http://sprunge.us/BaFS
To view the code with syntax highlighting and line numbers, just append “?c? (or “?lang” where ‘lang’ is the relevant programming language). So the final URL should look like this:
http://sprunge.us/BaFS?c
Enjoy
If you enjoyed this post, make sure you subscribe to my RSS feed!
Tiny Bash Functions to Convert Between Numeric Representations
I have these functions in my aliases file. They make it easy to convert between hex, binary and decimal representations of numbers. You might find it useful.
Just add these to your aliases file (imported from ~/.bashrc):
1 2 3 4 5 6 | function h2d { echo "obase=10; ibase=16; $( echo "$*" | sed -e 's/0x//g' -e 's/\([a-z]\)/\u\1/g' )" | bc; } function h2b { echo "obase=2; ibase=16; $( echo "$*" | sed -e 's/0x//g' -e 's/\([a-z]\)/\u\1/g' )" | bc; } function b2d { echo "obase=10; ibase=2; "$*"" | bc; } function b2h { echo "0x$(echo "obase=16; ibase=2;"$*"" | bc)"; } function d2b { echo "obase=2; ibase=10; "$*"" | bc; } function d2h { echo "0x$(echo "obase=16; ibase=10; "$*"" | bc)"; } |
Now you should be able to use it like this:
h2d 0xff
The output will be 255 in this case.
h2b is for “hex to binary”, the others are similar (h: hex, b: binary, d: decimal)
Enjoy
If you enjoyed this post, make sure you subscribe to my RSS feed!
A Program for Bit-Slicing
I wrote this short program in C because I needed to do some bit-slicing in some homework. It's not too generic (accepts only 32 bit integers) but it can help in most cases.
An example usage of the program:
bs 0x12345678 31 4
Will give the following output:
0x12345678[31:4] = 0x1234567› Continue reading
If you enjoyed this post, make sure you subscribe to my RSS feed!
Edit The Command Line With Vim
This is a quick (and great) tip I found at Daily Vim:
Open a linux terminal, and type some (long) command.
Now type [ctrl]+[x] and then [ctrl]+[e]
The command should be moved now to a vim window.
Edit the command (fix typos, change parameters, etc..) and save.
The command will now be executed.
Enjoy
If you enjoyed this post, make sure you subscribe to my RSS feed!
Renaming Files In Linux
In a previous post, I wrote about converting strings from camelCase to undescore delimited format.
In this post, we’ll do the same but for file names. We’ll use the command line program “rename” for this purpose.
Here we go:
1. Convert all file names in current directory which are of the form “myFileName” or “MyFileName” into the form of “my_file_name”:
1 | rename 's/(.)([A-Z])/$1_\l$2/g' * && rename 's/([A-Z])/\l$1/g' * |
2. Convert all file names in current directory which are of the form “my_file_name” into the form “myFileName”:
1 | rename 's/([a-z])_([a-z])/$1\u$2/g' * |
3. Convert all file names in current directory which are of the form “my_file_name” into the form “MyFileName”:
1 | rename 's/([a-z])_([a-z])/$1\u$2/g' * && rename 's/^([a-z])/\u$1/g' * |
Enjoy
If you enjoyed this post, make sure you subscribe to my RSS feed!
Convert camelCase to Underscores Using sed
This short post deals with converting strings of the form camelCase or CamelCase into camel_case, and vice versa. These are three different popular naming conventions for variable/function/class names.
Convert CamelCase or camelCase to camel_case:
1 | sed -e 's/\([A-Z]\)/_\l\1/g' -e 's/^_\([a-z]\)/\1/g' file.txt |
Convert camel_case to camelCase
1 | sed -e 's/_\([a-z]\)/\u\1/g' file.txt |
Convert camel_case to CamelCase:
1 | sed -e 's/_\([a-z]\)/\u\1/g' -e 's/^\([a-z]\)/\u\1/g' file.txt |
If you enjoyed this post, make sure you subscribe to my RSS feed!
Browse the Linux Kernel Source with LXR
LXR (stands for Linux Cross-Reference), is a great tool which indexes the source code of the Linux Kernel and allows you browse the source in your favorite browser (Firefox), with quick references for all functions/files referenced in that code. (it’s not limited to use in Linux Kernel though, and can be used to index any large project’s source code).
Whenever I need to touch the Linux Kernel code, I first open a new tab with LXR, another with Google ready to work, and a shell terminal ready for grep queries.
If you enjoyed this post, make sure you subscribe to my RSS feed!
About Me
Tags
My Twitter
Categories
Popular Posts
Archives
- July 2009 (1)
- June 2009 (6)
- May 2009 (4)
- March 2009 (18)
- February 2009 (10)
- January 2009 (10)
- December 2008 (7)
- November 2008 (8)
- October 2008 (1)
- August 2008 (1)
- July 2008 (1)
- June 2008 (2)

Me @ Social Media