trev915
trev915

Reputation: 411

Copy specific file from every subfolder with parent directory

I need a way of looping through sub-directories in a large directory (/home/data/playerdata/) in order to get a specific file while keeping the parent folders name of that specific file I'm trying to get.

I basically need this in order to restore one specific file from a backup.

Example:

Main directory to be looped = /home/minecraft/survival1/plugins/Survival/playerdata/

Path of the specific file I'm trying to restore = /home/minecraft/survival1/plugins/Survival/playerdata/004fc15d-294c-4a42-a1af-1206c148e39b/economy.yml

Example command for script to exucute in a loop = cp /home/minecraft/survival1/plugins/Survival/playerdata/004fc15d-294c-4a42-a1af-1206c148e39b/economy.yml /restored/playerdata/004fc15d-294c-4a42-a1af-1206c148e39b/economy.yml

I need a script that will loop through each folder (The UUID bit) in the "playerdata" directory and copy that one specific file while retaining its UUID parent folder.

This is what I've tried so far:

 for FILE in $BASEDIR
        cp $BASEDIR/FILE/economy.yml /home/restored/FILE/economy.yml

This is running on a Debian server

Upvotes: 0

Views: 851

Answers (1)

Ondřej Kolín
Ondřej Kolín

Reputation: 1612

I would suggest using find for this:

cd /home/minecraft/survival1/plugins/Survival/playerdata/
find -name "economy.yml" -exec cp --parents {} /home/minecraft/restore/ \; 

--parents saves the directory structure while making copies.

Or using a for cycle, like you tried:

find . -type f -print0 | while IFS= read -r -d '' filename;
do 
  mkdir --parents "$RESTOREDIR/$player"
  cp "$BASEDIR/$player/economy.yml" "$RESTOREDIR/$player/economy.yml"
done

Upvotes: 1

Related Questions