Home > Mobile >  Listing directories with tab as delimiter
Listing directories with tab as delimiter

Time:02-02

When we list files in unix using ls -l command the output is a table with space as a separator, for example the following

(jupyter-lab) ➜  mylab ls -l
total 2
drwxr-sr-x. 2 hs0424 ragr  0 Feb  1 12:17 A bad directory
drwxr-sr-x. 2 hs0424 ragr  0 Feb  1 12:18 A very bad directory

I want to convert to a tab separated file (.tsv), just changing spaces to \t, such as ls -l | sed -E 's/ /\t/g' would not work since filenames contain spaces. Do we have better solution ?

Hard to show expected output with tabs but if we use \t as a replacement of tab, I want something as follows,

(jupyter-lab) ➜  mylab ls -l
total 2
drwxr-sr-x.\t2\ths0424\tragr\t0\tFeb 1\t12:17\tA bad directory
drwxr-sr-x.\t2\ths0424\tragr\t0\tFeb 1\t12:18\tA very bad directory

(Edit 1) We can assume access to GNU tools

CodePudding user response:

Use GNU find -printf or stat, either of which let you provide an arbitrary format string, instead of ls.

find . -mindepth 1 -maxdepth 1 -printf '%M\t%y\t%g\t%G\t%u\t%U\t%f\t%l\n'

or

# for normal cases
stat --printf='%A\t%G\t%g\t%U\t%u\t%n\n' *

# for directories where filenames could exceed command line length limit
printf '%s\0' * | xargs -0 stat --printf='%A\t%G\t%g\t%U\t%u\t%n\n'
  • Related