You are not logged in.
Is there a way to quickly reference a previous argument from the command I'm currently typing? Let's say, for example, that I want to rename a file to simply add another extension to it?
mv dir/subdir/filename.ext dir/subdir/filename.ext.old
I'm thinking something like the history expansion that lets you reference an argument from the previous command.
ls dir/subdir/filename.ext
mv !$ !$.old
Except, I would like to reference the command I'm currently typing. In this example, #$% should be some designator referencing the first argument of the command in progress.
mv dir/subdir/filename.ext #$%.old
I'm aware of find dir/subdir/filename.ext -exec mv {} {}.old \;, but that's tedious for individual files. I also know this is scriptable, but I prefer to look for "built-in" solutions first.
The usefulness of this is not limited to mv, so I'm really seeking a generic solution that is not tied to mv or any other specific command.
Last edited by matthew02 (2023-03-23 20:59:55)
Offline
This would be shell specific. But for bash, and likely for other big modern shells (eg., zsh), there is brace-expansion:
mv dir/subdir/filename.ext{,.old}
Or you could just use a variable:
f=dir/subdir/filename.ext mv $f $f.old
Last edited by Trilby (2023-03-23 20:47:55)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
That's exactly what I was looking for. I was pretty confident there was a way to do it, but I was having trouble finding the solution. I definitely appreciate shell expansions, especially the more I learn (there seems to be quite a bit). Thank you for the help!
Edit: The variable method is also a nice trick. I can see myself putting that to good use, too.
Last edited by matthew02 (2023-03-23 20:58:09)
Offline
For renaming/moving/copying, you can use the renameutils package.
Instead of usual mv/cp, you call imv/icp with the source path, and then interactively modify it (e.g. append ".old" suffix) to produce destination path.
It's not a completely generic solution, but for file operations it's very quick and convenient.
Offline
I use vi-editing mode in bash so traditionally I would yank and put that last argument again. Nowadays, e.g. for your specific example, I use edir. Just `edir dir/subdir` and then append the extra extension to that file name (and however many others) via your favorite editor. Get it from the AUR.
Disclaimer: I am the author of edir and now use it a few times per day. Don't know how the rest of you are surviving without it!
Offline
If you're already looking at history expansion, it doesn't seem that easy to miss in the man pages (bash/zsh at least):
mv dir/subdir/filename.ext !#$.old
Offline
If you're already looking at history expansion, it doesn't seem that easy to miss in the man pages (bash/zsh at least):
mv dir/subdir/filename.ext !#$.old
Yep. Looking back at it, I also don't see how I missed it. That's even more exactly what I was looking for. It's nice because I can grab any earlier argument. Thanks!
Last edited by matthew02 (2023-03-24 22:02:34)
Offline
For renaming/moving/copying, you can use the renameutils package.
Instead of usual mv/cp, you call imv/icp with the source path, and then interactively modify it (e.g. append ".old" suffix) to produce destination path.
It's not a completely generic solution, but for file operations it's very quick and convenient.
Okay, I'm going to have to check that out. Those seem like some really handy tools. Thanks!
Offline
I use vi-editing mode in bash so traditionally I would yank and put that last argument again. Nowadays, e.g. for your specific example, I use edir. Just `edir dir/subdir` and then append the extra extension to that file name (and however many others) via your favorite editor. Get it from the AUR.
Disclaimer: I am the author of edir and now use it a few times per day. Don't know how the rest of you are surviving without it!
I'm still getting the hang of vi-mode in ZSH. It sometimes seems to not work how I expect, but I'm probably just not trying hard enough. I tend to work around annoyances for a while before I get tired and actually address them.
I'll check out `edir`. Thanks for the tip!
Offline