I'm currently going through a manual process of encoding a file list from DOS to Unix so that I can move files from x to y. I would like this process automated in one script but I'm a total noob.

This is the pseudo-code:

  1. Convert list.txt to unix encoding (:set ff=unix)

  2. Move files from /folder that are present in list.txt to /folder2 (in other words, /folder contains a big list, but i only want the files in list.txt to be moved)

    this is how i'm moving the files: while read line ; do cp /opt/home/first.last/Runs/clientPDFs/${line}.pdf ~/folder2 ; done < ~/list.txt

If possible I would like the script to check for list.txt first. If there isn't one it should move ALL files.

if list.txt then while read line ; do cp /opt/home/first.last/Runs/clientPDFs/${line}.pdf ~/folder2 ; done < ~/list.txt

else while read line ; do cp /opt/home/first.last/Runs/clientPDFs/${line}.pdf ~/folder2

1 Answer 1


I'd probably structure it something like this:


if [ -r "$filelist" ]; then
  sed 's/\r$//' "$filelist" | xargs -I{} cp -t "$dstdir"/ -- "$srcdir"/{}.pdf
  cp -t "$dstdir"/ -- "$srcdir"/*.pdf

You could append the pdf extension within the sed command if you prefer i.e.

  sed 's/\r$/.pdf/' "$filelist" | xargs -I{} cp -t "$dstdir" -- "$srcdir"/{}
  • thanks but I have a question. Is the list converted to Unix encoding anywhere?
    – reddish
    Nov 10, 2016 at 18:32
  • @reddish the sed command removes trailing carriage return (\r) characters - that should be exactly equivalent to vi's set ff=unix except that it's done 'on the fly': if you want to actually modify the contents of the file on disk, then add -i (or --in-place) to the sed command - or replace it altogether with dos2unix for example. Nov 10, 2016 at 19:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.