Shell Making Files
Making a file means creating a new file in the current folder. A new file can start empty, or it can start with words inside it.
Make an empty file
touch makes a new file with nothing in it.
touch empty.txt
wc -c empty.txtMake a file with words
echo writes words, and > puts them in a new file.
echo "Buy milk" > list.txt
cat list.txtWrite over a file
> writes over the old words when the file already exists.
echo "Buy milk" > list.txt
echo "Call Ana" > list.txt
cat list.txtMake a file in a folder
Give > a path, and the file is made in that folder.
echo "Meeting at noon." > docs/note.txt
cat docs/note.txtExample
touch blank.txt
echo "Buy milk" > list.txt
wc -c blank.txt
cat list.txtKey points
- touch: a command that creates an empty file.
- Use touch to make a new file with no words in it yet.
- Use > with echo to make a file that starts with words in it.
- wc -c: a command that counts the bytes in a file.
- Use wc -c to check that a new file holds 0 bytes.
In this example, touch makes an empty file called blank.txt. Then echo and > make a file called list.txt with one line of words in it. Next wc -c shows that blank.txt holds 0 bytes, and cat prints the line in list.txt.
Note
> replaces the words in a file if the file already exists.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
Which command makes a new empty file?