在 Linux 中给命令指定别名(alias)怎么用啊?

在 Linux 中给命令指定别名(alias)怎么用啊?,第1张

如果命令或命令序列太长,或不符合用户的习惯,那么为它指定一个别名是不错的办法。而指定别名则可以解决此类所有问题。只要举一些例子就可以了:
alias l='ls -l' ;用 l 代替 ls -l 命令(Xenix 下就有类似的 l 命令)
alias cd='cd ' ;用 cd 代替 cd 命令(对在 DOS 下使用惯了 cd 的人帮助很大)
alias md='mkdir' ;用 md 代替 mkdir 命令(对在 DOS 下…)
alias c:='mount /dev/hda1 /mnt/c & cd /mnt/c' ;用 c: 命令代替命令序列:安装 DOS 分区,再进入
希望对楼主有帮助

为什么要设立用户别名?这是一个很严重的安全隐患。
如果你要控制权限,最好使用用户组来管理,同时打开 Linux 的分区的 ACL 管理文件权限。
-----------------
看看 /etc/passwd 和 /etc/shadow 里面。UID 和 login name 是两个段落。
lishanjun2008 的意思就是让你新建两个 login name ,让他们使用一个 UID 。
不过我不认为这个办法可行……
用用户组或者 ACL 解决吧……

说白了 是环境变量去设置如你的用户是普通的用户,可以学习下bashrc文件 看看就明白了 ,给你个事例 lxu@libai:~$ pwd/home/lxulxu@libai:~$ cat bashrc # ~/bashrc: executed by bash(1) for non-login shells# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)# for examples
# If not running interactively, don't do anything[ -z "$PS1" ] && return
# don't put duplicate lines in the history See bash(1) for more options# don't overwrite GNU Midnight Commander's setting of `ignorespace'HISTCONTROL=$HISTCONTROL${HISTCONTROL+,}ignoredups# or force ignoredups and ignorespaceHISTCONTROL=ignoreboth
# append to the history file, don't overwrite itshopt -s histappend
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
# check the window size after each command and, if necessary,# update the values of LINES and COLUMNSshopt -s checkwinsize
# make less more friendly for non-text input files, see lesspipe(1)[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
# set variable identifying the chroot you work in (used in the prompt below)if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then debian_chroot=$(cat /etc/debian_chroot)fi
# set a fancy prompt (non-color, unless we know we "want" color)case "$TERM" in xterm-color) color_prompt=yes;;esac
# uncomment for a colored prompt, if the terminal has the capability; turned# off by default to not distract the user: the focus in a terminal window# should be on the output of commands, not on the prompt#force_color_prompt=yes
if [ -n "$force_color_prompt" ]; then if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then # We have color support; assume it's compliant with Ecma-48 # (ISO/IEC-6429) (Lack of such support is extremely rare, and such # a case would tend to support setf rather than setaf) color_prompt=yes else color_prompt= fifi
if [ "$color_prompt" = yes ]; then PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ 'else PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ 'fiunset color_prompt force_color_prompt
# If this is an xterm set the title to user@host:dircase "$TERM" inxterm|rxvt) PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1" ;;) ;;esac
# enable color support of ls and also add handy aliasesif [ -x /usr/bin/dircolors ]; then test -r ~/dircolors && eval "$(dircolors -b ~/dircolors)" || eval "$(dircolors -b)" alias ls='ls --color=auto' #alias dir='dir --color=auto' #alias vdir='vdir --color=auto'
alias grep='grep --color=auto' alias fgrep='fgrep --color=auto' alias egrep='egrep --color=auto'fi
# some more ls aliasesalias ll='ls -l'alias vi='vim'#alias la='ls -A'#alias l='ls -CF'
# Alias definitions# You may want to put all your additions into a separate file like# ~/bash_aliases, instead of adding them here directly# See /usr/share/doc/bash-doc/examples in the bash-doc package
if [ -f ~/bash_aliases ]; then ~/bash_aliasesfi
# enable programmable completion features (you don't need to enable# this, if it's already enabled in /etc/bashbashrc and /etc/profile# sources /etc/bashbashrc)if [ -f /etc/bash_completion ] && ! shopt -oq posix; then /etc/bash_completionfi

如果你频繁地使用参数相同的某个命令,你可以让bash为它创建一个别名。这个别名将组合需要的参数,所以你无需记住它们或每次手工输入它们。例如,如果你经常使用ls -l以获得详细的目录列表,你可以使用这个命令:
bash> alias ls='ls -l'
这样就创建了自动包含-l选项的别名。一旦该别名被创建,在bash提示符下输入ls将调用该别名就输出ls -l。
你可以通过调用没有任何参数的别名获得可用别名的列表,你也可以用unalias命令删除别名。

首先你这里犯了一个错误,alias是针对一个命令做的别名,比如说将”ls -l“命令重命名为 "ll"
那么这里直接执行”ll“就相当于执行了"ls -l"
你上面的错误是把arm当成了cd命令的参数,这样是不对的,两种解决方法
1将”cd /data/rootfs26/home/ex_task"设置为别名“cdarm(确保系统没有这个命令)”,写入~/bashrc文件,登录系统之后直接终端下执行"cdarm"
2直接将目录”/data/rootfs26/home/ex_task“连接到主目录下面的arm目录,用命令"ln -s /data/rootfs26/home/ex_task ~/arm"
这样以后就可以直接用"cd ~/arm"就可以进去了。
注意前面的“~”代表用户的home目录,如果已经在这个目录下就直接“cd arm”即可。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/13398931.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-28
下一篇 2023-07-28

发表评论

登录后才能评论

评论列表(0条)

保存