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'

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 2: Deleting Files

Now let’s delete files with style. The usual command to clean up a directory tree (e.g. to delete .o files) is

$ find . -name '*.o' -exec rm {} \;

As I wrote in an earlier post this calls rm for every single file. A first improvement is to change the terminator at the end of the line:

Continue reading ““find” with Style, Part 2: Deleting Files”

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.