Archive for the ‘Command Line’ Category
Crontab Code Generator
Written by BinnyVA on June 22, 2009 – 11:39 pm -
JSL Crontab Generator is a GUI application that can be used to create new crontab entries easily. Its a Javascript application – so it will run from your browser – there is no download/install involved. This was created as a demo for my JSL Javascript Library.
crontab syntax
Each line in a crontab file is a job and follows a particular format as a series of fields, separated by spaces or tabs(see example below). Each field can have a single value or a series of values.
crontab Operators
There are multiple ways of specifying several date/time values in a field:
- The comma(,) specifies a list of values, for example: “1,3,4,7,8″
- The dash(-) specifies a range. Example: “1-6″, which is equivalent to “1,2,3,4,5,6″
- The asterisk(*) operator specifies all possible values for a field. For example, an asterisk in the hour time field would be the same as ‘every hour’.
There is also an operator which some extended versions of cron support, the slash(/) operator, which can be used to skip a given number of values. For example, “*/3″ in the hour time field is equivalent to “0,3,6,9,12,15,18,21″. So “*” specifies ‘every hour’ but the “*/3″ means only those hours divisible by 3.
Example: the following will clear the Apache error log at one minute past midnight each day.
01 00 * * * echo "" > /www/apache/logs/error_log
Fields
.---------------- minute (0 - 59) | .------------- hour (0 - 23) | | .---------- day of month (1 - 31) | | | .------- month (1 - 12) OR jan,feb,mar,apr ... | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat | | | | | * * * * * <command to be executed>
For more information about the cron and crontab, run the command man cron and man crontab.
Javascript Crontab Code Generator
You can use this tool to generate the crontab commands easily. Just enter the command and the intervals it should be executed on – this tool will create a line in crontab syntax that will do the work for you. All you have to do is add the generated line to your crontab file. Once the result line is generated, run the command ‘crontab -e’ – this will open your crontab file in an editor. Just copy the generated line into this editor and save the file – you are done.
Apache Example
For example, lets say you want to clear the apache log file every day at midnight. The command to be executed is…
echo "" > /www/apache/logs/error_log
Enter that command into the command input field in the application. Next click on the ‘Choose’ radio button in the minute and set it to 0. Then select the ‘Choose’ option in Hour and set it to 12 Midnight. Live the rest as it it – we want the command to be executed every day. Now just click on the ‘Create Crontab Line’. The final crontab line will show up in the ‘Result crontab Line’ textarea.
Tags: app, cron, crontab, generator, javascript, jsl
Posted in Applications, Tools | 5 Comments »
Top 4 Terminal GUI Applications
Written by BinnyVA on June 19, 2009 – 1:19 am -
Terminal GUI Apps? Does sound oxymoronic doesn’t it? Well, there are GUI apps in the terminal – and here is a tribute to ones that I find most useful…
top/htop – Process Viewer
From the man page…
The top program provides a dynamic real-time view of a running system. It can display system summary information as well as a list of tasks currently being managed by the Linux kernel. The types of system summary information shown and the types, order and size of information displayed for tasks are all user configurable and that configuration can be made persistent across restarts.
I always have a processor load displaying applet(System Monitor) in my panel. Whenever I see a spike, I fire up a console and type in the top command – its very useful in finding which process is creating the load. Usually, it is some cron job like updatedb or makewhatis. But occasionally, I find a zombie processes this way.
htop is, for the lack of a better word, a better top. It provides a more colorful display(top has a color mode as well – open top and press ‘z’). It also makes it a bit more easier to kill processes.

mc – File Manager
mc(or Midnight Commander) is a file manager. Sure you can cp and mv your files around, but after a while, it gets tiring. mc is a dual pane file manager -it means you can see two folders at the same time. You can copy/move files from one to the other, delete, rename, view file etc – in short, everything a file manager is expected to have, mc has.

