2

I have a lot of documents on my drive with the .odt extension. Example: enter image description here

However, I find the .odt distracting when browsing through a lot of documents.

So, I managed to write this script (removeODT.sh):

#!/bin/bash

for file in *.odt
do
 mv "$file" "${file%.odt}"
done

Which removes the .odt extension from .odt files in the directory that I execute the script in.

e.g. Open terminal in ~/Desktop/MyDocuments and type

sh /home/user1/BashScripts/removeODT.sh

This removes the .odt extension from all the files in MyDocuments.

But after removing the extension, I can't use the files on other operating systems, and also can't search the contents of the document with loook.

So to remedy that, I created a second script to add the .odt extension back when needed (addODT.sh):

#!/bin/bash

for file in *
do
 mv "$file" "${file%}.odt"
done

Now, what I'd like to do is to make those scripts change every document in the specified parent directory and all of its sub-directories instead of only a single directory as they do now.

e.g. "Remove .odt extensions from files in Documents and all of its sub-folders "

This would allow me to change all of my files with a single script without having to execute the script for each folder separately. But I'm quite new to this so I have no idea how to go about it.

P.S. An ideal solution would be to hide the .odt extension in Nautilus but as far as I know it's not possible.

6
  • 1
    as a software engineer, this is horrifying (but i respect your life choices)
    – amara
    Oct 3, 2016 at 21:51
  • @naiad I have no idea what you've just said.
    – Rtsne42
    Oct 4, 2016 at 0:13
  • He means that removing the extension is a truly bad idea. Extensions add information to a file: they declare its type, making it easier to know what program should be used to open it. Additionally, on Windows, the fact that extensions are hidden by default allowed for certain social exploits. (You could trick people into running scripts more easily by putting a fake extension on it, and the real extension was hidden.) Disabling extension hiding is one of the first things I do on any Windows machine. I think in the long run, you'd be far better off just getting used to seeing them.
    – jpmc26
    Oct 4, 2016 at 15:57
  • He may also be referring to the large number of documents named "Untitled Document" instead of giving meaningful names. If these are really your document names, renaming them to something more meaningful may also make it easier to ignore the extension by giving you something that stands out more to focus on when skimming the list. It also appears you may be using copies to version your document. This is also difficult to manage, although actually versioning binary files like odt is a bit harder than plain text types. If you keep this scheme, placing old copies in a folder might also help.
    – jpmc26
    Oct 4, 2016 at 15:58
  • Bottom line: we think you have bigger problems than the extension distracting you, and we think they have better potential solutions than removing the extension. (In other words, we consider this an XY problem.)
    – jpmc26
    Oct 4, 2016 at 16:02

1 Answer 1

7

You can use find to walk directories recursively.

find -type f -exec mv {} {}.odt \;

Removing the extension is kind of more problematic:

find -name '*.odt' -exec bash -c 'f={}; mv "$f" "${f%.odt}"' \;

But you can also create a script to remove the extension and call it from find to make it simpler:

find -name '*.odt' -exec remove_odt.sh {} \;

As you can guess, {} will be replaced by each file's name with exec, the final \; says where the command to execute ends.

Also, don't use sh to run bash scripts, sh and bash are different beasts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.