0

SEE VISUAL EXPLANATION and EDIT

I'm trying to create a bash script to do a rename+move.

This is the example situation:

         |DOCTEMP - NameOfTheFolder---------|NameOfADocument.docx
FOLDER---|
         |DOCFINAL - OtherNameOfTheFolder---|OtherNameOfADocument.pdf
         |
         |etc.

What I want to do is a script that:

Part I

Only if the file has docx, pdf, ppt then rename it as its folder but without DOCTEMP - / DOCFINAL -.

NameOfADocument.docx => NameOfTheFolder.docx

OtherNameOfADocument.pdf => OtherNameOfTheFolder.pdf

Part II

After renaming, move that file to another folder depending on the first part of the name of the old folder.

NameOfTheFolder.docx => if **DOCTEMP** then => /ROOT/DOCTEMP/NameOfTheFolder.docx

OtherNameOfTheFolder.docx => if **DOCFINAL** then => /ROOT/DOCFINAL/OtherNameOfTheFolder.pdf

I tried using something like this but it doesn't work

#!/bin/bash

cd "$ROOT/FOLDER"
# for filename in *; do
find . -type f | while IFS= read filename; do # Look for files in all ~/FOLDER sub-dirs
  case "${filename,,*}" in  # this syntax emits the value in lowercase: ${var,,*}  (bash version 4)
     *.part) : ;; # Excludes *.part files from being moved
     move.sh) : ;;
     *test*)            mv "$filename" "$ROOT/DOCTEMP/" ;; # Using move there is no need to {&& rm "$filename"}
     *) echo "Don't know where to put $filename" ;;
  esac
done

EDIT: Visual explanation

given

ROOT/DOWNLOADS/OFFICE - House/file.docx
              /IWORK - Car/otherfile.docx

then Part I

ROOT/DOWNLOADS/OFFICE - House/House.docx
              /IWORK - Car/Car.docx

then Part II

ROOT/DOCUMENTS/OFFICE/House.docx
              /IWORK/Car.docx

docx can be pdf or ppt; here only an example

EDIT2: Final script

 #!/bin/bash
shopt -s nullglob
for filename in /ROOT/DOWNLOADS/{OFFICE,IWORK}/*/*.{docx,pdf,ppt}; do
    new_path="$(dirname $filename).${filename##*.}"
    new_path="${new_path/DOWNLOADS/DOCUMENTS}"
    echo "moving $filename -> $new_path"
    mv "$filename" "$new_path" 
done
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
DylanDog
  • 109
  • 8
  • You don't have to edit your question's title once it's solved, just accept the correct answer ;) – Thomas Ayoub Feb 11 '16 at 15:32
  • Don't edit "SOLVED" into titles here, **or** edit answers into questions. Clicking the checkbox to accept an answer marks it solved, and makes it clear to readers which answer you're using. – Charles Duffy Feb 11 '16 at 15:32
  • ...if you're using something different from the 3rd-party answers offered, add that as an answer yourself, don't edit it into the question. – Charles Duffy Feb 11 '16 at 15:33
  • (Also, flagging your edits as such is frowned on -- a question should read as a single, cohesive narrative, not a history of modifications; someone who wants to see that history can ask StackOverflow to show them the diffs between historically posted versions). – Charles Duffy Feb 11 '16 at 15:34

1 Answers1

1

I might have missed some points in that explanation. But would this do what you want?

#!/bin/bash
shopt -s nullglob
for filename in /ROOT/DOWNLOADS/{OFFICE,IWORK}/*/*.{docx,pdf,ppt}; do
    new_path="$(dirname $filename).${filename##*.}"
    new_path="${new_path/DOWNLOADS/DOCUMENTS}"
    echo "moving $filename -> $new_path"
    mv "$filename" "$new_path" 
