how to remove characters between square brackets of file names.

rupeshforu3

In the zone
Hi I am Rupesh from India and I have Fedora 36 Linux and some files which consists of files with names consisting of square brackets and some text in between. I want to remove these square brackets including text.

Recently I have downloaded youtube videos using yt-dlp which is youtube downloader in Linux and succeeded but its file name has the following form.

This is youtube video [abcdefg-].mp4

Here in the above file name "This is youtube video" is the title of YouTube video and abcdefg- is the id for that youtube video. mp4 is the video file extension.

Now I want to remove characters between square brackets including square brackets ie., [abcdefg-]. Here before square brackets space character is present and I want to remove it also.

Finally I want file with name as "This is youtube video.mp4".

I have 100s of files as above.

I have searched web and found some sed commands but failed.Please try to suggest how to remove characters between brackets including square brackets from file name.

Regards,
Rupesh.

Sent from my LM-G710 using Tapatalk
 

khalil1210

In the zone
You can use bulk rename utility

When you use the "crop" option, you have the option of specifying a "special" value using the wildcard (*).
This will remove the specified string, and any characters occupied by the wildcard.
So for example, specifying [*] would convert "Hello[ABC] Joe" to just "Hello Joe", as it has removed the two square brackets and everything between.

Source: Bulk Rename Utility • View topic - Remove all ( ) and characters within
 
Last edited:
OP
R

rupeshforu3

In the zone
The problem has been solved in Linux itself without any other software.

The code is

Code:
for x in *.mp4; do mv "$x" "${x// \[*\]/}"; done

Does this code works same for all situations I mean in the same way ie., just removing characters between square brackets and nothing else like deleting other characters in file name etc.,.

Sent from my LM-G710 using Tapatalk
 

Zangetsu

I am the master of my Fate.
^^You can search for batch scripts in Linux. Which will take directory path as input and will rename all files as per rules defined in it.


*ostechnix.com/how-to-rename-multiple-files-at-once-in-linux/

*linuxhint.com/rename_files_linux/
 

Desmond

Destroy Erase Improve
Staff member
Admin
The problem has been solved in Linux itself without any other software.

The code is

Code:
for x in *.mp4; do mv "$x" "${x// \[*\]/}"; done

Does this code works same for all situations I mean in the same way ie., just removing characters between square brackets and nothing else like deleting other characters in file name etc.,.

Sent from my LM-G710 using Tapatalk

Are you sure this is working for you? It doesn't seem to be working on my machine.

I have used this instead and it works as expected:

rename 's/(.+)\[.*\](.*)$/$1$2/' *.txt

Output for reference:

Code:
desmond@stigmata2:~/temp$ ls -l
total 0
-rw-rw-r-- 1 desmond desmond 0 Jun 13 19:21 'test1[234].txt'
-rw-rw-r-- 1 desmond desmond 0 Jun 13 19:24 'test[123].txt'
desmond@stigmata2:~/temp$ rename 's/(.+)\[.*\](.*)$/$1$2/' *.txt
desmond@stigmata2:~/temp$ ls -l
total 0
-rw-rw-r-- 1 desmond desmond 0 Jun 13 19:24 test.txt
-rw-rw-r-- 1 desmond desmond 0 Jun 13 19:21 test1.txt
desmond@stigmata2:~/temp$

Edit:

Contrast with the for loop method you posted:

Code:
desmond@stigmata2:~/temp$ ls -l
total 0
-rw-rw-r-- 1 desmond desmond 0 Jun 13 19:24 'test1[234].txt'
-rw-rw-r-- 1 desmond desmond 0 Jun 13 19:31 'test[123].txt'
desmond@stigmata2:~/temp$ for x in *.txt; do mv "$x" "${x// \[*\]/}"; done
mv: 'test1[234].txt' and 'test1[234].txt' are the same file
mv: 'test[123].txt' and 'test[123].txt' are the same file
desmond@stigmata2:~/temp$ ls -l
total 0
-rw-rw-r-- 1 desmond desmond 0 Jun 13 19:24 'test1[234].txt'
-rw-rw-r-- 1 desmond desmond 0 Jun 13 19:31 'test[123].txt'
desmond@stigmata2:~/temp$
 
Top Bottom