Bash - files, files and more files
Easy manipulation of files is an essential part of our administration. From creating and deleting them to adding (appending) content to them. All from the command line.
We'll look at text editors later, this is all command line stuff.
As you navigate your way around your server you begin to see that everything is made of simple files. Of course, they link up and compliment each other but the basics of a server are directories and files. Lots and lots of files.
We need to get into this action. We can start by creating the directory sandbox:
mkdir sandbox
Now move into that directory:
cd sandbox
Did you see the sneaky way I introduced the 'cd' command? cd is short for 'change directory'.
Let's create two files: one called file1.txt and the other file2.txt and have a look at them:
touch file1.txt
touch file2.txt
ls
That's OK, but what if we want to make lots of files? Now we start to see the power of the command line by using braces. Braces are the squiggly brackets to the right of a standard keyboard.
touch {file3.txt,file4.txt}
ls
You can see file3.txt and file4.txt have been created - but there must be an even easier way. Well, try this:
touch file{5,6,7,8}.txt
ls
That created 4 new files that shared the prefix 'file' and suffix '.txt' - a quick ls will show 8 files. Right, let's delete them:
rm file*
ls
The * is counted as a 'wild card' - it will delete the files in the current directory that start with 'file'. With power comes great responsibility and I am sure you can appreciate how this could go horribly wrong when you realise there was that file_must_keep.txt there. The rm command doesn't care, it's gone!
Oh well.
So how do we add a line to a file? Easily is the answer. We'll start with a simple test.txt file:
touch test.txt
Now we can add some content:
echo "This is the first line" > test.txt
There are various ways of reading what's inside a file but we'll use 'cat' in these examples (ask the man if you are not sure).
cat test.txt
That will output the contents as:
This is the first line
Now we can add a second line and have a look at the contents:
echo "This is the second line" > test.txt
cat test.txt
And that will output:
This is the second line
Where's the first line gone? Good question. Here is where a simple slip of the keyboard can destroy a file (which is bad jaja!).
When we use a single '>', it overwrites everything in the file with that information.
Most of the time we want to append a line to a file. That is to say, add a line at the end. In that case we need two '>>' in the command. Try this:
echo "This is the third line" >> test.txt
cat test.txt
Good. That's what we wanted to do.
I can't express enough how important it is to remember the difference between a single '>' and two '>>'. Using the command line is simple, quick and powerful.
And so is the ability to completely hose your system.
Don't be put off - you will make mistakes. Goodness knows I have! But learn from them and try and understand what it is you are doing rather than repeating commands from a book.
We've done it before, but to remove the test file, issue the command:
rm test.txt
That's it for now. We'll keep the sandbox directory as we'll use it in later articles.
Now we know how to create and manipulate files, the next article will be an introduction to the nano text editor.
PickledOnion.
Digg it |
del.icio.us |
reddit |
StumbleUpon

Subscribe to Feed