I have a directory full of files and sub-directories which I've pulled from an emulator. But it has extra filename extensions on it. I would like to traverse this directory, stripping off the extensions and so I've been trying to write a script to do it but it's failing.
An example of a file is this :- lamborghini miura_1024.jpg,c85 and so there, I'd like to remove the ,c85. But a bigger problem is where I have the odd file which has a numeric extensions such as .;1 and that's where I struggle. How do I do a substitution in my script to cope with things like that?
I've been reading things like this - http://www.cyberciti.biz/tips/renaming-multiple-files-at-a-shell-prompt.html and http://www.unix.com/answers-frequently-asked-questions/13774-unix-tutorials-... but I'm still struggling.
Can somebody point me in the right direction please, even if it's towards the door ;-)
On 24 Nov 14:43, Chris Walker wrote:
I have a directory full of files and sub-directories which I've pulled from an emulator. But it has extra filename extensions on it. I would like to traverse this directory, stripping off the extensions and so I've been trying to write a script to do it but it's failing.
An example of a file is this :- lamborghini miura_1024.jpg,c85 and so there, I'd like to remove the ,c85. But a bigger problem is where I have the odd file which has a numeric extensions such as .;1 and that's where I struggle. How do I do a substitution in my script to cope with things like that?
I've been reading things like this - http://www.cyberciti.biz/tips/renaming-multiple-files-at-a-shell-prompt.html and http://www.unix.com/answers-frequently-asked-questions/13774-unix-tutorials-... but I'm still struggling.
Can somebody point me in the right direction please, even if it's towards the door ;-)
You'll want to be typing "man rename", rename is in the main perl package, so it's very likely you've got it installed (assuming debian or ubuntu, as I usually do!).
Cheers,
On 24/11/11 15:26, Brett Parker wrote:
On 24 Nov 14:43, Chris Walker wrote:
I have a directory full of files and sub-directories which I've pulled from an emulator. But it has extra filename extensions on it. I would like to traverse this directory, stripping off the extensions and so I've been trying to write a script to do it but it's failing.
You'll want to be typing "man rename", rename is in the main perl package, so it's very likely you've got it installed (assuming debian or ubuntu, as I usually do!).
Been there, done that, fallen at the first hurdle.
I'm running Mandriva 2011.0, not that makes any difference.
If I run this :- rename -n “s/.jpg/.;1/” *
It fails with bash: 1/”: No such file or directory
I'll have a try at Mark's suggestion now.
On Thu, 24 Nov 2011, Chris Walker wrote:
Been there, done that, fallen at the first hurdle.
I'm running Mandriva 2011.0, not that makes any difference.
If I run this :- rename -n “s/.jpg/.;1/” *
This probably means you've got some other executable called "rename" getting in the way of you running the perl rename command. Sometimes the executable for the latter is called "prename" instead of "rename", so you'd need
prename -n “s/.jpg/.;1/” *
On 24 Nov 15:41, Chris Walker wrote:
On 24/11/11 15:26, Brett Parker wrote:
On 24 Nov 14:43, Chris Walker wrote:
I have a directory full of files and sub-directories which I've pulled from an emulator. But it has extra filename extensions on it. I would like to traverse this directory, stripping off the extensions and so I've been trying to write a script to do it but it's failing.
You'll want to be typing "man rename", rename is in the main perl package, so it's very likely you've got it installed (assuming debian or ubuntu, as I usually do!).
Been there, done that, fallen at the first hurdle.
I'm running Mandriva 2011.0, not that makes any difference.
If I run this :- rename -n “s/.jpg/.;1/” *
It fails with bash: 1/”: No such file or directory
try: rename -n 's#.;1##;' *.jpg*
On 24/11/11 15:41, Chris Walker wrote:
If I run this :- rename -n “s/.jpg/.;1/” *
Actually rename will do it: rename -n "s/.jpg.;1$/.jpg/" *
or more generally: rename -n "s/.jpg..*$/.jpg/" *
I'll have a try at Mark's suggestion now.
.. which was rather long winded by comparison :-)
On 24/11/11 14:43, Chris Walker wrote:
An example of a file is this :- lamborghini miura_1024.jpg,c85 and so there, I'd like to remove the ,c85. But a bigger problem is where I have the odd file which has a numeric extensions such as .;1 and that's where I struggle. How do I do a substitution in my script to cope with things like that?
I came up with something like this (please only work on a backup set of images!)
Use "find" to find files matching <something>.jpg.<something>: $ find . -name '*.jpg.*' ./x y.jpg.;1
For each one, create the command mv "<something>.jpg.<something>" "<something>.jpg" $ find . -name '*.jpg.*' | sed -r 's/^(.*.jpg)..*$/mv "\0" "\1"/' mv "./x y.jpg.;1" "./x y.jpg"
Pipe the commands through Bash to run them. $ find . -name '*.jpg.*' | sed -r 's/^(.*.jpg)..*$/mv "\0" "\1"/' | bash
If in doubt, just use it to create a script: $ find . -name '*.jpg.*' | sed -r 's/^(.*.jpg)..*$/mv "\0" "\1"/' > myscript.sh .. and then sanity check the script before running it.
On 24/11/11 15:31, Mark Rogers wrote:
On 24/11/11 14:43, Chris Walker wrote:
An example of a file is this :- lamborghini miura_1024.jpg,c85 and so there, I'd like to remove the ,c85. But a bigger problem is where I have the odd file which has a numeric extensions such as .;1 and that's where I struggle. How do I do a substitution in my script to cope with things like that?
I came up with something like this (please only work on a backup set of images!)
Use "find" to find files matching <something>.jpg.<something>: $ find . -name '*.jpg.*' ./x y.jpg.;1
For each one, create the command mv "<something>.jpg.<something>" "<something>.jpg" $ find . -name '*.jpg.*' | sed -r 's/^(.*.jpg)..*$/mv "\0" "\1"/' mv "./x y.jpg.;1" "./x y.jpg"
Pipe the commands through Bash to run them. $ find . -name '*.jpg.*' | sed -r 's/^(.*.jpg)..*$/mv "\0" "\1"/' | bash
If in doubt, just use it to create a script: $ find . -name '*.jpg.*' | sed -r 's/^(.*.jpg)..*$/mv "\0" "\1"/' > myscript.sh .. and then sanity check the script before running it.
Fallen at the second hurdle!
I modified the find command slightly (see below)
find . -name '*.jpg;1' | sed 's/^(.*.jpg)..*$/mv "\0" "\1"/' sed: -e expression #1, char 31: invalid reference \1 on `s' command's RHS
The file in that directory is cover.jpg;1 and not jpg.;1 as I said above. Sorry if that's complicated things.
I've also just tried Brett's suggestion and that didn't return any errors but didn't do anything either :-(
The file I'm trying it with is a piccy I've just grabbed from my own folder as a test. That's test.jpg;1 but whatever I do, makes no difference.
For some reason, Thunderbird has decided to take ages connecting to my smtp server and hence replies are a bit delayed. That seems to be a consequence of doing the upgrade from Mandriva 2010 to 2011.
On 24/11/11 16:18, Chris Walker wrote:
Fallen at the second hurdle!
I modified the find command slightly (see below)
find . -name '*.jpg;1' | sed 's/^(.*.jpg)..*$/mv "\0" "\1"/' sed: -e expression #1, char 31: invalid reference \1 on `s' command's RHS
Try: find . -name '*.jpg;1' | sed 's/^(.*.jpg).*$/mv "\0" "\1"/'
The "." after the "(.*.jpg)" was specifying the dot in the pattern to match, which was therefore failing to find a match, therefore \1 had nothing to refer to.
Similarly, changing: rename -n "s/.jpg..*$/.jpg/" * to rename -n "s/.jpg.*$/.jpg/" *
.. should also work.
[I always try to make sure that the search pattern is as accurate as possible to avoid false positives, although the patter above is still likely to be specific enough for your needs.]
Note I originally said: rename -n "s/.jpg..*$/.jpg/" * which should have been rename -n "s/.jpg..*$/.jpg/" * where the first matched any character which was followed by jpg, where the latter only matches a dot followed by jpg. Again, it's all about trying to make it avoid false positives.
Mark
On 24/11/11 16:44, Mark Rogers wrote:
On 24/11/11 16:18, Chris Walker wrote:
Fallen at the second hurdle!
Third, or fourth now?
Note I originally said: rename -n "s/.jpg..*$/.jpg/" * which should have been rename -n "s/.jpg..*$/.jpg/" * where the first matched any character which was followed by jpg, where the latter only matches a dot followed by jpg. Again, it's all about trying to make it avoid false positives.
This is still failing with this message :- mv: cannot stat `(3439)/test.jpg;1': No such file or directory
I *think* it's because there are spaces in the directory names. I've been searching all morning for ways round that but without success so far.
Any help appreciated.
On 25 Nov 14:33, Chris Walker wrote:
On 24/11/11 16:44, Mark Rogers wrote:
On 24/11/11 16:18, Chris Walker wrote:
Fallen at the second hurdle!
Third, or fourth now?
Note I originally said: rename -n "s/.jpg..*$/.jpg/" * which should have been rename -n "s/.jpg..*$/.jpg/" * where the first matched any character which was followed by jpg, where the latter only matches a dot followed by jpg. Again, it's all about trying to make it avoid false positives.
This is still failing with this message :- mv: cannot stat `(3439)/test.jpg;1': No such file or directory
I *think* it's because there are spaces in the directory names. I've been searching all morning for ways round that but without success so far.
is that using rename or using find with a mv?
rename isn't recursive by default so I suggest doing: find . -type f -name '*.jpg.*' -execdir rename 's/.jpg.*$/.jpg/' {} +
Note: if there are 2 files with the same name except the bit after the .jpg, then the second will *not* be renamed and it'll warn you.
I've just tested that on a local set of files where the files and the directories both have spaces in them.
Cheers,
On 25/11/11 15:14, Brett Parker wrote:
On 25 Nov 14:33, Chris Walker wrote:
Third, or fourth now?
is that using rename or using find with a mv?
that's using find with mv.
rename isn't recursive by default so I suggest doing: find . -type f -name '*.jpg.*' -execdir rename 's/.jpg.*$/.jpg/' {} +
Note: if there are 2 files with the same name except the bit after the .jpg, then the second will *not* be renamed and it'll warn you.
I've just tested that on a local set of files where the files and the directories both have spaces in them.
Some files here have spaces in the name and so do virtually all the directories. I've run that command with slight modifications (I'll explain that) and although it runs and doesn't exhibit any errors, it also doesn't rename the files.
The modifications are because the files are not named test.jpg.;1, they're named test.jpg;1 so I changed the command to find . -type f -name 'test.jpg;1' -execdir rename 's/.jpg;1$/.jpg/' {} +
I then modified it some more so that it then looked like this find . -type f -name 'test.jpg;1' -execdir rename 's/\test.jpg;1$/test.jpg/' {} +
But that also fails to do anything. Is there any other way for me to find out why it's failing? Can I run bits of the command and then add on bits to refine the testing?
Can I thank you for being so patient.
On 25 Nov 15:36, Chris Walker wrote:
On 25/11/11 15:14, Brett Parker wrote:
On 25 Nov 14:33, Chris Walker wrote:
Third, or fourth now?
is that using rename or using find with a mv?
that's using find with mv.
rename isn't recursive by default so I suggest doing: find . -type f -name '*.jpg.*' -execdir rename 's/.jpg.*$/.jpg/' {} +
Note: if there are 2 files with the same name except the bit after the .jpg, then the second will *not* be renamed and it'll warn you.
I've just tested that on a local set of files where the files and the directories both have spaces in them.
Some files here have spaces in the name and so do virtually all the directories. I've run that command with slight modifications (I'll explain that) and although it runs and doesn't exhibit any errors, it also doesn't rename the files.
The modifications are because the files are not named test.jpg.;1, they're named test.jpg;1 so I changed the command to find . -type f -name 'test.jpg;1' -execdir rename 's/.jpg;1$/.jpg/' {} +
I then modified it some more so that it then looked like this find . -type f -name 'test.jpg;1' -execdir rename 's/\test.jpg;1$/test.jpg/' {} +
But that also fails to do anything. Is there any other way for me to find out why it's failing? Can I run bits of the command and then add on bits to refine the testing?
Can I thank you for being so patient.
OK - so, here we go...
$ find . . ./d space ./d space/c space .jpg;1 ./d space/d space .jpg;1 ./c space ./c space/b space .jpg;1 ./c space/a space .jpg;1 $ find . -type f -name '*.jpg*' -execdir rename 's/.jpg.*$/.jpg/' {} + $ find . . ./d space ./d space/c space .jpg ./d space/d space .jpg ./c space ./c space/b space .jpg ./c space/a space .jpg $
Now, that looks to work to me... but maybe I'm missing something.
On 25/11/11 16:47, Brett Parker wrote:
OK - so, here we go...
$ find . . ./d space ./d space/c space .jpg;1 ./d space/d space .jpg;1 ./c space ./c space/b space .jpg;1 ./c space/a space .jpg;1 $ find . -type f -name '*.jpg*' -execdir rename 's/.jpg.*$/.jpg/' {} + $ find . . ./d space ./d space/c space .jpg ./d space/d space .jpg ./c space ./c space/b space .jpg ./c space/a space .jpg $
Now, that looks to work to me... but maybe I'm missing something.
I don't think you are but plainly I am!
I've created directories and sub-directories as you did, viz:- find . . ./sub-directory 1 ./sub-directory 1/metadata.opf;1 ./sub-directory 1/Infamous - Ace Atkins.epub;1 ./sub-directory 1/cover.jpg;1
I then ran the command line script as per the above because I did a cut and paste. Here's the result :- find . -type f -name '*.jpg*' -execdir rename 's/.jpg.*$/.jpg/' {} + [chris@localhost directory 1]$ find . . ./sub-directory 1 ./sub-directory 1/metadata.opf;1 ./sub-directory 1/Infamous - Ace Atkins.epub;1 ./sub-directory 1/cover.jpg;1
I'm going to give up with this as it's really beginning to annoy me. But in time I will take another look at it and see if I can discover why the wretched thing is not working.
Can I give a very public thank you to Brett for persevering for so long. I fear I've tested his patience!
On 26/11/11 22:44, Chris Walker wrote:
On 25/11/11 16:47, Brett Parker wrote:
OK - so, here we go...
$ find . . ./d space ./d space/c space .jpg;1 ./d space/d space .jpg;1 ./c space ./c space/b space .jpg;1 ./c space/a space .jpg;1 $ find . -type f -name '*.jpg*' -execdir rename 's/.jpg.*$/.jpg/' {} + $ find . . ./d space ./d space/c space .jpg ./d space/d space .jpg ./c space ./c space/b space .jpg ./c space/a space .jpg $
Now, that looks to work to me... but maybe I'm missing something.
I don't think you are but plainly I am!
I found what the problem was. As the files were copied from a DVD, all the folders were read-only. I spotted it while browsing using a Windows box!
I'm going to give up with this as it's really beginning to annoy me. But in time I will take another look at it and see if I can discover why the wretched thing is not working.
As I said, I did give it another go and having changed all the folders by removing the read-only attribute, it works - as you said it would :-)
On 24/11/11 14:43, Chris Walker wrote:
An example of a file is this :- lamborghini miura_1024.jpg,c85 and so there, I'd like to remove the ,c85. But a bigger problem is where I have the odd file which has a numeric extensions such as .;1 and that's where I struggle. How do I do a substitution in my script to cope with things like that?
I came up with something like this (please only work on a backup set of images!)
Use "find" to find files matching <something>.jpg.<something>: $ find . -name '*.jpg.*' ./x y.jpg.;1
For each one, create the command mv "<something>.jpg.<something>" "<something>.jpg" $ find . -name '*.jpg.*' | sed -r 's/^(.*.jpg)..*$/mv "\0" "\1"/' mv "./x y.jpg.;1" "./x y.jpg"
Pipe the commands through Bash to run them. $ find . -name '*.jpg.*' | sed -r 's/^(.*.jpg)..*$/mv "\0" "\1"/' | bash
If in doubt, just use it to create a script: $ find . -name '*.jpg.*' | sed -r 's/^(.*.jpg)..*$/mv "\0" "\1"/' > myscript.sh .. and then sanity check the script before running it.