Techolac - Computer Technology News
  • Home
  • Internet
  • Business
  • Computers
  • Gadgets
  • Lifestyle
  • Phones
  • Travel
  • Tech
  • More
    • Automotive
    • Education
    • Entertainment
    • Health
    • SEO
    • Linux
    • WordPress
    • Home Improvement
    • How to
    • Games
No Result
View All Result
  • Home
  • Internet
  • Business
  • Computers
  • Gadgets
  • Lifestyle
  • Phones
  • Travel
  • Tech
  • More
    • Automotive
    • Education
    • Entertainment
    • Health
    • SEO
    • Linux
    • WordPress
    • Home Improvement
    • How to
    • Games
No Result
View All Result
Techolac - Computer Technology News
No Result
View All Result
Home Linux

Bash read file names from a text file and take action

by Editorial Staff
December 4, 2018
in Linux
Reading Time: 5 mins read

[ad_1]

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"

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 '%sn' "$file"
done < "$input"

input=”data.txt”
while IFS= read -r file; do
printf ‘%sn’ “$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}"

#!/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}”

See also  How to install networked HP printer and scanner on Ubuntu Linux

Simple run it as follows:
./script.sh
Bash read file names from a text file and take action

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}"

#!/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.

[ad_2]

Related Posts

Top Commands to Check the Running Processes in Linux
Linux

Top Commands to Check the Running Processes in Linux

May 27, 2022
4 Tips to Prevent and Troubleshoot the ImagePullBackOff Kubernetes Error
Linux

4 Tips to Prevent and Troubleshoot the ImagePullBackOff Kubernetes Error

March 30, 2022
How Common Signals are Used in Kubernetes Pod Management
Linux

How Common Signals are Used in Kubernetes Pod Management

November 20, 2021

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Bitcoin Cloud Mining Sites

12 Best Bitcoin Cloud Mining Sites In 2022

August 9, 2022
Why did the Parag Parikh Long Term Equity Fund change its name to Parag Parikh Flexi Cap Fund?

Why did the Parag Parikh Long Term Equity Fund change its name to Parag Parikh Flexi Cap Fund?

August 9, 2022
Why Measuring Customer Experience Score Generate More Business Value?

Why Measuring Customer Experience Score Generate More Business Value?

August 9, 2022
The Key Phases for Successful Onboarding

The Key Phases for Successful Onboarding

August 9, 2022
30 ZiniTevi Alternatives To Watch Movies And TV Shows Online

30 ZiniTevi Alternatives To Watch Movies And TV Shows Online

August 8, 2022
  • DashTech
  • TechDaddy
  • Terms and Conditions
  • Disclaimer
  • Write for us

© Techolac © Copyright 2019 - 2022, All Rights Reserved.

No Result
View All Result
  • Home
  • Internet
  • Business
  • Computers
  • Gadgets
  • Lifestyle
  • Phones
  • Travel
  • Tech
  • More
    • Automotive
    • Education
    • Entertainment
    • Health
    • SEO
    • Linux
    • WordPress
    • Home Improvement
    • How to
    • Games

© Techolac © Copyright 2019 - 2022, All Rights Reserved.

Go to mobile version