1

I want to print all the files in my chosen directory. I want to format the files to be displayed under the column headers. But when I'm trying to run the below code I'm getting errors in the console.

list_files()
{   
    FILES=/home/student/.junkdir/
    echo "Listing files in Junk Directory"
    format="%8s%10s%10s   $-s\n"
    printf "$format" "Filename" "Size(Bytes)" "Type"
    printf "$format" "--------" "-----------" "----"
    for listed_file in $FILES; do
        file_name=$(du $listed_file | awk '{print $2}')
        file_size=$(du $listed_file | awk '{print $1}')
        file_type=$(file $listed_file | cut -d ' ' -f2-)
        printf "$format" $file_name $file_size $file_type
    done
}

This is the output

Listing files in Junk Directory
FilenameSize(Bytes)      Type   hBs
-------------------      ----   hBs
du: cannot access ‘/home/student/.junkdir/*’: No such file or directory
du: cannot access ‘/home/student/.junkdir/*’: No such file or directory
ERROR:    cannot      open   hBs
`/home/student/.junkdir/*'       (No      such   hBs
file        ordirectory)   hBs
9
  • 3
    Did you edit the script when posting it? The error messages lead me to think your for loop is actually for listed_file in $FILES/* with a wildcard. Commented Oct 17, 2017 at 16:32
  • The directory "/home/student/.junkdir/" exists? Commented Oct 17, 2017 at 16:32
  • Yes sorry, my original code had the wildcard. I think I've realised part of my problem. Commented Oct 17, 2017 at 16:36
  • See here: cyberciti.biz/faq/bash-for-loop FILES is not what you think it is. Commented Oct 17, 2017 at 16:36
  • I didn't actually have any files in the .junkdir yet. Now that I've added the wildcard again, I am getting more success Commented Oct 17, 2017 at 16:37

1 Answer 1

1

Use the stat command to get the data, and column to make the output pretty.

stat -c $'%n\t%s\t%F' * | column -ts $'\t'

In a function

list_files() {   
    local dir=/home/student/.junkdir/
    echo "Listing files in Junk Directory"
    {
        printf "%s\t%s\t%s\n" "Filename" "Size(Bytes)" "Type"
        stat -c $'%n\t%s\t%F' "$dir"/*
    } | column -t -s $'\t'
}
Sign up to request clarification or add additional context in comments.

4 Comments

Could you advise how to use this method using the same commands I have in my example? I've tried it myself but not having any joy.
You only need to call du once: read -r filesize filename < <(du "$listed_file")
files are allowed to have spaces and colons, so I would use substrings to chop off the name from the file output: output=$(file "$listed_file"); filetype=${output:${#listed_file}+2}
The <(...) is a process substitution. For the substring stuff, see parameter expansion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.