To read file names line by line in Linux and copy matching files to a specified directory, you can follow the tutorial below:
- Create a text file, for example
filelist.txt
, write the filenames that need to be matched and copied line by line into the file. Make sure each filename is on its own line. -
Create a target directory for storing the copied files:
mkdir /path/to/destination
will
/path/to/destination
be replaced with the actual target directory path. -
Use a loop structure to read
filelist.txt
the file line by line and perform the copy operation:while IFS= read -r filename; do cp "$filename" /path/to/destination done < filelist.txt
will
/path/to/destination
be replaced with the actual target directory path. - After executing the above command, the system will read
filelist.txt
the file names in the file line by line, and copy the matching files to the specified directory.
filelist.txt
Make sure to double check that the filenames in the file are correct before performing a copy operation , and back up important files to prevent accidental data loss.
Hope the above tutorial is helpful for you to read the file name line by line and copy the file to the specified directory in the Linux system. If you have other questions, please provide more details or refer to the relevant documentation for further support.