1

Just started a scripting class and could use some help. I am writing a script to inspect the file names of every file and directory in a given directory and accumulate a count of Every file, subdirectory, symlink, old files, graphics files, tmp files, exe files, and total byte count of each file within that directory.

The output needs to be formatted in this way:

  • Directories n,nnn,nnn
  • Files n,nnn,nnn
  • etc...

When I run my script, it doesn't seem to count anything because all count items return 0. I am also having issues with formatting the numbers to have comma's. $1 is the directory the user will enter for searching when they run the script ie: ./srpt /etc. To run the search I am using the find command with printf to format the output and piping the output to wc to count the number of lines. I assume this would give me the number of files granted the file names are just one line. There are probably a billion ways to do this and this way might not be the most elegant, but I would appreciate anyone's input.

if [ -d "$1" ]; then
        directories=$(find "$1" -type d -printf "%'d" | wc -l)
        files=$(find "$1" -type f -printf "%'d" | wc -l)
        sym=$(find "$1" -type l -printf "%'d" | wc -l)

etc...

#Printing the output to the terminal
      echo "Directories" $directories
      echo "Files" $files
      echo "Sym links" $sym
      exit 0
else
        echo "[ERROR]: need path to perform search"
        exit 1
fi

My output:

Directories 0
Files 0
Sym Links 0
1

You are needed change your commands to this form:

directories=$(printf "%'d\n" $(find "$1" -type f | wc -l))

printf uses the thousands separator for your current locale, which might be a comma, dot, or nothing at all. You can change locale if you want to force commas.

Also, you can do by such way:

if [ -d "$1" ]; then
    directories=$(find "$1" -type d | wc -l)
    files=$(find "$1" -type f | wc -l)
    sym=$(find "$1" -type l | wc -l)

and replace echo to printf

#Printing the output to the terminal

  printf "%s %'d\n" "Directories" $directories
  printf "%s %'d\n" "Files" $files
  printf "%s %'d\n" "Sym links" $sym

The error was happened, because you mixed up two different command - the bash's printf and the find's -printf.

For the find's -printf the "%'d" format is incorrect. The singular quote is not allowed and %d is file's depth in the directory tree. You can find all -printf options in the man find, by typing: /printf format.

By the way, it needed to add \n to both printf (bash's and find's): "%d\n", otherwise they will print the all output in the one line.

  • Wow, thank you for the feedback! My output is now formatted correctly. I didn't realize that find and bash printf functions differed. – Sharrison Sep 8 '17 at 1:36
1

-printf "%'d" does nothing there so the counts are resulting 0, you need to change it with -printf "%p\n" instead.

  • Thanks for the feedback! When I changed everything to "%p\n" I started getting correct counts for my print statements. I did go with @MiniMax's suggestion of scraping the find's -printf and using prinf instead of the echo statements to format the thousands separator. Thanks again, you steered my in the right direction! I am wondering if all of the find statements could be combined into one find statement? Would this be a more elegant way of writing the script? Would it have any relation to the execution time of the script? – Sharrison Sep 8 '17 at 1:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.