0

I've been trying to do this for months, but I couldn't get it to work. I'm trying to do it in bash. All files are in a linux machine, so bash I guess?

What I'd like to do: for all files in a directory rename them with following criteria:

  • if the file has square brackets in the filename, delete the brackets and including numbers [312646416198]
  • if the file has a year (2018), keep that (not all have that)
  • if the file has brackets with a single number in it, (1) delete the brackets and the number

create a folder based on the first part of the file name (e.g. everything leading up to the first hyphen "-", and move that file into the created folder.

So for example the following names should after some processing look like this (ideally). Somethings will be placed wrong, like when title comes before author, so the new folder would be named after the title, not the author, but I can live with that, there's only a small percentage that are named like that.

So this:

The Brotherhood of the Rose - David Morrell.epub Abbi Glines - Bad for You (2014) [9781481420761] (1).epub Kristin Hannah - The Great Alone (2018) [9781250165619].epub Stephanie Dray, Laura Kamoie - America's First Daughter - A Novel (2016) [9780062347268] (1).epub Terence Hanbury White - The Once and Future King (1987) [9780441627400] (1).epub 

becomes this:

The Brotherhood of the Rose The Brotherhood of the Rose - David Morrell.epub Abbi Glines Abbi Glines - Bad for You (2014).epub Kristin Hannah Kristin Hannah - The Great Alone (2018).epub Stephanie Dray, Laura Kamoie Stephanie Dray, Laura Kamoie - America's First Daughter - A Novel (2016).epub Terence Hanbury White Terence Hanbury White - The Once and Future King (1987).epub 
1
  • 1
    You'd like to do, but don't show what you have done so far and where yo are stuck. Sorry to say, but this website is not a code-writing platform. Commented Aug 9, 2020 at 17:20

2 Answers 2

1

This might work:

$ cat epub-cleanup.sh #! /bin/bash for i in *.epub; do mv -iv "$i" "$(echo "$i" | sed -r 's/\[[0-9]+\]//;s/\([0-9]\)//;s/[ ]*.epub/.epub/')" done 
  1. removes a single instance of [0123456789]
  2. removes a single instance of (1)
  3. cleans up trailing spaces before the file extension
0

I'd use the zsh shell instead of bash:

set -o extendedglob for file (*' - '*.epub) { newfile=${file// #(\[<->\]|\((<->~<1000-2020>)\))} dir=${newfile%% - *} mkdir -p -- $dir && mv -i -- $file $dir/$newfile } 

Here removing the (number)s only if that number is not in the range 1000-2020.

$ tree . ├── Abbi Glines - Bad for You (2014) [9781481420761] (1).epub ├── Kristin Hannah - The Great Alone (2018) [9781250165619].epub ├── Stephanie Dray, Laura Kamoie - America's First Daughter - A Novel (2016) [9780062347268] (1).epub ├── Terence Hanbury White - The Once and Future King (1987) [9780441627400] (1).epub └── The Brotherhood of the Rose - David Morrell.epub 0 directories, 5 files $ zsh ~/that-script $ tree . ├── Abbi Glines │   └── Abbi Glines - Bad for You (2014).epub ├── Kristin Hannah │   └── Kristin Hannah - The Great Alone (2018).epub ├── Stephanie Dray, Laura Kamoie │   └── Stephanie Dray, Laura Kamoie - America's First Daughter - A Novel (2016).epub ├── Terence Hanbury White │   └── Terence Hanbury White - The Once and Future King (1987).epub └── The Brotherhood of the Rose └── The Brotherhood of the Rose - David Morrell.epub 5 directories, 5 files 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.