Professional Documents
Culture Documents
1 Pipe-lining
By “Pipe-lining” we mean taking the output of a certain command and passing it as an input to another
command.
Command_1 | Command_2
The “vertical bar” symbol is the one used to redirect the output of the command into another
command.
* This syntax can be extended, which means you can use as many commands as needed
command_1 | command_2 | command_3 | command_4 | …
command_1 will be carried out and its output will be the input to command_2 which in turn gets
executed and have its output moved to command_3 as an input .. and so on.
[ahmad@localhost /home]$ ls | wc -l
This, as mentioned above, takes the output of the command “ls” and uses it as an input to the
commands “wc -l”
Back to our pipe-lined command, now we know that it shows the #lines in the output of the command
“ls”.
Now let’s move on to another example, but before that let’s explain first the commands that will be used
in it.
the famous “cat” command .. prints the contents of the file “passwd”.
If we look closely at the output we get something like this
root:x:0:0:root:/root:/bin/bash
7 fields, each separated from the other by a colon “:”
So in this file, our delimiter is the colon.
Now, if we wanted to extract the 7th field (/bin/bash) only we have to use another command called
“cut”
the command “cut” works by specifying
1. the delimiter
2. the field you want to output
in this example, we used the “-d” option to tell the command that the delimiter in this file is the “:”
colon character … then we used the “-f” option to say that we need the field #7
Now if we look at the output, we get only the 7th field of each line.
The delimiter doesn’t have to be a colon; depending on the structure of your file, you specify the
appropriate delimiter.
Back to pipe-lining
ahmad@localhost /home]$ cut -d : -f 7 /etc/passwd | sort
This redirects the output of the first command that we’ve just seen into the command “sort”
The command “sort” is self-explanatory. It simple sorts whatever the thing that’s passed to it.
So if we considered the previous output sample, the “sort” command will output this.
/bin/bash
/bin/sync
/usr/sbin/nologin
/usr/sbin/nologin
/usr/sbin/nologin
/usr/sbin/nologin
the command “uniq” removes duplicates, so we expect the output to be something like:
/bin/bash
/bin/sync
/usr/sbin/nologin
Try this one for yourself, and try to figure out what it does.
2 Editing files using nano
to start editing files with nano, simple write the word “nano” followed by the file
name .. note that if the file doesn’t exist nano creates it.
it opens up a window like this, where all the shortcuts needed to (save, search, exit,…) are listed at the
bottom of the window.
it opens up a window like this, But NO Shortcuts, nothing at all but an empty space .. even if you try to
type something, it will probably show you some error message at the bottom.
1. The Command mode (Allows you to save, exit, exit without saving, ...)
2. The Insert mode
to move from one mode to the other you have to press the Escape button first on
your keyboard.
1. The Command mode [First hit the Esc button on the keyboard] then to
start typing, and then to save what you’ve written go back to the command mode
by hitting the Esc button and then ‘:w’
3. Save the file, and then open any file using vim, you’ll notice line numbers
were added.
Another thing that can be done with .vimrc is creating shortcuts .. If you’re into
the netbeans editor you probably know already that you can write the main
method of a java program by typing psvm and then hitting tab
One last thing is creating a shortcut (like Ctrl + S that is used in most editors to
save changed made to a file).