Wade McCasland Wade McCasland - 1 year ago 64
Bash Question

script to remove middle character from multiple directories then rename files in subdirectories to include directory + file name

I have two objectives I am trying to accomplish and need some help.

Here is the overview:

enter image description here

There are hundreds of folders/directories with random numbers ( 5555#5555 ) containing the same 001.pdf file. I would like to:

1: Remove the "#" symbol from every folder/directory

2: Rename the 001.pdf file to include the new folder numbers and the 001.pdf

Desired result: The folder

containing the
file that becomes

enter image description here

Hope that makes sense and thanks.

Answer Source

so update: Thanks for the suggestions. Here is what I got to work for me. Might not be the best way but hey.

  for f in `find . -maxdepth 1 -type d -name "*#*" -print`;
   echo "folder is $f"
    newFolder=`echo $f | sed 's/#//g'`
    echo mv $f $newFolder
    mv $f $newFolder
    echo newfolder is $newFolder
    cd $newFolder
    for file  in `find . -maxdepth 1 -type f -printf '%f\n'`; do
      echo "mv $file to ${newFolder}_$file"
      mv $file ${newFolder}_$file
    cd $curdir
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download