Shell Script To Create Files and Directories That Do Not Exist

#!/bin/bash
# Shell script to create files and directories that do not exist
# This script also demonstrate use of functions and command line arguments using getopts command
# -------------------------------------------------------------------------
# Copyright (c) 2004 nixCraft project <http://www.cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# ----------------------------------------------------------------------
usage(){
	echo "Usage: $0 {-f filename} {-d dirname}"
	exit 1
}
 
createDir(){
	if [ ! -d $1 ]
	then
		/bin/mkdir -p $1 >/dev/null 2>&1 && echo "Directory $1 created." ||  echo "Error: Failed to create $1 directory."
	else
		echo "Error: $1 directory exits!"
	fi
}
 
createFile(){
	if [ ! -f $1 ] 
	then
		touch $1 > /dev/null 2>&1 && echo "File $1 created."  ||  echo "Error: Failed to create $1 files."
	else
		echo "Error: $1 file exists!"
	fi
}
 
while getopts f:d:v option
do
        case "${option}"
        in
                f) createFile ${OPTARG};;
                d) createDir ${OPTARG};;
                \?) usage
                    exit 1;;
        esac
done
Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics:
CategoryList of Unix and Linux commands
File Management cat
Firewall 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 CentOS 8 Debian 10 Firewall Ubuntu 20.04
5 comments… add one
  • Amandeep Feb 11, 2012 @ 7:08

    Yeah, this script isn’t multi-process safe. Is there any other way, which is multiprocess safe as well?

  • polo Jul 7, 2010 @ 7:40

    This script isn’t multi-process safe.
    Test it by calling the same process with same file names multiple times in a wrapper script. ‘touch’ only changes last modification time, couldn’t tell you if a file is already been created.

  • Chris F.A. Johnson Jan 3, 2010 @ 4:28
    ## Print only the command name, not the full path.
    ## Send the output to stderr, not stdout
    usage(){
      echo "Usage: ${0##*/} [-f filename ] ... | [ -d dirname ] ..." >&2
      exit 1
    }
    
    ## Quote $1 or it will fail if an argument contains whitespace
    createDir(){
      if [ ! -d "$1" ]
      then
        mkdir -p "$1" >/dev/null 2>&1 && echo "Directory $1 created." ||  echo "Error: Failed to create $1 directory."
      else
        echo "Error: $1 directory exits!"
      fi
    }
    
    ## Quote $1 or it will fail if an argument contains whitespace
    createFile(){
      ## There's no need for an external command (touch)
      [ -f "$1" ] && echo "Error: $1 file exists!" || > "$1"
    }
    
    while getopts f:d:v option
    do
      case $option in
        f) createFile "$OPTARG";;
        d) createDir "$OPTARG";;
        *) usage ;;
      esac
    done
    shift "$(( $OPTIND - 1 ))"
    
    [ $# -gt 0 ] && usage
    
  • egoleo Nov 28, 2009 @ 15:06

    michael: u can run this script by below.

    shell> chmod +x script_name
    shell> ./script_name

    above is one way of running the script where script_name is the name you give to the shellscript you saved.

    you can also run it as below;
    shell> sh script_name

  • michael Nov 6, 2009 @ 2:34

    How to run this script??

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.