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 ↓
🐧 1 comment so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
I was looking for a shell script to read server name from a text file and this helped me a lot. so thankz