tail和ls的使用, bash-completion的安装和使用

本文介绍了几种在Linux环境下高效查看日志文件的方法,包括使用tail命令查看最新日志记录、利用ls命令筛选非空日志文件及通过bash-completion提高输入效率等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

tail -50 catalina.out 用来显示这个Log文件最后50行。这个文件1.8G,如果用VI就太慢了。


tail -f  localhost_access_log.2015-10-10.txt 用来输出实时log信息,查看最新的网站被访问的信息,含远端的IP地址,时间(本地时间),以及访问的细节。


ls -lh来查看文件。其中-l是长信息,h是人类阅读方式。通过这个略去查看字节数为0的log文件。


bash-completion非常有用,可以通过按Tab键的方式自动输入文字,节省大量输入的时间。Centos中通过yum install bash-completion来安装。






# ~/.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 case $- in *i*) ;; *) return;; esac # don't put duplicate lines or lines starting with space in the history. # See bash(1) for more options HISTCONTROL=ignoreboth # append to the history file, don't overwrite it shopt -s histappend # for setting history length see HISTSIZE and HISTFILESIZE in bash(1) HISTSIZE=1000 HISTFILESIZE=2000 # check the window size after each command and, if necessary, # update the values of LINES and COLUMNS. shopt -s checkwinsize # If set, the pattern "**" used in a pathname expansion context will # match all files and zero or more directories and subdirectories. #shopt -s globstar # 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|*-256color) 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= fi fi 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\$ ' fi unset color_prompt force_color_prompt # If this is an xterm set the title to user@host:dir case "$TERM" in xterm*|rxvt*) PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1" ;; *) ;; esac # enable color support of ls and also add handy aliases if [ -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 # colored GCC warnings and errors #export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01' # some more ls aliases alias ll='ls -alF' alias la='ls -A' alias l='ls -CF' # Add an "alert" alias for long running commands. Use like so: # sleep 10; alert alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"' # 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_aliases fi # enable programmable completion features (you don't need to enable # this, if it's already enabled in /etc/bash.bashrc and /etc/profile # sources /etc/bash.bashrc). if ! shopt -oq posix; then if [ -f /usr/share/bash-completion/bash_completion ]; then . /usr/share/bash-completion/bash_completion elif [ -f /etc/bash_completion ]; then . /etc/bash_completion fi fi 我想让提示符显示两行
最新发布
07-29
### 如何在 Linux 中通过指定时间文字过滤实时查看日志文件 在 Linux 系统中,`tail -f` 是一种常用的工具用于实时监控日志文件的变化。为了进一步增强其实用性,可以通过管道符 `|` 结合其他命令(如 `grep` 或自定义 Shell 脚本)实现基于时间或关键字的文字过滤功能。 以下是具体的解决方案: #### 方法一:结合 `tail -f` `grep` 的简单方式 可以直接利用 `tail -f` 配合 `grep` 来实现实时的日志筛选[^1]。例如,要查找包含“连接失败”的日志条目,可以运行如下命令: ```bash tail -f /path/to/logfile | grep "连接失败" ``` 此方法适用于简单的场景,但如果需要更复杂的逻辑(比如按时间段过滤),则需借助额外的脚本来完成。 --- #### 方法二:使用带有定时器退出机制的 Shell 脚本 当希望不仅能够按照特定字符串匹配,还能够在一定时间内自动停止监听时,可采用以下脚本形式[^2]: ```bash #!/bin/bash # 设置日志文件路径 log_file="/var/log/example.log" # 设定过滤的关键字 filter_keyword="连接失败" # 开始记录起始时刻的时间戳 start_time=$(date +%s) # 执行 tail -f 并配合 grep 对目标关键词进行过滤 tail -f "$log_file" | grep --line-buffered "$filter_keyword" | while IFS= read -r line; do # 输出当前符合条件的日志行 echo "$line" # 计算已经过去的秒数 current_time=$(date +%s) elapsed=$((current_time - start_time)) # 判断是否超出设定的最大持续时间 (单位: 秒),这里设置为 30 秒为例 if ((elapsed >= 30)); then echo "已达到最大监听时限,即将退出..." break fi done echo "监听已完成!" ``` 上述脚本实现了两个主要的功能点: 1. **动态过滤**:只显示含有预设关键字的内容; 2. **超时控制**:一旦累计工作超过给定阈值便会主动中断操作流程。 注意,在某些情况下可能还需要加上选项 `--line-buffered` 参数传递至 `grep` ,以确保即时更新的结果能被及时捕获处理而不是等到缓冲区满才刷新输出。 --- #### 方法三:高级定制化需求下的 Cron Job 方案 对于更加复杂的需求——例如每天固定时段启动/关闭服务状态跟踪,则推荐考虑将此类任务交由系统的计划作业调度程序 cron 来管理[^3]。创建一个新的 crontab 表项即可轻松达成目的: 编辑用户的个人 Crontab 文件: ```bash crontab -e ``` 添加类似这样的新规则(假设我们想让其每晚凌晨两点整开始执行直至早晨六点钟为止) : ```cron 0 2 * * * bash /home/user/start_log_monitor.sh >> /tmp/cronjob_output.log 2>&1 & 59 5 * * * pkill -f "/home/user/start_log_monitor.sh"; echo "$(date): Stopped monitoring at $(date)" >> /tmp/cronjob_output.log ``` 在这里,“start_log_monitor.sh”应该就是前面提到过的那种具备自我约束能力的 Bash Script 。这样安排的好处在于无需人工干预就能全自动化的开启与结束整个过程。 --- ### 总结 综上所述,无论是快速验证还是长期部署环境中的应用场合,都有相应的技术手段可供选择来满足不同层次的要求。从基础命令组合到自动化运维实践均有所涉猎,希望能帮助解决实际工作中遇到的相关难题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值