The Linux command line is less frightening than you might think
Redirection and Piping
Every shell command has the ability to take input from the keyboard or to output
text back to the terminal. This is how you would use the shell in day-to-day
tasks.
However, you can also redirect the input and output to or from a file instead. Redirecting from the keyboard is quite rare but redirecting output is common.
As an example, running ls -l will produce a screen full of files and directories; the output from the ls command is being sent to the terminal. You could redirect this output to the file ‘output.txt’ with the following:
$ ls -l >output.txt
The forward arrow makes perfect logical sense, as it implies the flow of text is going into the output.txt command. Using a left arrow tells the command to take its input from a file.
A related option the shell gives is pipelining. Here, the output from one command can be treated as the input to another. A simple and useful example is piping the output from a command such as ls -l into the ‘more’ program, which presents data a page at a time instead of allowing it to flow off the screen. To do this the long way would involve three steps:
$ ls -l >output.txt
$ more output.txt
$ rm output.txt
The more efficient way is to send the output from the ls -l command directly to more over a pipe without using a temporary file:
$ ls -l | more
Many commands allow you to redirect their output to the ‘standard output’ even those that produce binary data instead of plain text. The standard output is usually sent directly to the terminal, but can always be redirected or piped instead via the shell. Multiple commands can be linked through redirection and piping, for example:
$ cat names.txt | sort>sorted.txt
The first command reads in the data from names.txt (a list of first names), and instead of printing it in the terminal, sends it to the sort command. This then sorts the names alphabetically and again redirects the output to the file sorted.txt.
Permissions
Unix permissions can seem confusing but are in fact straightforward. If you run
the ls -l command in a directory or on files, you will see the permission
information on the left-hand side. At first glance it may appear to be a random
mix of dashes and letters but it’s simpler than that. The first character tells
you what kind of file it is: a dash for a regular file, d for a directory, an l
for a symbolic link, and c, b, s and p for special kinds of files. Following the
file type come the permissions.
There are three standard permission types: read, write and execute. These are represented by the letters r, w and x respectively, and they are listed in that order. If the file is readable, it has an r, and if not a dash, and so on. Thus a file that is readable and executable but not writeable is listed as r-x.
There are also three groups for permissions. These are the file’s owner, the file’s group, and all others. Again, they are listed in that order. Thus rwxr-r- would indicate the file’s owner has all permissions, while the group and others have read-only access.
Permissions behave differently on regular files and directories. On a file, read access means the ability to see the contents of the file; write allows the file to be modified (but not deleted) and execute enables the shell to run the file as a command. On a directory, read access allows you to see the contents of the directory; write allows you to create and delete files in the directory and execute allows you to enter the directory in the first place.
A file that has no read or write permissions can still be deleted in a writeable directory; it cannot, however, be modified or opened.
To change permissions use the ‘chmod’ command. Set the flags on and off with plus and minus, and use u, g and o to represent the three permission blocks user, group and other, or a for all three. For example:
$ chmod u+rw file
$ chmod o-rw file
$ chmod a+rx file
The first command enables read and write for the file’s user; the second removes read and write permissions for others; the third sets read and execute for the user, group and others. A graphical alternative is to use KDE or Gnome. Select a file’s properties with the right mouse button and click the appropriate buttons.
Related articles
Q.Why are some of the keys on my keyboard doing strange...
Q.Is my phone’s Bluetooth any use?
Q.Can I switch boot drives so that I can work on older...
Old Street roundabout is being touted by the Government as the UK's answer to Silicon Valley, but it seems our best innovations are coming from all over the UK
|
|
|
|
|
Computeractive Excel (2010) Online tutorialPrice: £19.99 |
Computeractive Word (2010) Online TutorialPrice: £19.99 |
Computeractive Powerpoint (2010) Online TutorialPrice: £19.99 |
Angry BirdsPrice: £9.99 |
Back Issue CD-Rom 14 (2011)Price: £15.99 |