<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://www.slackwiki.com/index.php?action=history&amp;feed=atom&amp;title=Automating_tasks_with_BASH</id>
	<title>Automating tasks with BASH - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://www.slackwiki.com/index.php?action=history&amp;feed=atom&amp;title=Automating_tasks_with_BASH"/>
	<link rel="alternate" type="text/html" href="https://www.slackwiki.com/index.php?title=Automating_tasks_with_BASH&amp;action=history"/>
	<updated>2026-04-08T01:00:54Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.40.0</generator>
	<entry>
		<id>https://www.slackwiki.com/index.php?title=Automating_tasks_with_BASH&amp;diff=75&amp;oldid=prev</id>
		<title>Erik: Copy from old</title>
		<link rel="alternate" type="text/html" href="https://www.slackwiki.com/index.php?title=Automating_tasks_with_BASH&amp;diff=75&amp;oldid=prev"/>
		<updated>2009-06-02T03:18:06Z</updated>

		<summary type="html">&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;</summary>
		<author><name>Erik</name></author>
	</entry>
</feed>