Tag Archives: Linux - Page 6

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

A quick intro to crontab

Every now and then I need to create jobs that run on intervalls or at special times. In *nix there is a handy command called crontab that takes care of this. However the crontab command is not always self-explanatory so I made this quick intro to the command so I don’t have to keep it in my head

Command line options:

crontab -l   # Show my current cron jobs
crontab -e   # Edit my cron jobs. Opens your default editor (e.g. VIM)
crontab -r   # Removes all cron jobs

Crontab syntax (user):

#Example
#min hour day month weekday command
  5   *    *    *      *    echo Hi #Runs 5 min past every hour
 */5  *    *    *      *    echo Hi #Runs in 5 min intervall
  *  8,20  *    *      *    echo Hi #Runs at 08:00 and 20:00  
  *  12   */2   *      *    echo Hi #Runs every other day at 12:00
 30  23    *    *      0    echo Hi #Runs every Sunday at 23:30   

Crontab syntax (root):

#Example
#min hour day month weekday user command
  0   *    *    1      *    root   echo Hi #Runs every hour in January
 */10 *    *    *      *    niklas echo Hi #Runs in 10 min intervall  

Only differens between root crontab and a user crontab is that root needs to tell cron who will be running the job

Syntax details:
min = minutes (0-59)
hour = hours (0-23)
day = day of month (1-31)
month = month (1-12)
weekday = day of week (0-6) (0 is Sunday)
user = the user that runs the job
command = command to run. Same syntax as if you would have run it on the command line. More then one command can be run, just separate the commands with a semicolon (;)

Run a cron job in “silent mode”:
Cron likes to mail that everything has gone well after running the command. If you do not want these mails you can put a cryptic pice of code at the end of your command like this:

#min hour day month weekday command
  0   0    *    *      *    rm -rf /home/niklas/tmp/* >/dev/null 2>&1

This will delete everything in my /home/niklas/tmp directory every night at 00:00. No mail will be sent

Tested on Red Hat Enterprise Linux AS release 3 (Taroon Update 9)