Simple clear advice in plain English

Hands on: A word in your shell

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.

Reader Comments

   

Add your comment

All fields must be completed. Your email address will not be displayed or used to send marketing messages.

All messages will be checked by moderators before appearing on the site.

See our Privacy Policy for more information.

Related articles

Security shield illustration

How to use the Command Prompt

Lurking under Windows is the little-known world of the command line. We explain how to use this to fix faults and make your PC much more secure

Make email newsletters with Word illustration

Use Word 2003 or 2007 to create attractive email newsletters

Word can help you make an email newsletter containing photos and clickable links. We show you how to make the most of Microsoft's templates

Manage files and folders with FarManager illustration

Bypass Windows and speed up the management of files and folders

Handle files and folders faster than ever by reverting to the time before Windows. For the confident, Far Manager works like the file-management tools of yesteryear

Question & Answer

Q.Why are some of the keys on my keyboard doing strange...

> Read the answer

Q.Is my phone’s Bluetooth any use?

> Read the answer

Q.Can I switch boot drives so that I can work on older...

> Read the answer

Best deals on the web

img

Samsung RV520-A07

£359.98- Buy it now

img

Acer Aspire 5750G (LX.RXP02.019)

£399.99- Buy it now

img

Apple MacBook Pro (MD313B/A)

£904.37- Buy it now

Latest issue & subscription deals

Poll

Are you concerned about viruses that target mobile phones?

Jargon Buster

Computing terms explained in plain English

Restore point

A Windows backup of system files and settings.

Great shopping deals from Computeractive