I looked for relevant posts extensively before writing but I apologise if it has been already asked and solved.

I'd need to find, select and move/copy some files from a folder starting from a list in a text file (but could also paste it directly if needed).

I've found some posts that could've helped and more precisely the ones at the following link: https://stackoverflow.com/questions/29311880/finding-files-from-list-and-copying-them-into-new-directory

This seems perfect but I've got a permission error (cp: /found_files: Permission denied) and couldn't find a work around (tried sudo and many other combinations).

This one seemed fine as well: Find multiple files at once via Terminal. It works very well to find the files but I couldn't find a way to select them in order to be able to move/copy them to a new directory.

Tried all this on macOS Sierra 10.12.6 with no special configurations or changes in the Terminal.

  • 3
    It's helpful if you provide a sample list and the command that you're using so we can troubleshoot the error.
    – Allan
    Commented Aug 19, 2018 at 12:37
  • Are all the files in the same directory or are some in subdirectories?
    – fd0
    Commented Aug 19, 2018 at 13:37
  • The files are all in the sam directory.
    – Michael
    Commented Aug 20, 2018 at 10:14

1 Answer 1


You get cp: /found_files: Permission denied because the script in the linked post, in its current form, is written to copy the listed files to the /found_files directory. The /found_files directory doesn't exist on your Mac and cp interprets found_files as a file located in /. Since / is not writable, you get the permission error.

I modified the script so that it works with other destination directories and expanded it to support copying and moving files:

  1. Launch Terminal. Select a directory where you save your scripts, for example ~/bin:

    cd ~/bin
  2. Create a file named cplist.sh with these contents:

    # Sanity checks
    if [[ $# -ne 3 ]]; then
        echo "Usage: $0 <source dir> <target dir> <file list>"
        exit 0
    if [[ ! -d "$source_dir" ]]; then
        echo "$0: '$source_dir' could not be found"
        exit 1
    if [[ ! -d "$destination_dir" ]]; then
        echo "$0: '$destination_dir' could not be found"
        exit 1
    if [[ ! -f "$file_list" ]]; then
        echo "$0: '$file_list' could not be found"
        exit 1
    # Copy or move?
    if [[ $(basename $0) == "mvlist.sh" ]]; then
        command="cp -a"
    # Copy files    
    while read filename
        echo "$action file '$filename'"
        if [[ -f "$source_dir/$filename" ]]; then
            find "$source_dir" -name "$filename" -exec $command "{}" "$destination_dir" \;
            echo "File '$filename' is listed in '$file_list' but could not be found"
    done < "$file_list"
  3. Make the file executable:

    chmod a+x cplist.sh
  4. Link the file to mvlist.sh:

    ln -s cplist.sh mvlist.sh
  5. To copy a set of files, use cplist.sh as follows:

    ./cplist.sh <source dir> <destination dir> <file list>

    where <file list> is a text file that contains file names, for example:

    file 1.pdf
    file 2.pdf

    To move a set of files, use mvlist.sh as follows:

    ./mvlist.sh <source dir> <destination dir> <file list> 

For example:

 Usage: ./mvlist.sh <source dir> <target dir> <file list>

 ./mvlist.sh /tmp/ /nonexistentfolder filelist.txt
 ./mvlist.sh: '/nonexistentfolder' could not be found

 ./mvlist.sh /tmp/source/ /tmp/destination filelist.txt
 Moving file 'file 1.pdf'
 Moving file 'file 2.pdf'
 Moving file 'file 3.pdf'
 Moving file 'file 4.pdf'
 Moving file 'file 5.pdf'
 Moving file 'file a.pdf'
 File 'file a.pdf' is listed in 'filelist.txt' but could not be found
 Moving file 'file 6.pdf'
 Moving file 'file 7.pdf'
 Moving file 'file 8.pdf'
 Moving file 'file 9.pdf'

You must log in to answer this question.

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