The Cookie Machine - Click here to drag window

DUMMY TEXT - Real text set in assets/js/theCookieMachine.js

Views: 3,230β€…    Votes:  2β€…    βœ… Solution
Tags: command-line   ls   locate  
Link: πŸ” See Original Answer on Ask Ubuntu πŸ”—

Title: How to make locate output look like `ll` or `ls -la` but nicer?
ID: /2018/05/23/How-to-make-locate-output-look-like-_ll_-or-_ls-la_-but-nicer_
Created: May 23, 2018    Edited:  July 5, 2020
Upload: August 14, 2022    Layout:  post
TOC: false    Navigation:  false    Copy to clipboard:  false

I created a script called llocate for this purpose:

$ 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/
-rw-r--r--  root   root   580   Oct 18  2017  /etc/profile.d/
-rw-r--r--  root   root   663   May 18  2016  /etc/profile.d/
-rw-r--r--  root   root   1003  Dec 29  2015  /etc/profile.d/
-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/
-rw-r--r--  root   root   1941  Mar 16  2016  /etc/profile.d/

real	0m0.760s
user	0m0.754s
sys 	0m0.020s

It takes .76 seconds to run versus .70 seconds for the regular locate command. The difference is negligible.

Bash script

The bash code is fairly straight forward. Copy the script below into the directory /home/YOUR_NAME/bin (you may have to create it first) or /usr/local/bin and mark it executable using:

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

Here’s the script llocate:


# 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

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

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 == "" ]] && 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')
        HumanDate=$(date -d @$Seconds +'%b %_d  %Y')


done <<< "$tmpLine"                           # Read next locate line.

column -t -s '|' <<< "$tmpForm"

exit 0
⇧ Set of countdown timers with alarm How do you manage journald log history on ubuntu core 16  β‡©