Find and replace mulitple charaters in filenames | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Find and replace mulitple charaters in filenames

UNIX for Dummies Questions & Answers


Tags
filename, multiple characters, remove

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-19-2013
barrydocks barrydocks is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 25 November 2014, 10:35 AM EST
Location: Barry, UK
Posts: 83
Thanks: 10
Thanked 1 Time in 1 Post
Find and replace mulitple charaters in filenames

I have a virtual pdf printer set up on my server which produces files with the following prefix:

Code:
smbprn_00000044_Microsoft_Word_-_OriginalFilename.pdf

the number in the center of the file increase by one for each new file.

I want to remove all the charaters infront of OriginalFilename.pdf using the following code:

Code:
find . -type f -name '*[:smbprn_*_Microsoft_Word_-_*.pdf"]*' | while IFS= read -r; do mv -- "$REPLY" "${REPLY//[:smbprn_*_Microsoft_Word_-_*.pdf\"]}" done;

which I modified form here

If I run this directly from the command prompt I simply end up with a second > prompt?

I think I must be pretty close, any help would be welcome
Sponsored Links
    #2  
Old 01-19-2013
elixir_sinari's Avatar
elixir_sinari elixir_sinari is offline Forum Advisor  
Registered User
 
Join Date: Mar 2012
Last Activity: 9 October 2014, 4:50 PM EDT
Location: India
Posts: 1,412
Thanks: 101
Thanked 496 Times in 473 Posts
Try:

Code:
find . -type f -name 'smbprn_*_Microsoft_Word_-_*.pdf' | while read -r; do echo mv -- "$REPLY" "${REPLY/smbprn_*_Microsoft_Word_-_/}"; done;

Remove the echo when the command lines look OK.
And, I hope your original filename will not have _Microsoft_Word_-_ or smbprn_ .
You may also do this with find itself using -exec (with some assumptions).
Sponsored Links
    #3  
Old 01-19-2013
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 December 2014, 5:43 AM EST
Location: San Jose, CA, USA
Posts: 5,266
Thanks: 207
Thanked 1,759 Times in 1,499 Posts

Code:
find . -type f -name '*[:smbprn_*_Microsoft_Word_-_*.pdf"]*' | while IFS= read -r; do mv -- "$REPLY" "${REPLY//[:smbprn_*_Microsoft_Word_-_*.pdf\"]}" ;done;


Last edited by Scrutinizer; 01-19-2013 at 09:37 AM.. Reason: code tags
    #4  
Old 01-19-2013
barrydocks barrydocks is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 25 November 2014, 10:35 AM EST
Location: Barry, UK
Posts: 83
Thanks: 10
Thanked 1 Time in 1 Post
Thanks for the replies;

This code:

Code:
find . -type f -name '*[:smbprn_*_Microsoft_Word_-_*.pdf"]*' | while  IFS= read -r; do mv -- "$REPLY"  "${REPLY//[:smbprn_*_Microsoft_Word_-_*.pdf\"]}" ;done;

Gives this result:

Code:
$ find . -type f -name '*[:smbprn_*_Microsoft_Word_-_*.pdf"]*' | while  IFS= read -r; do mv -- "$REPLY"  "${REPLY//[:smbprn_*_Microsoft_Word_-_*.pdf\"]}" ;done;
mv: cannot open `./smbprn_00000046_Microsoft_Word_-_Letters2.pdf' for reading: Permission denied
mv: cannot create regular file `/TePage': Permission denied
mv: cannot open `./smbprn_00000045_Microsoft_Word_-_Letters1.pdf' for reading: Permission denied

I get the same result even if run as root, the user I am running the command with has read/write permissions

This code:

Code:
find . -type f -name 'smbprn_*_Microsoft_Word_-_*.pdf' | while read -r;  do echo mv -- "$REPLY" "${REPLY/smbprn_*_Microsoft_Word_-_/}"; done;

Does move the files, but I'm not sure where to and the original file are still there:

Code:
find . -type f -name 'smbprn_*_Microsoft_Word_-_*.pdf' | while read -r; do echo mv -- "$REPLY" "${REPLY/smbprn_*_Microsoft_Word_-_/}"; done;
mv -- ./smbprn_00000046_Microsoft_Word_-_Letters2.pdf ./Letters2.pdf
mv -- ./smbprn_00000045_Microsoft_Word_-_Letters1.pdf ./Letters1.pdf

but then:

Code:
$ ls
smbprn_00000045_Microsoft_Word_-_Letters1.pdf  Test_Page.pdf
smbprn_00000046_Microsoft_Word_-_Letters2.pdf

I am running these commands from within the directory containing the files
Sponsored Links
    #5  
Old 01-19-2013
elixir_sinari's Avatar
elixir_sinari elixir_sinari is offline Forum Advisor  
Registered User
 
Join Date: Mar 2012
Last Activity: 9 October 2014, 4:50 PM EDT
Location: India
Posts: 1,412
Thanks: 101
Thanked 496 Times in 473 Posts
Hey, I told you to remove the echo.
I was echo'ing the mv command lines so that you could know what is being moved...
The Following User Says Thank You to elixir_sinari For This Useful Post:
barrydocks (01-19-2013)
Sponsored Links
    #6  
Old 01-19-2013
barrydocks barrydocks is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 25 November 2014, 10:35 AM EST
Location: Barry, UK
Posts: 83
Thanks: 10
Thanked 1 Time in 1 Post
Quote:
Originally Posted by elixir_sinari View Post
Hey, I told you to remove the echo.
I was echo'ing the mv command lines so that you could know what is being moved...
Sorry, skipped over that bit in my eagerness. Thanks it works!

Just out of interest what would be the easiest way to append the file date to the filename for these files?

Thanks
Sponsored Links
    #7  
Old 01-19-2013
elixir_sinari's Avatar
elixir_sinari elixir_sinari is offline Forum Advisor  
Registered User
 
Join Date: Mar 2012
Last Activity: 9 October 2014, 4:50 PM EDT
Location: India
Posts: 1,412
Thanks: 101
Thanked 496 Times in 473 Posts
How do you mean? Please elaborate with an example.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
replace word with special charaters drtabc UNIX for Advanced & Expert Users 4 02-24-2010 12:56 AM
Help with Find/Replace Javascript Injected Strings in mulitple files zzlegs Shell Programming and Scripting 1 05-28-2009 08:29 PM
feeding filenames to find command sanjay1979 Solaris 4 03-31-2009 06:43 PM
find filenames like unix commands deepakgang Shell Programming and Scripting 6 06-30-2008 12:41 AM
find lowercase filenames jpprial UNIX for Dummies Questions & Answers 4 01-28-2002 04:57 PM



All times are GMT -4. The time now is 09:25 AM.