how to remove characters between square brackets of file names.

rupeshforu3

New Member
Joined
Feb 28, 2022
Messages
17
Reaction score
1
Credits
257
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.
 


Alexzee

Well-Known Member
Joined
Jun 1, 2019
Messages
2,295
Reaction score
1,117
Credits
11,914
Two thoughts-

Right click on the file and rename it.
OR>
Open the file in your favorite text editor with root privileges and edit it that way.
 

osprey

Member
Joined
Apr 15, 2022
Messages
78
Reaction score
43
Credits
680
If there are lots of files to be renamed, the command "rename" is useful. The below commands are on debian using the "rename" command which is actually a perl construction.

In the following, 3 different video files are created.
"rename" is used with the -n option which just shows what rename will do without doing it.
Then the -n option is omitted and the newly named files are renamed without the characters which were marked for excision.
Code:
[[email protected] ~]$ touch 'video1 [abcdefg3].mp4'
[[email protected] ~]$ touch 'video2 [abcdefg].mp4'
[[email protected] ~]$ touch 'video3 [abcdefwq].mp4'
[[email protected] ~]$ rename -n 's/\ \[.*\]//' *
rename(video1 [abcdefg3].mp4, video1.mp4)
rename(video2 [abcdefg].mp4, video2.mp4)
rename(video3 [abcdefwq].mp4, video3.mp4)
[[email protected] ~]$ rename 's/\ \[.*\]//' *
[[email protected] ~]$ ll
total 0
-rw-r--r-- 1 bird bird 0 Jun 14 14:32 video1.mp4
-rw-r--r-- 1 bird bird 0 Jun 14 14:32 video2.mp4
-rw-r--r-- 1 bird bird 0 Jun 14 14:32 video3.mp4
 
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Linux.org Hosting Donations
Consider making a donation

Members online


Top