LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-12-2018, 10:54 AM   #1
Freaksta
Member
 
Registered: Jan 2003
Distribution: Slackware 14.1
Posts: 281

Rep: Reputation: 33
Bulk remove all text after extension


Hello,

I have a lot of files in a directory that end in .rar?RandomText

I would like to remove all text in all files in current directory after the string .rar

Would anyone be so kind as to show me how to do this?

Thank you in advance!
 
Old 03-12-2018, 11:35 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,815
Blog Entries: 15

Rep: Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662
Is the question mark (?) literal or is it any single character (or a typo)?

Listing 2-3 sample files would help.
 
Old 03-12-2018, 11:39 AM   #3
Freaksta
Member
 
Registered: Jan 2003
Distribution: Slackware 14.1
Posts: 281

Original Poster
Rep: Reputation: 33
Quote:
Originally Posted by MensaWater View Post
Is the question mark (?) literal or is it any single character (or a typo)?

Listing 2-3 sample files would help.
Literal.

FileDownload.rar?AWSAccessKeyID=
DifferentFile.rar?AWSAccessKeyID=

Thanks!
 
Old 03-12-2018, 11:53 AM   #4
Freaksta
Member
 
Registered: Jan 2003
Distribution: Slackware 14.1
Posts: 281

Original Poster
Rep: Reputation: 33
Quote:
Originally Posted by Freaksta View Post
Literal.

FileDownload.rar?AWSAccessKeyID=
DifferentFile.rar?AWSAccessKeyID=

Thanks!
I was able to do this with the non-standard mmv application using:

mmv "*.rar?Amazon*" "#1.rar"

But would be happy to see a bash option if anyone likes the challenge.
 
1 members found this post helpful.
Old 03-12-2018, 12:02 PM   #5
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,815
Blog Entries: 15

Rep: Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662
You could do it with sed:

Code:
for file in $(ls *.rar?*)
do newfile=$(echo $file | sed s/?.*/?/)
mv $file $newfile
done
The above assumes you want to keep the "?" before the random text. If you want to remove that as well just remove the "?" between the last two slashes:
Code:
newfile=$(echo $file | sed s/?.*//)
 
Old 03-12-2018, 12:05 PM   #6
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,013

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
e.g.

Code:
for f in ./*.rar*; do mv $f ${f/.rar?*/.rar}; done
Edit: The above will break if you have white spaces in your file names. To make it work with whitespaces, you also need to quote the filenames in the mv command like so:

Code:
for f in ./*.rar*; do mv "$f" "${f/.rar?*/.rar}"; done

Last edited by joe_2000; 03-12-2018 at 12:19 PM. Reason: added ? to make the string replacement safer and added whitespace version
 
Old 03-12-2018, 12:17 PM   #7
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,013

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Quote:
Originally Posted by MensaWater View Post
Code:
for file in $(ls *.rar?*)
This will invariably break when file names contain whitespaces. Do not use ls to iterate over files. Better use

Code:
for file in ./*
 
Old 03-12-2018, 12:21 PM   #8
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,299

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
Quote:
Originally Posted by joe_2000 View Post
e.g.

Code:
for f in ./*.rar*; do mv $f ${f/.rar?*/.rar}; done
Edit: The above will break if you have white spaces in your file name. To make it work with whitespaces, you also need to quote the filenams in the mv command like so:

Code:
for f in ./*.rar*; do mv "$f" "${f/.rar?*/.rar}"; done
It would be best to add the "-n" (--no-clobber) option to avoid overwriting a file should removing that text result in a duplicate name.
Code:
for f in ./*.rar*; do mv -n "$f" "${f/.rar?*/.rar}"; done
But btw, that still won't work if there are embedded spaces in the names. The "for" loop will try to process each "word" separately.
 
Old 03-12-2018, 12:43 PM   #9
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,013

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Quote:
Originally Posted by rknichols View Post
But btw, that still won't work if there are embedded spaces in the names. The "for" loop will try to process each "word" separately.
No it won't. Have you tried it?
 
Old 03-12-2018, 12:53 PM   #10
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,013

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Quote:
Originally Posted by rknichols View Post
It would be best to add the "-n" (--no-clobber) option to avoid overwriting a file should removing that text result in a duplicate name.
The problem you mention is valid, but the proposed solution will not prevent the error message "bla and bla are the same file"

If you really wanted to catch this case it would be better to change the expression in the for loop:
Code:
for f in ./*.rar?*; do mv "$f" "${f/.rar?*/.rar}"; done
That way the files concerned by the issue would not be considered in the first place.
Note that this would cause an error message if none of the files in the directory matched. But we start getting very theoretical
 
Old 03-12-2018, 01:04 PM   #11
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,013

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Quote:
Originally Posted by rknichols View Post
It would be best to add the "-n" (--no-clobber) option to avoid overwriting a file should removing that text result in a duplicate name.
Ah, I think I get what you mean now. When writing my previous post I thought you were talking about multiple execution.

If of course you have two files with the same name before the ? but different name parts after that it gets even more complex.
With -n or without does not matter, you won't get the desired result. Either you lose a file (without -n), or you don't rename a file (with -n).
I don't know that you could catch this case with a oneliner.
But I also don't suspect that this case is relevant for the OP?
 
Old 03-12-2018, 01:18 PM   #12
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,299

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
Quote:
Originally Posted by joe_2000 View Post
No it won't. Have you tried it?
You're right. I keep forgetting that filename expansion occurs after word splitting and can never increase the number of words in a command line.

Quote:
Originally Posted by joe_2000 View Post
With -n or without does not matter, you won't get the desired result. Either you lose a file (without -n), or you don't rename a file (with -n).
Exactly, but it's better to have the rename fail (with an error message) than to silently overwrite a file. Then you can decide what you really want to do with the two files.

Another possibility would be to use the "-b" (--backup) option to save a backup of any file that is about to be overwritten. But, there would be no message to alert you that this presumably unexpected event happened.
 
1 members found this post helpful.
Old 03-12-2018, 01:24 PM   #13
Freaksta
Member
 
Registered: Jan 2003
Distribution: Slackware 14.1
Posts: 281

Original Poster
Rep: Reputation: 33
You're all awesome!

Thank you :-)
 
Old 03-12-2018, 01:34 PM   #14
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,013

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Quote:
Originally Posted by rknichols View Post
Exactly, but it's better to have the rename fail (with an error message) than to silently overwrite a file. Then you can decide what you really want to do with the two files.
Agreed
Quote:
Originally Posted by rknichols View Post
Another possibility would be to use the "-b" (--backup) option to save a backup of any file that is about to be overwritten. But, there would be no message to alert you that this presumably unexpected event happened.
Nice one! Maybe in combination with -v (verbose). Then in these events you'd get the output:

Code:
renamed 'bar' -> 'foo' (backup: 'foo~')
which you could grep for "backup:" to be alerted of such events.
We are getting there :-)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How can I bulk copy files to a new extension in the same directory? jbruyet Linux - Software 6 09-15-2014 01:54 PM
[SOLVED] Remove extension from image Dafydd Programming 5 09-10-2014 02:54 AM
remove all files with same extension? scratchnz Linux - General 1 11-04-2005 03:40 AM
How to remove a php zend extension docbrazen Linux - Software 0 01-19-2004 02:17 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration