Find and replace mulitple charaters in filenames


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Find and replace mulitple charaters in filenames
# 8  
Old 01-19-2013
So the final process I would like to achieve is:
find all files with this string smbprn_000_Microsoft_Word_-_OriginalFile.pdf

remove the smbprn_*_Microsoft_Word_-_part

append the file creation date onto the end of the filename: OriginalFile_DD_MM_YY.pdf
Move the file to a new location.

This can be in any order, I suspect moving the files to a new location first would be the best option not sure about the rest?
Was thinking of using this:
Code:
cp filename filename`date +%Y%m%d`

# 9  
Old 01-19-2013
Wouldn't that be a rather arbitrary date, depending on when you run the command? Mayhap it's wiser to use creation or modification date for those files?
# 10  
Old 01-19-2013
Quote:
Originally Posted by barrydocks
So the final process I would like to achieve is:
find all files with this string smbprn_000_Microsoft_Word_-_OriginalFile.pdf

remove the smbprn_*_Microsoft_Word_-_part

append the file creation date onto the end of the filename: OriginalFile_DD_MM_YY.pdf
Move the file to a new location.

This can be in any order, I suspect moving the files to a new location first would be the best option not sure about the rest?
Was thinking of using this:
Code:
cp filename filename`date +%Y%m%d`

I'm afraid I didn't really pay attention to what you were trying to do when I noticed that your script was missing the semicolon and just pointed out that adding the semicolon would get around the syntax error.

Looking at what you've come up with so far, I note that some of the things you're doing rely on features that are not portable (assuming that read with no operand will store the line read in the shell variable REPLY and depending on the ${var/.../...} variable expansions), or may be overly complex (using find if all of the files you want to process are in the current working directory). The following scripts don't depend on anything that is not required by the POSIX standards and the Single UNIX Specification, and they also add in the current date in the new names of the files in the format requested. (Both of these also include "echo" to verify that the commands that they execute will do what you want. If one of them does do what you want, remove the "echo " to actually rename the files.) As RudiC noted, the date supplied here will be the date when the file is renamed; not the date when the file was created.

