If you have a variable myPath, you can use:

echo > "${myPath}/fileName"

as per the following transcript:

pax:~$ export myPath="/tmp"

pax:~$ ls -al /tmp/xyzzy
ls: cannot access /tmp/xyzzy: No such file or directory

pax:~$ echo > "${myPath}/xyzzy"

pax:~$ ls -al /tmp/xyzzy
-rw-r--r-- 1 pax pax 1 2011-12-06 12:30 /tmp/xyzzy
Answer from paxdiablo on Stack Overflow
Discussions

Full Bash script on creating txt file, directory, copy file, display info - Unix & Linux Stack Exchange
Im still very new to codeing and Im still learning the basics and would love some help with explanation to learn what to do. Im stuck on this matter atm: Bash script to create a file with text inside and then create a new directory on /home, then copy the file and change the name into the ... More on unix.stackexchange.com
๐ŸŒ unix.stackexchange.com
September 27, 2021
How to create a create a directory with files?
The command mkdir creates a folder, touch creates a file and multiple tools can be used to populate said file depending on what you want to do. Echo is probably the most common one which is entirely unfit for purpose with large files if you ask me... but each to their own. The command cp will copy files from one place to another in the event youโ€™ve already got the files in place. More on reddit.com
๐ŸŒ r/bash
8
1
February 12, 2020
unix - Create text file and fill it using bash - Stack Overflow
Creating a text file in unix can be done through a text editor (vim, emacs, gedit, etc). But what you want might be something like this ... That will put the text 'insert text here' into a file myfile.txt. To verify that this worked use the command 'cat'. ... Sign up to request clarification or add additional context in comments. ... If you're wanting this as a script, the following Bash ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
bash - Shell script to create a file if it doesn't exist? - Unix & Linux Stack Exchange
Yes, it's a Bash builtin, too, but there's also a binary at /usr/bin/[ on most systems. ... Permissions of the created file or directory component are affected by the current umask value. If you don't want the "modification" time of an existing file.txt to be changed by touch, you can use touch -a /Scripts... More on unix.stackexchange.com
๐ŸŒ unix.stackexchange.com
๐ŸŒ
Super User
superuser.com โ€บ questions โ€บ 1291646 โ€บ how-do-i-create-a-file-within-a-directory-at-the-same-time
bash - How do I create a file within a directory at the same time? - Super User
February 3, 2018 - ... You can chain commands together in bash like this: mkdir folder1 && touch folder1/file1 if that's what you mean ? You can even create a 100 files with mkdir folder1 && touch folder1/file{1..100}.
๐ŸŒ
nixCraft
cyberciti.biz โ€บ nixcraft โ€บ howto โ€บ linux โ€บ how to create a file in linux using the bash terminal
How to create a file in Linux using the bash terminal - nixCraft
June 24, 2025 - Like format should be name_fol, i need to specify the name like Kenny or Jenny when it prompts and the script needs to create a file Kenny_fol.txt or Jenny_fol.txt .. ... #!/bin/bash file="" read -p "May I know your file name please?
๐ŸŒ
Stack Exchange
unix.stackexchange.com โ€บ questions โ€บ 670911 โ€บ full-bash-script-on-creating-txt-file-directory-copy-file-display-info
Full Bash script on creating txt file, directory, copy file, display info - Unix & Linux Stack Exchange
September 27, 2021 - #!/bin/bash mkdir -p $HOME/newdirectory ls $HOME > $HOME/newdirectory/ls_home.txt ... yes i do believe its something like this. But instead of directly creating the file with whats on my home page into the directory. Its more like creating the file that shows my home page info, then creating a directory. Then copying that file into that new directory. And all of this should be a single script aswell.
Find elsewhere
Top answer
1 of 6
73

Possibly simpler solution, no need to do explicit tests, just use:

mkdir -p /Scripts &&
  touch /Scripts/file.txt

Permissions of the created file or directory component are affected by the current umask value.

If you don't want the "modification" time of an existing file.txt to be changed by touch, you can use touch -a /Scripts/file.txt to make touch only change the "access" and "change" times.

Beware that if /Scripts/file.txt exists and is of type symlink, that will create or touch the target of the symlink if the parent directory of that target exists but will not create that directory otherwise.

2 of 6
43

You are getting the error because there is no space between [ and ! however there are also some flaws in your code. First you are checking if the file does not exist, and if not you are doing nothing. If the file DOES exist you are making a directory (but not doing anything to create the file).

You also don't need the null operation, you should be able to simply do:

#! /bin/bash -
if [[ ! -e /Scripts/file.txt ]]; then
    mkdir -p /Scripts
    touch /Scripts/file.txt
fi

[command2]

This is checking if /Scripts/file.txt does not exist it will create the /Scripts directory and then the file.txt file. You could also check for the existence of the directory separately if you wanted. Additionally notice I am using -e instead of -f as you asked simply to check for the existence of a file which is what -e will do where -f checks that it is a "regular file" http://tldp.org/LDP/abs/html/fto.html

๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-create-a-new-file-in-linux-from-bash
How to Create a File in Linux from the Command Line?
You can simply use the redirection operator ">" to create a new blank file in the current directory.
Top answer
1 of 2
3

If I understand you correctly, you are asking how to pass values to a bash script. This is very easy, for example:

#!/usr/bin/env bash
directory=$1;
echo "Directory is $directory"

$1 is the first command line argument of a bash script. $2 is the second etc etc. So, you could run the script above like so:

./foo.sh /path/to/bar
Directory is /path/to/bar

If you want the command to be a variable as well, you could do something like this:

 #!/usr/bin/env bash
 command=$1;
 directory=$2
 $command $directory

So, to run ls /etc, you would run the script above like this:

./foo.sh ls /etc
2 of 2
0

I imagine that you have files in a place (/path/to/originals) and want to copy them to a target location (/path/to/destination) and modify them afterwards. Your current script looks like:

mkdir /path/to/destination
cp /originals/this-file /path/to/destination
cp /originals/this-other-file /path/to/destination
modify-somehow /path/to/destination/this-file
modify-somehow /path/to/destination/this-other-file

but you don't like to have to hardcode /path/to/destination everywhere. So you can ask to use "the value of the first positional parameter" instead of hardcoding /path/to/destination. As others mentioned, the value of the first positional parameter is $1.

So your script should be:

mkdir $1
cp /originals/this-file $1
cp /originals/this-other-file $1
modify-somehow $1/this-file
modify-somehow $1/this-other-file

And you should invoke this by adding the destination path as an argument:

my-script /path/to/destination

I tried to keep the script simple, but you could improve it, like using a single cp command to copy several files. You can also use a variable for your /originals path (but instead of an argument, this one sounds like a constant declaration at the beginning of your script)

Lastly, consider that if your filenames have spaces, you'll need to surround your $1 in double quotes.

๐ŸŒ
Ask Ubuntu
askubuntu.com โ€บ questions โ€บ 1239549 โ€บ shell-script-to-create-a-folder-with-files-name-and-copy-files-inside
bash - Shell script to create a folder with files name and copy files inside - Ask Ubuntu
May 14, 2020 - ABC_100.0_k_1x1x1.xyz ... ... #!/bin/bash for file in *_???.*_?x?x?.* do dir=${file%} dir=${dir%.*} mkdir -p "./$dir" && scp -r "$file" "./$dir" done...
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ linux โ€บ bash create directory
How to Create Directory in Bash | Delft Stack
February 2, 2024 - #!/bin/bash function mkdirectory() ... user gives the input through arguments. The user inputs the directory name like this, ./rcrdir.sh Dir. The rcrdir.sh is the Bash file name, and Dir is the directory name the user wants to ...
๐ŸŒ
Tania's Website
taniarascia.com โ€บ how to create and use bash scripts
How to Create and Use Bash Scripts | Tania Rascia's Website
Note: In order to run a bash script without specifying the directory (using ./, for example) you would have to add the directory of the script to the PATH by running export PATH=$PATH:/path/to/script/directory.
๐ŸŒ
SwCarpentry
swcarpentry.github.io โ€บ shell-novice โ€บ 03-create.html
The Unix Shell: Working With Files and Directories
4 days ago - Delete, copy and move specified files and/or directories. Create files in that hierarchy using an editor or by copying and renaming existing files.
๐ŸŒ
Quora
quora.com โ€บ What-command-can-you-use-to-create-a-file-using-Bash-Why
What command can you use to create a file using Bash? Why? - Quora
The syntax is straightforward: touch [filename] will create a new file with the specified name in the current working directory. For example, to create a new file named "example.txt", you would use the following command: ... To create a file ...
๐ŸŒ
nixCraft
cyberciti.biz โ€บ nixcraft โ€บ howto โ€บ bash shell โ€บ how to create files in linux from a bash shell prompt
How To Create Files in Linux From a Bash Shell Prompt - nixCraft
August 14, 2017 - Iโ€˜m a new to CentOS Linux user. How do I create a file from bash prompt without using GUI tools? Linux / UNIX like operating system offers many command line tools and text editors for creating text files. You can use vi or joe text editor. It is a terminal-based text editor for Linux/Unix like systems, available under the GPL.