1

I am trying to use the find command to edit files named "msf.txt" in many different sub-directories and I want it to take the same filename and extension as whatever file shares the same extension in it's directory. So as an example, let's pretend I'm inside one of the sub directories which has an msf.txt file and an msf-01.txt file. I want to rename msf.txt to msf-01.txt. I want to automate this across many sub-directories, but only do it in sub-directories that have the phrase "data" in the directory name. Can anyone provide some suggestions? I'd prefer to use something like find for this.

This is what the current directory looks like

main/
|--sub01    
    ├── data/
    │   ├── mgw.txt
    │   ├── mgw-01.txt
    ├── pictures/
    │   ├── bootstrap1.png
    │   └── bootstrap2.png
    └── extra/
        ├── stuff.txt

 |--sub02   
    ├── data/
    │   ├── mgw.txt
    │   ├── mgw-02.txt
    ├── pictures/
    │   ├── bootstrap1.png
    │   └── bootstrap2.png
    └── extra/
        ├── stuff.txt

This is what I want it to look like and the .txt files inside the data directories are meant to contain what was inside the mgw.txt files

main/
|--sub01    
    ├── data/
    │   ├── mgw-01.txt
    ├── pictures/
    │   ├── bootstrap.png
    │   └── bootstrap.png
    └── extra/
        ├── stuff.txt

 |--sub02   
    ├── data/
    │   ├── mgw-02.txt
    ├── pictures/
    │   ├── bootstrap1.png
    │   └── bootstrap2.png
    └── extra/
        ├── stuff.txt

closed as unclear what you're asking by Sparhawk, Stephen Harris, G-Man, schily, RalfFriedl Dec 3 '18 at 17:55

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    I'm pretty confused here. Could you please edit your question, removing all extraneous information, and distilling the problem to its core? i.e. what is (a minimal version of) the current situation, and what is the expected version. – Sparhawk Dec 3 '18 at 3:46
  • @Sparkhawk Okay I see what you mean. Just edited. Hopefully will be more clear – 762 Dec 3 '18 at 4:51
  • I'm still confused. Just show the literal directory structure, before and after. – Sparhawk Dec 3 '18 at 4:52
  • @Sparhawk okay I added the directory structure – 762 Dec 3 '18 at 5:48
  • Thank you. So in the first sub-directory (main/sub01/data/), mgw.txt is renamed to mgw-01.txt. Presumably the 01 in this filename comes from sub01. Where does the original mgw-01.txt go? Is it just deleted? – Sparhawk Dec 3 '18 at 6:06
0

Maybe something like:

#! /bin/zsh -
would-do() print -r -- "$@"

for f (**/data/mgw.txt(N)) {
   others=($f:r-*.txt(N))
   if (($#others)); then
     would-do mkdir -p $f:h:h/backup &&
       would-do mv $others $f:h:h/backup/ &&
       would-do mv $f $others[1]
   fi
}

Change the would-do definition to would-do() "$@" to actually do it.

  • How does the backup of the old files work? Where do they get sent? Also, I'm guessing if I ever wanted to apply this to other directories with many (+5) levels of sub directories then the wildcard expansion (**/data/mgw.txt(N) would still work right? Or would I need more? – 762 Dec 4 '18 at 16:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.