Bash read file names from a text file and take action

ADVERTISEMENTS



Bash read file names from a text file

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.

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 '%s

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

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

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