20 articles

Insert text from a file recursively using sed

To add some context, my goal here was to insert a copyright statement at the start of each PHP file present in my working directory. I knew how to replace the content of a file with the sed utility, so I first started to look for a way to read input from a file.

It appears that the GNU version of sed can do it by using the r option, which stands for "read". So to make sed insert the content of a file named header after the first line of the PHP file source.php:

$ sed '1r header' source.php > source.php.temp

The use of redirections force us to create a temporary file. Redirecting directly to source.php would give us an empty file! Fortunately, there is a utility called sponge, from a collection of tools called moreutils1, which allows us to redirect the modified content to the same source.php file:

$ sed '1r header' source.php | sponge source.php

Now, all there is left to do is executing that command on each PHP file present in the main directory and all its subfolders. For that, I just wrote a for loop in a bash script but I learned how to select all the files recursively using the following glob pattern: **/*.php

This is available from bash v4 and to use it in a script, just add the following line before:

shopt -s globstar

1 The moreutils package is available on Arch Linux in the community repository.


Linux: display images type and size in command line

Front-end developers probably often need to verify the properties of some images like their type or their dimensions. To avoid firing Gimp or similar software just for a quick look, GNU/Linux distributions1 offer as usual handy tools like identify, coming from the ImageMagick suite.

$ identify /home/didier/images/myimage.jpg
/home/didier/images/myimage.jpg JPEG 502x482 502x482+0+0 8-bit sRGB 31.7KB 0.000u 0:00.000

You can of course use glob patterns to display several images at once:

$ identify /home/didier/images/gnulinuxrocks*
/home/didier/images/gnulinuxrocks.png PNG 54x84 54x84+0+0 8-bit sRGB 190c 1.6KB 0.000u 0:00.000
/home/didier/images/gnulinuxrocks-even-more.png[1] PNG 42x84 42x84+0+0 8-bit sRGB 187c 1.5KB 0.000u 0:00.000
/home/didier/images/gnulinuxrocks-arch-logo.jpg[2] JPEG 396x140 396x140+0+0 8-bit sRGB 7.14KB 0.000u 0:00.000

Don't forget to check the manual for many more options.

$ man identify

1 I know, ImageMagick is available on other operating systems but I prefer trolling... though installing it on a GNU/Linux distribution with a package manager is way simpler and safer!

The Pitivi team still needs our help for bringing their simple to use open source video editor to a professional level. The campaign has past half of its goal and counting...

Delete lines containing a specific string with sed

The following command makes sed delete every lines containing the matching pattern from the file named foo. If this file is important, we make sure to back it up first!

$ sed -i '/pattern/d' /path/to/foo

The -i flag (GNU version only) edit the file in-place. For non-GNU version, we can redirect the output with >.

$ sed '/pattern/d' /path/to/foo > /path/to/foo

Less than 20 days to help fund the Gooseberry project from the Blender Foundation

Update: having more than 3000 supporters, the team has decided to extend the campaign until May 8th. It is not too late to join in!

Less than 20 days to help fund the Gooseberry project from the Blender Foundation. Blender is a free (as in freedom and free beer) 3D modelling software. After several small films showing all its capabilities, the foundation has launched a crowd-funding campaign to help realise its first full-length film.

Source [FR]