0

I'm having issues trying to write a script that will move and rename multiple directories. Each directory represents a user from a specific server, therefore, the same user can have multiple folders e.g. 12345678_sv1, 12345678_sv2.

I'd like to move specific users from "All" directory to a consolidated one. For example:

All/12345678_sv1 & sv2 >>> Consolidated/12345678

I tried the following:

#!/bin/bash

User_List=`cat ./User_List.txt`
Destination_List=`cat ./Destination_List.txt`


for i in $User_List 
do 
    echo "Name of User:  $i "
    echo
    echo
    echo "Copying $i User to new destination"
    echo
    echo
    cp -R $i /Consolidated/$Destination_List
    echo
    echo
    echo "Moved $i to $Destination_List" 
    echo
    echo
done

However, the folders stop getting copied after the first user. Any assistance would be greatly appreciated.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • 1
    Use [Shellcheck](https://www.shellcheck.net/) to find common problems in your code, and fix them. Run the program with `bash -x progname` to get a line-by-line trace of its execution. See [How can I debug a Bash script?](https://stackoverflow.com/q/951336/4154375). – pjh Mar 19 '22 at 01:25
  • Provide details (e.g. first few lines of each of them) of what is in `User_List.txt` and `Destination_List.txt`. Make sure that neither the program nor any of the input files has Windows (CRLF) line endings. (Double-check that. Nobody believes they have them until they check carefully.) Provide exact details of any error messages that you are seeing. – pjh Mar 19 '22 at 01:29
  • 1
    Unless you are *concatenating* two (or more) files, `cat file` is an *Unnecessary Use of `cat`* (*UUOc*) and should be avoided. Instead use redirection, command substitution or `readarray`. In your case you can feed a `while` loop from `User_List.txt`, e.g. `while read -r i; do ... done < User_List.txt` Unless `$Destination_List` contains a single value, your `cp -R` isn't going to work.. Instead use `find` to locate all files below `$i` and move to below a consolidated directory, e.g. `find "$i" -type f -execdir mv '{}' /path/to/consolidated +` – David C. Rankin Mar 19 '22 at 03:06
  • Look at https://mywiki.wooledge.org/BashFAQ/001 for methods of looping on file content (or commands). Also, start using `$( )` instead of back ticks. – Nic3500 Mar 20 '22 at 00:50
  • When you figure out how to loop through User_List.txt, think about Destination_List.txt . Is it single line or needs to be read line by line? For your task I would generate the destination by parameters expansion (see e.g. https://wiki.bash-hackers.org/syntax/pe ) – dr_agon Mar 20 '22 at 01:05

0 Answers0