Using commands on more than one file


Author: Helmut Heidegger

creation date: 2002-09-09

Sometimes one wants to convert or delete multiple files at once, or just replace some string in them. This can be quite time consuming using grapical tools like konqueror or nautilus or it even does not work at all. For example "convert all GIF pictures into PNG format". The following command executes the convert command on each found gif image in the current directory. One has to simply give it the name of the existing picture and then the new name with the wanted format. In our case the ".gif" suffix of the name is cut off and ".png" is added instead.

user@host:> for picture in *.gif ; do
> convert $picture "${picture/.gif}.png"
> done

Or you may be in need of Thumbnails (small images for a possible preview) for your homepage. The following command adds to all the .png files in the current directory a "small_" prefix. These new .png files have a maximum height and width of 300 pixels.

user@host:> for picture in *.png; do
> convert -size 300x300 $picture -resize 300x300 "small_${picture}"
> done

The recursive removal of files with a certain suffix is something which is often needed. The following command deletes all files with the extension .tmp in the actual directory and all subdirectories.

user@host:> find . -name '*.tmp' -exec rm '{}' ';'

But take care: typing errors in one of the above commands can have fatal consequences. There is no undo for the deletion of all .doc files in your own home directory ...!! If you want a more secure version, you should use the following command that moves the files not needed any longer into the /tmp directory, which of course should result in the occasional cleaning of your /tmp directory. That could ideally be done by a cron job, that wipes this directory upon each reboot. But that's another story :-)

user@host:> find . -name '*.tmp' -exec mv '{}' /tmp ';'

If you want further information or are interested in more complex possibilities, you can gain information using the command below:

user@host:> info bash
user@host:> info find
consulting IT Eclipse RCP eclipse rcp Eclipse-RCP tirol innsbruck österreich tyrol austria europa europeSoftware, Development, Softwareentwicklung, programming, Programmierung, SE, SD, Tirol, Tyrol, Innsbruck, Austria, Österreich, Europa, EuropeSystemadministration, Linux, Tirol, Sysadmin, Netzwerke, network, Linux, Windows, migration, tirol, tyrol, innsbruck, Österreich austria Europa, EuropeIT-Sicherheit, IT-Security, security network, EDV-Sicherheit, Netzwerksicherheit, tirol, tyrol innsbruck, Österreich Austria Europa, EuropeHosting, combeso, typo3, xims, Linux, server, tirol, tyrol innsbruck, Österreich, Austria Europa, EuropeLinux, Open Source, OSS, UNIX, Windows, tirol, tyrol innsbruck, Österreich, Austria, Europa, EuropeBISCAT, BiSCAT, Personenverwaltung, client administration tool, Mitarbeiterverwaltung, staff, Österreich, Austria, tirol innsbruck Europa, EuropedrawSWF, flash, grafiken, graphics, erstellung, development, tirol tyrol, innsbruck, Österreich, Austria, Europa, Europehomepage, webshop, günstig, easy cheap, umfangreich, comprehensive, tirol tyrol, innsbruck, Österreich, Austria, Europa, Europezooners die eventplattform, Orte, Veranstaltungen, Wien, Landeck, Tirol, Munich, Germany, Austria, zooncards, zooncard