The Cookie Machine - Click here to drag window

DUMMY TEXT - Real text set in assets/js/theCookieMachine.js

If you can read me, I'm broken!

Views: 437     Votes:  1     ✅ Solution
Tags: delete   text   rename  
Link: 🔍 See Original Answer on Ask Ubuntu ⧉ 🔗

URL: https://askubuntu.com/q/1249785
Title: How to remove text from a folder name after a certain word
ID: /2020/06/12/How-to-remove-text-from-a-folder-name-after-a-certain-word
Created: June 12, 2020    Edited:  June 13, 2020
Upload: April 8, 2024    Layout:  post
TOC: false    Navigation:  false    Copy to clipboard:  false


Here’s the code you need plus some test data you don’t need:

#!/bin/bash

# NAME: truncate-dname
# PATH: $HOME/askubuntu/
# DESC: Answer for: https://askubuntu.com/questions/1249771/how-to-remove-text-from-a-folder-name-after-a-certain-word/1249785#1249785
# DATE: June 12, 2020

echo ==============  CREATE TEST DATA  ==================
mkdir -p dnames
cd dnames
mkdir -p "Song Band - Song Name download 9038450985934853434"
mkdir -p "Music List download dkjge3j6lk45j45756567"
mkdir -p "Video Chart download 4k645jel43k5yk574yryryrrtyryrryryrt"
ls

echo ==============  RENAME DIRECTORIES  ================
for old_name in ./*
do
    new_name="${old_name% download*}"
    mv -v "$old_name" "$new_name"
done

echo ==============  DELETE TEST DATA  ==================
ls
cd ..
rm -rf dnames

When you run the script you get this:

$ truncate-dname
============== CREATE TEST DATA ==================
Music List download dkjge3j6lk45j45756567
Song Band - Song Name download 9038450985934853434
Video Chart download 4k645jel43k5yk574yryryrrtyryrryryrt
============== RENAME DIRECTORIES ================
'./Music List download dkjge3j6lk45j45756567' -> './Music List'
'./Song Band - Song Name download 9038450985934853434' -> './Song Band - Song Name'
'./Video Chart download 4k645jel43k5yk574yryryrrtyryrryryrt' -> './Video Chart'
============== DELETE TEST DATA ==================
Music List  Song Band - Song Name  Video Chart

The key operation you want is to extract sub-string within string before a search string. For example:

$ a="Song Band - Song Name download 9038450985934853434"

$ b="${a% download*}"

$ echo $b
Song Band - Song Name

This process is a little tricky if the word “download” appears in the song title so it’s in the filename twice:

$ c="People keep downloading my songs download 9038465489643541"

$ d="${c%% download*}"

$ echo $d
People keep

$ d="${c% download*}"

$ echo $d
People keep downloading my songs

When you have two %% it deletes everything after the first “download” occurrence which you don’t want. So in this case you want to just use a single % so the last “download” is the cut off point.

There is also the # operation which searches in the opposite direction you definitely don’t want:

$ c="People keep downloading my songs download 9038465489643541"

$ e="${c##* download}"

$ echo $e
9038465489643541

$ e="${c#* download}"

$ echo $e
ing my songs download 9038465489643541

Here is a thorough reference.

⇧ How to convert w command idle timeout in seconds? 4 clicks to shut down Ubuntu - can we reduce this?  ⇩