mpg123 – Audio Player
I am not writing too much about this – considering the fact that I have already wrote a post about mpg123/mpg321 in the audio player series.
aptitude
aptitude is a ncurces based GUI for the apt package manage – as a result, this is only available in Debian based systems(Ubuntu, Knopix, etc.). I have not yet seen anyone using aptitude – if they have a GUI system, they use synaptic – and if they are comfortable with the command line, they use apt-get command. But still, there is a middle ground – if for some reason you need it.

Thanks to Rajesh for the aptitude screenshot.
Anything Else?
Any other Terminal GUI application? I can only think of these at the moment. If you can think of others, comment.
Tags: application, aptitude, gui, htop, mc, mpg123, review, terminal, top
Posted in Applications, Command Line, Reviews, Tools | 10 Comments »
Customizing the Terminal: 6 Command Line Tips and Tricks
Written by BinnyVA on April 26, 2009 – 9:06 pm -
A few tips and tricks on the terminal to make you more efficient when using it. If you know of any other tips, add it in the comments section.
1. ls Without ls
When you are trying to cd into a deep folder, you might not know the correct folder name some levels deep. You might have to do something like…
$ cd ~/Scripts/Perl
$ ls
bin SedGUI ToSee Cronjobs Maintenance
$ cd Maintenance
There is an easier way – go to the wanted folder
$ cd ~/Scripts/Perl
Now, without pressing enter, double tap the TAB key. You will get a list of files. And the command prompt waiting to be filled…
$ cd ~/Scripts/Perl/[TAB TAB]
bin SedGUI ToSee Cronjobs Maintenance
$ cd ~/Scripts/Perl/_
You can also use double-TAB to auto-complete commands.
2. Searching the history with Ctrl+R
If you have to use a command you have already used before, press CTRL+R and then type a few characters of the command. The latest command with those characters will be shown – if that is the command you want to execute, press enter and it will be executed. If not, just press CTRL+R again and it will show the next command.
You have no idea how useful this tip is if you haven’t been using it. I use this all the time.
For more details, read this article.
3. Open Terminal using a Shortcut
If you are a GUI user, chances are you prefer using a Terminal emulator(like gnome-terminal or konsole) instead of going into the Terminal mode by pressing CTRL+ALT+F1. If so, assign a shortcut to those emulator apps. I prefer using the shortcut ‘Ctrl+Alt+A’ to do this.
Gnome
If you are in gnome, there is a very easy way to do this…
- Go to System > Preferences > Keyboard Shortcuts
- Find ‘Run a Terminal’ – assign the shortcut ‘Ctrl+Alt+A’
KDE
- Right Click on the K-Menu > Menu Editor
- Find your terminal application in the list(usually System > Terminal Applications > Terminal)
- Select the ‘Current Shortcut Key’ option and set it to ‘Ctrl+Alt+A’
You can also do this by opening the KHotKeys application.
4. Bash Keyboard Shortcuts
Learn the bash keyboard shortcuts – these are the ones I use the most…
- CTRL+R
- Search the history. We already talked about this.
- CTRL+L
- Clears the screen. Use this instead of the
clearcommand. - CTRL+D
- Use this instead of the
exitcommand. - CTRL+C
- Kill whatever is running
- CTRL+Z
- Puts whatever is running into a suspended background process. Use
fgto restore it.
5. Find Command using apropos
Find the command you want using the apropos command. Just type in a description of the command as the first argument. For example, lets say you want to find the command to list the directory contents. Use the command…
$ apropos "directory contents"
dir (1) - list directory contents
ls (1) - list directory contents
ls (1p) - list directory contents
ntfsls (8) - list directory contents on an NTFS filesystem
vdir (1) - list directory contents
The only problem is that I can never spell ‘apropos’ – so I keep this in my .bashrc file…
alias apox='apropos'
6. Learn New Commands
There are a few sites that publish cool commands on a daily/semi-daily basics – subscribe to those and learn new commands…
Tags: command, key, shortcut, tab, terminal, tips, tricks
Posted in Command Line, Configuration, Tools | 3 Comments »
Customizing the Terminal: 5 Configuration Settings in Bash that makes you a CLI Power User
Written by BinnyVA on April 13, 2009 – 1:10 am -
There are some settings that are very useful if you work on the terminal a lot. Many of the cool ones are not enabled by default – this is a small list of the configuration settings that I use to make my terminal usage more productive.
This is part 3 of the Customizing the Terminal series. Already published posts in this series are…
1. Case Correction
I like to title case my folders and files’ names. The folders in my home are ‘Scripts’, ‘Documents’, ‘Temp’ etc. The first character is in upper case. But when I work on the command line, I don’t always remember to uppercase the first character when trying to cd into a folder. Consequently, the tabbing will not work. Fortunately, there is an option that auto corrects the case for you. Just open a terminal and type in this command…
shopt -s nocaseglob
Other useful shopt option are…
- cdspell
- Corrects typos in your file/directory name.
- histappend
- Makes sure that histories in multiple simultaneous shells don’t overwrite each other.
2. Select Which Commands to Store in History
By default, all commands you type in are stored in the history. You can pick and chose the commands you want to store by putting the option…
export HISTCONTROL=ignorespace
in your ~/.bash_profile file. This will make sure that bash don’t store any command beginning with the space character. So if you want bash to forget that you typed in ‘ls’, just type in ‘ ls’(<space>ls).
3. Don’t Store Duplicate Commands in your History
As I said earlier, all commands you type are stored – even the duplicate ones. You can prevent this by putting this text in your .bash_profile file…
export HISTCONTROL=ignoredups
If you want to ignore spaced commands and want to prevent storing of duplicate commands, use the option…
export HISTCONTROL=ignoreboth
4. Auto-complete Command from History
Picture this – you type in ’ssh’ and press the ‘Page Up’ key – and bash automatically fetches the last command that starts with ssh – and completes the command for you. Well, its possible – add the following line in your ‘.bash_profile’ file…
export INPUTRC=$HOME/.inputrc
Now, create a file called .inputrc in your home and enter this into it…
#Page up/page down
"\e[5~": history-search-backward
"\e[6~": history-search-forward
Yes, I am aware of the up Ctrl+R trick – that comes in the next post.
5. Infinite History
You can increase or decrease the size of the history by adding this line in the .bash_profile file…
export HISTSIZE=500
export HISTFILESIZE=500
This will limit the commands to be stored in the history to 500. If you want to remove the limit use these lines…
unset HISTSIZE
unset HISTFILESIZE
There is a good chance that this will make your history file quite huge – use with care.
Please share your configuration settings for bash in the comments.
Related Links
Tags: bash, cli, command, Configuration, history, power, setting, user
Posted in Command Line, Configuration | 4 Comments »
Customizing the Terminal: Create Useful Aliases
Written by BinnyVA on March 28, 2009 – 12:35 am -
This is part two of the ‘Customizing the Terminal’ series. Part one is ‘Customizing the Terminal: The Prompt‘. In this part, we’ll see how to create aliases to make working in the console easier.
How To Create an Alias
You can create a temporary alias using this command…
alias new_name='old command'
This will stop working when you exit the terminal. If you want to make the alias permanent, put the same command in your ~/.bashrc file.
There is another way to create an alias – create a executable file and place it in a folder in your path. This is not technically an alias – but it works the same way. I use this for alias that tend to change often. Its easier to find a file in a folder and edit it. YMMV.
My Aliases
This is a incomplete list of the aliases I use. Feel free to copy them to your .bashrc file.
Quick Directory Jumps
Create an alias to jump to folders you have to visit often. This is my list…
alias www='cd /var/www/html'
alias e='cd /mnt/x'
Relative Jumps
The above jumps are absolute jumps – relative jumps are possible too…
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias .....='cd ../../../..'
Some prefer this syntax…
alias ..='cd ..'
alias ..2='cd ../..'
alias ..3='cd ../../..'
alias ..4='cd ../../../..'
Often Used Commands
If you use some commands a lot, create smaller alternative for it…
alias x='exit'
alias q='exit'
alias rmdir='rm –rf'
Many of my own scripts are also alias’ed this way…
alias bk='perl "/home/binnyva/Scripts/Perl/Maintenance/Rsync Backup/RsyncBackup.pl"'
alias rbk='perl "/home/binnyva/Scripts/Perl/Maintenance/Rsync Backup/RsyncRemoteBackup.pl"'
alias nbk='perl "/home/binnyva/Scripts/Perl/Maintenance/Rsync Backup/RsyncNetworkBackup.pl"'
alias bdb='perl "/home/binnyva/Scripts/Perl/Maintenance/Database Backup/Dbbackup.pl"'
Complex Commands
Create a short version of long and complex command using alias…
alias gitstat='git status | perl -pe "exit if(/Untracked files\:/)"'
alias ra='ruby script/server'
alias wikipedia='cd /mnt/x/Data/Wikipedia/mywiki; firefox "http://localhost:8001/"; python manage.py runserver 8001; '
alias sup='svn update'
and more.
Command Changes
When I came from Windows to Linux, I was used to the dos commands – but not to the linux’s mv,cp commands. So I used to have aliases for those(I don’t have these now)
alias move='mv'
alias copy='cp'
alias ren='mv'
alias del='rm'
If you go from Red Hat/Fedora to Debian/Ubuntu(or vise versa), you can set up a few alias to make the change easier…
alias yum='apt-get'
You can get a lot of ideas for more aliases by looking at others .bashrc files.
Now tell me you aliases…
Tags: alias, cli, command, customize, terminal
Posted in Command Line, Configuration | 6 Comments »
Customizing the Terminal: The Prompt
Written by BinnyVA on March 10, 2009 – 11:34 pm -
Most Linux ‘gurus’ spend a lot of time working in the terminal. If you belong to that group, this post is for you. This is a tutorial to configure the terminal prompt to the best possible value for your use. Note: This tutorial is for bash users – these instructions will not work in other shells.
The Prompt
You must have seen the prompt if you have use the terminal – it is the first few characters in each line. Usually, it will be…
[username@localhost] ~ $
In this case, the user is shown three piece of information in the prompt –
- Username of the current user
- Hostname
- Current folder name
This post will show you how to customize this prompt to your needs.
Editing the Prompt
Editing the prompt is very simple – you just have to edit a shell variable. To see the current prompt’s value, open a shell and type the command…
echo $PS1
The result will be something like this(in Ubuntu)…
binnyva@binlap:~$ echo $PS1
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$
Which is functionally the same as…
\u@\h:\W\$
To edit this variable, run the command…
export PS1=<New Prompt Value>
Most desktop systems don’t need the username and hostname in the prompt – this is only relevent if your are connected to a remote system. So the first thing to do, if you are on a desktop system, is to remove those two. To do that, run the command…
export PS1="[\W]\$ "
This will change the prompt in the current terminal. To make it permanent, edit the ~/.bashrc and set the PS1 variable there. Just add this line at the end of the file…
export PS1="[\W]\$ "
A Better Prompt
Currently, the prompt has the basename of the current working directory. That is, if we are in ‘~/Sites/Lindesk/posts’, the prompt will be ‘[posts]$ ‘. This is good enough for most people. But I have a problem with this. If I go to another folder, say, ‘~/Sites/OpenJS/posts’, the prompt is still ‘[posts]$ ‘. The prompt is a bit ambiguous in this case. This can be done using a different character – in this case \w(small ‘w’ – the default was capital ‘W’).
[posts]$ export PS1="[\w]$ "
[~/Sites/OpenJS/posts]$ _
This is nice – but you will have a problem if the directory you are in is several levels deep. It might be something like this…
[/var/www/html/sites/Lindesk/lindesk.com/wp-content/plugins/eventr/langs]$ _
That’s long – and inconvenient. There are better ways of doing this.
Show the Beginning and the End.
A better way of doing this is to cut of a part of the folder – so the above path will look something like…
[/var/www/html.../eventr/langs] $ _
This option will show the first 15 characters of the path and then the last 15 characters – if the directory path is bigger than 30 characters. To enable this mode, open up the file ~/.bashrc and add this code…
PROMPT_COMMAND='DIR=`pwd|sed -e "s!$HOME!~!"`; if [ ${#DIR} -gt 30 ]; then CurDir=${DIR:0:12}...${DIR:${#DIR}-15}; else CurDir=$DIR; fi'
PS1="[\$CurDir] \$ "
The First Character of Each Directory
There is yet another method – I got this idea from the fish shell. In this approach, the big path will appear as…
[/v/w/h/s/L/l/w/p/e/langs] $ _
In this option, only the first character of each parent folder will be shown. Only the base folder name will be shown entirely. This is the approach I use. If you want to use this, open the ~/.bashrc file and add this…
PROMPT_COMMAND='CurDir=`pwd|sed -e "s!$HOME!~!"|sed -re "s!([^/])[^/]+/!\1/!g"`'
PS1="[\$CurDir] \$ "
Prompt Variables
The other values you can insert into the prompt are…
- \d
- the date in “Weekday Month Date” format (e.g., “Tue May 26″)
- \D{format}
- the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required
- \e
- an ASCII escape character (033)
- \h
- the hostname up to the first ‘.’
- \H
- the hostname
- \j
- the number of jobs currently managed by the shell
- \l
- the basename of the shell’s terminal device name
- \n
- newline
- \r
- carriage return
- \s
- the name of the shell, the basename of $0 (the portion following the final slash)
- \t
- the current time in 24-hour HH:MM:SS format
- \T
- the current time in 12-hour HH:MM:SS format
- \@
- the current time in 12-hour am/pm format
- \A
- the current time in 24-hour HH:MM format
- \u
- the username of the current user
- \v
- the version of bash (e.g., 2.00)
- \V
- the release of bash, version + patch level (e.g., 2.00.0)
- \w
- the current working directory, with $HOME abbreviated with a tilde
- \W
- the basename of the current working directory, with $HOME abbreviated with a tilde
- \!
- the history number of this command
- \#
- the command number of this command
- \$
- if the effective UID is 0, a #, otherwise a $
- \nnn
- the character corresponding to the octal number nnn
- \\
- a backslash
- \[
- begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
- \]
- end a sequence of non-printing characters
Tags: bash, command, customize, prompt, shell, terminal
Posted in Command Line, Configuration | 20 Comments »
Free eBook – Linux 101 Hacks
Written by BinnyVA on February 15, 2009 – 11:09 pm -
Ramesh, who blogs at The Geek Stuff have released a free eBook ‘Linux 101 Hacks‘. Go download it – if you haven’t done it already.
About the Book
There are total of 101 hacks in this book that will help you build a strong foundation in Linux. All the hacks in this book are explained with appropriate Linux command examples that are easy to follow.
Its kinda like my txt site on Linux Commands – except for the fact that the explanation on the book is much better than my site.
Here is a copy-paste of the table of contents for the book.
Table of Contents
Chapter 1: Powerful CD Command Hacks
- Hack 1. Use CD PATH to define the base directory for cd command
- Hack 2. Use cd alias to navigate up the directory effectively
- Hack 3. Perform mkdir and cd using a single command
- Hack 4. Use “cd -†to toggle between the last two directories
- Hack 5. Use dirs, pushd and popd to manipulate directory stack
- Hack 6. Use “shopt -s cdspell†to automatically correct mistyped directory names on cd
Chapter 2: Date Manipulation
- Hack 7. Set System Date and Time
- Hack 8. Set Hardware Date and Time
- Hack 9. Display Current Date and Time in a Specific Format
- Hack 10. Display Past Date and Time
- Hack 11. Display Future Date and Time
Chapter 3: SSH Client Commands
- Hack 12. Identify SSH Client Version
- Hack 13. Login to Remote Host using SSH
- Hack 14. Debug SSH Client Session
- Hack 15. Toggle SSH Session using SSH Escape Character
- Hack 16. SSH Session Statistics using SSH Escape Character
Chapter 4: Essential Linux Commands
- Hack 17. Grep Command
- Hack 18. Find Command
- Hack 19. Suppress Standard Output and Error Message
- Hack 20. Join Command
- Hack 21. Change the Case
- Hack 22. Xargs Command
- Hack 23. Sort Command
- Hack 24. Uniq Command
- Hack 25. Cut Command
- Hack 26. Stat Command
- Hack 27. Diff Command
- Hack 28. Display total connect time of users
Chapter 5: PS1, PS2, PS3, PS4 and PROMPT_COMMAND
- Hack 29. PS1 – Default Interaction Prompt
- Hack 30. PS2 – Continuation Interactive Prompt
- Hack 31. PS3 – Prompt used by “select†inside shell script
- Hack 32. PS4 – Used by “set -x†to prefix tracing output
- Hack 33. PROMPT_COMMAND
Chapter 6: Colorful and Functional Shell Prompt Using PS1
- Hack 34. Display username, hostname and current working directory in the prompt
- Hack 35. Display current time in the prompt
- Hack 36. Display output of any command in the prompt
- Hack 37. Change foreground color of the prompt
- Hack 38. Change background color of the prompt
- Hack 39. Display multiple colors in the prompt
- Hack 40. Change the prompt color using tput
- Hack 41. Create your own prompt using the available codes for PS1 variable
- Hack 42. Use bash shell function inside PS1 variable
- Hack 43. Use shell script inside PS1 variable
Chapter 7: Archive and Compression
- Hack 44. Zip command basics
- Hack 45. Advanced compression using zip command
- Hack 46. Password Protection of Zip files
- Hack 47. Validate a zip archive
- Hack 48. Tar Command Basics
- Hack 49. Combine gzip, bzip2 with tar
Chapter 8: Command Line History
- Hack 50. Display TIMESTAMP in history using HISTTIMEFORMAT
- Hack 51. Search the history using Control+R
- Hack 52. Repeat previous command quickly using 4 different methods
- Hack 53. Execute a specific command from history
- Hack 54. Execute previous command that starts with a specific word
- Hack 55. Control the total number of lines in the history using HISTSIZE
- Hack 56. Change the history file name using HISTFILE
- Hack 57. Eliminate the continuous repeated entry from history using HISTCONTROL
- Hack 58. Erase duplicates across the whole history using HISTCONTROL
- Hack 59. Force history not to remember a particular command using HISTCONTROL
- Hack 60. Clear all the previous history using option -c
- Hack 61. Substitute words from history commands
- Hack 62. Substitute a specific argument for a specific command
- Hack 63. Disable the usage of history using HISTSIZE
- Hack 64. Ignore specific commands from the history using HISTIGNORE
Chapter 9: System Administration Tasks
- Hack 65. Partition using fdisk
- Hack 66. Format a partition using mke2fsk
- Hack 67. Mount the partition
- Hack 68. Fine tune the partition using tune2fs
- Hack 69. Create a swap file system.
- Hack 70. Create a new user
- Hack 71. Create a new group and assign to an user
- Hack 72. Setup SSH passwordless login in OpenSSH
- Hack 73. Use ssh-copy-id along with ssh-agent
- Hack 74. Crontab
- Hack 75. Safe Reboot Of Linux Using Magic SysRq Key
Chapter 10: Apachectl and Httpd Examples
- Hack 76. Pass different httpd.conf filename to apachectl
- Hack 77. Use a temporary DocumentRoot without modifying httpd.conf
- Hack 78. Increase the Log Level temporarily
- Hack 79. Display the modules inside Apache
- Hack 80. Show all accepted directives inside httpd.conf
- Hack 81. Validate the httpd.conf after making changes
- Hack 82. Display the httpd build parameters
- Hack 83. Load a specific module only on demand
Chapter 11: Bash Scripting
- Hack 84. Execution Sequence of .bash_* files
- Hack 85. How to generate random number in bash shell
- Hack 86. Debug a shell script
- Hack 87. Quoting
- Hack 88. Read data file fields inside a shell script
Chapter 12: System Monitoring and Performance
- Hack 89. Free command
- Hack 90. Top Command
- Hack 91. Ps Command
- Hack 92. Df Command
- Hack 93. Kill Command
- Hack 94. Du Command
- Hack 95. lsof commands.
- Hack 96. Sar Command
- Hack 97. vmstat Command
- Hack 98. Netstat Command
- Hack 99. Sysctl Command
- Hack 100. Nice Command
- Hack 101. Renice Command
About the Author
Ramesh Natarajan is the blogger behind The Geek Stuff. To know more about him and the site, take a look at the about page.
This is what he has to say about himself…
My name is Ramesh Natarajan. I live in Los Angeles, California. I have been adding 1’s and 0 ’s for more than 15 years in the IT industry. I am very much interested in anything that runs on electricity. I have done intensive programming on several languages and C is my favorite. I have done lot of work on the infrastructure side in Linux system administration, DBA, Hardware and Storage (EMC).
Tags: book, ebook, hacks, site, tips
Posted in Command Line, News, Reviews | 1 Comment »
Introducing txt
Written by BinnyVA on July 9, 2008 – 10:47 pm -
I have a small but very useful site called txt. Its a collection of code snippets, Linux commands and other such tidbits of information. The posts will be minimal and often crude. The purpose of that site is just to help me remember the information. To make it easier for me to look up the commands later.
For example, say that I need to remove all subversion information from a folder. That means deleting the ‘.svn’ folders in the current folder and all the folders under it. I have used this command before and have saved it to the txt site. So all I have to do is look up the tag that may have this command – in this case, the svn tag.
Soon, I get the page about removing SVN data from a folder. Nifty, huh?
The purpose of this post is two fold. First, I want to introduce you to my txt site – and hopefully get more traffic for that site. Second, and more important purpose is to get you intrested in this idea – to get a similar site for yourself. I saved a lot of time by using that site. Because its online, I was able to use my commands even when away from my computer. And its helpful for others as well.
You can see the reasons for creating the txt site here…
If you want to create a similar site, just go to WordPress and register for a new site. Trust me, its easy. If you have a similar system, please let me know – post a comment.
Tags: command, personal, site, text, txt
Posted in Command Line, Opinion | 7 Comments »
Perl Script to Backup MySQL Databases
Written by BinnyVA on June 19, 2008 – 11:13 pm -So far, we covered backing up files locally and over a network. Now let let see how to backup databases(only mysql supported – yet).
The script uses ‘mysqldump’ command to backup the data. That means that the backups are in the SQL dump format. The dumps of all the databases that are backed up are compressed and stored in the destination folder. They will be named in this format – YYYY-MM-DD.tar.gz.
Configuration File
This script reads a configuration file named ‘dbbackup.config’ and backups all the databases specified in that file to another location in the same system. This configuration file must be in the same folder as the perl script. The configuration file format is given below…
Data
Project_Nexty
App_activecollab
# Unwanted_DB - commented - will not be backedup
binco
binnyva
The Perl Script
#!/usr/bin/perl
# Backups all the databases specified in the dbbackup.config file
$backup_folder = '/var/Backup/Special/Databases'; #EDIT THIS LINE
use File::Basename;
my $config_file = dirname($0) . "/dbbackup.config";
my @databases = removeComments(getFileContents($config_file));
chdir($backup_folder) or die("Cannot go to folder '$backup_folder'");
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year += 1900;
$mon++;
#Zero padding
$mday = '0'.$mday if ($mday<10);
$mon = '0'.$mon if ($mon<10);
my $folder = "$year-$mon-$mday";
mkdir($folder) or die("Cannot create a folder called '$folder'");
foreach my $database (@databases) {
next if ($database eq '');
chomp($database);
my $table = '';
if(index($database,' ')+1) { #Get just 1 table in the database - if there is a ' '(space) in the db name
my @parts = split(' ',$database);
$database = $parts[0];
$table = $parts[1];
}
print "Backing up $database ... ";
my $file = $database;
$file .= '_' . $table if($table ne '');
$file .= ".sql";
`mysqldump -u root $database $table > $folder/$file`;
print "Done\n";
}
print "Compressing the folder ... ";
`tar -czf $folder.tar.gz $folder/`;
print "Done\nRemoving Folder ... ";
`rm -rf $folder`;
print "Done\n\n";
sub getFileContents {
my $file = shift;
open (FILE,$file) || die("Can't open '$file': $!");
my @lines=<FILE>;
close(FILE);
return @lines;
}
sub removeComments {
my @lines = @_;
@cleaned = grep(!/^\s*#/, @lines); #Remove Comments
@cleaned = grep(!/^\s*$/, @cleaned); #Remove Empty lines
return @cleaned;
}
If you need, you can set this script as a cron job – this will make sure that you don’t have to worry about the backup.
Tags: backup, database, mysql, perl, script
Posted in Programming, Scripting, Shell Scripts | 15 Comments »
Script to Backup Files Over a Network Using Rsync
Written by BinnyVA on June 4, 2008 – 9:47 pm -
This script will backup the specified files to another computer on your network. You can also use this to send your files to a remote server. This script compliments the last Rsync Backup script. Its possible to combine both the script together, I prefer to keep them separate.
The Setup
For this to work, you need to have a password-less login system over ssh. You should configure the remote system to accept your credentials by giving your public key to the remote server. If you are not sure how to do that, just leave a comment and I’ll make a post on how to set it up.
The configuration file is the same format as the one used in the last Rsync script. But in this case, the file name will be ‘rsyncnetworkbackup.config‘.
The Code
#!/usr/bin/perl
#The folder on the remote system that must be used to store the data
$backup_folder = '/home/neo/Backup'; #Final '/' must NOT be there.
# The user for whom we have set up the key based login
$backup_user = 'neo';
# The IP address/domain name of the remote system.
$backup_server = '192.168.0.30';
use File::Basename;
my $config_file = dirname($0) . "/rsyncnetworkbackup.config";
my @all_locations = removeComments(getFileContents($config_file));
foreach my $folder_locations (@all_locations) {
my($folder,$backup_location) = split(/\s+/,$folder_locations);
print "Backing up $folder to $backup_location ... ";
`rsync -avze ssh $folder $backup_user\@$backup_server:\"$backup_folder/$backup_location\"`;
print "Done\n";
}
sub getFileContents {
my $file = shift;
my @lines;
open (FILE,$file) || die("Can't open '$file': $!");
@lines=<FILE>;
close(FILE);
return @lines;
}
sub removeComments {
my @lines = @_;
@cleaned = grep(!/^\s*#/, @lines); #Remove Comments
@cleaned = grep(!/^\s*$/, @cleaned); #Remove Empty lines
return @cleaned;
}
Tags: backup, network, perl, remote, rsync, script
Posted in Networking, Scripting, Shell Scripts | 7 Comments »

Follow me(@binnyva) on Twitter