侧边栏壁纸
  • 累计撰写 57 篇文章
  • 累计创建 10 个标签
  • 累计收到 2 条评论

目 录CONTENT

文章目录

Centos7 文件查找命令(FZF)

yilee
2023-04-04 / 0 评论 / 0 点赞 / 128 阅读 / 0 字
温馨提示:
本文最后更新于2024-05-31,若内容或图片失效,请留言反馈。 部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

Centos7 文件查找命令(FZF)

GITHUB镜像站

1.通过插件管理器安装fzf,nerdtree

# 1.安装vim插件管理器(vim-plug)
curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
    
# 2. 编辑.vimrc文件(最好不要操作vim安装时的文件 [/etc/vimrc])
vim ~/.vimrc

对( ~/.vimrc )文件追加以下内容:

" 插件
" 添加在线插件
call plug#begin('~/.vim/plugged')
" 此处为将fzf安装到 ~/.fzf 目录,并执行 ./install --all 命令
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all'  }
Plug 'junegunn/fzf.vim'
Plug 'preservim/nerdtree'
call plug#end()

插件的安装与卸载

打开vim
使用以下命令安装在.vimrc中配置的插件:
:PlugInstall
使用以下命令清理插件:
:PlugClean

2.安装FD

# gcc 编译版本
wget https://github.com/sharkdp/fd/releases/download/v8.3.0/fd-v8.3.0-x86_64-unknown-linux-gnu.tar.gz
//解压
tar -zxvf fd-v8.3.0-x86_64-unknown-linux-gnu.tar.gz
cd fd-v8.3.0-x86_64-unknown-linux-gnu/

cp fd /usr/local/bin/
cp fd.1 /usr/local/share/man/man1/
mandb

#出现错误:fd: /lib64/libc.so.6: version `GLIBC_2.18' not found (required by fd)
# 安装glibc-2.18
# ----------------------------------
wget https://ftp.gnu.org/gnu/glibc/glibc-2.18.tar.gz
cd glibc-2.18 && mkdir build && cd build
../configure --prefix=/usr
# 多线程编译
make -j4
# 单线程编译
make 
# 安装
make install
# ----------------------------------

# Ubuntu安装AG
apt-get install silversearcher-ag

# Centos安装AG
 yum install the_silver_searcher

3.配置预览脚本

# !!!请确保已下载fzf
#安装 w3m,unzip,transmission-show
yum install epel-release
yum install -y w3m unzip transmission-cli transmission-common transmission-daemon

#安装unrar
wget http://www.rarlab.com/rar/rarlinux-x64-4.2.0.tar.gz
tar -zvxf rarlinux-x64-4.2.0.tar.gz -C /usr/local
ln -s /usr/local/rar/rar /usr/local/bin/rar
ln -s /usr/local/rar/unrar /usr/local/bin/unrar

#安装bat(比cat支持更多功能)
wget -c http://repo.openfusion.net/centos7-x86_64/bat-0.7.0-1.of.el7.x86_64.rpm
yum install -y bat-0.7.0-1.of.el7.x86_64.rpm

# 编写脚本
vim /root/.fzf/file_preview.py
#输入以下内容
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys


def fzf_preview(rg_name):
    rg_list = rg_name.split(':')
    if len(rg_list) == 1:
        bat_range = 0
    else:
        bat_range = rg_list[1].replace('\n', '')
    file_path_list = rg_list[0].replace('\n', '').split('/')
    for i, filep in zip(range(len(file_path_list)), file_path_list):
        path_space = filep.find(' ')
        if not path_space == -1:
            file_path_list[i] = "'{}'".format(filep)
        file_path = '/'.join(file_path_list)
    preview_nameandline = [file_path, bat_range]
    return preview_nameandline


if __name__ == "__main__":
    rg_name = sys.stdin.readlines()[0]
    preview_nameandline = fzf_preview(rg_name)
    differ = preview_nameandline[0].replace("'", '').lower()
    if os.path.isdir(preview_nameandline[0]):
        os.system('ls -la {}'.format(preview_nameandline[0]))
    elif differ.endswith('.gz'):
        os.system('gzip -l {}'.format(preview_nameandline[0]))
    elif differ.endswith('.xz'):
        os.system('xz -l {}'.format(preview_nameandline[0]))
    elif differ.endswith('.gz'):
        os.system('gzip -l {}'.format(preview_nameandline[0]))
    elif differ.endswith('.zip'):
        os.system('unzip -l {}'.format(preview_nameandline[0]))
    elif differ.endswith('.rar'):
        os.system('unrar l {}'.format(preview_nameandline[0]))
    elif differ.endswith('.torrent'):
        os.system('transmission-show {}'.format(preview_nameandline[0]))
    elif differ.endswith('.json'):
        os.system('cat {}|jq'.format(preview_nameandline[0]))
    elif differ.endswith(('.html', '.htm', '.xhtml')):
        os.system('w3m -dump {}'.format(preview_nameandline[0]))
    elif differ.endswith(('.tar.gz', '.tar.xz', '.tar.bz2')):
        os.system('tar -tvf {}'.format(preview_nameandline[0]))
    else:
        f = os.popen("file {}|grep text|wc -l".format(preview_nameandline[0]))
        if int(f.readline().strip()) > 0:
            os.system('bat --style=numbers --color=always {}'.format(preview_nameandline[0]))
        else:
            os.system('echo "This file does not support preview!"')

2. 配置FZF

# 添加环境变量
vim ~/.bashrc
# 追加以下内容
export FZF_DEFAULT_COMMAND='fd --hidden --follow -E ".git" -E "node_modules" . /etc /usr /home'
export FZF_DEFAULT_OPTS='--height 90% --layout=reverse --border --preview "echo {} | /root/.fzf/file_preview.py"'

# use fzf in bash and zsh
# Use ~~ as the trigger sequence instead of the default **
#export FZF_COMPLETION_TRIGGER='~~'

# Options to fzf command
#export FZF_COMPLETION_OPTS=''

# Use fd (https://github.com/sharkdp/fd) instead of the default find
# command for listing path candidates.
# - The first argument to the function ($1) is the base path to start traversal
# - See the source code (completion.{bash,zsh}) for the details.
_fzf_compgen_path() {
  fd --hidden --follow -E ".git" -E "node_modlsules" . /etc /home
}

# Use fd to generate the list for directory completion
_fzf_compgen_dir() {
  fd --type d --hidden --follow -E ".git" -E "node_modules" . /etc /home
}

[ -f ~/.fzf.bash ] && source ~/.fzf.bash
0
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin

评论区