InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Bash shell scripting

  • Bash - add a number to a variable
  • Bash - append text to a variable
  • Bash - how to check if a variable is set
  • Bash - how to compare file timestamps
  • Bash - how to find last command exit status code
  • Bash - how to get main program and current file dir location
  • Bash - how to redirect stderr to stdout or file
  • Bash - how to run custom commands at script exit
  • Bash - how to stop at error
  • Bash - how to use functions - quick tutorial
  • Bash - iterate over array
  • Bash - local and global variables
  • Bash - newline and other escape character in string
  • Bash - pass all arguments from one script to another
  • Bash - set default value if a variable is empty
  • Bash - variables in double quotes vs without quotes
  • Bash associative array tutorial
  • Bash check if file begins with a string
  • Bash shell - check if file or directory exists
  • Can global variables be modified in bash function?
  • Find memcache request hit rate on linux command line
  • How to return a value from bash function
  • Iterate over specific file extension in a dir in shell script
  • Linux - Yesterday's Date in YYYYMMDD format
  • bash - extract urls from xml sitemap
  • bash - how to use regex in if condition
 
  • Home
  • > Tutorials
  • > Bash shell scripting

Iterate over specific file extension in a dir in shell script

By admin on Nov 25, 2015

Sometimes we need to iterate over all files of a specific file extension using a bash script. This can be used to convert one type of file to another or any other use case. Here are some approaches for iterating over .txt files in a directory tmp.

Using ls or find in sub-shell

You can use ls in a sub shell to get the desired list of .txt files.

#!/bin/bash
## cd to destination dir
cd ~/data/tmp
FILES=$(ls *.txt)
for i in $FILES ; do
  echo $i
done

Or you can use find command in sub-shell find .txt files.

#!/bin/bash
## cd to destination dir
cd ~/data/tmp
FILES=$(find . -name "*.txt")
for i in $FILES ; do
  echo $i
done

Approach 2

#!/bin/bash
## cd to destination dir
cd ~/data/tmp
for i in *.txt ; do
  echo $i
done

Approach 3

#!/bin/bash
## cd to destination dir
cd ~/data/tmp
FILES=*.txt
for i in $FILES ; do
  echo $i
done

Suggested posts:

  1. Git – show log of one user’s commits
  2. WordPress – how to make a post sticky on home page
  3. How to show environment variable for a process id (pid)
  4. Jenkins how to clone a project
  5. Command line – top IP list from apache access log
  6. Bash shell – check if file or directory exists
  7. Bash check if file begins with a string
  8. Mac – enable and use kubernetes in docker
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Bash shell scripting, Linux, Mac, Tutorials, Ubuntu Linux
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress