Hacker News new | past | comments | ask | show | jobs | submit login

> alias mv='mv -i'

While --interactive mode is certainly useful, a better option for this problem that I don't see mentioned often is the --no-clobber option:

    -n, --no-clobber
              do not overwrite an existing file
I often add --no-clobber to scripts to make automated file movement safer. An alternate "safe mv" alias can be useful, e.g.:

    alias smv='mv --no-clobber'
(I don't recommend changing the behavior of normal 'mv' directly; getting used to assuming 'mv' means the safer 'mv --no-clobber' can be dangerous when you use use a different computer without your custom environment)



bash also has a noclobber option, IIRC. It is to prevent overwriting existing files with the > redirection operator.

Google:

bash set noclobber option

and

bash set command

and see

https://www.gnu.org/software/bash/manual/html_node/The-Set-B...


Is there a convenient way to make this type of alias work when using with _sudo_ too?

i guess one can alias for the root user too but its annoying.


  alias sudo='sudo '
If the last character of the alias value is a blank, then the next command word following the alias is also checked for alias expansion.

https://www.gnu.org/software/bash/manual/bash.html#Aliases


goodness. I never knew of this black magic.

sort of like a space before a command and it doesn't go into bash's history (if enabled)


Strange that Sudo can be made to use your aliases (outside of sudo control) but makes an effort to ignores your $PATH.


It isn't sudo that's expanding the alias, it's your shell. In other words, your shell is expanding sudo mv to sudo mv -i before sudo is involved.


Right. The 'alias' word is a shell built-in[1], so is interpreted by the shell, which is anyway the first process to parse any command line that you type at the shell prompt. It is only after the shell has done its processing of the command line that the processed line is passed to the actual command being invoked (such as sudo or mv or ls or ...), which is done in the argc/argv pair of arguments (using C terminology) to the invoked program. (In Python it is the sys.argv list.)

[1] Section 6.6 in https://www.gnu.org/software/bash/manual/html_node/Aliases.h...

And the above is also why the shell metacharacters (such as dollar, asterisk, square brackets) that you may use in any Unix command invocation, get expanded by the shell, not by any individual command[2], which is why these metacharacters work for any command, old or new, built-in, external or user-written (in any language, as long as it supports argc/argv-like conventions), . This is unlike MS-DOS, for example, where some commands supported wildcards but others did not. There, the support was programmed into (only some of) the individual commands. (CMD.EXE of later Windows versions may be different, and IIRC, is more Unix-like.)

[2] That may be a less-known fact.


An executable can’t control aliases of it AFAIK, that’s all up to your shell.


The aliases are expanded before calling sudo. It is equivalent to just typing out the full command.

IIUC the reason sudo ignores the path is because if you use per-command sudo rules you often depend on the path being correct.


Thank you!


Just put it in a script.

    #!/bin/sh
    # call this file 'smv' or whatever
    exec mv --no-clobber "$@"
You only have to pay the 'annoying' cost once to add /some/util/script/dir/ to both the user's and root's $PATH.


> You only have to pay the 'annoying' cost once to add /some/util/script/dir/ to both the user's and root's $PATH

Put it in /.../sbin. It’ll be accessible to both root and non-root users.


This works too but I personally prefer etcet's way as it don't require to change anything in root user.




Consider applying for YC's Summer 2025 batch! Applications are open till May 13

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: