mycodingusername
mycodingusername

Reputation: 11

In bash recursive script, how can I create folder and move files within sub-directory?

I have a simple problem which I can probably do manually but want automated script because I have 820 folders!

There is one directory 'data' that has 820 folders: /data/001../data/820.

Within each folder I have identical file structure and file names. I want to create a new folder called 'thrash' and move two files called 'one.exe' and 'nine.dat' into thrash.

I want to do this recursively for all folders within my 'data' folder.

So create /data/001/thrash and then move one.exe and nine.dat to /data/001/thrash. Then create /data/002/thrash and then move one.exe and nine.data to /data/002/thrash etc.

Is there a neat way for this? Please help.

Upvotes: 1

Views: 834

Answers (3)

Barmar
Barmar

Reputation: 781088

Just loop through the directories.

#!/bin/bash

mkdir -p data/*/thrash

for folder in data/*/; do
    mv "${folder}one.exe" "${folder}nine.exe" "${folder}thrash"
done

Upvotes: 2

M. Nejat Aydin
M. Nejat Aydin

Reputation: 10123

A one-liner (should be run in data directory):

for d in {001..820}; do mkdir $d/thrash; mv $d/{one.exe,nine.dat,thrash}; done

Upvotes: 0

Adrián Bíro
Adrián Bíro

Reputation: 39

for dir in rootdir/*
do
  if [[ -d ${dir} ]]; then
    (
    cd ${dir} || return
    mv one.exe newfolder/one.exe
    )
  fi
done

adapt this

Upvotes: 0

Related Questions