Skip to main content

Posts

Showing posts from August, 2015

Linux find command (exec vs xargs)

As a matter of fact, i detest having to learn more than one method to achieve a job when it comes to shell scripting. But most of the time, sysadmins should find their needs to be met in the best way.
Find has the -exec option to perform actions on the files that are found. It is a common way of deleting unnecessary files without xargs.
$ find . -name "*.tmp" -type f -exec rm -f {} \;
In the above example "{}" is safe to substitute for every file with a space in its name. But "rm" command is executed once for every single file that is found. If we think about tons of files to be removed then a lot of fork processes are likely to take place.
How about using xargs:
$ find . -name "*.tmp" -type f -print0 | xargs -0 -r rm -f
With xargs, "rm" will be executed once for all files, decreasing overhead of the fork. It would be safe to use "-print0" option for files with space. Xargs "-r" option is for not running if stdin is …

Ansible Playbook for cleaning all print jobs

- hosts: print_servers   tasks:     - name: Clears all print jobs from the queues of the specified printers.       shell: for i in $(/usr/bin/lpstat -o {{ item }} | awk '{ print $1 }'); do /usr/bin/cancel $i; done       with_items:         - printer1         - printer2