RSS Subscription
Linux Howtos & Tutorials

Enter your email:

Delivered by


NOTE:New tutorials are from LinuxCareer.com

Poll

Do you own or wish to have iPhone?
 


Linux eBooks FREE Download
A guide to programming Linux kernel modules
Introduction to Linux - A Hands on Guide
A Newbie's Getting Started Guide to Linux

Linux from Scratch - Create Your Own Linux System - Free eBook

Linux: The Hacking Solution (v.3.0)

SQLite 3 with PHP Essential Training – Free Video Training Tutorials

This guide will introduce you to the world of GNU/Linux

The GNU/Linux Advanced Administration

A Complete Beginner's Manual for Ubuntu 10.04 (Lucid Lynx)

Advanced Bash-Scripting Guide

Set up, maintain, and secure a small office email server

Partner Linux Sites:
How-To.LinuxCareer.com
Jobs.LinuxCareer.com
TuxMachines
Monsterb
LinuxBloggers
AdamsInfo
LinuxScrew
All For Linux

Bash for loop with files containing white space

Question:

i've got a problem when using wild cards like "*.jpg". maybe you have a hint for that resp. want to add an example for the community:

  • there is a directory with files,that also have spaces in the name
  • i want to write a script that creates thumbs from its jpgs (using mogrify)
  • if i call the script with arg "*.jpg" then the names are resolved as space delimitered strings
  • inside the script i can't find out anymore, if space is a delimiter or part of the name

What i found is an option in ls "-b" that escapes the spaces. This could be the solution. Is there a way to resolve "*.jpg" in a escaped way?

Answer:

You may want to set $IFS ( Internal Field Separator ) variable to avoid your for loops break when encountered with a whitespace character such as space or tabs.

I have written a small script for you to match your scenario ( that is, if I interpreted your question correctly ). I have tested this script to create thumbnails inside a directory containing *.jpg" images with a space within a file name.

WARNING: The script below will overwrite your existing files !!! Do not execute if you are not sure what you are doing.

#!/bin/bash 
 
IFS=$(echo -en "\n\b") 
 
for image in $( ls *.jpg ) ; do 
		mogrify -geometry 100x65 $image 
done 

Hope this helps...

Linux questions and answers

Share this linux post:

Submit Bash for loop with files containing white space in Delicious Submit Bash for loop with files containing white space in Digg Submit Bash for loop with files containing white space in FaceBook Submit Bash for loop with files containing white space in Google Bookmarks Submit Bash for loop with files containing white space in Stumbleupon Submit Bash for loop with files containing white space in Technorati Submit Bash for loop with files containing white space in Twitter
 
Comments for this page are closed !!!
Please visit our new Linux Forum for additional help or discussion.


Linux eBooks FREE Download