0

Right now i am able to list all the files using -l, and i can break it down to filename etc. using awk

e.g

ls -l ~/directory1 | awk '{print $9, $5}'

which returns something like

filename filesize

How could i also display the file type?

3
  • 3
    what do you mean by "file type"? the one returned by file(1)? the first column of ls -l already tells if the file is a directory, character device, regular file, etc.
    – user313992
    Commented Oct 22, 2018 at 16:23
  • 2
    You might consider using stat instead of parsing ls Commented Oct 22, 2018 at 16:26
  • 1
    Note that your snippet will fail for file names with spaces in them. Commented Oct 22, 2018 at 16:34

1 Answer 1

3

Is this what you are looking for?

find . -exec stat --format="%n %s %F" {} \;

This finds all items in the current directory, change to suit your needs, and runs the stat(1) command on each item.  The %n represents the item's name.  The %s is the item's size.  The %F is the item's type.

The find command has a -type f option to process only plain files and a -type d option for directories.

2
  • Hi, thanks this is what i was looking for, is there anyway i could get it to only display the files and not the directory itself aswell?
    – J K
    Commented Oct 22, 2018 at 17:46
  • 1
    The find command has a "-type f" to only process files. "-type d" is for directories. You can probably do something like "-type d -prune -or -type f" to have it skip directories.
    – Lewis M
    Commented Oct 22, 2018 at 17:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .