每次更改目录时调用ls命令的shell脚本

时间:2022-05-08 14:43:14

I am wondering if there is any way to make a script, that would run in the background and which will call the "ls" command every time I change directories("cd") in Linux.

我想知道是否有什么方法可以制作一个脚本,它可以在后台运行,并且每当我在Linux中更改目录(“cd”)时,它就会调用“ls”命令。

I know that in order to put a process in the background you add a "&" when you run it.

我知道,为了把一个进程放到后台,你在运行时添加一个“&”。

Thanks in advance!

提前谢谢!

2 个解决方案

#1


2  

You could replace cd with a shell function in your ~/.bashrc or similar startup script:

您可以用~/中的shell函数替换cd。bashrc或类似的启动脚本:

function cd {
    builtin cd "$@"
    RET=$?
    ls
    return $RET
}

this would also return the exit code of cd, just in case...

这也会返回cd的退出码,以防万一……

builtin is a shell builtin to execute the shell builtin cd instead of the cd funciton, to avoid running into a recursive loop - at least in bash - but should also work with other shells...

builtin是一个shell内建,用于执行shell内建cd而不是cd函数,以避免陷入递归循环(至少在bash中),但也应该与其他shell一起工作……

#2


0  

You can define a function in your .bashrc like this to achieve that:

你可以在你的。bashrc中定义一个函数,如下所示:

cdlist(){
        cd "$1" && ls
}

#1


2  

You could replace cd with a shell function in your ~/.bashrc or similar startup script:

您可以用~/中的shell函数替换cd。bashrc或类似的启动脚本:

function cd {
    builtin cd "$@"
    RET=$?
    ls
    return $RET
}

this would also return the exit code of cd, just in case...

这也会返回cd的退出码,以防万一……

builtin is a shell builtin to execute the shell builtin cd instead of the cd funciton, to avoid running into a recursive loop - at least in bash - but should also work with other shells...

builtin是一个shell内建,用于执行shell内建cd而不是cd函数,以避免陷入递归循环(至少在bash中),但也应该与其他shell一起工作……

#2


0  

You can define a function in your .bashrc like this to achieve that:

你可以在你的。bashrc中定义一个函数,如下所示:

cdlist(){
        cd "$1" && ls
}