Introductiontolinux




Download 4,86 Mb.
Pdf ko'rish
bet13/18
Sana23.12.2023
Hajmi4,86 Mb.
#127288
1   ...   10   11   12   13   14   15   16   17   18
Bog'liq
introductiontolinux

24
 |
Creating Files
Please note: To complete this exercise, you must have completed the previous exercises, and have 
a directory called ‘unixintro’ in your home directory
In this exercise, we created a new file called ‘sample.txt’, and then wrote the text ‘Hello World’ to 
it. This was done using the command ‘echo’. ‘echo’ will print whatever text you feed into it. Try it 
now on the command line by typing: ‘echo “Hello everyone” ‘. This will print ‘Hello everyone’ on 
the command line. To write this text to a file, we redirected the output using the ‘>’ option. This 
redirected the example text ‘Hello World’ into the file called ‘sample.txt’. Once we did this, we then 
read the contents of the file by using the ‘cat’ command (short for concatenate). 
Try running the command again, but this time replace ‘Hello World’ with some text of your own. 
Make sure to write to the file ‘sample.txt’. Remember; if you use spaces in a command, you must 
encapsulate that section of the command in speech-marks (“) (as per the example).
Now cat the file. What do you notice?
The words ‘Hello World’ have now vanished and been replaced with the text you inputted. This is 
not very useful if we want to ‘append’ (add) text to the file. This time, run the command again (with 
different text), but instead of a single ‘>’ use two: ‘>>’. Now ‘cat’ the file again. This time you should 
see that the text was added to the bottom of the file.
Play around with the ‘echo’ command, and use the ‘man’ pages to find out more about it.
cd ~
pwd
touch unixintro/sample.txt
cd unixintro
ls -la
echo “Hello World” > sample.txt
cat sample.txt
Changes Directory to your home 
directory (donated by ~)
Print the current working directory - 
You should be in /home/msai135 (where 
msai135 is your own username)
Create a Text file in hte folder 
‘unixintro’ called sample.txt
Change your Working Directory to 
‘unixintro’
List the contents of the working 
directory. What does ‘-la’ do?
Write the text “Hello World” to the 
file you created (sample.txt)
Concatenate (read) the file ‘sample.
txt. ‘cat’ will print the contents.



Download 4,86 Mb.
1   ...   10   11   12   13   14   15   16   17   18




Download 4,86 Mb.
Pdf ko'rish