I have a folder on my desktop called "Images", inside that folder are 3 images...
File-A-B.gif
File-C-D.gif
File-E-F.gif
...I want to rename the files on the command line so the second hyphen/dash is removed. So the result would be files named like so...
File-AB.gif
File-CD.gif
File-EF.gif
...I'm using a combination of commands piped through to each other but not having much luck in getting them to work.
Initially I had ls File-* | sed 's/\(File-[^-]\)-\(.+\)/mv & \1\2/' | sh
The break-down of this command is...
File-
sed
sed
to capture different parts of the nameFile-A
and the B.gif
sed
we then replace the matched file name with a move command and effectively tell it to rename the original file to a file made up of the two pieces of data we've capturedsh
The initial error I was getting was a permissions error. I'm not sure why as I had 'read and write' access to the files.
To work around this I chmod 777
each of the files.
This then resulted in a new error which was cannot execute binary file
.
To work around that error I saw this post: http://blog.mpdaugherty.com/2010/05/27/difference-with-sed-in-place-editing-on-mac-os-x-vs-linux/ which suggests adding an extra pair of single quotes. The reason this works (apparently) is because sed
will error because it doesn't know what file extension to use, but putting an empty String neutralises that error (on a side note: my understanding was that you also need to use the -i
flag - which I'm not using - but doing that caused a different error so I scrapped that and just used the single quotes to avoid the binary file error I was getting).
So now I have ls File-* | sed '' 's/\(File-[^-]\)-\(.+\)/mv & \1\2/' | sh
But this errors with No such file or directory
?
I tried to be explicit with the location of the file, so for example...
ls File-* | sed '' 's/\(File-[^-]\)-\(.+\)/mv ~\/Desktop\/Images\/& ~/Desktop\/Images\/\1\2/' | sh
...notice I've had to escape the forward slashes ~\/Desktop\/Images\/
but this still didn't work.
I also installed the GNU version of sed
using Homebrew but it also errored in a similar way with gsed: can't read s/\(Vim-[^-]\)-\(.+\)/mv & \1\2/: No such file or directory
I'm a bit at a loss with how I can get this to work.
I don't want to have to resort to writing a bash/zsh script or a Ruby script.
Any help you can give me would be greatly appreciated.
If you're using zsh
, you don't need to use sed
: there's a zsh module called zmv
that'll achieve this pretty simply:
$ ls -F -G
File-A-B.gif File-C-D.gif File-E-F.gif
$ zmv -n 'File-(*)-(*).gif' 'File-${1}${2}.gif'
mv -- File-A-B.gif File-AB.gif
mv -- File-C-D.gif File-CD.gif
mv -- File-E-F.gif File-EF.gif
Note: Omit -n
to get it to actually run.
It works by matching things inside brackets. In this case: File-(*)-(*).gif
will both match File-A-B.gif
and also save A
and B
so we can refer to them later. Then, we move the file we've just matched to a new filename, omitting the hyphen between the two letters and inserting the letters using references: File-${1}${2}.gif
.
It's quite a powerful little module, provided you can give it the two regexes: one to match files to rename, and the second to match the renamed-file name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With