Bash scripting

Search and replace text in many files

January 6, 2012
By

This is a handy one liner that searches and replaces text in multiple files. (You can use find for example to run it in multiple folders) View Code BASH1 perl -pi -e 's/old_text/new_text/g' *.conf This line replaces old_text with new_text in all the .conf files in the current folder. Of course you can use...

Read more »

Quickly check if a process is running or not

December 22, 2011
By

When writting bash scripts one often needs to see if a process is running or not I like the following code; though I’m sure many ways of doing this exists, what’s your favorite? View Code BASH1 2 3 4 5 6 if [ -z "$(pgrep process)" ] then echo "process is not running" else...

Read more »

Optimizing all mysql tables (from all databases)

October 4, 2011
By

You want to optimize mysql tables from time to time in order to reduce fragmentation. Here’s an easy way to do it automatically (put it in a script and run it from cron for example) View Code BASH1 mysqlcheck -Aop -h hostname -u user -pPASSWORD What all those mean: –all-databases, -A; Check all tables...

Read more »

List crontab entries for all users on a linux system

January 20, 2011
By

This is a good one liner to remember: for user in $(cut -f1 -d: /etc/passwd); do crontab -u $user -l; done

Read more »

Force fsck on next reboot

July 19, 2010
By
fsck

Let’s say you run a headless server and you want to fsck the main partition. The easy solution in this case is (if possible) to force fsck to run on the next boot. If you run slackware than all you really need is to create an empty file in /etc called forcefsck You could...

Read more »

Wma to Mp3 a folder in one command line

June 29, 2010
By

I have some albums that were ripped in wma form (yes a shame I know). I wanted them converted into mp3 so my collection remains consistent. So this would be a command to run in a folder filled with .wma files. This keeps the names but not the tags so be sure to use...

Read more »

Easily send mail from a bash script

March 18, 2010
By

Did you ever need to have e-mail's sent from within a bash script? Maybe alert you of errors encountered? Me too. Here's an easy way on how to do it:

Read more »

Rename all files in a folder from upper to lower case.

February 12, 2010
By

Sometimes is needed to have all files in a folder in the same case. Here’s a small script to rename all files in the folder to lower case: View Code BASH1 for i in *; do mv $i `echo $i | tr [:upper:] [:lower:]`; done

Read more »

Export a MySQL database using the command line.

February 2, 2010
By
logo-mysql-110x57

Sometimes it’s easier to just drop to the command line then browse for fancy tools like phpMyAdmin. This is useful, especially for quick jobs. View Code BASH1 mysqldump -u user -ppassword your_database > database_backup.sql Of course you need to replace: user – mysql username password – the password your_database – the database you want...

Read more »

Checking directory sizes from the BASH prompt

January 18, 2010
By
bash_script

The command ls can be made to show directories and permission but it will not display directory sizes. Then it’s time for other commands. For example on my hosting account there is no file manager installed and evidently x and kde is missing too so, to find out directory sizes I can use the...

Read more »

Follow Me

Subscribe via RSS