Monday, May 6, 2024
Homepage · ls
 Popular · Latest · Hot · Upcoming
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 14043  / 2 Years ago, tue, june 28, 2022, 1:24:36

How can the output of ls -l be modified to separate fields using tabs instead of spaces? I want to paste the output into a spreadsheet; the padding with a variable number of spaces makes it difficult to do so. To illustrate:




drwxr-xr-x 2 root root 4096 Sep 26 11:43 wpa_supplicant
-rw-r----- 1 root dialout 66 Sep 26 11:43 wvdial.conf
drwxr-xr-x 9 root root 4096 Oct 8 08:21 X11
drwxr-xr-x 12 root root 4096 Feb 18 23:31 xdg
drwxr-xr-x 2 root root 4096 Jan 31 06:11 xml
drwxr-xr-x 2 root root 4096 Nov 22 07:26 xul-ext
-rw-r--r-- 1 root root 349 Jan 13 2012 zsh_command_not_found


In the excerpt from ls -l /etc shown above, rows 1, 2 and 3 have a single digit in column 2 whereas row 4 has two. That means the alignment is achieved by using two spaces for separating columns 1 and 2 in rows 1-3, but just one space in row 4.


More From » ls

 Answers
2

I've made a shell script for the same. It takes care of the cases when the filenames have spaces or any other special characters.




#! /bin/bash

SAVEIFS=$IFS
IFS=$(echo -en "
")
for file in $(ls)
do
stat --printf="%A %h %U %G %s " $file
mod_epoch=$(stat --format="%Y" $file)
mod_month=$(date -d @$mod_epoch +"%b")
mod_day=$(date -d @$mod_epoch +"%d")
mod_time=$(date -d @$mod_epoch +"%H:%M")
printf "%s %s %s %s
" $mod_month $mod_day $mod_time $file
done
IFS=$SAVEIFS



  • Save it to a file, say ls_tab.sh

  • Make it executable:




chmod +x ls_tab.sh



  • Run it:




./ls_tab.sh


Note: This can be done by parsing the output of ls, however the reason why it should not be done is given here.


[#32024] Wednesday, June 29, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
anxietunnel

Total Points: 66
Total Questions: 120
Total Answers: 115

Location: Norway
Member since Sat, Mar 4, 2023
1 Year ago
;