Bleakley
Bleakley

Reputation: 733

copying files into named directories in bash

I have a directory structure like so /dir01/dir02/files

I want to copy the first file in dir02 onto a separate drive and into a directory with the same name as dir01

I wrote the following script

while [ "${*}" != "" ] ; do

INPUT="${1}"
FOLDER="${INPUT}"/*DPX
TARGET_FOLDER="/Users/user/Desktop/folder"/$(basename "${INPUT}")

for file in "${FOLDER}"; do
    echo cp "$file" "${TARGET_FOLDER}"
    break 1
done
done

Here INPUT is dir01 , FOLDER is dir02 and TARGET_FOLDER is the new directory with the same name as dir02 I want the file to copy to.

When I run the script it looks for a folder named *DPX in the INPUT path, which doesn't exist. There are many folders in the INPUT directory named *DPX and I want it to pull the first file from all of them.

Upvotes: 0

Views: 43

Answers (1)

Matei David
Matei David

Reputation: 2362

Try replacing your for with:

for file in "$INPUT"/*DPX/*

Notes:

  • Your version is looking for a file called *DPX because ${FOLDER} is quoted on the for line.
  • for f in "$dir" will execute the for loop once, with f=$dir. To look for files under $dir, you need another /*.
  • Also, you want a shift before the last done.

Upvotes: 1

Related Questions