1

Please can anybody explain what exactly these commands and options do:

find /home/mike/Duck/ -type f -iname '*.txt' -print 0 | while IFS= read -r -d '' f;
 do lpr "$f" && mv "$f" /home/mike/Duck/printed/ ; done

and what if i want to print all extension in that directory what should i do ?

1 Answer 1

4

The code snippet you have given requires a modification, you need -print0 instead of print 0.

This script will find (find) all the files having extension txt in /home/mike/Duck/, send them to printer (lpr) one at a time and then if that returns a success (&&) then the file will be moved (mv) to /home/mike/Duck/printed/.

If you want to print files having any extensions, use this:

find /home/mike/Duck/ -type f -name '*.*' -print0 | while IFS= read -r -d '' f;
 do lpr "$f" && mv -i "$f" /home/mike/Duck/printed/ ; done

This assumes that all files having extensions are in the format file.ext, it will also print the files having multiple dots. The -i with mv will give you a prompt before overwriting.

Also note that this can be done using only find:

find /home/mike/Duck/ -type f -name '*.*' -exec lpr {} \; -exec mv -i {} /home/mike/Duck/printed/ \;
5
  • I don't think it's the use of read -d '' that removes the need to unset IFS, is it? rather, it is because bash's read slurps "leftover words and their intervening separators" into the last (in this case, the only) variable. May 24, 2015 at 1:35
  • @steeldriver For some weird reason, I thought -d will take precedence over IFS rather than only the newline (as I have checked now)..modifying..
    – heemayl
    May 24, 2015 at 1:48
  • I think the purpose of -i to overwrite the file , if the file has been previously printed May 24, 2015 at 2:53
  • @Serg Its other way around actually.. mv -i will give you a prompt if a file having same name already exists..
    – heemayl
    May 24, 2015 at 3:00
  • @heemayl ah, right. Because the script clears the Duck folder of printed files and moves them to printed. May 24, 2015 at 3:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .