TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Renaming files with mv without typing the name two times

669 pointsby premekabout 5 years ago

39 comments

kelnosabout 5 years ago
Something like this isn&#x27;t really necessary. I do something like this (bash) all the time when I want to change a part of a filename (renames &quot;foo-bar-baz.txt&quot; to &quot;foo-bar-quux.txt&quot;):<p><pre><code> mv foo-bar-{baz,quux}.txt </code></pre> You can have an &#x27;empty&#x27; bit to add or remove something from the name (renames &quot;foo-bar.txt&quot; to &quot;foo-bar-baz.txt&quot;):<p><pre><code> mv foo-bar{,-baz}.txt </code></pre> That will work with pathname parts as well (as in the linked demo video) if you include them in the command.<p>I guess the linked script is useful if you need to do some complex edits to the filename, since you can&#x27;t usefully have more than one curly-brace-group for this use case. But in that case honestly I&#x27;m fine just double-clicking the first argument to select, and then middle-clicking to paste, and then using the arrow keys to edit.
评论 #22861657 未加载
评论 #22861057 未加载
评论 #22860305 未加载
评论 #22861290 未加载
评论 #22863526 未加载
评论 #22861563 未加载
评论 #22861120 未加载
评论 #22865909 未加载
评论 #22860233 未加载
评论 #22871814 未加载
评论 #22862022 未加载
评论 #22862162 未加载
评论 #22864656 未加载
评论 #22863192 未加载
评论 #22861735 未加载
kazinatorabout 5 years ago
To make two copies of the last argument, so you can edit one of them in place, in Bash (or anything that uses GNU Readlne), simply do this:<p><pre><code> $ mv oldname _ # _ is your cursor </code></pre> Now type Ctrl-W to erase the last word:<p><pre><code> $ mv _ </code></pre> Then type Ctrl-Y twice to yank it:<p><pre><code> $ mv oldname oldname _ </code></pre> Now edit in place as needed.<p>No utility needed, and just a regular mv command is issued whose inputs are completely recorded in the history.<p>Now,let&#x27;s automate that. Add this line to ~&#x2F;.inputrc:<p><pre><code> # ~&#x2F;.inputrc entry \C-T: &quot; \C-W\C-Y\C-Y\b&quot; </code></pre> Reload with<p><pre><code> $ bind -f ~&#x2F;.inputrc </code></pre> Now Ctrl-T does it:<p><pre><code> $ mv oldname_ </code></pre> Hit Ctrl-T<p><pre><code> $ mv oldname oldname_ </code></pre> How about having Ctrl-T end up on the first character of the name?<p><pre><code> # ~&#x2F;.inputrc entry \C-T: &quot; \C-W\C-Y\C-Y\b\eb&quot;</code></pre>
评论 #22863179 未加载
评论 #22862629 未加载
评论 #22863095 未加载
评论 #22864151 未加载
评论 #22862171 未加载
评论 #22864942 未加载
jacobsenscottabout 5 years ago
All the &quot;something like this isn&#x27;t really necessary, just memorize all the esoteric bash globbing rules&quot; posts are so far off, given the solutions in all those posts aren&#x27;t really necessary either. It is nice not to need to &quot;pre-think&quot; how you are going to type your `mv` command. You just start typing it, and when you realize it will be a bit complex you just hit enter and you have an editor.
评论 #22864559 未加载
评论 #22863104 未加载
barrkelabout 5 years ago
Any time renaming gets more complicated than (a) bash curly braces or (b) rename command (Perl script that applies sed expression to each file name), I break out Emacs wdired.<p>Then you can use all the Emacs tools to perform a mass edit across multiple file names, and get them right before committing to the rename. Multiple cursors makes a great addition, as does iedit-mode.
评论 #22860426 未加载
评论 #22860823 未加载
评论 #22860760 未加载
JoshTriplettabout 5 years ago
You can also hit Ctrl-x Ctrl-e to edit the command line in your preferred text editor, and run it when saved and closed in the editor. So you can type the mv, tab-complete the current filename, and use the editor for the new filename.
评论 #22862060 未加载
评论 #22866194 未加载
评论 #22863286 未加载
评论 #22861110 未加载
bb88about 5 years ago
Graybeard here (never thought I&#x27;d say that). 25 years of unix&#x2F;linux in production systems.<p>Changing the default behavior of a posix command is a footgun.<p>If I wanted to get help from mv:<p><pre><code> $ mv --help Usage: file [OPTION...] [FILE...] Determine type of FILEs. </code></pre> I get the help for the file command.
评论 #22864012 未加载
评论 #22867381 未加载
评论 #22863783 未加载
test1235about 5 years ago
I can&#x27;t remember where it was I read (saw this years ago), but the way to actually get help online isn&#x27;t to ask a question &#x27;cos nobody responds ... you have to propose an incorrect solution where suddenly everyone jumps up to correct you with the answer you&#x27;re looking for.
评论 #22863943 未加载
lopsidedBrainabout 5 years ago
My trick has always been to rely on tab auto-completion.<p><pre><code> mv foo-&lt;tab&gt; mv foo-bar-baz foo-&lt;tab&gt; mv foo-bar-baz foo-bar-baz </code></pre> Now I can edit the second part pretty quickly.<p>Downside: you have to at least type `foo-` twice.<p>Upside: command line history still has the full command.
评论 #22861027 未加载
评论 #22860783 未加载
评论 #22861253 未加载
评论 #22860628 未加载
kemitchellabout 5 years ago
`qmv`, `imv`, `qcp`, `icp`, and friends from the `renameutils` package come in handy. I use `qmv -f do ...` with `EDITOR=vim` quite a bit.
评论 #22864440 未加载
Symbioteabout 5 years ago
Zsh users can instead add this to their .zshrc:<p><pre><code> autoload copy-earlier-word zle -N copy-earlier-word bindkey &#x27;^[,&#x27; copy-earlier-word </code></pre> Then it&#x27;s the default Alt-dot to copy the final argument of the previous command, and Alt-comma to copy the final argument of the current command. The move command is then &quot;mv filename &lt;Alt-Comma&gt;&quot;.<p>Also, given this:<p><pre><code> echo 1 2 3 echo 4 5 6 echo 7 8 9 </code></pre> Then on the next command, Alt-dot will copy&#x2F;replace 9→6→3. Pressing Alt-comma after Alt-dot will replace that with 8→7→echo.
评论 #22861830 未加载
l8againabout 5 years ago
This is one of those times when I just love coming to hacker news. Seemingly trivial quality of life improvement makes it to the top and the comments section is lit up with several other cool alternatives.
BiteCode_devabout 5 years ago
I think it&#x27;s very nice, and should be the default behavior. Ergonomics of GNU tooling is lacking to say the least. That&#x27;s why we love fdfind and ripgrep and love find&#x2F;grep way less.<p>I would use it if it were the default behavior, but the problem is already solved by the &quot;moreutils&quot; package, which I install on all my machines. This lets you do:<p><pre><code> vidir filename </code></pre> or<p><pre><code> vidir directory # default to . </code></pre> And it will open your $EDITOR with one file name per line. You edit it in the comfort of your favorite editor, and it batch renames for you, or rename the single file for the first case.<p>Note that if you use vscode, $EDITOR should be &quot;code -w&quot;, not just &quot;code&quot;.
babyabout 5 years ago
For things like this, I don’t understand why we don’t have more interactive CLIs, instead of CLIs that expect one line of a bunch of arguments.
评论 #22863655 未加载
gitgudabout 5 years ago
If you rename the &#x27;mv&#x27; command, won&#x27;t this break a lot of things relying on it?
评论 #22861365 未加载
评论 #22861187 未加载
reality101about 5 years ago
type file then ctrl-a mv then ctrl-k ctrl-y space ctrl-y change file name enter.
评论 #22860905 未加载
zwischenzugabout 5 years ago
I posted about some &#x27;God-like&#x27; bash shortcuts here:<p><a href="https:&#x2F;&#x2F;zwischenzugs.com&#x2F;2019&#x2F;08&#x2F;25&#x2F;seven-god-like-bash-history-shortcuts-you-will-actually-use&#x2F;" rel="nofollow">https:&#x2F;&#x2F;zwischenzugs.com&#x2F;2019&#x2F;08&#x2F;25&#x2F;seven-god-like-bash-hist...</a><p>Number 6 was a &#x27;refer to current line&#x27; one.<p>There are so many ways to do these things that it&#x27;s hard to get them all under your fingers though. Most of the time I tab my way through the &#x27;problem&#x27; anyway.
Sierra-Samabout 5 years ago
Here is my take on part of the problem.<p><pre><code> https:&#x2F;&#x2F;github.com&#x2F;Guy-Shaw&#x2F;libmmv https:&#x2F;&#x2F;github.com&#x2F;Guy-Shaw&#x2F;pmmv </code></pre> The original perl-rename has the expressive power, but not the safety features. mmv has been around since 1990 and, in my opinion, has always been under-appreciated, but it could use some modernization.
ara24about 5 years ago
As has already been said, bash brace expansion is quite powerful. It has many usecases, such as,<p><pre><code> diff file{.original,} mkdir -p path&#x2F;{a,b,c}&#x2F;folder for i in image{001..060}; do echo $i; done </code></pre> Parameter Expansion is also equally useful, like changing a few characters in long names,<p><pre><code> mv ${i}.png ${i&#x2F;imaeg&#x2F;image}.png</code></pre>
qsdf38100about 5 years ago
Nice, feels like Windows File Explorer ;D
评论 #22861093 未加载
评论 #22860874 未加载
idoubtitabout 5 years ago
I use zsh&#x27;s function <i>copy-prev-shell-word</i>. You can bind it to some key, e.g. alt-m with `bindkey &quot;^[m&quot; copy-prev-shell-word`. Use <i>ctrl-v shortcut</i> for another key.<p>For instance, adding a suffix to a file name: mv myfile <i>alt-m</i>.suffix
评论 #22861369 未加载
lottinabout 5 years ago
You can accomplish the same with basic editing commands, e.g.<p><pre><code> mv &lt;filename&gt; C-M-b C-k C-y C-y </code></pre> It may seem overly complicated but notice that you don&#x27;t have to release the control key, so it&#x27;s actually very few key strokes.
评论 #22861097 未加载
评论 #22861994 未加载
dejjabout 5 years ago
Links for mass moving mentioned in the page:<p>- 2018: <a href="https:&#x2F;&#x2F;github.com&#x2F;lgommans&#x2F;vinamer" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;lgommans&#x2F;vinamer</a> - 2017: <a href="https:&#x2F;&#x2F;github.com&#x2F;thameera&#x2F;vimv" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;thameera&#x2F;vimv</a> - 2017: <a href="https:&#x2F;&#x2F;github.com&#x2F;abaldwin88&#x2F;roamer" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;abaldwin88&#x2F;roamer</a> - 2006: <a href="https:&#x2F;&#x2F;joeyh.name&#x2F;code&#x2F;moreutils&#x2F;" rel="nofollow">https:&#x2F;&#x2F;joeyh.name&#x2F;code&#x2F;moreutils&#x2F;</a> , `vidir`
asamarinabout 5 years ago
The latest addition to my renaming-toolbelt has been `perl-rename&#x27;. This thing is just wonderful: I can unleash all the power of perl-based regexes to rename things in bulk.<p>Case in point: I had a directory containing thousands of .jpg images imported from a foreign filesystem, and all of those files had tildes in them, something like:<p><pre><code> $ ls -1 EL+�CTRICO_0001.jpg EL+�CTRICO_0002.jpg EL+�CTRICO_0003.jpg ... </code></pre> You get the idea; note those ugly unrepresentable characters over there. On the original filesystem they read as &quot;ELÉCTRICO&quot;, but that tilde was saved using who knows what encoding, and I simply wanted to get rid of them and have nice ascii &quot;ELECTRICO_xxxx.jpg&quot; files. After finding out that the strange unrepresentable character was the byte 0xEB (so, in order to form an &quot;É&quot; you needed those two characters together: a literal &#x27;+&#x27; and 0xEB), I could do the bulk renaming with just:<p><pre><code> $ perl-rename &#x27;s&#x2F;\+\xeb&#x2F;E&#x2F;&#x27; *.jpg </code></pre> Felt so good!
alexellisukabout 5 years ago
For faas-cli-darwin -&gt; faas-cli, I just do `mv faas-cli{-darwin,}` or `faas-cli.tgz` -&gt; `faas-cli.tar.gz` -&gt; `mv faas-cli faas-cli.{tgz,tar.gz}`<p>This rarely saves me any time, but it&#x27;s a nice hack to know about.
globular-toastabout 5 years ago
I discovered the `rename` command surprisingly late in my career. You could write something like this: `rename .htm .html index.htm`. The nice thing is it works for any number of files at once (you could put a glob on the right, for example).<p>Oddly I just checked my Ubuntu machine and it had the man page for rename but not the command. After being prompted to install it it installed a completely different perl command and upon removing that the original manpage was gone. Very strange.
dnrabout 5 years ago
Here&#x27;s my contribution to this useful class of scripts:<p><a href="http:&#x2F;&#x2F;dnr.im&#x2F;tech&#x2F;articles&#x2F;mvdir&#x2F;" rel="nofollow">http:&#x2F;&#x2F;dnr.im&#x2F;tech&#x2F;articles&#x2F;mvdir&#x2F;</a><p><a href="https:&#x2F;&#x2F;bitbucket.org&#x2F;davidn&#x2F;mvdir&#x2F;src&#x2F;default&#x2F;mvdir" rel="nofollow">https:&#x2F;&#x2F;bitbucket.org&#x2F;davidn&#x2F;mvdir&#x2F;src&#x2F;default&#x2F;mvdir</a><p>Similar to vidir, but predates it by a few years. I still use it regularly!
anthkabout 5 years ago
I just use vidir(1) an ed(1), I&#x27;m lazy :).
评论 #22861068 未加载
reanimusabout 5 years ago
This is a nice alternative to using curly braces, especially when the changes aren&#x27;t simple substitutions :D
评论 #22860788 未加载
yakshaving_jgtabout 5 years ago
Renaming a single file is not very interesting. Renaming a whole bunch of files is much more interesting, and when I need to do that, I use vimv[0].<p>[0]: <a href="https:&#x2F;&#x2F;github.com&#x2F;thameera&#x2F;vimv" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;thameera&#x2F;vimv</a>
评论 #22864615 未加载
lkutyabout 5 years ago
That&#x27;s why I love HN comments. Always learning a lot of stuff and regularly the comments are more interesting than the original post itself. This is the case for this one I think. Or at least a nice complement.
inopinatusabout 5 years ago
For an interactive edit I just use &lt;ESC&gt;-. to repeat the last parameter:<p><pre><code> ls foo.txt mv &lt;ESC&gt;-. &lt;ESC&gt;-. # and edit</code></pre>
评论 #22863402 未加载
评论 #22863367 未加载
rurbanabout 5 years ago
Always surprising to hear of people not knowing the basic readline key assignments.<p>Ctrl-w Ctrl-y Ctrl-y is still easiest for single files, rename for multiple.
bmn__about 5 years ago
`prename` is a thing; much more ergonomic than the solution in the article:<p><pre><code> $ prename &#x27;$_=lc; s&#x2F;jpeg&#x2F;jpg&#x2F;&#x27; IMG0001.jpeg ### IMG0001.jpeg → img0001.jpg </code></pre> This is probably the most reimplemented program I use, seven times last time I counted.
评论 #22860705 未加载
bobowzkiabout 5 years ago
&quot;without typing the name two times&quot;<p>Is that pun intentional? It&#x27;s hilarious.
pvaldesabout 5 years ago
or... you can do this<p>mmv &#x27;&lt;asterisk&gt;foo&lt;asterisk&gt;&#x27; &#x27;#1bar#2&#x27;<p>where &lt;asterisk&gt; is the common symbol (Will not appear in HN)
matthewhartmansabout 5 years ago
I think this is really neat! Well done OP! :)
calvinmorrisonabout 5 years ago
Shout out to visit(1) for lots of renames
xixixaoabout 5 years ago
This post and the comments here show what a poor UI the command line is (by default).
评论 #22861769 未加载
评论 #22861346 未加载
评论 #22865030 未加载
sashavingardt2about 5 years ago
Why is this at the top of HN?
评论 #22861423 未加载
评论 #22860954 未加载
评论 #22865057 未加载
评论 #22868351 未加载
评论 #22860924 未加载