<?xml version="1.0"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
	<channel>
		<title>Automating tasks with BASH - Revision history</title>
		<link>https://www.slackwiki.com/index.php?title=Automating_tasks_with_BASH&amp;action=history</link>
		<description>Revision history for this page on the wiki</description>
		<language>en</language>
		<generator>MediaWiki 1.40.0</generator>
		<lastBuildDate>Wed, 08 Apr 2026 01:00:53 GMT</lastBuildDate>
		<item>
			<title>Erik: Copy from old</title>
			<link>https://www.slackwiki.com/index.php?title=Automating_tasks_with_BASH&amp;diff=75&amp;oldid=prev</link>
			<guid isPermaLink="false">https://www.slackwiki.com/index.php?title=Automating_tasks_with_BASH&amp;diff=75&amp;oldid=prev</guid>
			<description>&lt;p&gt;Copy from old&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;[[Category:Tips]]&lt;br /&gt;
Often you will want to automate certain commands with BASH. The easiest way to do this is by using file globbing with wildcards otherwise called expansion. For example say I have a directory with the following files in it&lt;br /&gt;
&lt;br /&gt;
  $ ls&lt;br /&gt;
  file1.txt  file2.txt  file3.txt&lt;br /&gt;
&lt;br /&gt;
If I type&lt;br /&gt;
&lt;br /&gt;
  $ ls *.txt&lt;br /&gt;
  file1.txt  file2.txt  file3.txt&lt;br /&gt;
&lt;br /&gt;
The Shell will expand *.txt to match any file that matches the pattern of any number of characters followed by a .txt   it then sends this list to the ls command. The ls command takes that list as a set and displays the directory listing  for each one. Now most commands will work with file globbing but sometimes there are problems. For example if I wish to convert a heap of .jpeg files to 800x600 resolution with one line:&lt;br /&gt;
&lt;br /&gt;
  $ ls *.jpeg&lt;br /&gt;
  img_2992.jpeg  img_2994.jpeg  img_2996.jpeg  img_2998.jpeg  img_3000.jpeg  img_3002.jpeg&lt;br /&gt;
  img_2993.jpeg  img_2995.jpeg  img_2997.jpeg  img_2999.jpeg  img_3001.jpeg&lt;br /&gt;
&lt;br /&gt;
  $ convert *.jpeg -resize 800x600 *.jpeg&lt;br /&gt;
&lt;br /&gt;
  $ ls *.jpeg&lt;br /&gt;
  img_2992.jpeg  img_2998.jpeg   img_3002-10.jpeg  img_3002-16.jpeg  img_3002-3.jpeg  img_3002-9.jpeg&lt;br /&gt;
  img_2993.jpeg  img_2999.jpeg   img_3002-11.jpeg  img_3002-17.jpeg  img_3002-4.jpeg  img_3002.jpeg&lt;br /&gt;
  img_2994.jpeg  img_3000.jpeg   img_3002-12.jpeg  img_3002-18.jpeg  img_3002-5.jpeg&lt;br /&gt;
  img_2995.jpeg  img_3001.jpeg   img_3002-13.jpeg  img_3002-19.jpeg  img_3002-6.jpeg&lt;br /&gt;
  img_2996.jpeg  img_3002-0.jpeg  img_3002-14.jpeg  img_3002-2.jpeg   img_3002-7.jpeg&lt;br /&gt;
  img_2997.jpeg  img_3002-1.jpeg  img_3002-15.jpeg  img_3002-20.jpeg  img_3002-8.jpeg&lt;br /&gt;
&lt;br /&gt;
Of course that was not what we expected. There is an easier solution to this and that is to use BASH's for loop command.&lt;br /&gt;
&lt;br /&gt;
  $ for var1 in *.jpeg; do convert $var1 -resize 800x600 $var1; done&lt;br /&gt;
&lt;br /&gt;
  $ ls *.jpeg&lt;br /&gt;
  img_2992.jpeg  img_2994.jpeg  img_2996.jpeg  img_2998.jpeg  img_3000.jpeg  img_3002.jpeg&lt;br /&gt;
  img_2993.jpeg  img_2995.jpeg  img_2997.jpeg  img_2999.jpeg  img_3001.jpeg&lt;br /&gt;
&lt;br /&gt;
What this command does is loop through all the .jpeg files in the directory and for each file it stores the name of that file in the variable $var1 which is then used as two arguments to convert, the input and output file names respectively.&lt;br /&gt;
&lt;br /&gt;
Basically it looks to the system like this&lt;br /&gt;
&lt;br /&gt;
  convert img_2992.jpeg -resize 800x600 img_2992.jpeg&lt;br /&gt;
  convert img_2993.jpeg -resize 800x600 img_2993.jpeg&lt;br /&gt;
  convert img_2994.jpeg -resize 800x600 img_2994.jpeg&lt;br /&gt;
  convert img_2995.jpeg -resize 800x600 img_2995.jpeg&lt;br /&gt;
  convert img_2996.jpeg -resize 800x600 img_2996.jpeg&lt;br /&gt;
  convert img_2997.jpeg -resize 800x600 img_2997.jpeg&lt;br /&gt;
  convert img_2998.jpeg -resize 800x600 img_2998.jpeg&lt;br /&gt;
  convert img_2999.jpeg -resize 800x600 img_2999.jpeg&lt;br /&gt;
  convert img_3000.jpeg -resize 800x600 img_3000.jpeg&lt;br /&gt;
  convert img_3001.jpeg -resize 800x600 img_3001.jpeg&lt;br /&gt;
  convert img_3002.jpeg -resize 800x600 img_3002.jpeg&lt;br /&gt;
&lt;br /&gt;
You can play with this by echoing the output of the expansion to the console.&lt;br /&gt;
&lt;br /&gt;
  $ for var2 in *.txt; do echo $var2 ;done&lt;br /&gt;
  file1.txt&lt;br /&gt;
  file2.txt&lt;br /&gt;
  file3.txt&lt;br /&gt;
&lt;br /&gt;
  $ echo *.txt&lt;br /&gt;
  file1.txt file2.txt file3.txt&lt;br /&gt;
&lt;br /&gt;
Those are similar expansions but not exactly the same.&lt;br /&gt;
&lt;br /&gt;
Note: var1 and var2 are random variable names that I have picked. You can use other variable names like images or textfiles.&lt;/div&gt;</description>
			<pubDate>Tue, 02 Jun 2009 03:18:06 GMT</pubDate>
			<dc:creator>Erik</dc:creator>
			<comments>https://www.slackwiki.com/Talk:Automating_tasks_with_BASH</comments>
		</item>
</channel></rss>