how to create a file and make it executable in one command?
I am tired of creating a file with nano, saving it and then making it executable. Is there a command that makes it in one step?
I am tired of creating a file with nano, saving it and then making it executable. Is there a command that makes it in one step?
eruchitanda, ![]()
Use
&&
to use multiple commands one after the other, don’t use;
.
eruchitanda, ![]()
&&
means execute if the command before ended successfully
||
means execute if the commnad before failed
;
just means execute the command - no matter if succeeded or failed
ArcaneSlime, My dude, thanks for this. I’ve been using && for a long time now but never knew the rest, I’m still pretty new to linux comparatively.
NegativeLookBehind, ![]()
You want the install command.
At least, I think this will do it. I haven’t used it in a while.
kureta,
touch file && chmod +x file
is good but this here is the one true command for the purpose.
2xsaiko, ![]()
install -m755 /dev/null target
was the first thing I thought of. I would never use this but it is a single command.
NegativeLookBehind, ![]()
Why would you never use it?
2xsaiko, ![]()
I’m going to write (at least part of) the script first anyway, and then I can just use chmod +x after the file is saved which is shorter.
Llituro, ![]()
touch file && chmod +x file
CameronDev, Write an alias/function to do it and add to your bashrc.
<span style="color:#323232;">function nanox() { </span><span style="color:#323232;"> nano "$1" </span><span style="color:#323232;"> chmod +x "$1" </span><span style="color:#323232;">} </span>
psmgx, This is the way.
cyborganism, You mean like
touch file && chmod +x file
?
MrAlternateTape, Wrap it up folks, we’re done here.
sneakyninjapants, Here’s one I have saved in my shell aliases.
<span style="color:#323232;">nscript() { </span><span style="color:#323232;"> local name="${1:-nscript-$(printf '%s' $(echo "$RANDOM" | md5sum) | cut -c 1-10)}" </span><span style="color:#323232;"> echo -e "#!/usr/bin/env bashn#set -Eeuxo pipefailnset -e" > ./"$name".sh && chmod +x ./"$name".sh && hx ./"$name".sh </span><span style="color:#323232;">} </span><span style="color:#323232;">alias nsh='nscript' </span>
Admittedly much more complicated than necessary, but it’s pretty full featured. first line constructs a filename for the new script from a generated 10 character random hash and prepends “nscript” and a user provided name.
The second line writes out the shebang and a few oft used bash flags, makes the file executable and opens in in my editor (Helix in my case).
The third line is just a shortened alias for the function.
kayaven, ![]()
You could define a function that takes a parameter, which touches a file with the parameters value, chmods it and then opens it with nano?
<span style="color:#323232;">create_exec() { </span><span style="color:#323232;"> touch "$1" </span><span style="color:#323232;"> chmod +x "$1" </span><span style="color:#323232;"> nano "$1" </span><span style="color:#323232;">} </span>
Then you could type
create_exec file.sh
and it would do the rest for you.
bjoern_tantau, ![]()
You could append the chmod command with && but that’s probably not what you wanted.
Add comment