Re: The rename command…
James Gray
james at gray.net.au
Sat Aug 30 01:48:50 UTC 2008
On 26/08/2008, at 12:00 AM, Johnny Rosenberg wrote:
> Since there is a serious bug in Nautilus, that adds the very
> unnecessary text "Link to " to a link created with Ctrl+Shift
> +Drag&Drop, I searched the Internet for a way to remove that
> ridiculous part of the file name, and I came up with this:
> rename -v 's/Länk till //' *
>
> Or, if your language is English:
>
> rename -v 's/Link to //' *
>
> However, there doesn't seem to be an option for recursivity built in
> to that command. I am not good with scripts, but I am sure there is
> a way around that.
>
> What could I add to this command to also make it work in all the
> subfolders of the current folder?
>
Hrm, I'm not a big fan of all these new alternatives like "rename"
when ye olde "mv" is just as good if used with some lateral thought.
Also, piping a string through sed/awk just to get a substring when
isn't always the most efficient way of completing a task. So as a
footnote to this discussion I offer the following:
#!/bin/bash
# Needs to be "bash" AFAIK...I'm going to be using bash-isms ;)
find . -type f -name "Link to*" -print0 | while read LINE
do
FILE="$(basename $LINE)"
DIR="$(dirname $LINE)"
mv "$LINE" "$DIR/${FILE:8}"
done
Now I know this spawns metric truck load of processes (ok, 3 per
file), BUT it will recurse from the current working directory and
handle all files beginning with "Link to" even if the directory tree
has spaces or weird characters. You could also put it into a one-
liner and replace the declarations for $FILE and $DIR...but I'll leave
that as an exercise for the reader ;)
The real "magic" is using the bash substring notation to rip the "Link
to " string off the front of the $FILE that is found - don't fear the
shell...it's faster at that stuff than sed/awk!! "man bash" and
search for "substring".
Whilst this exercise probably isn't that practical for the OP's
original purpose, it can be adapted for any recursive file/dir
operation you may want to do - just add to or replace the "mv ..."
line with something else (maybe chmod/chown...etc) and you can
probably start to see the power of being able to poke around with the
directory and file name components independently.
Anyway, it's Saturday, my day off, and my coffee is getting cold.
Peace,
James
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2417 bytes
Desc: not available
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20080830/6f8c50f5/attachment.bin>
More information about the ubuntu-users
mailing list