AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1039235
Accepted
WinEunuuchs2Unix
WinEunuuchs2Unix
Asked: 2018-05-23 18:45:51 +0800 CST2018-05-23 18:45:51 +0800 CST 2018-05-23 18:45:51 +0800 CST

如何使定位输出看起来像`ll`或`ls -la`但更好?

  • 772

定位命令输出有点乏味:

$ time locate etc/profile
/etc/profile
/etc/profile.d
/etc/profile.d/appmenu-qt5.sh
/etc/profile.d/apps-bin-path.sh
/etc/profile.d/bash_completion.sh
/etc/profile.d/cedilla-portuguese.sh
/etc/profile.d/jdk.csh
/etc/profile.d/jdk.sh
/etc/profile.d/vte-2.91.sh

real    0m0.696s
user    0m0.671s
sys     0m0.024s

我怎样才能给它更多的信息呢ll?ls -la也许也包括标题?

command-line ls locate
  • 2 2 个回答
  • 3233 Views

2 个回答

  • Voted
  1. muru
    2018-05-23T19:04:14+08:002018-05-23T19:04:14+08:00

    使用xargs和ls:

    $ locate -0 etc/profile | xargs -0 ls -ld
    -rw-r--r-- 1 root root  575 Oct 23  2015 /etc/profile
    drwxr-xr-x 2 root root 4096 Apr 20 10:10 /etc/profile.d
    -rw-r--r-- 1 root root   40 Nov 30  2015 /etc/profile.d/appmenu-qt5.sh
    -rw-r--r-- 1 root root  663 May 18  2016 /etc/profile.d/bash_completion.sh
    -rw-r--r-- 1 root root 1003 Dec 29  2015 /etc/profile.d/cedilla-portuguese.sh
    -rwxr-xr-x 1 root root   31 Oct 18  2017 /etc/profile.d/go.sh
    -rwxr-xr-x 1 root root  301 Feb 20  2013 /etc/profile.d/jdk.csh
    -rwxr-xr-x 1 root root  299 Feb 20  2013 /etc/profile.d/jdk.sh
    -rw-r--r-- 1 root root  999 Aug 23  2017 /etc/profile.d/libvirt-uri.sh
    -rw-r--r-- 1 root root 1941 Mar 16  2016 /etc/profile.d/vte-2.91.sh
    -rw-r--r-- 1 root root 1557 Apr 15  2016 /etc/profile.d/Z97-byobu.sh
    
    • 3
  2. Best Answer
    WinEunuuchs2Unix
    2018-05-23T18:45:51+08:002018-05-23T18:45:51+08:00

    llocate我为此目的创建了一个脚本:

    $ time llocate etc/profile
    ACCESS      OWNER  GROUP  SIZE  MODIFIED      NAME (updatdb last ran: 2018-07-01 11:30:05)
    -rw-r--r--  root   root   575   Nov 12  2017  /etc/profile
    drwxr-xr-x  root   root   4096  Jun  4 17:19  /etc/profile.d
    -rw-r--r--  root   root   40    Feb 16  2017  /etc/profile.d/appmenu-qt5.sh
    -rw-r--r--  root   root   580   Oct 18  2017  /etc/profile.d/apps-bin-path.sh
    -rw-r--r--  root   root   663   May 18  2016  /etc/profile.d/bash_completion.sh
    -rw-r--r--  root   root   1003  Dec 29  2015  /etc/profile.d/cedilla-portuguese.sh
    -rwxr-xr-x  root   root   301   Feb 20  2013  /etc/profile.d/jdk.csh
    -rwxr-xr-x  root   root   299   Feb 20  2013  /etc/profile.d/jdk.sh
    -rw-r--r--  root   root   1941  Mar 16  2016  /etc/profile.d/vte-2.91.sh
    
    real    0m0.760s
    user    0m0.754s
    sys     0m0.020s
    

    与常规命令.76相比,运行需要几秒钟。差异可以忽略不计。.70locate


    bash 脚本

    bash 代码相当简单。将下面的脚本复制到目录中/home/YOUR_NAME/bin(您可能必须先创建它)或/usr/local/bin使用以下命令将其标记为可执行:

    chmod a+x /home/YOUR_NAME/bin/llocate`
    

    这是脚本llocate:

    #!/bin/bash
    
    # NAME: llocate
    # PATH: /mnt/e/bin
    # DATE: May 22, 2018. Modified July 5, 2020.
    # DESC: Use locate command but format output like `ll` with headings
    # PARM: Parameter 1 = locate search string
    
    # UPDT: 2018-07-01 Format date with Time or Previous Year like `ls -al`.
    #       2018-11-09 Filenames trunctated after first space.
    #       2020-07-05 Speed up processing. Handle permission denied.
    
    if [[ $# -eq 0 ]]; then
        echo "First parameter must be full or partial file names to search for."
        exit 1
    fi
    
    tmpLine=$(locate "$1")
    
    # Was anything found?
    if [[ ${#tmpLine} -eq 0 ]] ; then
        echo "No files found. If files created today, did you run 'sudo updatedb' after?"
        exit 1
    fi
    
    LastRun=$(stat --printf=%y /var/lib/mlocate/mlocate.db | sed 's/\.[^\n]*//')
    
    # Build output with columns separated by "|" for column command
    tmpForm="ACCESS|OWNER|GROUP|SIZE|MODIFIED|NAME (updatdb last ran: $LastRun)"$'\n'
    
    ThisYear=$(date +%Y)
    
    while read -r Line; do
    
        StatLine=$(stat --printf='%A|%U|%G|%s|%Y|%N\n' "$Line" | sed "s/'//g")
    
        IFS="|" Arr=($StatLine)
        Seconds="${Arr[4]}"
        [[ $Seconds == "" ]] && continue    # Permission denied
    
        # Format date with time if it's this year, else use file's year
        if [[ $(date -d @$Seconds +'%Y') == "$ThisYear" ]]; then
            HumanDate=$(date -d @$Seconds +'%b %_d %H:%M')
        else
            HumanDate=$(date -d @$Seconds +'%b %_d  %Y')
        fi
    
        StatLine="${StatLine/$Seconds/$HumanDate}"
        tmpForm="$tmpForm$StatLine"$'\n'
    
    done <<< "$tmpLine"                           # Read next locate line.
    
    column -t -s '|' <<< "$tmpForm"
    
    exit 0
    
    • 2

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve