Reputation: 103
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
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
Reputation: 293
#Enter designated directory
cd designatedDirectory
#Create file in directory
cat > file.txt
#OR
touch file.txt
Upvotes: -1