![폴더의 "로컬" bash 기록을 기억하시나요?](https://rvso.com/image/97254/%ED%8F%B4%EB%8D%94%EC%9D%98%20%22%EB%A1%9C%EC%BB%AC%22%20bash%20%EA%B8%B0%EB%A1%9D%EC%9D%84%20%EA%B8%B0%EC%96%B5%ED%95%98%EC%8B%9C%EB%82%98%EC%9A%94%3F.png)
긴 인수와 함께 사용하는 폴더에 스크립트가 있습니다. 전체 기록을 살펴보는 대신 해당 특정 디렉터리에서 실행된 명령 기록을 가질 수 있는 기회가 있습니까?
답변1
bash의 PROMPT_COMMAND에 연결하면 새 프롬프트가 나타날 때마다 이 함수가 실행되므로 사용자 정의 기록을 원하는 디렉토리에 있는지 확인하는 것이 좋습니다. 이 함수에는 네 가지 주요 분기가 있습니다.
- 현재 디렉터리(
$PWD
)가 변경되지 않은 경우 아무 작업도 수행하지 않습니다(반환).
장애인인 경우가지다변경한 다음 "사용자 정의 디렉터리" 코드를 한 곳으로 모으는 것이 유일한 목적인 로컬 함수를 설정합니다. 내 테스트 디렉터리를 자신의 디렉터리로 바꾸고 싶을 것입니다( 로 구분 |
).
- 사용자 정의 디렉토리로 변경하거나 변경하지 않은 경우 간단히 "이전 디렉토리" 변수를 업데이트하고 함수에서 돌아갑니다.
디렉토리를 변경했으므로 "이전 디렉토리" 변수를 업데이트하고 메모리 내 기록을 HISTFILE에 저장한 다음 메모리 내 기록을 지웁니다.
우리가 변했다면~ 안으로
.bash_history
사용자 정의 디렉토리를 선택한 다음 HISTFILE을 현재 디렉토리의 파일 로 설정하십시오 .그렇지 않으면 우리는 변했어요밖으로사용자 정의 디렉토리이므로 HISTFILE을 기본 디렉토리로 재설정하세요.
마지막으로 기록 파일을 변경했으므로 이전 기록을 다시 읽어보세요.
작업을 진행하기 위해 스크립트는 PROMPT_COMMAND 값을 설정하고 두 개의 내부 사용 변수(스톡 HISTFILE 및 "이전 디렉터리")를 저장합니다.
prompt_command() {
# if PWD has not changed, just return
[[ $PWD == $_cust_hist_opwd ]] && return
function iscustom {
# returns 'true' if the passed argument is a custom-history directory
case "$1" in
( */tmp/faber/somedir | */tmp/faber/someotherdir ) return 0;;
( * ) return 1;;
esac
}
# PWD changed, but it's not to or from a custom-history directory,
# so update opwd and return
if ! iscustom "$PWD" && ! iscustom "$_cust_hist_opwd"
then
_cust_hist_opwd=$PWD
return
fi
# we've changed directories to and/or from a custom-history directory
# save the new PWD
_cust_hist_opwd=$PWD
# save and then clear the old history
history -a
history -c
# if we've changed into or out of a custom directory, set or reset HISTFILE appropriately
if iscustom "$PWD"
then
HISTFILE=$PWD/.bash_history
else
HISTFILE=$_cust_hist_stock_histfile
fi
# pull back in the previous history
history -r
}
PROMPT_COMMAND='prompt_command'
_cust_hist_stock_histfile=$HISTFILE
_cust_hist_opwd=$PWD
답변2
제프의답변단일 디렉토리에 대한 기록을 원할 경우 유용하지만 설치에 문제가 없다면zsh당신은 사용할 수 있습니다기록 디렉토리별모든 디렉토리에 대한 디렉토리 관련 기록을 가져옵니다.
다음 방법으로 zsh를 설치할 수 있습니다.
brew install zsh
또는 설치하려는 경우오-내-zsh, 당신은histdb플러그인을 사용하고 histdb가 추가하는 sqlite db를 쿼리하는 사용자 정의 쿼리를 작성합니다. 나는 그것에 대해 썼고개발자 일기우편. 을 체크 해봐보너스 명령부분.
쿼리는 다음과 같습니다
show_local_history() {
limit="${1:-10}"
local query="
select history.start_time, commands.argv
from history left join commands on history.command_id = commands.rowid
left join places on history.place_id = places.rowid
where places.dir LIKE '$(sql_escape $PWD)%'
order by history.start_time desc
limit $limit
"
results=$(_histdb_query "$query")
echo "$results"
}
이는 선택적 제한도 허용합니다.
show_local_history 50
예를 들어.
답변3
긴 인수가 포함된 명령을 여러 번 사용해야 하는 경우 일반적으로 my에 별칭을 만들 거나 원하는 경우 ~/.bash_aliases
your에 넣을 수 있습니다 . ~/.bashrc
역사에서 오래된 명령을 찾는 대신 쉽고 시간을 절약할 수 있습니다.