Bash read file names from a text file and take action

I need to read a list of file names from a text file named input.txt and take action each file name. How do I read file names from a text file and say run /bin/foo command on each file? How do I read filenames from a text file and take certain action on those files?

Introduction – Often you need to read a file line-by-line and process data. It is a pretty common task for Linux and Unix sysadmin shell scripts. You need to use a bash while loop and the read command.

Bash read file names from a text file

The syntax is as follows to read filenames from a text file:

while IFS= read -r file; do
  echo "Do something on $file ..."
done < "filenames.txt"

OR

input="data.txt"
while IFS= read -r file; do
  printf '%s\n' "$file"
done < "$input"

Read filenames from a text file using bash while loop

Let us create a new file named input.txt with the filename on each line using the cat command or vim command:
cat > input.txt
Append data:

foo.txt
bar.txt
delta.jpg
nixcraft.logo.png
sales.pdf
/etc/resolv.conf
/home/vivek/Documents/fy-19-2020.ods
#/home/vivek/Documents/yearly-sales-data.ods

Related: How To Create Files in Linux From a Bash Shell Prompt

Here is a sample bash shell script to read a file line-by-line:

#!/bin/bash
# Name - script.sh
# Author - Vivek Gite under GPL v2.x+
# Usage - Read filenames from a text file and take action on $file 
# ----------------------------------------------------------------
set -e
in="${1:-input.txt}"
 
[ ! -f "$in" ] && { echo "$0 - File $in not found."; exit 1; }
 
while IFS= read -r file
do
	echo "Working on $file ..."
done < "${in}"

Simple run it as follows:
./script.sh

It is also possible to ignore filenames starting with #

Here is an updated version of the script:

#!/bin/bash
# Name - script.sh (bash read file names list from file)
# Author - Vivek Gite under GPL v2.x+
# Usage - Read filenames from a text file and take action on $file 
# ----------------------------------------------------------------
set -e
in="${1:-input.txt}"
 
[ ! -f "$in" ] && { echo "$0 - File $in not found."; exit 1; }
 
while IFS= read -r file
do
	## avoid commented filename ##
	[[ $file = \#* ]] && continue
	echo "Running rm $file ..."
done < "${in}"

Conclusion

You learned how to read a list of filenames from a file in bash and take action on them.


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 1 comment so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
1 comment… add one
  • verytd Dec 7, 2020 @ 13:58

    I was looking for a shell script to read server name from a text file and this helped me a lot. so thankz

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum