Tag Archives: Linux - Page 6

Bash script simulating the ‘tree’ command

When trying to get to know a new system I really like the ‘tree‘ command. It gives me a fast and nice overview of the filesystem of the application. Sometimes I work on systems that does not have this great tool available and for those occasions I made this bash script:

#!/bin/bash
olddir=$PWD;
declare -i dirdepth=0;
function listfiles {
        cd "$1";
        for file in *
        do
                for ((i=0; $i < $dirdepth; i++))
                do
                        ##Tab between each level
                        printf "\t";
                done
                ## Print directories with brackets ([directory])
                if [ -d "$file" ]
                then
                        printf "\1[$file]\n";
                else
                        printf "$file\e[0m\n";
                fi

                ##Work our way thru the system recursively
                if [ -d "$file" ]
                then
                        dirdepth=$dirdepth+1;
                        listfiles "$file";
                        cd ..;
                fi
        done
        ##Done with this directory - moving on to next file
        let dirdepth=$dirdepth-1;
}
listfiles "$1";
##Go back to where we started
cd $olddir;
unset i dirdepth;

To use the script just do the following:

  1. Create a new file called 'tree.sh' (or whatever you like)
  2. Paste the code into the file and save
  3. Make the file executable
  4. Run the file: . tree.sh

This script is tested on OSX 10.6.8, Red Hat Enterprise Linux AS release 3 (Taroon Update 9)

Change extension of many files using bash

Sometimes I need to rename a bunch (thousands) of file extensions. Doing this manual is really not feasible, so after some digging I found this solution:

for filename in *.MQ; do mv $filename `basename $filename .MQ`.XML; done

This snippet finds all files with the extension ‘MQ’ in your current folder and renames them with the extension ‘XML’.

Can be run directly on the command line in most *nix variants. Tested on Red Hat Enterprise Linux AS release 3 (Taroon Update 9)

My Tmux “cheat sheet”

I work a lot in terminal windows and sometimes from different locations. Starting new sessions and logging in to every server (sometimes up to 10 servers with different users) can be quite tiresome. In the beginning I was a fan of the GNU Screen program but after I found Tmux I never looked back. Now dont get me wrong, GNU Screen is a great program but I believe that Tmux handles splits, windows and commands a little better. Here are my most used commands i Tmux

Start tmux

tmux

Start saved session

tmux attach

Display sessions

tmux ls

Start a new shared session

tmux -S /tmp/shared_session
chmod 777 /tmp/shared_session #Anyone can now join your session

Attach to a shared session

tmux -S /tmp/shared_session attach

Command mode

ctrl + b

All commands below start with this key combination. It makes Tmux waiting for a command to run

Exit session (session is automatically saved)

ctrl + b + d

Help screen

ctrl + b + ?

This will show you alla commands available. If you make changes to command in the configuration file you will also see them here. To exit the help press q

Create new window command

ctrl + b + c

Rename window you are currently in

crtl + b + , (comma)

Switch between windows (You can see all created windows in the bottom list of Tmux window)

ctrl + b + 0-9 #eg ctrl + b + 1 will take you to window number 1 

Switch between panes (ctrl + b + arrow keys)
* Key Up to switch to pane above
* Key Down to switch to pane below
* Key Right to switch to pane to the right
* Key Left to switch to pane to the left

If you stand in the pane the most to the right and press to go right the cursor will move to the leftmost pane. Same goes for all directions.

Scroll mode (this let you scroll window/pane by using Page Up/Page Down and the Up and Down keys)

ctrl + b + PageUp

Exit scroll mode

Esc or q key

The following commands depend om my config (default options are found in the help menu if you are not using my config)

Split window horizontal

ctrl + b + h

Split window vertically

ctrl + b + v

Grow pane horizontally

ctrl +b + + (plus)

Shrink pane horizontally

ctrl + b + - (minus)

Grow pane vertically

ctrl + b + *

Shrink pane vertically

ctrl + b + /

My Tmux configuration file (place it in your home directory named .tmux.conf to use it)

# Splitting windows into panes with h and v 
bind-key h split-window -v
bind-key v split-window -h

# Set up resize-pane keys
bind-key + resize-pane -D 3
bind-key / resize-pane -L 3
bind-key - resize-pane -U 3
bind-key * resize-pane -R 3

NEW! For all you spanish speaking people out there, my friend Stephen Chow has translated this post in his blog: stephenchow.es