Home > Mobile >  Bash - Copying a file/folder into every subfolder, then renaming it to the parent folder
Bash - Copying a file/folder into every subfolder, then renaming it to the parent folder

Time:11-11

I am attempting to perform the following operation:

Init state:

  |--- Foobar Project
             |--- Foobar.file
  |--- My Projects
      |--- mainfolder 1
      |          |--- subfolder 1
      |          |--- subfolder 2
      |
      |--- mainfolder 2
      |          |
      |          |--- subfolder 1
      |          |--- subfolder 2

After script:

  |--- Foobar Project
             |--- Foobar.file
  |--- My Projects         
      |--- mainfolder 1
      |          |--- mainfolder 1 Project                    <----
                                 |--- mainfolder 1.file       <----
      |          |--- subfolder 1
      |          |--- subfolder 2
      |
      |--- mainfolder 2
      |          |--- mainfolder 2 Project                    <----
      |                          |--- mainfolder 2.file       <----
      |          |--- subfolder 1
      |          |--- subfolder 2

These steps involve first copying the Foobar Project folder into each directory within My Projects, then renaming it to the parent name.

I have this current work in progress script, but I believe I am running into the problem of handling white space characters

From My Projects folder:

for d in */; do 
        cp -R ../Foobar\ Project "$d";
        mv -- "./$d\Foobar Project\" "./$d/$d Project\";
done

I do not currently have anything written for renaming the Foobar.file as I am stuck on renaming the folder first.

The problem I am facing is that the whitespace characters in the mv command are (I believe?) being treated as separate inputs, despite being in the same quotation marks. I am therefore a little lost as I have attempted to do trimming solutions with sed but I keep running into issues with the mv command not responding properly. I appreciate if someone knows a better way to approach this solution as I feel there is an easier way to achieve this.

CodePudding user response:

Try this:

for d in */; do
    d=${d%/};
    cp -R "../Foobar Project" "${d}";
    mv "${d}/Foobar Project" "${d}/${d} Project";
done

You need to remove the last / after the name of each directory with d=${d%/}.

And for the Foobar.file you can do this:

for d in */; do
    d=${d%/};
    cp -R "../Foobar Project" "${d}";
    mv "${d}/Foobar Project/Foobar.file" "${d}/Foobar Project/${d}.file";
    mv "${d}/Foobar Project" "${d}/${d} Project";
done

CodePudding user response:

If the directory you want to copy only contains a single file, it can be simplified to just

for proj in ./"My Projects"/*/; do
    pname=${proj#./}
    pname=${pname%%/*}
    dname="$proj/$pname/$pname Folder"
    mkdir -p "$dname"
    cp ./"Foobar Project"/Foobar.file "$dname/$pname".file
done

The parameter expansion ${proj#./} returns the value of $proj with any prefix matching ./ removed, and similarly $(pname%%/*} returns the value of $pname with the longest suffix matching /* removed.

  • Related