简体   繁体   中英

Bash script to work with a file list txt file

I have a txt file which contain a big list of files. Each filename is 1 row in the list.

I was able to read it like this :

#!/bin/bash
set -e
in="${1:-file.txt}"

[ ! -f "$in" ] && { echo "$0 - File $in not found."; exit 1; }

while IFS= read -r file
do
    echo "Copy $file ..."
done < "${in}"

What I actually want to achieve in the end is to read these lines, then issue cp command for first 20 or 30 or them, and then delete them from the file.txt and then do again same thing.

With readarray you can easily read a count of lines of input at a time:

while readrray -n 20 -t lines; do
    for line in "${lines[@]}"; do
         echo "Copy $file ..."
    done
done < "${in}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM