Shell script to generate random files using RANDOM variable

by on April 10, 2008 · 3 comments

  1. #!/bin/bash
  2. # Shell script to generate random file method # 1
  3. #
  4. # This is a free shell script under GNU GPL version 2.0 or above
  5. #
  6. # Copyright (C) 2005 nixCraft
  7. #
  8. # Feedback/comment/suggestions : http://cyberciti.biz/fb/
  9.  
  10. echo "List of temporary files : "
  11. for i in 1 2 3 4 5
  12. do
  13. FILE="/tmp/$(basename $0).$RANDOM.txt"
  14. echo $FILE # show file name
  15. > $FILE # create files
  16. done


4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!

Click here to subscribe via email.

  • Nerf

    This is bad practice. A much better way would be to use $$, which is the process ID. In the loop above I would create a filename of “/tmp/$(basename $0).$$.txt”

  • guy

    or both… /tmp/$(basename $0).$RANDOM.$$.txt

    say hello to the safe side

    :D

  • doukremt

    Not testing if the file to be created already exists is a rather bad idea

Previous Script:

Next Script: