Как сделать вывод локации похожим на `ll` или` ls -la`, но лучше?

Вывод команды locate немного невелик:

$ 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? Возможно, включите заголовки?

0
задан 1 July 2018 в 20:51

4 ответа

Для этой цели я создал скрипт под названием 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 секунд в течение .70 секунд для регулярной команды locate. Разница незначительна.

Bash script

Код 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 1, 2018. # 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`. if [[ $# -eq 0 ]]; then echo "First parameter must be full or partial file names to search for." exit 1 fi # Create unqique temporary file names tmpLine=$(mktemp /tmp/llocate.XXXXX) tmpForm=$(mktemp /tmp/llocate.XXXXX) # Function Cleanup () Removes temporary files CleanUp () { [[ -f $tmpLine ]] && rm -f $tmpLine # Remove temporary files created [[ -f $tmpForm ]] && rm -f $tmpForm # at various program stages. } locate "$1" > $tmpLine # Was anything found? if [[ ! -s $tmpLine ]] ; then echo "No files found. If files created today, did you run 'sudo updatedb' after?" CleanUp exit 1 fi LastRun=$(stat --printf=%y /var/lib/mlocate/mlocate.db | sed 's/\.[^\n]*//') # Build output with columns separated by "|" echo "ACCESS|OWNER|GROUP|SIZE|MODIFIED|NAME (updatdb last ran: $LastRun)" \ > $tmpForm while read -r Line; do StatLine=$(stat --printf='%A|%U|%G|%s|%Y|%N\n' "$Line" | sed 's/ [^|]*//' | \ sed "s/'//g") IFS="|" Arr=($StatLine) Seconds="${Arr[4]}" # Format date with time if it's this year, else use file's year if [[ $(date -d @$Seconds +'%Y') == $(date +%Y) ]]; then HumanDate=$(date -d @$Seconds +'%b %_d %H:%M') else HumanDate=$(date -d @$Seconds +'%b %_d %Y') fi StatLine="${StatLine/$Seconds/$HumanDate}" echo "$StatLine" >> $tmpForm done < $tmpLine # Read next locate line. cat $tmpForm | column -t -s '|' CleanUp exit 0
0
ответ дан 17 July 2018 в 13:41

Для этой цели я создал скрипт под названием 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 секунд в течение .70 секунд для регулярной команды locate. Разница незначительна.

Bash script

Код 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 1, 2018. # 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`. if [[ $# -eq 0 ]]; then echo "First parameter must be full or partial file names to search for." exit 1 fi # Create unqique temporary file names tmpLine=$(mktemp /tmp/llocate.XXXXX) tmpForm=$(mktemp /tmp/llocate.XXXXX) # Function Cleanup () Removes temporary files CleanUp () { [[ -f $tmpLine ]] && rm -f $tmpLine # Remove temporary files created [[ -f $tmpForm ]] && rm -f $tmpForm # at various program stages. } locate "$1" > $tmpLine # Was anything found? if [[ ! -s $tmpLine ]] ; then echo "No files found. If files created today, did you run 'sudo updatedb' after?" CleanUp exit 1 fi LastRun=$(stat --printf=%y /var/lib/mlocate/mlocate.db | sed 's/\.[^\n]*//') # Build output with columns separated by "|" echo "ACCESS|OWNER|GROUP|SIZE|MODIFIED|NAME (updatdb last ran: $LastRun)" \ > $tmpForm while read -r Line; do StatLine=$(stat --printf='%A|%U|%G|%s|%Y|%N\n' "$Line" | sed 's/ [^|]*//' | \ sed "s/'//g") IFS="|" Arr=($StatLine) Seconds="${Arr[4]}" # Format date with time if it's this year, else use file's year if [[ $(date -d @$Seconds +'%Y') == $(date +%Y) ]]; then HumanDate=$(date -d @$Seconds +'%b %_d %H:%M') else HumanDate=$(date -d @$Seconds +'%b %_d %Y') fi StatLine="${StatLine/$Seconds/$HumanDate}" echo "$StatLine" >> $tmpForm done < $tmpLine # Read next locate line. cat $tmpForm | column -t -s '|' CleanUp exit 0
0
ответ дан 20 July 2018 в 13:45

Используя 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
ответ дан 17 July 2018 в 13:41

Используя 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
ответ дан 20 July 2018 в 13:45
  • 1
    Это было мое первоначальное мышление, но у меня были старые проблемы с xargs и я попытался использовать {} placeholder. Плюс я хотел вывести счетчик ссылок и добавить заголовок с последним временем sudo updatedb. Тем не менее у меня есть догадка, что большая часть мира будет лучше, чем ваш лайнер. Отличная работа! – WinEunuuchs2Unix 23 May 2018 в 06:27

Другие вопросы по тегам:

Похожие вопросы: