Wade McCasland Wade McCasland - 4 months ago 10
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

5555#5555
become
55555555
containing the
001.pdf
file that becomes
55555555.001.pdf


enter image description here

Hope that makes sense and thanks.

Answer

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

  curdir=`pwd`
  for f in `find . -maxdepth 1 -type d -name "*#*" -print`;
  do
   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
    done
    cd $curdir
  done
Comments