CLI search and replace in multiple files

March 11, 2010 – 20:23

Here is a quick tip about how to find and replace a text in multiple files, from CLI.

In this example you have some text files (.txt) with urls like “http://example.com, http://another-example.com…”. What you are trying to do is to replace all “http” references with “https”.

You can find all text files using the following command in the directory where the files are located:

find -type f -name *.txt

You can execute a command on the search result using theĀ -exec parameter for the find command:

find -type f -name *.txt -exec my_command

The actual search and replace will be performed using sed command:

find -type f -name *.txt -exec sed -i 's/http/https/' {} \;

Tags: , , , ,

Midnight Commander preserve saved settings

December 3, 2009 – 20:37

After installing Ubuntu 9.10 (Karmic Koala) I have noticed that Midnight Commander, one of the most used CLI file managers available for Linux is unable to preserve settings over different sessions. The solution is quite simple: in order to save it’s settings, Midnight Commander requires a “.mc” folder (notice the dot – it means it is a hidden folder) to be created in your user’s home directory. Just create the “.mc” directory, go and change the default settings to your favourite ones and Midnight Commander should create proper configuration files required to keep “mc” settings over different sessions.

Find all folders with same name in a directory tree and execute commands on them

October 11, 2009 – 15:57

In a Linux CLI environment you can use the “find” command to search for files or folder. In this tutorial I will explain how to find folders having the same name in a directory tree and execute commands on them.

Since the best way to learn is by example, let’s consider the following scenario: a collection of images along with their corresponding thumbnails in directories named “thumbs”; so what you are trying to do is remove all “thumbs” directories from your images collection.

The first step is to determine the path of the directory tree. Assuming that “/home/cristian/wallpapers” is the path you can execute the command by providing the entire path as parameter for the “find” command. If you are already in this directory you can use the dot “.” instead of the path (the dot is the equivalent of the current directory).

Now that you have the path, you need two more parameters for the find command – the name of the directories and the type. For the type you must use the letter “d” since this is how the “find” command will know that you want to search for a directory. For the name just provide “thumbs” since this is what we want to remove (see above scenario).

So far the command is:

find "/home/cristian/wallpapers" -name thumbs -type d

You found the directories, now pass this list to the deleting command with “xargs”:

find "/home/cristian/wallpapers" -name thumbs -type d | xargs rm -rf

If you need root permissions to delete the files just add the “sudo”:

find "/home/cristian/wallpapers" -name thumbs -type d | xargs sudo rm -rf

then enter your password.

Tags: ,

PHP CLI increase memory limit

October 3, 2009 – 17:55

PHP logo

When running a PHP script from command line interface (CLI), a higher memory limit may be required in order for the script to be successfully executed. This can be achieved using the “-d” or “–define” option in the command. According to the manual:

This option allows you to set a custom value for any of the configuration directives allowed in php.ini.

An example of running a PHP script in CLI with custom memory limit:

php -d memory_limit=128M my_script.php

Of course the “-d” or “–define” option is not limited to the memory limit directive; it can be use to alter other php.ini directives as well.

Tags: , ,

Insert value “0″ for primary keys with auto increment in MySQL

July 12, 2009 – 22:11

In order to insert value 0 in a field that is set as primary key for a MySQL table you need to execute the following query prior to any insert query:

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";

Now you can successfully run the insert query with 0 set as value for a primary key, without getting any error message.

Tags: , , , ,