tools:How do I create a permanent Bash alias?

博客主要探讨如何创建永久Bash别名。介绍了两种常用方法,一是直接在特定文件中添加别名,二是创建单独的别名文件。还提及不同方法的操作步骤及一些用户在使用过程中遇到的问题,如部分标志无效等。

 

How do I create a permanent Bash alias?

There are a lot of ways to create an alias. The most used ways are:

  1. Add aliases directly in your ~/.bashrc file

    For example: append these line to ~/.bashrc file

    alias ll='ls -l'
    alias rm='rm -i'

    Next time (after you have logged out/in, or done . ~/.bashrc) when you type rm the rm -icommand will be executed.

  2. The second method lets you make a separate aliases file, so you won't have to put them in .bashrc, but to a file of your choice. First, edit your ~/.bashrc file and add the following lines if they don't already exist, or uncomment them if they do:

    if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
    fi

    Save it and close the file. After that, all you have to do is create a ~/.bash_aliases file and add your aliases there, with the same format specified in the first method.

    Contents of my ~/.bash_aliases file:

    alias cs='cd;ls'

shareimprove this answer

edited Feb 14 at 16:44

Dan

7,38055 gold badges4747 silver badges7474 bronze badges

answered Dec 15 '10 at 8:21

aneeshep

23.1k1212 gold badges5656 silver badges7474 bronze badges

  • 71

    +1 for using ~/.bash_aliases. – ændrük Jun 10 '11 at 5:48

  • Yep, using another file for aliases is much more clean, also portable between distros. I do use that file to because some alias are not enough and a function is needed. So it's more much clean if you use a file for that task instead. I have another alias -> alias aliases="xdg-open ~/.bash_aliases && source ~/.bash_aliases", so the alias became avail at saving, and if you make some mistake it will advert you. – erm3nda Apr 27 '17 at 12:08

  • somehow after I added the alias such as alias ls='ls -althr', some of the flags given would not take effect, in this case the -t flag didn't take effect. do you know why? – Sajuuk Mar 22 '18 at 8:43 

  • 2

    By default ~/.bashrc contains inclusion for ~/.bash_aliases, no need to edit it. – Jaakko May 14 '18 at 9:49

  • 1

    Not always ~/.bashrc contains inclusion for ~/.bash_aliases as was in my case with Ubuntu terminal Windows 10 this solution came very handy. – Jose May 30 '18 at 18:10

add a comment

<think> 我们遇到两个问题: 1. 在CentOS系统上执行`lsof -i :3306`提示未找到命令,这是因为系统没有安装`lsof`工具。 2. 我们需要解决端口占用问题,以便启动MySQL容器。 解决方案: 步骤1:安装lsof工具 步骤2:使用lsof或其他方法检查端口占用 步骤3:根据端口占用情况停止占用进程或修改Docker映射端口 详细步骤: </think> ### 🔍 解决 CentOS 执行 `lsof -i :3306` 命令报错的方法 #### 📌 错误原因分析 `-bash: lsof: 未找到命令` 表示您的 CentOS 系统**未安装 lsof 工具**。lsof(List Open Files)是用于查看端口占用情况的专业工具[^1],在最小化安装的 CentOS 中通常不包含此工具。 #### 🛠️ 解决方法(分步操作) ##### 步骤 1:安装 lsof 工具 ```bash # 使用 yum 安装 lsof sudo yum install -y lsof # 验证安装 (显示版本号即成功) lsof -v # 输出示例: lsof version 4.87 ``` ##### 步骤 2:替代方法查看端口占用(无需 lsof) ```bash # 方法 1:使用 netstat sudo netstat -tulnp | grep :3306 # 输出示例: # tcp6 0 0 :::3306 :::* LISTEN 1234/mysqld # 方法 2:使用 ss 命令 sudo ss -ltnp | grep :3306 # 方法 3:检查服务状态 sudo systemctl status mysqld* ``` ##### 步骤 3:处理占用 3306 端口的进程 ```bash # 1. 获取进程ID (PID) sudo netstat -tulnp | grep :3306 | awk '{print $7}' | cut -d'/' -f1 # 输出示例: 1234 # 2. 停止进程 (根据实际情况选择) sudo kill -9 1234 # 强制停止 # 或 sudo systemctl stop mysqld.service # 优雅停止服务 # 3. 禁止自启动 (防止重启后再次占用) sudo systemctl disable mysqld.service ``` ##### 步骤 4:验证端口释放 ```bash # 检查3306端口无监听 sudo netstat -tuln | grep :3306 # 应无输出 # 尝试启动 MySQL 容器 docker run -d --name mysql \ -p 3306:3306 \ -e MYSQL_ROOT_PASSWORD=your_password \ mysql:latest ``` #### 💡 替代方案:直接使用 Docker 端口映射 如果仍需保留本地 MySQL 服务,可修改 Docker 映射端口: ```bash # 使用3307端口映射 (避开本地服务) docker run -d --name mysql \ -p 3307:3306 \ # 主机端口:容器端口 -e MYSQL_ROOT_PASSWORD=secret \ mysql:latest ``` #### ⚠️ 注意事项 1. **最小化安装问题**:CentOS 最小化安装仅包含基础工具,需手动安装常用工具 2. **防火墙同步**:若使用非标准端口(如3307),需更新防火墙规则 ```bash sudo firewall-cmd --zone=public --add-port=3307/tcp --permanent sudo firewall-cmd --reload ``` 3. **服务残留**:使用 `systemctl list-unit-files | grep mysql` 检查所有 MySQL 相关服务 #### 📚 知识扩展 端口占用检测原理: $$ \text{netstat} \equiv \text{读取 } /proc/net/tcp \text{ 文件} $$ $$ \text{lsof} \equiv \text{遍历 } /proc/\text{PID}/fd \text{ 文件描述符} $$
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值