Janikole
Janikole

Reputation: 103

Creating a file in a specific directory using Bash?

Normally to create a file in a directory I would use:

echo > /home/user/fileName

but, in this case, my directory path I want to create the file in is stored in a variable in my script so I can't write out the path like that. How do I create a new file inside of it?

Upvotes: 10

Views: 32504

Answers (4)

paxdiablo
paxdiablo

Reputation: 881573

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

Upvotes: 11

jmr333
jmr333

Reputation: 293

 #Enter designated directory

 cd designatedDirectory

 #Create file in directory

 cat > file.txt

 #OR

 touch file.txt

Upvotes: -1

Jaco Van Niekerk
Jaco Van Niekerk

Reputation: 4182

Won't something like

touch $variable/filename 

work?

Upvotes: -1

David Schwartz
David Schwartz

Reputation: 182769

Just use:

echo > ${WHATEVER}/fileName

Upvotes: 3

Related Questions