Posted in Linux commands

“find” with Style, Part 3: Displaying File Attributes

If you search files with the find command, you get a list of filenames:

$ find . -name '*.py'
./projekte/sensornet/bmp280/fill_bmp280.py
./projekte/sensornet/bmp280/baro.py
./github/pi_stuff/unsorted/cheerlights.py
./github/pi_stuff/sensornet/bmp280/baro.py
./progs/python/mqtt/mqtttest.py
./progs/python/database/get_last_bmp280.py

If you need the file attributes (like size, file permissions, …) you can do some tricks to let ls do the job. Or do you?

find has the parameter -ls which does this:

$ find . -name '*.py' -ls

Posted in Linux commands

“find” with Style, Part 1: executing commands

How to find files and do some action with them? E. g. you want to find log files and want to see their content. Or you want to delete .o files.
Every now and then you can see examples like

find . -name ‘*.log’ -exec less {} \;

What happens here? For every found file find will start a less with this single file.
Now suppose you have several log files you want to browse through.

Every time find finds a file a new less is started. In this case the additional load is negligible, but imagine you want to delete thousands of .o files (this will be handled in a later blog post).

The better way is to terminate the -exec with +:

find . -name ‘*.log’ -exec less {} +

What happens here? Find collects the file names (until the maximum of a command line is reached) and and starts one command with many file names. With this less example you can check this by opening the next or previous file – which is not possible with the first example above.