Score:2

Remove/Delete a string of characters from filenames

ng flag

I have a bunch of text files that end with the line www.thethinkfoundation.org, for example:

file1.www.thethinkfoundation.org.txt

file2.www.thethinkfoundation.org.txt . . .

file100.www.thethinkfoundation.org.txt

I wish to remove/delete www.thethinkfoundation.org

I tried the following command in a terminal to achieve it. It is:

for f in www.thethinkfoundation.org*; do mv "$f" "${f#thethinkfoundation.org}"; done

The result was:

file1..txt

file2..txt . . .

file100..txt

Thank you in advance for a solution that works.

P.S.: I do not wish to use the rename command. I am actually looking for one that works across Linux and *BSD distros. My preferred text editor is nano

alien.on.earth avatar
ng flag
@Raffa Your solution works like a charm. Thanks.
Raffa avatar
jp flag
You are most welcome ... I added an explanation below that I hope you find useful as well :-)
Score:3
jp flag

Sub-string replacement based on Shell Parameter Expansion in bash and a few other shells as well can be achieved in two ways:

First, you can replace only the first occurrence(from left to right) of a pattern like so:

${var/pattern/replacement}

Second, you can replace all occurrences of a pattern like so:

${var//pattern/replacement}

Where var is the variable/parameter containing the string and pattern is the pattern that you wish to find and replace in that string and replacement is the replacement that you wish to substitute that pattern with which can be left empty as well i.e. ${var//pattern/} if you wish to just delete that pattern from the string.

Therefore, your desired solution would look like this:

for f in *www.thethinkfoundation.org*; do
    echo mv -n -- "$f" "${f//www.thethinkfoundation.org./}"
done

I added echo for a dry-run so that you can test it first and then take out echo and run it again to do the real work.

alien.on.earth avatar
ng flag
Thank you for the tutorial which I find very helpful to my understanding of how your solution works. But what do the `-n` and the two dashes `-- `mean? I ask because they do not appear in your first answer.
Raffa avatar
jp flag
@alien.on.earth The `-n` also `--no-clobber` option means "do not overwrite an existing file" in case a file with the same name already exists ... and the `--` indicates the end of options so that `mv` doesn't choke on filenames which start with a `-` ... Both are safety measures that are good to set in such case but `mv` should work without them (overwriting existing files and choking on filenames starting with `-` if they occur though :-) ... You don't want that) that's why.
alien.on.earth avatar
ng flag
Thanks for your explanation.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.