1

windows or linux, either works, it's on an external drive either can read

e.g. change "version2" to "version3" in every file and folder of a project?

Though in this case i want to change patient initials to "patient-1" in every file name and folder name, it's dozens to hundreds of files, tens of gigabytes, and about 10 patients (ten different things to change).

3 Answers 3

0

In Linux I'd use find to locate all the relevant files, and then rename:

$ find . -name "*version2*" -exec rename version2 version3  {} \;

EDIT:
To clarify, as per @choroba's comment below, this solution relates to rename (1) distributed as part of the util-linux package.

2
  • 2
    Note that there are two different rename commands with totally different meaning of the arguments.
    – choroba
    Sep 14, 2017 at 7:40
  • what if it is only part of the file name. i.e. "JS-sample2.txt" to "patient1-sample2.txt"
    – Kirt
    Sep 14, 2017 at 19:38
0

an answer i got from elsewhere, but i haven't tried it and i don't know if it's optimal:

"This isn't as elegant as I hoped but it changes "-kr" to "-patient-123" on the end of "*.txt" files"

find . -type f -iname '*.txt' | perl -ne 'chomp; my $orig = $; if (s/-kr(.txt)$/-patient-123$1/) { print "$orig\0$\0"; }' | xargs -0rn2 mv -i

"it only does filenames, the folders can be done separately like this:"

find . -type d | tac | perl -ne 'chomp; my $orig = $; if (s/-kr$/-patient-123/) { print "$orig\0$\0"; }' | xargs -0rn2 mv -i

0

Interactive solution in Linux: vidir. See this answer of mine.

Sole vidir doesn't descend into subdirectories. You need

find . | vidir -

or like

find . | EDITOR=kate vidir -

Then use the "search and replace" feature of your text editor to make the desired changes. Save the text file (do not change its name) and exit the editor. Done.


Note every file and directory has its own independent line. If you need to rename a directory named version2 to version3, it's important to make the change in its line and in all the lines associated with its subdirectories and files within. If you miss just one object then you will end up with two directories: version2 and version3 (so there will be mkdir involved). Make sure you are permitted to rename/move all objects you want to alter. Even if you get two directories in place of one, this can still be resolved by running vidir again (although one of the directories may have wrong ownership/permissions because it's newly created).

A directory you want to rename should precede its content. Fortunately find . works this way (unless one uses -depth, so don't use -depth).

On the other hand if vidir doesn't list any deeper content (e.g. you used find with -maxdepth) then you don't need to worry about it. Renaming a directory whose content is not on the list will work as if you used mv.

In any case it's relatively easy to make changes you ask for. Many depends on the chosen editor: searching with regex, highlighting changes, undoing – features like these may get handy.

I find vidir useful in cases like yours mainly because it allows me to make sure the resulting paths are really what I want, before any renaming/moving occurs. If I mess anything up, I can abort without saving changes.

Test the solution and get familiar with the tool by working on some expendable directory first. Notice what happens when there's a name conflict. Experiment and adjust the approach to your needs.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .