By gdi2290
How do I do this in one line reusing and reversing the arguments? Also what’s the syntax to create a function that will do this?
mv newname.js oldname.js git mv oldname.js newname.js
for example to make a folder then cd into it you
mkdir new-folder-name && cd $_
is it something like mv newname.js oldname.js && git mv $2 $1
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
This question was answered by @sierracircle:
have you considered writing a script? It is fairly simple, and you can add variables when you run it.
just create a text file, save it with whatever you want to name it, and then make it executable with:
chmod +x file-namea quick example of a script, which I can pass two variable to when I run it:
#!/bin/bash echo $1 echo $2 exit 0now if I run that script like this:
file-name "first variable" "second variable"it should echo:
first variable second variable
Heya,
Yes, you’re on the right track. In bash, positional parameters are referred to as $1, $2, etc. However, when you’re in a one-liner in the shell itself (rather than in a script), there isn’t a straightforward way to reverse the positional parameters. You’d have to use a shell function if you want to reuse and reverse arguments.
Here’s how you can achieve your goal:
Creating a function:
Add the following function to your ~/.bashrc or ~/.bash_profile:
gitmv() {
mv "$1" "$2" && git mv "$2" "$1"
}
This function, named gitmv, first uses the mv command to rename a file, then uses git mv to reflect that change in git.|
2. Using the function:
After adding the function, you need to reload your shell or run:
source ~/.bashrc
(or source ~/.bash_profile if that’s where you added it)
Then, you can use your new function:
gitmv newname.js oldname.js
This will first rename newname.js to oldname.js on your filesystem, then inform git of the rename.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.