Welcome to my website. I am always posting links to photo albums, art, technology and other creations. Everything that you will see on my numerous personal sites is powered by the formVistaTM Website Management Engine.

icon.linkedin.jpgicon.twitter.jpg

  • Looping Through a List of Files with Spaces in the File Name with Bash
    02/16/2016 8:58PM

    If you have a list of files that you want to operate on in a loop in bash and some of them have spaces in the file name the default IFS (Internal Field Separator) will match with the space and tokenize the file.

    The simple approach is to temporarily set the IFS as follows.  This can be done in a shell script, but the following example is directly on the command line for 'one-liner' usage.

    $ OIFS="$IFS"

    $ IFS=$'\n'

    $ for i in `find ./ -type f -iname '*some_criteria*'`; do "something with $i"; done

    $ IFS="$OIFS"

    The previous commands will:

    1. Save the existing IFS
    2. Update the IFS to a newline char
    3. Execute your loop with the results of a find command
    4. Reset the IFS
Advanced Search

Categories

Archives