Shell scripting is a powerful tool for automating tasks in Unix and Linux environments. In this tutorial, we’ll explore how to create a directory and a file within it using a simple shell script. This fundamental skill is crucial for anyone looking to streamline their workflow in a Unix-like system.
Create a New Script File: Use a text editor (like nano
or vi
) to create a new file. For example:
Add the following lines to the script:
#!/bin/bash
# Create a directory named MyFolder
mkdir MyFolder
# Navigate to MyFolder
cd MyFolder
# Create a file named MyFile.txt
touch MyFile.txt
Save and Exit: Save your script and exit the editor. In nano
, you can do this by pressing CTRL+X
, then Y
, and Enter
.
Step 3: Making the Script Executable
Before running the script, you need to make it executable. Type the following command:
chmod +x myscript.sh
Now, run the script with:
./myscript.sh
Check if the directory and file were created successfully:
ls MyFolder/
You should see MyFile.txt
listed.
Read more on Shell and Linux related articles
Other urls to refer