alias [change dir] - Linux

Suhail Hany avatar
By Suhail Hany
at 2011-08-31T08:42

Table of Contents

※ 引述《mswethan (@竹北)》之銘言:
: 每次cd 到別的目錄後,下個動作一定是ls,看一下該目錄內有哪些東西...
: 這樣子真的很麻煩..
: cd xx ls
: cd yy ls
: ...
: 我想要alias cd指令,使它做三個動作..
: 1. change dir
: 2. clear
: 3. ls
: 使用的是bash,第一個動作不曉得有沒有人知道該如何設定??
: alias cd='_____;clear;ls'
: 感謝

借標題一問

我想做到上述的功能

但唯有cd到家目錄時不列出檔案清單

我的寫法如下:

alias la='ls -FA --color=auto'
cd()
{
builtin cd "$1";
if [ "$1" != "$HOME" ]; then la; fi;
}

但這寫法有兩個問題

1.
user@local_host:~ $ cd folder_1
file_1 file_2 file_3 => 預期地顯示檔案清單
user@local_host:~/f1 $ cd ..
.bashrc .vimrc .vim .bash_profile => 不應該顯示檔案清單卻顯示了
(原因應該是".."沒被expand成$HOME的內容)

2.
user@local_host:~ $ cd folder_2
bash: cd: folder_2: No such file or directory
.bashrc .vimrc .vim .bash_profile => 不應該顯示檔案清單卻顯示了
(原因是folder_2資料夾不存在, if判斷式就成立了)

問題2比較好處理, 檢查cd的路徑是資料夾而非檔案, 且存在即可
問題1我還不知道怎麼解, 請問有什麼解法嗎?

--
Tags: Linux

All Comments

Elma avatar
By Elma
at 2011-09-02T16:34
改成檢查 $PWD 如何?
Callum avatar
By Callum
at 2011-09-05T04:07
用 realpath 更好,能應付有 soft link 的情形
Elizabeth avatar
By Elizabeth
at 2011-09-08T22:54
例如: ln -s $HOME /tmp/myhome 再 cd /tmp/myhome 試看看
Tracy avatar
By Tracy
at 2011-09-09T13:23
用 realpath 可以解決 soft link 產生的問題

CentOS iptables 問題

Freda avatar
By Freda
at 2011-08-31T02:19
我想在 CentOS 6 上跑 nginx iptables 是裝好預設的 policy 打入要加入 80 port 的指令 iptables -A INPUT -p tcp --dport 80 -j ACCEPT 按下 enter 後沒出現錯誤訊息 但連進來連不到,後來停掉 iptables 就連到了 i ...

CentOS 6 使用 VirtualBox 抓不到網路卡

Kristin avatar
By Kristin
at 2011-08-31T01:45
※ 引述《chan15 (ChaN)》之銘言: : OS: Windows 7 : 虛擬軟體:VirtualBox 4.1.2 : Linux: CentOS6 : 網路卡:Realtek 8169 : VirtualBox 網路設定為 NAT,也嘗試過其他的選項 : 但 CentOS 一直無法順利使用網路 ...

Atomeye的安裝

Ophelia avatar
By Ophelia
at 2011-08-31T01:34
最近再跑一些分子模擬的東西 需要用到ATOMEYE 安裝手冊上有提到一句 You must ensure that the and#34;xtermand#34; command is in your shell PATH and can be directly called 這個意思是說我需要先安裝 ...

CentOS 6 使用 VirtualBox 抓不到網路卡

Zora avatar
By Zora
at 2011-08-31T00:00
OS: Windows 7 虛擬軟體:VirtualBox 4.1.2 Linux: CentOS6 網路卡:Realtek 8169 VirtualBox 網路設定為 NAT,也嘗試過其他的選項 但 CentOS 一直無法順利使用網路 請問是 VirtualBox 設定問題還是 Linux 那邊該怎麼處理 ...

請教busybox的printer問題

Belly avatar
By Belly
at 2011-08-30T22:59
請問各位大大: 最近在研究有關busybox的內建套件, 關於lpr這隻程式應如何使用呢? 就是假若我希望先將列印文件暫存至機器上, 再從機器慢慢消化至印表機列印的話, 我應該如何做呢? 謝謝。 - ...