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

Time:07-22

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.

CodePudding user response:

Just loop through the directories.

#!/bin/bash

mkdir -p data/*/thrash

for folder in data/*; do
    if [ -d "$folder" ]
    then mv "$folder/one.exe" "$folder/nine.exe" "$folder/thrash"
    fi
done

CodePudding user response:

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

adapt this

  • Related