What I'm trying to accomplish is to move files in my directory that match with the records in my text file based on 2 parameters.

For example I have a record in my text file that reads:


I have a file in my directory that reads:


So if last name UPHOLD and 4th field M1123 match up to my fields in my text file, then I want to move them to a specified directory.

for files in test/* ; do
 echo $files | awk -F "_" '{print $3,$4}'
  • 1
    Which field of the text file must 4534534 match with? is the lastname always capitalized in the filename? Jun 14, 2018 at 13:10
  • The second field, and the last names aren't capitalized in the text file
    – Josh Adams
    Jun 14, 2018 at 13:12
  • 1
    @JoshAdams steeldriver asked about the name in the filename.
    – Kusalananda
    Jun 14, 2018 at 13:15
  • My bad, thank you, 4th field in the filename and they are always captalized
    – Josh Adams
    Jun 14, 2018 at 13:20
  • Could you please give an example where the data in your file and the parts of the filename match up? Also, could you clarify whether you are looking for files based on the data, or whether you want to create the data from the filenames? I know your title says you want to move the files, but your loop makes no sense. Also clarify the field numbers, you refer the 4th field of the filename (M1123) as the "last name", which confuses me.
    – Kusalananda
    Jun 15, 2018 at 17:28

2 Answers 2


Try this one. Change "echo" command to cp or mv after testing:

for f in samples.txt ; do 
  echo /source/path/*$(cut -d '|' -f 5,2 $f | tr '|'a-z '_'A-Z)*.pdf /destination/path

I'm sorry, my bad. Thanks @Kusalananda. Please, consider the above answer wrong and try this one instead. Supposing you are filtering on 5-th and 4-th columns it copies files from /src to /dst:

awk -F "|" '{system("cp /src/*"toupper($5)"_"$4"*.pdf /dst")}' samples.txt
  • Thank You definitely what I'm looking, just one question, why does not exist show up at the end, I'm a noob to scripting
    – Josh Adams
    Jun 14, 2018 at 13:04
  • 1
    Why are you looping over that single file name?
    – Kusalananda
    Jun 14, 2018 at 13:11
  • Ok this won't work I tried copying, but It won't copy because the original filename is being changed, thus the file doesn't exist, I wish to retain the original file when copying
    – Josh Adams
    Jun 14, 2018 at 13:16
  • 1
    cut -f 5,2 will give you column 2 and column 5, but will not swap the order of them.
    – Kusalananda
    Jun 15, 2018 at 17:22

I would approach this the other way around:

  • read and split the text file into fields line-by-line
  • match and move the corresponding files

E.G. (assumes bash and GNU mv):


shopt -s nullglob

while IFS='|' read -r loc a b c last first d e; do 
  echo mv --no-clobber -t path/to/target/ -- *_"$a"_"${last^^}"_*_*_*_*.pdf
done < file.txt

Remove the echo once you are satisfied that it is doing what you want

  • Ok I'm getting this error when I run it: 0403-011 The specified substitution is not valid for this command.
    – Josh Adams
    Jun 14, 2018 at 13:30
  • @JoshAdams what is your bash version (bash --version)? how are you executing the script? Jun 14, 2018 at 13:55
  • I'm using korn shell
    – Josh Adams
    Jun 14, 2018 at 13:59
  • @JoshAdams if you must do it in a Korn shell, then you will need to modify the upper-casing (setting typset -u last and then replacing ${last^^} by just $last should be a fairly portable alternative) and finding a way to do the nullglob - the latter will depend on which exact implementation of ksh you are using IIRC Jun 14, 2018 at 14:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .