Shell script to generate random files using RANDOM variable

#!/bin/bash
# Shell script to generate random file method # 1 
#
# This is a free shell script under GNU GPL version 2.0 or above
#
# Copyright (C) 2005 nixCraft
#
# Feedback/comment/suggestions : http://cyberciti.biz/fb/
 
echo "List of temporary files : "
for i  in 1 2 3 4 5
do
   FILE="/tmp/$(basename $0).$RANDOM.txt"
   echo $FILE # show file name
   > $FILE # create files
done
Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics:
Category List of Unix and Linux commands
File Management cat
Firewall Alpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN Alpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
3 comments… add one
  • doukremt Jan 19, 2013 @ 22:11

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

  • guy May 19, 2009 @ 8:17

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

    say hello to the safe side

    :D

  • Nerf Jun 18, 2008 @ 13:40

    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”

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.