First, if all of the files you want are in the current directory, try the following:
Code:
#!/bin/ksh
for f in smbprn_*_Microsoft_Word_-_*.pdf
do      x=${f##*_}
        t=${x%.pdf}$(date "+_%d_%m_%Y.pdf")
        echo mv "$f" "$t"
done

I use the Korn shell, but these scripts will work with any shell that behaves as specified by the standards.

Second, if some of the files you want to rename are in subdirectories try the following:
Code:
#!/bin/ksh
find . -name 'smbprn_*_Microsoft_Word_-_*.pdf' | while IFS= read -r f
do      d=${f%/*}
        x=${f#*smbprn_*_Microsoft_Word_-_}
        t="$d/${x%.pdf}$(date "+_%d_%m_%Y.pdf")"
        echo mv "$f" "$t"
done

If you want all of the renamed files to be moved to the current directory instead of leaving them in the directory where they were found, change:
Code:
        t="$d/${x%.pdf}$(date "+_%d_%m_%Y.pdf")"

in the above script to:
Code:
        t="${x%.pdf}$(date "+_%d_%m_%Y.pdf")"

or if you want to move all of the files to another (single) directory:
Code:
        t="$newdir/${x%.pdf}$(date "+_%d_%m_%Y.pdf")"

As long as you leave in the echo until confirm that one of these scripts does what you want, I don't think you need the cp. You could also turn the echo into a prompt showing that mv that is about to be executed and force the person running the script to reply "yes" to execute the mv command or "no" to skip the rename.
# 11  
Old 01-19-2013
Thanks for the replies.

Quote:
Originally Posted by RudiC
Wouldn't that be a rather arbitrary date, depending on when you run the command? Mayhap it's wiser to use creation or modification date for those files?
Yes you are probably correct, creation date would probably be the best.

@Don Cragun
Thanks for your advice, unfortunately most of it is completely over my head - hence why I post on here for help - I am not an IT professionalSmilie

The idea is that a pdf will be produced by front-of-house staff from a word document that is populated from an MS Access applicationSmilie using a virtual cups-pdf printer on my ubuntu server. The pdf will then need to have the characters added by the pdf printer removed, the creation date added and moved to a new directory. From the new location the pdf will be edited by a user (me) on a tablet pc (probably an iPadSmilie) and then attached back to the MS Access record. I was planning to run the script that alters the pdf filename every 30secs or so as a cron job from a unprivileged user home directory. Hopefully this all makes sense?

I am open to better suggestions if you have anySmilieSmilie

Thanks
# 12  
Old 01-19-2013
Quote:
Originally Posted by barrydocks
Thanks for the replies.



Yes you are probably correct, creation date would probably be the best.

@Don Cragun
Thanks for your advice, unfortunately most of it is completely over my head - hence why I post on here for help - I am not an IT professionalSmilie

The idea is that a pdf will be produced by front-of-house staff from a word document that is populated from an MS Access applicationSmilie using a virtual cups-pdf printer on my ubuntu server. The pdf will then need to have the characters added by the pdf printer removed, the creation date added and moved to a new directory. From the new location the pdf will be edited by a user (me) on a tablet pc (probably an iPadSmilie) and then attached back to the MS Access record. I was planning to run the script that alters the pdf filename every 30secs or so as a cron job from a unprivileged user home directory. Hopefully this all makes sense?

I am open to better suggestions if you have anySmilieSmilie

Thanks
OK. Back to basics:
1. Will all of the files you want to rename be in the same directory? If so, what is the name of that directory? If not, what are the names of all of the directories that will contain pdf files you want to rename?
2. What is the name of the directory where you want to place the renamed pdf files?
3. When you run the command:
Code:
uname -a

what is the output?

If you want a script to run every 30 seconds, cron won't be sufficient; its finest granularity is 1 minute. And, if the system is busy, you could end up having two copies of your script running at the same time. If you run a script to rename files every few minutes after a file is converted, the likelihood that the current date and the file's creation date are different is pretty low (especially if the front end people who create the pdf files don't work between 11pm and 1am).
# 13  
Old 01-19-2013
OK
Code:
$ uname -a
Linux ubuntu 2.6.32-45-generic #102-Ubuntu SMP Wed Jan 2 21:53:06 UTC 2013 i686 GNU/Linux

But I want this script to work on later kernels and 64bit versions as well.

Quote:
1. Will all of the files you want to rename be in the same directory? If so, what is the name of that directory? If not, what are the names of all of the directories that will contain pdf files you want to rename?
Yes they will all be in the same directory, /media/PDF which is also mounted as an smb share on the LAN.
Quote:
2. What is the name of the directory where you want to place the renamed pdf files?
somewhere like /media/new_PDF but please make it apparent so I can alter it :-)

Quote:
If you want a script to run every 30 seconds, cron won't be sufficient; its finest granularity is 1 minute. And, if the system is busy, you could end up having two copies of your script running at the same time.
Thanks I didn't know this, is there a better method to achieve this?

Quote:
If you run a script to rename files every few minutes after a file is converted, the likelihood that the current date and the file's creation date are different is pretty low (especially if the front end people who create the pdf files don't work between 11pm and 1am).
true

Again thanks for your help and advice
# 14  
Old 01-19-2013
Run the command:
Code:
type ksh

The output should be something like:
Code:
ksh is a tracked alias for absolute_path

Save the value marked in blue italics.
Create a file named something like get_pdf containing the following Korn shell script:
Code:
#!/bin/ksh
cd /media/PDF
for f in smbprn_*_Microsoft_Word_-_*.pdfdo      x=${f#smbprn_*_Microsoft_Word_-_}
        t=${x%.pdf}$(date "+_%d_%m_%Y.pdf")
        echo mv "$f" "../new_PDF/$t"
done

and replace the string marked in red in this script with the string you saved from the output of the type command.

Then run the commands:
Code:
chmod +x get_pdf
if [ ! -d $HOME/bin ]
then    mkdir $HOME/bin
fi
mv get_pdf $HOME/bin

You should then be able to run the command get_pdf no matter what directory you are in and it will display the commands it would use to rename and move PDF files from the directory /media/PDF to the directory /media/new_PDF.

If the commands displayed are the commands that you want this script to perform, change the line in $HOME/bin/get_pdf that currently says:
Code:
        echo mv "$f" "../new_PDF/$t"

to:
Code:
        mv "$f" "../new_PDF/$t"

Change the ../new_PDF in that line to any other directory if you want to change the directory to which the renamed files should be moved.

If you already have a script that you run after you have edited a PDF file to attach it "back to the MS Access record", I would suggest adding a call to get_pdf to the end of that script rather than trying to run it through cron. It won't consume as many resources in the background while you're doing other work and it will look for more PDF files to move as soon as you're ready to work on one. (Of course, if you do it this way and you get ahead of the "front-of-house staff", you'll have to run get_pdf manually until they catch up to you.)

Last edited by Don Cragun; 01-19-2013 at 11:22 PM.. Reason: quote out of place
This User Gave Thanks to Don Cragun For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to append timestamp in the filenames using find?

Hi, How to change the filenames with timestamp in sub folders I have the following code to select the records. find . -type f -name '*pqr*' -ctime 1 -print The following is the example app_root_dir="/`echo $ScriptDir | cut -d'/' -f2`" $app_root_dir/../BadFiles directory uvw.bad... (3 Replies)
Discussion started by: bobbygsk
3 Replies

2. UNIX for Dummies Questions & Answers

Find the list of filenames that have the string 31 at 4th and 5th position

Hi, Can anyone let me know the command to know the list of filenames that have string 31 in their 4th and 5th positions inside the file: grep -l "31" main*.txt The above grep lists all the files which have 31 at any position but I want filenames having 31 at position 4 and position 5. (8 Replies)
Discussion started by: okkadu
8 Replies

3. UNIX for Dummies Questions & Answers

find & remove characters in filenames

I have a group of files in different directories with characters such as " ? : in the file names. How do I find these files and remove these characters on mass? Thanks (19 Replies)
Discussion started by: barrydocks
19 Replies

4. UNIX for Advanced & Expert Users

replace word with special charaters

I have input file called file1 with characters that have \\ in it. I cannot change input file, because it is generated earlier in script. Now would like to replace string on line in file called bfile with output from file1 I have been using sed command. $cat file1 pc//6sPxp== $ cat scr1... (4 Replies)
Discussion started by: drtabc
4 Replies

5. Shell Programming and Scripting

awk help with find command and filenames with spaces

I have the following code: find /usr/local/test5 -type f -mtime +30 -exec ls -l {} \; | awk '{print $5, $6, $7, $8, $9}' I have this as output: 14 Aug 12 00:00 /usr/local/test5/file1 14 Aug 12 00:00 /usr/local/test5/lastname, The bolded part is where I run into trouble. The actual... (4 Replies)
Discussion started by: streetfighter2
4 Replies

6. Shell Programming and Scripting

Find duplicate filenames and remove in different mount point

Hi Gurus, Do any kind souls encounter have the same script as mentioned here. Find and compare filenames in different mount point and remove duplicates. Thanks a million!!! wanna13e (7 Replies)
Discussion started by: wanna13e
7 Replies

7. Shell Programming and Scripting

Help with Find/Replace Javascript Injected Strings in mulitple files

Hi, guys, I'm not a high-end programmer, but I've been trying to write a script to remove all of the b.rtbn2.cn (and b.adserv.cn and any future variation) injected script tags on the server. (Still working on security fixes to prevent it in the future, just need to clean up now.) My approach is... (1 Reply)
Discussion started by: zzlegs
1 Replies

8. Solaris

feeding filenames to find command

Hi, I am having set of files whose names are stored in a file say "filelist.txt" Now, I want to find all files contained in "filelist.txt" from my parent directory. Is there any way to let find command understand "filelist.txt" just like we have option -f in awk. I donot want to run a... (4 Replies)
Discussion started by: sanjay1979
4 Replies

9. Shell Programming and Scripting

find filenames like unix commands

Hi, I need to write a small script to search in some specific directories to check if any file is present with a unix command name... Means if the directory contains any files like cat, vi, grep, find etc i need to list those files into a file. Please help Thanks, D (6 Replies)
Discussion started by: deepakgang
6 Replies

10. UNIX for Dummies Questions & Answers

find lowercase filenames

How do I write the command to find all files with any lower case letters in the filename? I have tried find . -name *\(a-z\) and a lot of combinations like that, without success. thanks JP:confused: (4 Replies)
Discussion started by: jpprial
4 Replies
Login or Register to Ask a Question