7

I make a list of all files in my PC.

FILES=$(find . -type f -name '*' -printf "%s/%f\n" | sort -n)

The output should be:

"size/file_name"
56872/file.txt
98566/test
1000254/foo

My PC give me error

find: -printf: unknown primary or operator

Any solutions?

2
  • Please expand the question to show the filenames in your directory.
    – steve
    Mar 27, 2016 at 19:19
  • 2
    More to the point, what type of system is running on the PC, and what version of find is used? Mar 27, 2016 at 19:26

2 Answers 2

6

The -printf option is not in POSIX find. It is a feature of GNU find, e.g., on Linux.

The particular implementation you are using is not shown; it might be POSIX without extensions. For instance, it is not in FreeBSD, or OSX.

Without that, you can use some alternative, e.g., this (which will not handle embedded blanks, etc., but makes few assumptions about your tools):

find . -type f -exec ls -ld {} \; | awk '{ gsub("^.*/","",$9); printf "%s/%s\n", $5, $9; }'

With more information about the available tools, it is (usually) possible to improve the solution.

3
  • Please and how can I make list with output "file_size/file_name" like above?
    – Joozty
    Mar 27, 2016 at 19:24
  • why parse ls when it would be much better to use find ... -print0 and pipe to xargs -0r stat ...? Both GNU and FreeBSD versions of stat have (unfortunately different/incompatible) options to format the output, and can display size/filename as the OP requires.
    – cas
    Mar 28, 2016 at 1:18
  • 1
    OP provided no information which would allow one to assume a particular platform-specific solution. Mar 28, 2016 at 8:04
3

find and xargs with GNU stat:

find . -type f -print0 | xargs -0r stat -c '%s/%n'

This GNU version isn't really needed for this particular question because GNU find supports printf anyway (but it might be useful in other contexts because there are things that stat can find out about a file that find ... -printf can't display).

FreeBSD & Mac OS X, however, are a different story.

find and xargs with FreeBSD (& Mac OS X) stat:

find . -type f -print0 | xargs -0r stat -f '%z/%N'

Optionally pipe the output through sed -e 's:/./:/:' to remove the ./ at the beginning of each filename.

Sample output, from my FreeBSD test VM:

# find . -type f -print0 | xargs -0r stat -f '%z/%N' | sed -e 's:/./:/:' | head
149/.k5login
254/.profile
1169/.cshrc
297/.login
5589/.history
171/.ssh/known_hosts
1803/.ssh/authorized_keys
6699/.bash_history
368/.bashrc
4065/.viminfo

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.