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 OverflowIf 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
Just use:
echo > ${WHATEVER}/fileName
Just use output redirection. E.g.
#!/bin/bash
echo "some file content" > /path/to/outputfile
The > will write all stdin provided by the stdout of echo to the file outputfile here.
Alternatively, you could also use tee and a pipe for this. E.g.
echo "some file content" | tee outputfile
Be aware that any of the examples will overwrite an existing outputfile.
If you need to append to a currently existing file, use >> instead of > or tee -a.
If you don't accept user input in this line, no user input can change the behaviour here.
I think it is superior to use a here doc to create a new file in a script. It is cleaner looking, which I believe encourages readability.
For example:
cat > filename <<- "EOF"
file contents
more contents
EOF
The "-" in <<- is optional, allowing for tabbed indents which will be stripped when the file is created. The quotes around the "EOF" prevent the "here doc" from doing any substitutions.
Full Bash script on creating txt file, directory, copy file, display info - Unix & Linux Stack Exchange
How to create a create a directory with files?
unix - Create text file and fill it using bash - Stack Overflow
bash - Shell script to create a file if it doesn't exist? - Unix & Linux Stack Exchange
Videos
Instead of using the read command in order to get the value ofNEW_DIR from the input, You can set hard-coded value for the NEW_DIR variable in the following way:
replace the following line in your script:
read NEW_DIR
with the following line:
NEW_DIR="new_dir_hard_coded_value"
Link for more info about bash-scripting-tutorial/bash-variables
If you want a surprise, instead of hardcoding the name you could use a technique to generate a random string, for example
NEW_DIR=$(tr -cd '[:alnum:]' < /dev/urandom | fold -w8 | head -n1)
This sets NEW_DIR to a string of eight alphanumeric characters. Every time you run the script, the new directory will have a different random name...
Or to get a random word, pick a dictionary from /usr/share/dict/ and use shuf, for example:
$ shuf -n1 /usr/share/dict/british-english
soupier
$ shuf -n1 /usr/share/dict/british-english
penguins
So
NEW_DIR=$(shuf -n1 /usr/share/dict/british-english)
mkdir "$NEW_DIR"
...
Hi everyone I am a total noob in the bash world but I find it SO exciting. I am trying to make a script that creates a folder with a js file, a css file and a json file. I don't know exactly what I should search for to find the answer. A hint will be greatly appreciated. Thanks
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
echo "insert text here" > myfile.txt
That will put the text 'insert text here' into a file myfile.txt. To verify that this worked use the command 'cat'.
cat myfile.txt
If you want to append to a file use this
echo "append this text" >> myfile.txt
If you're wanting this as a script, the following Bash script should do what you want (plus tell you when the file already exists):
#!/bin/bash
if [ -e $1 ]; then
echo "File $1 already exists!"
else
echo >> $1
fi
If you don't want the "already exists" message, you can use:
#!/bin/bash
if [ ! -e $1 ]; then
echo >> $1
fi
Edit about using:
Save whichever version with a name you like, let's say "create_file" (quotes mine, you don't want them in the file name). Then, to make the file executatble, at a command prompt do:
chmod u+x create_file
Put the file in a directory in your path, then use it with:
create_file NAME_OF_NEW_FILE
The $1 is a special shell variable which takes the first argument on the command line after the program name; i.e. $1 will pick up NAME_OF_NEW_FILE in the above usage example.
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.
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
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
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.
install is your friend:
install -Dv /dev/null some/new/path/base-filename
Here's a shell function:
mkfileP() { mkdir -p "$(dirname "$1")" || return; touch "$1"; }
# Sample call
mkfileP "./newSubDir/test.txt" && echo 'created or touched' || echo 'failure'
You can place it in your shell profile, for instance.
Alternatively, implement it as a script (add error handling and command-line help as needed):
#!/usr/bin/env bash
mkdir -p "$(dirname "$1")" || exit
touch "$1"
mkdir B && touch B/myfile.txt
Alternatively, create a function:
mkfile() { mkdir -p -- "$1" && touch -- "$1"/"$2" }
Execute it with 2 arguments: path to create and filename. Saying:
mkfile B/C/D myfile.txt
would create the file myfile.txt in the directory B/C/D.
For this purpose, you can create your own function. For example:
$ echo 'mkfile() { mkdir -p "$(dirname "$1")" && touch "$1" ; }' >> ~/.bashrc
$ source ~/.bashrc
$ mkfile ./fldr1/fldr2/file.txt
Explanation:
- Insert the function to the end of
~/.bashrcfile using theechocommand - The
-pflag is for creating the nested folders, such asfldr2 - Update the
~/.bashrcfile with thesourcecommand - Use the
mkfilefunction to create the file
touch is not able to create directories, you need mkdir for that.
However, mkdir has the useful -p/--parents option which creates a full directory structure.
From man mkdir:
-p, --parents
no error if existing, make parent directories as needed
So the command you need in your specific situation is:
mkdir -p ~/Desktop/a/b/c/d/e/f/g/h/ && touch ~/Desktop/a/b/c/d/e/f/g/h/1.txt
If you think you will need this more often and don't want to type the path twice every time, you can also make a Bash function or a script for it.
Bash function (append this line to
~/.bashrcto persitently have it available to your user, otherwise it will vanish again when you exit your terminal):touch2() { mkdir -p "$(dirname "$1")" && touch "$1" ; }It can be simply used like this:
touch2 ~/Desktop/a/b/c/d/e/f/g/h/1.txtBash script (store it in
/usr/local/bin/touch2using sudo to make it available for all users, else in~/bin/touch2for your user only):#!/bin/bash mkdir -p "$(dirname "$1")" && touch "$1"Don't forget to make the script executable using
chmod +x /PATH/TO/touch2.After that you can also run it like this:
touch2 ~/Desktop/a/b/c/d/e/f/g/h/1.txt
mkdir -p parent/child && touch "$_"/file.txt
From the BASH man page under Shell Variables:
_ โฆ expands to the last argument to the previous simple command executed in the foreground, after expansion.