regex help
Linux for blind general discussion
blinux-list at redhat.com
Wed Feb 9 19:43:36 UTC 2022
Tim here, replying inline again
> so instead of typing something like:
>
> mv longTitle:longSubtitle.pdf longTitle.pdf
>
> when I want to truncate an overly long filename to something more
> manageable, I could instead type
>
> mv longTitle{:longSubtitle,}.pdf
You've got it! It expands the filenames at the shell level, before
handing it off to the program, so it works with multiple items as
well, such as
$ mkdir -p Mail/work/{cur,new,tmp}
which expands to the same as
$ mkdir -p Mail/work/cur Mail/work/new Mail/work/tmp
Or, if you have a select a subset of items out of many, you might copy
them like
$ cp IMG_{1002,1039,1053}.JPG wedding_pictures/
I use it all the time. Welcome to the club. (grins)
> Also, some of those examples are starting to remind me of sed
> commands... if I copy a bash script that's just the shebang line an
> a bunch of sed commands and edit the copy to replace sed with
> rename, is the resulting script likely to do to file names what the
> original scrip does for the contents of text files? Or is my
> brain's pattern matching throwing a false positive?
Not quite. But you can come close by creating your sed command(s)
like
ls | sed "s/.*/mv '&' '&'/" > modifications.sh
this will create a file containing a whole bunch of mv(1) commands
that are null-operations. You can then go edit the second parameter
to mv, changing the name to the ones you want, and deleting the mv
commands you don't want, then run the whole thing with
sh modifications.sh
If you're nervous about doing such bulk changes, possibly deleting or
renaming lots of files, you can use "echo rm" in the sed command
ls | sed "s/.*/echo mv '&' '&'/" > check_me.sh
and do your modifications/renames in there and run them
sh check_me.sh
if it looks like it will be running the right commands, you can then
either modify the file
sed 's/^echo *//' check_me.sh > for_real.sh
and run the results:
sh for_real.sh
or you can run them directly without the intermediate file:
sed 's/^echo *//' check_me.sh | sh
All sorts of fun that can be had here. (grins)
Hope it helps,
-Tim
More information about the Blinux-list
mailing list