0

I have 100 directories within a parent directory (/home/anik/"all my directories") with the following naming structure:

  • Zt.isolate1.Spades
  • Zt.isolate2.Spades
  • Zt.isolate100.Spades

I want to trim off the "Zt." and the ".Spades" part from each of the directory name.

Can you please tell me how can I do that in a loop for all the directories at once? Thank you.

  • Hi Anik and welcome to unix.stackexchange. If your problem was solved and your question correctly answered, you have the option of checking the green mark next to your favorite answer. This way several things happen: (i) you indicate that a question was satisfactorily answered, thereby signalling to others in the community that the answer is worth consulting, (ii) you reward the answerers by adding points to their karma, (iii) in doing so you show your gratitude and acceptance. -- Cheers. – Cbhihe Apr 15 '21 at 18:04

2 Answers2

2

Loop over the names and use variable substitutions to delete the unwanted bits. The following assumes that $HOME is /home/anik and that this is where your files are located (change topdir to some other value if the files are located elsewhere):

#!/bin/sh

topdir=$HOME

for dirpath in "$topdir"/Zt.*.Spades; do
    [ ! -d "$dirpath" ] && continue

    newdirpath=$topdir/${dirpath#$topdir/Zt.}
    newdirpath=${newdirpath%.Spades}

    if [ -e "$newdirpath" ]; then
        printf 'Can not rename "%s" into "%s", name taken\n' "$dirpath" "$newdirpath"
    else
        printf 'Renaming "%s" into "%s"\n' "$dirpath" "$newdirpath"
        echo mv "$dirpath" "$newdirpath"
    fi
done

The expression $topdir/${dirpath#$topdir/Zt.} will be replaced by the directory's pathname, but without the Zt. in the start of the directory filename. It does this by removing the prefix $topdir/Zt. and then adding $topdir/ at the start again.

The expression ${newdirpath%.Spades} expands to $newdirpath but removes the string .Spades from the end of the value.

I've added echo in front of the mv command to output the command that would be executed instead of executing it. Test the code and then remove the echo if it looks correct.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
  • Okay that worked like a charm. Thanks a lot. Just another question: what if I just want to remove only the `Zt.` part? Should I delete the `newdirpath=${newdirpath%.Spades}` from the above code? – Anik Dutta Apr 15 '21 at 13:29
  • @AnikDutta That is correct. – Kusalananda Apr 15 '21 at 13:32
1

You can use Perl rename tool:

rename -n 's/Zt\.(.+?)\.Spades$/$1/' /home/anik/Zt.*.Spades

(Remove the -n to actually rename if you're happy with the output)


Check this to choose the correct rename tool.

pLumo
  • 22,231
  • 2
  • 41
  • 66
  • Can I use this code in Linux? Sorry if it's a naive question. – Anik Dutta Apr 15 '21 at 12:52
  • Sure, this is for Linux... but make sure that `rename` is the correct command name. It differs between distributions. – pLumo Apr 15 '21 at 12:54
  • Sorry, it does not work. The command ran and nothing has changed. – Anik Dutta Apr 15 '21 at 12:58
  • 1
    @AnikDutta, there's the util-linux rename and the various Perl-based ones. Try `rename --version` and see if it mentions `util-linux` or not, and see https://unix.stackexchange.com/q/275254/170373 and https://unix.stackexchange.com/a/510583/170373 – ilkkachu Apr 15 '21 at 13:17
  • Yes, it mentioned util-linux – Anik Dutta Apr 15 '21 at 13:21
  • 1
    If you're running debian (or derivatives like ubuntu, mint, etc), run `apt-get install rename`. On other distros, find out how to install the perl rename because the rename from util-linux is primitive and nearly useless in comparison. It may already be installed but called `prename` or `file-rename`. – cas Apr 15 '21 at 13:28