done
Håken Lid
  • 22,318
  • 9
  • 52
  • 67
  • Thank you I test it now. What point it is not clear? I can try to explain it better. – DylanDog Feb 11 '16 at 13:06
  • The thing that is unclear is that the script you posted contains references to things that is not mentioned in the text. For instance `test` and `part`. PS: I've changed the glob pattern in my answer slightly to only match files in subfolders of DOCTEMP or DOCFINAL. – Håken Lid Feb 11 '16 at 13:11
  • "test" is only an example word as "NameOfTheDocument". "part" is because of sometimes these PDF are zipped (multiple .part). EDIT: And if instead of DOCTEMP and DOCFINAL I want to use completely different words for example OFFICE and IWORK? – DylanDog Feb 11 '16 at 13:13
  • Updated original post with better visual explanation – DylanDog Feb 11 '16 at 13:28
  • you can just use glob pattern `{OFFICE,IWORK}` instead of `{DOCTEMP,DOCFINAL}` – Håken Lid Feb 11 '16 at 13:30
  • And what about move to folder? Where in the script I have to add ROOT/DOCUMENTS/DOCTEMP and ROOT/DOCUMENTS/DOCFINAL ? – DylanDog Feb 11 '16 at 13:34
  • The `$new_path` should be the path you want to move the file to. It's done in a single step. `mv` is used both to rename and move files. – Håken Lid Feb 11 '16 at 13:36
  • See EDIT2. Is correct? because I don't understand where to put in $new_path two folders – DylanDog Feb 11 '16 at 13:42
  • `new_path` will be the full path of the new file. For instance. `/ROOT/DOCUMENTS/DOCTEMP/House.docx`. So the target folder name is part of the path already. If there's any single part of my answer you don't understand, just ask. – Håken Lid Feb 11 '16 at 13:46
  • OK! Sorry but I'm new with bash scripting. So, last question (I hope): the only thing I have to change is "new_path" (there are two of them) with "/ROOT/DOCUMENTS/" (all without quotes), LIKE EDIT2? – DylanDog Feb 11 '16 at 13:50
  • You want to move from `DOWNLOADS` to `DOCUMENTS`? – Håken Lid Feb 11 '16 at 13:53
  • yes, move files from /ROOT/DOWNLOADS/OFFICE - Name/ to /ROOT/DOCUMENTS/OFFICE/ and /ROOT/DOWNLOADS/IWORK - OtherName/ to /ROOT/DOCUMENTS/IWORK/ – DylanDog Feb 11 '16 at 13:56
  • Use parameter substitution `new_path="${new_path/DOWNLOADS/DOCUMENTS}"`. See my updated answer. – Håken Lid Feb 11 '16 at 14:03
  • Now I understand but DOCUMENTS isn't inside ROOT/DOWNLOADS/ but simply /ROOT/DOCUMENTS so I think it will be new_path="${new_path/DOCUMENTS}". Right? – DylanDog Feb 11 '16 at 14:09
  • `${var/Pattern/Replacement}` will replace the first `Pattern` in `$var` with `Replacement`. So the `/` is not a path separator inside this expression, and both `/`s are mandatory. https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html – Håken Lid Feb 11 '16 at 14:35
  • OK now it's clear; I tried now but it seems not working: I copied script.sh in ROOT folder, then, done chmod +x script.sh and then bash script.sh – DylanDog Feb 11 '16 at 14:44
  • What do you mean "not working". It will only process the files that match `/ROOT/DOWNLOADS/{OFFICE,IWORK}/*/*.{docx,pdf,ppt}`. **If your folders are named something else, then you must change the script to reflect that.** You seem to be very confused about the fundamentals of bash, and also about what your files and folders are actually named. I can't help you any further, since I'm not a mind reader. – Håken Lid Feb 11 '16 at 15:16
  • Folders and file names are as I wrote. Probably I'm doing something wrong. Sorry but here it is very late and I'm confusing. For now I put the question as settled. Really apologize and thank you for your patience. – DylanDog Feb 11 '16 at 15:26
  • No problem. I suggest you try to test out each line by itself from the shell to figure out what's going wrong. For instance try `for f in /ROOT/DOWNLOADS/{OFFICE,IWORK}/*/*.{docx,pdf,ppt}; do echo $f; done`. If this doesn't list all the files that you want to move, then that line must be fixed before anything else will work. – Håken Lid Feb 11 '16 at 16:22