Jack McIvor
Jack McIvor

Reputation: 1

How do I list all files in a directory in Linux using bash script?

I have been trying to list all the files from the "junk_dir" in a formatted style, listing them off by file name, file size and file type. Although I am not getting these printed off in a formatted way, I am getting them printed out. However, on line 10, I get an error from the command promp tsaying

./junk-skeleton.sh: line 10: [1001=: command not found

The following is my code.

#! /bin/bash 

list_junk()
{
    echo "Junk Directory"

   format="%8s%10s%10s  %-s\n"
   printf "$format" "File" "Size" "Type"
   printf "$format" "----" "----" "----"
   if [$(id -u)= "0"]; then
      dir_list="/home/student/bin/junk_dir/*"
   else
      dir_list=$HOME
   fi

  for file in $dir_list; do
      filename=$(ls -al /home/student/bin/junk_dir)
      filesize=$(wc -c /home/student/bin/junk_dir)
      filetype=$(ls --file-type /home/student/bin/junk_dir)

      printf "$format" $filename $filesize $filetype
   done
}

Also, if anybody has help formatting the printed results, I would appreciate it.

Upvotes: 0

Views: 67

Answers (1)

tink
tink

Reputation: 15204

You need spaces around both the brackets, and between the shell command and the equals sign.

if [ $(id -u) = "0" ]; then echo ROOT; fi

Upvotes: 1

Related Questions