简体   繁体   中英

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.

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

adapt this

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

单行(应该在数据目录中运行):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM