1

Wondering if there is a best way to call directory names and iterate as parameter from a list.

Example

cat /dropbox/script/DirList.txt


DIR_A
DIR_B
DIR_C
DIR_D


/dropbox/dev/inbox/<DIR_A>/ *.*
/dropbox/dev/inbox/<DIR_B>/ *.*
/dropbox/dev/inbox/<DIR_C>/ *.*
/dropbox/dev/inbox/<DIR_D>/ *.*

considering folders/directories already exist If file exist in each of the mentioned directories then move them to outbox

mv /dropbox/dev/inbox/<DIR_D>/ *.* to /dropbox/dev/outbox/<DIR_D>/ *.*

I tried

if [ -d /dropbox/dev/inbox/<DIR_D>/ ]; then
  mv  /dropbox/dev/inbox/<DIR_D>/ *.* to /dropbox/dev/outbox/<DIR_D>/ 
fi
jesse_b
  • 35,934
  • 12
  • 91
  • 140

2 Answers2

1

Something like this should work for looping over lines in a file:

while read dir_name; do
    cp -pR /dropbox/dev/inbox/$dir_name/* /dropbox/dev/outbox/$dir_name/
done < /dropbox/script/DirList.txt

You may need a mkdir -p /dropbox/dev/outbox/$dir_name in there if outbox directories don't already exist.

Jeff A
  • 471
  • 2
  • 6
  • You many need to quote `$dir_name` in the `cp` (`"$dir_name"`). You might also want `read -r` just in case there's a backslash in the file! – Stephen Harris Jan 03 '19 at 01:55
0

If I understand correctly, this should work. Note that "*.*" only matches files with a period in the name (unlike Windows where it matches all files).

#!/bin/sh
INBOX=/dropbox/dev/inbox
OUTBOX=/dropbox/dev/outbox
for d in $(cat /dropbox/script/DirList.txt); do
    if [ -d $INBOX/$d ]; then
        mkdir -p $OUTBOX/$d
        mv $INBOX/$d/* $OUTBOX/$d/
    fi
done
Ken Jackson
  • 307
  • 1
  • 5
  • `for d in $(cat ...)` needs some caveats - see for example [Reading lines from a file with bash: for vs. while](https://unix.stackexchange.com/questions/24260/reading-lines-from-a-file-with-bash-for-vs-while) – steeldriver Jan 03 '19 at 00:08