bashfindshellcheck

How to list all files in a directory without absolute paths along with their file sizes, while using find


GNU bash, version 4.4.0
Ubuntu 16.04

I am wanting to list all the files from a directory and print them to column two while printing the size of the files in column one. example

1024 test.jpg
1024 test.js
1024 test.css
1024 test.html  

I have already done so using the ls command but shellcheck is not liking it. Example:

In run.sh line 47:
ls "$localFiles" | tail -n +3 | awk '{ print $5,$9}' > "${tmp_input3}"
^-- SC2012: Use find instead of ls to better handle non-alphanumeric filenames.  

When I use the find command, it also returns the absolute path. Example:

root@me ~ # mkdir -p /home/remove/test/directory
root@me ~ # cd /home/remove/test/directory && truncate -s 1k test.css test.js test.jpg test.html && cd
root@me ~ # find /home/remove/test/directory -type f -exec ls -ld {} \; | awk '{ print $5, $9 }'
1024 /home/remove/test/directory/test.jpg
1024 /home/remove/test/directory/test.js
1024 /home/remove/test/directory/test.css
1024 /home/remove/test/directory/test.html  

What's the most efficient way to accomplish my goal. It can be any command, as long as shellcheck is cool with it, I'm happy.


Solution

  • Please try:

    find dir -maxdepth 1 -type f -printf "%s %f\n"