Yesterday we looked at the commands grep, wc and less. Today we will show you how to store and edit files in UNIX . First, as always, you need to startup your Virtual Catmandu (hint: see our day 1 tutorial) . Start up the UNIX prompt (hint: see our day 2 tutorial) and type in the command ‘nano’:
You will be presented with the GNU nano text editor.
In this text editor you can type text or programs you can save on disk for later use. In this short tutorial I will guide you to some basic commands we will need in later tutorials. Type for instance a short text in this screen:
“Hello world. My name is …”
When you want to save this text into a file type Ctrl-o (that is pressing the Ctrl-key and ‘o’ key on your keyboard). In the bottom of the screen nano will ask for a filename.
Type for instance ‘hello.txt’ as filename as press return. The file ‘hello.txt’ is now created on disk. We can test this with the commands we learned in the previous tutorial.
First exit the nano editor by typing Ctrl-x. And type ‘cat hello.txt’
$ cat hello.txt
You will see now the text created in the nano editor. With the UNIX command ‘ls‘ you can view all the filenames in the current directory.
If you want to add more text to this file you can start again the nano editor with a file name.
$ nano hello.txt
You will again see the text you can edit and save again with Ctrl-o and exit nano with Ctrl-x.
Output of UNIX commands can also be written to a file. Lets try to find all the lines in War and Piece that contain Bolkonski and inspect the results with nano:
$ cat Documents/war_and_peace.txt | grep Bolkonski > bolkonski.txt
Here we use the key ‘>’ to redirect the output of the command grep to a file named ‘bolkonski.txt’. Next we can use nano to inspect the contents of this file.
$ nano bolkonski.txt
By the way, you don’t need to type in the complete filenames in all the commands we have shown in the examples. When you type ‘bo’ and hit the tab-key then UNIX will autocomplete the file name to ‘bolkonski.txt’. I’m lazy and would type ‘cat bol’ and press tab .
Again you can use Ctrl-x to exit nano. You can view all the files with the ls command.
bolkonski.txt Documents hello.txt Pictures Templates Videos Desktop Downloads Music Public test.fix
If you want to delete a file you can use the rm command. We can try to remove our bolkonski.txt file with like:
$ rm bolkonski.txt
This concludes our short excursion into UNIX. Monday we will be back with a new chapter: processing JSON with Catmandu. Have a nice weekend!
Continue with Day 6: Introduction to Catmandu >>