Linux Command Handling
A. File-Handling Utilities
In this context, file handlingmeans copying, moving and deleting files. Later, we will look at ways of changing file attributes (owner, permissions).
A.1. mkdir, touch: Creating Empty Directories and Files
mkdir (MaKe DIRectory) is used to create directories. Its syntax is simple:
mkdir [options] <directory> [directory ...]
Only one option is worth noting: the -p option. It does two things:
1. it will create parent directories if they did not exist previously. Without this option, mkdir would just fail, complaining that the said parent directories do not exist; [More...]
2. it will return silently if the directory you wanted to create already exists. Similarly, if you did not specify the -p option, mkdir will send back an error message, complaining that the directory already exists. Here are some examples:
• mkdir foo: creates a directory foo in the current directory;
• mkdir -p images/misc docs: creates the misc directory in the images directory. First, it creates the latter if it does not exist (-p); it also creates a directory named docs in the current directory.
Initially, the touch command was not intended for creating files but for updating file access and modification times. However, touch will create the files listed as empty files if they do not exist. The syntax is:
touch [options] file [file...]
So running the command:
touch file1 images/file2
will create an empty file called file1 in the current directory and an empty file file2 in directory images, if the files did not previously exist.
A.2. rm: Deleting Files or Directories
The rm command (ReMove) replaces the DOS commands del and deltree, and adds more options. Its syntax is as follows:
rm [options] <file|directory> [file|directory...]
Options include:
• -r, or -R: delete recursively. This option is mandatory for deleting a directory, empty or not. However, you can also use rmdir to delete empty directories.
• -i: request confirmation before each deletion. Note that by default in Mandrakelinux, rm is an alias to rm-i, for safety reasons (similar aliases exist for cp and mv). Your mileage may vary as to the usefulness of these aliases. If you want to remove them, you can create an empty ~/.alias file which will prevent setting system wide aliases. Alternatively you can edit your ~/.bashrc file to disable some of the system wide aliases by adding this line: unalias rm cp mv• -f, the opposite of -i, forces deletion of the files or directories, even if the user has no write access on the files.
Some examples:
• rm -i images/*.jpg file1: deletes all files with names ending in .jpg in the images directory and deletes file1 in the current directory, requesting confirmation for each file. Answer y to confirm deletion, n to cancel.
• rm -Rf images/misc/ file*: deletes, without requesting confirmation, the whole directory misc/ in the images/ directory, together with all files in the current directory whose names begin with file.
“Using rm deletes files irrevocably. There is no way to restore them!
(Well, actually there are several ways to do this but it is no trivial
task.) Do not hesitate to use the -i option to ensure that you do
not delete something by mistake.”
A.3. mv: Moving or Renaming Files
The syntax of the mv (MoVe) command is as follows:
mv [options] <file|directory> [file|directory ...] <destination>
Some options:
• -f: forces operation –– no warning if an existing file is overwritten.
• -i: the opposite. Asks the user for confirmation before overwriting an existing file.
• -v: verbose mode, report all changes and activity.
Some examples:
• mv -i /tmp/pics/*.png .: move all files in the /tmp/pics/ directory whose names end with .png to the current directory (.), but request confirmation before overwriting any files already there.
• mv foo bar: rename file foo to bar. If a bar directory already existed, the effect of this command would be to move file foo or the whole directory (the directory itself plus all files and directories in it, recursively) into the bar directory.
• mv -vf file* images/ trash/: move, without requesting confirmation, all files in the current directory
whose names begin with file, together with the entire images/ directory to the trash/ directory, and show each operation carried out.
A.4. cp: Copying Files and Directories
cp (CoPy) replaces the DOS commands copy and xcopy and adds more options. Its syntax is as follows:
cp [options] <file|directory> [file|directory ...] <destination>
cp has a lot of options. Here are the most common:
• -R: recursive copy; mandatory for copying a directory, even an empty directory.
• -i: request confirmation before overwriting any files which might be overwritten.
• -f: the opposite of -i, replaces any existing files without requesting confirmation.
• -v: verbose mode, displays all actions performed by cp.
Some examples:
• cp -i /timages/* images/: copies all files in the /timages/ directory to the images/ directory located in the current directory. It requests confirmation if a file is going to be overwritten.
• cp -vR docs/ /shared/mp3s/* mystuff/: copies the whole docs directory, plus all files in the /shared/mp3s directory to the mystuff directory.
• cp foo bar: makes a copy of the foo file with the name bar in the current directory.
B. Handling File Attributes
The series of commands shown here are used to change the owner or owner group of a file or its permissions.
We looked at the different permissions in chapter Basic UNIX System Concepts.
B.1. chown, chgrp: Change the Owner and Group of One or More Files
The syntax of the chown (CHange OWNer) command is as follows:
chown [options] <user[:group]> <file|directory> [file|directory...]
The options include:
• -R: recursive. To change the owner of all files and subdirectories in a given directory.
• -v: verbose mode. Displays all actions performed by chown; reports which files have changed ownership as a result of the command and which files have not been changed.
• -c: like -v, but only reports which files have been changed.
Some examples:
• chown nobody /shared/book.tex: changes the owner of the /shared/book.tex file to nobody.
• chown -Rc queen:music *.mid concerts/: changes the ownership of all files in the current directory whose name ends with .mid and all files and subdirectories in the concerts/ directory to user queen and group music, reporting only files affected by the command.
The chgrp (CHange GRouP) command lets you change the group ownership of a file (or files); its syntax is very similar to that of chown:
chgrp [options] <group> <file|directory> [file|directory...]
The options for this command are the same as for chown, and it is used in a very similar way. Thus, the command:
chgrp disk /dev/hd*
changes the ownership of all files in directory /dev/ with names beginning with hd to group disk.
B.2. chmod: Changing Permissions on Files and Directories
The chmod (CHange MODe) command has a very distinct syntax. The general syntax is:
chmod [options] <change mode> <file|directory> [file|directory...]
but what distinguishes it is the different forms that the mode change can take. It can be specified in two ways:
1. in octal. The owner user permissions then correspond to figures with the form <x>00, where <x> corresponds to the permission assigned: 4 for read permission, 2 for write permission and 1 for execute permission.
Similarly, the owner group permissions take the form <x>0 and permissions for “others” the form
<x>. Then, all you need to do is add together the assigned permissions to get the right mode. Thus, the permissions rwxr-xr– correspond to 400+200+100 (owner permissions, rwx) +40+10 (group permissions,
r-x) +4 (others’ permissions, r–) = 754; in this way, the permissions are expressed in absolute terms. This means that previous permissions are unconditionally replaced;
2. with expressions. Here permissions are expressed by a sequence of expressions separated by commas. Hence an expression takes the following form: [category]<+|-|=><permissions>.
The category may be one or more of:
• u (User, permissions for owner);
• g (Group, permissions for owner group);
• o (Others, permissions for “others”).
If no category is specified, changes will apply to all categories. A + sets a permission, a – removes the
permission and a = sets the permission. Finally, the permission is one or more of the following:
• r (Read);
• w (Write) or;
• x (eXecute).
The main options are quite similar to those of chown and chgrp:
• -R: changes permissions recursively.
• -v: verbose mode. Displays the actions carried out for each file.
• -c: like -v but only shows files affected by the command.
Examples:
• chmod -R o-w /shared/docs: recursively removes write permission for others on all files and subdirectories in the /shared/docs/ directory.
• chmod -R og-w,o-x private/: recursively removes write permission for group and others for the whole private/ directory, and removes the execution permission for others.
• chmod -c 644 misc/file*: changes permissions of all files in the misc/ directory whose names begin with file to rw-r–r– (i.e. read permission for everyone and write permission only for the owner), and reports only files affected by this command.
C. Shell Globbing Patterns
You probably already use globbing characters without knowing it. When you specify a file in Windows® or when you look for a file, you use * to match a random string. For example, *.txt matches all files with names ending with .txt. We also used it heavily in the last section. But there is more to globbing than just *.
When you type a command like ls *.txt and press Enter, the task of finding which files match the *.txt
pattern is not done by the ls command, but by the shell itself. This requires a little explanation about how a command line is interpreted by the shell. When you type:
$ ls *.txt
readme.txt recipes.txt
the command line is first split into words (ls and *.txt in this example). When the shell sees a * in a word, it will interpret the whole word as a globbing pattern and will replace it with the names of all matching files.
Therefore, the command, just before the shell executes it, has become ls readme.txt recipe.txt, which
gives the expected result. Other characters make the shell react this way too:
• ?: matches one and only one character, regardless of what that character is;
• [...]: matches any character found in the brackets. Characters can be referred to either as a range of
characters (i.e. 1-9) or discrete values, or even both. Example: [a-zBE5-7] will match all characters between a and z, a B, an E, a 5, a 6 or a 7;
• [!...]: matches any character not found in the brackets. [!a-z], for example, will match any character
which is not a lowercase letter3;
• {c1,c2}: matches c1 or c2, where c1 and c2 are also globbing patterns, which means you can write {[0-9]*,[acr]} for example.
Here are some patterns and their meanings:
• /etc/*conf: all files in the /etc directory with names ending in conf. It can match /etc/inetd.conf, /etc/
conf.linuxconf, and also /etc/conf if such a file exists. Remember that * can also match an empty string.
• image/{cars,space[0-9]}/*.jpg: all file names ending with .jpg in directories image/cars, image/
space0, (…), image/space9, if those directories exist.
• /usr/share/doc/*/README: all files named README in all of /usr/share/doc’s immediate subdirectories.
This will make /usr/share/doc/mandrake/README match, for example, but not /usr/share/doc/myprog/
doc/README.
• *[!a-z]: all files with names which do not end with a lowercase letter in the current directory.
D. Redirections and Pipes
D.2. Redirections
Imagine, for example, that you wanted a list of files ending with .png4 in the images directory. This list is very long, so you may want to store it in a file in order to look through it at your leisure. You can enter the following
command:
$ ls images/*.png 1>file_list
This means that the standard output of this command (1) is redirected (>) to the file named file_list. The > operator is the output redirection operator. If the redirection file does not exist, it is created, but if it does exist, its previous contents are overwritten. However, the default descriptor redirected by this operator is the standard output and does not need to be specified on the command line. So you can write more simply:
$ ls images/*.png >file_list
and the result will be exactly the same. Then you could look at the file using a text file viewer such as less. Now, imagine you want to know how many of these files exist. Instead of counting them by hand, you can use the utility called wc (Word Count) with the -l option, which writes on the standard output the number of lines in the file. One solution is as follows:
wc -l 0<file_list
and this gives the desired result. The < operator is the input redirection operator, and the default redirected descriptor is the standard input one, i.e. 0, and you simply need to write the line:
wc -l <file_list
Now suppose you want to remove all the file “extensions” and put the result in another file. One tool for doing this is sed (Stream EDitor). You simply redirect the standard input of sed to the file_list file and redirect its output to the result file, i.e. the_list:
sed -e ’s/\.png$//g’ <file_list >the_list
and your list is created, ready for you to view at your leisure with any viewer.
It can also be useful to redirect standard errors. For example, you want to know which directories in
/shared you cannot access: one solution is to list this directory recursively and to redirect the errors to a file, while not displaying the standard output:
ls -R /shared >/dev/null 2>errors
which means that the standard output will be redirected (>) to /dev/null, a special file in which everything you write is discarded (i.e. the standard output is not displayed) and the standard error channel (2) is redirected (>) to the errors file.
D.3. Pipes
Pipes are in some ways a combination of input and output redirections. The principle is that of a physical pipe, hence the name: one process sends data into one end of the pipe and another process reads the data at the other end. The pipe operator is |. Let us go back to the file list example above. Suppose you want to find out directly how many corresponding files there are without having to store the list in a temporary file, you would then use the following command:
ls images/*.png | wc -l
which means that the standard output of the ls command (i.e. the list of files) is redirected to the standard input of the wc command. This then gives you the desired result.
You can also directly put together a list of files “without extensions” using the following command:
ls images/*.png | sed -e ’s/\.png$//g’ >the_list
or, if you want to consult the list directly without storing it in a file:
ls images/*.png | sed -e ’s/\.png$//g’ | less
Pipes and redirections are not restricted solely to text which can be read by human beings. For example, the following command sent from a Terminal:
xwd -root | convert – ~/my_desktop.png
will send a screen shot of your desktop to the my_desktop.png 5 file in your personal directory.
E. Command-Line Completion
Completion is a very handy function, and all modern shells (including bash) have it. Its role is to give the user as little work to do as possible. The best way to illustrate completion is to give an example.
E.1. Example
Suppose your personal directory contains the file_with_very_long_name_impossible_to_type file, and you want to look at it. Suppose you also have, in the same directory, another file called file_text. You are in your personal directory, so type the following sequence:
$ less fi<TAB>
(i.e., type less fi and then press the TAB key). The shell will then expand the command line for you:
$ less file_
and also give the list of possible choices (in its default configuration, which can be customized). Then type the following key sequence:
less file_w<TAB>
and the shell will extend the command line to give you the result you want:
less file_with_very_long_name_impossible_to_type
All you need to do then is press the Enter key to confirm and read the file.
E.2. Other Completion Methods
The TAB key is not the only way to activate completion, although it is the most common one. As a general rule, the word to be completed will be a command name for the first word of the command line (nsl<TAB>
will give nslookup), and a file name for all the others, unless the word is preceded by a “magic” character like
~, @ or $, in which case the shell will try to complete a user name, a machine name or an environment variable name respectively6. There is also a magic character for completing a file name (/) and a command to recall a command from the history (!).
The other two ways to activate completion are the sequences Esc-<x> and Ctrl+x <x>, where <x> is one of the magic characters already mentioned. Esc-<x> will attempt to come up with a unique completion. If it fails, it will complete the word with the largest possible substring in the choice list. A beep means either that the choice is not unique, or simply that there is no corresponding choice. The sequence Ctrl+x <x> displays the list of possible choices without attempting any completion. Pressing the TAB key is the same as successively
pressing Esc-<x> and Ctrl+x <x>, where the magic character depends on the context.
Thus, one way to see all the environment variables defined is to type the sequence Ctrl+x $ on a blank line.
Another example: if you want to see the man page for the nslookup command, you simply type man nsl then
Esc-!, and the shell will automatically complete the command to man nslookup.
F. Starting and Handling Background Processes: Job Control
You have probably noticed that when you enter a command from a Terminal, you normally have to wait for the command to finish before the shell returns control to you. This means that you have sent the command in the foreground. However, there are occasions when this is not desirable.
Suppose, for example, that you decide to copy a large directory recursively to another. You have also decided to ignore errors, so you redirect the error channel to /dev/null:
cp -R images/ /shared/ 2>/dev/null
Such a command can take several minutes until it is fully executed. You then have two solutions: the first one is violent, and means stopping (killing) the command and then doing it again when you have the time. To do this, press Ctrl+c: this will terminate the process and take you back to the prompt. But wait, don’t do it yet!
Read on.
Suppose you want the command to run while you do something else. The solution is then to put the process into the background. To do this, press Ctrl+z to suspend the process:
$ cp -R images/ /shared/ 2>/dev/null
# Type C-z here
[1]+ Stopped cp -R images/ /shared/ 2>/dev/null
$
and there you are again at the prompt. The process is then on standby, waiting for you to restart it (as shown by the Stopped keyword). That, of course, is what you want to do, but in the background. Type bg (for Back- Ground) to get the desired result:
$ bg
[1]+ cp -R images/ /shared/ 2>/dev/null &
$
The process will then start running again as a background task, as indicated by the & (ampersand) sign at the end of the line. You will then be back at the prompt and able to continue working. A process which runs as a background task, or in the background, is called a background job.
Of course, you can start processes directly as background tasks by adding an & character at the end of the command. For example, you can start the command to copy the directory in the background by writing:
cp -R images/ /shared/ 2>/dev/null &
If you want, you can also restore this process to the foreground and wait for it to finish by typing fg
(ForeGround). To put it into the background again, type the sequence Ctrl+z, bg.
You can start several jobs this way: each command will then be given a job number. The shell command jobs lists all the jobs associated with the current shell. The job preceded by a + sign indicates the last process begun as a background task. To restore a particular job to the foreground, you can then type fg <n> where <n> is the
job number, i.e. fg 5.
Note that you can also suspend or start full-screen applications this way, such as less or a text editor like Vi, and restore them to the foreground when you want.
T1 Transport Over Ethernet For Wireless Networks
Flanger™ from MemoryLink™ is a low cost solution that enables point-to-point
tunneling of T1 traffic across wireless Ethernet devices such as the Canopy™
Wireless Broadband Platform from Motorola. This small, simple-to-use unit
provides seamless transport of T1 across an Ethernet link at a price point that
offers a quick return on investment.
Designed for Wireless
The MemoryLink system uses a unique algorithm to transport the T1 data stream. This algorithm has been optimized for Wireless Ethernet transport. Because of the potential for
changing conditions that could affect wireless network throughput, the Flanger
unit provides the capability for recovering from data loss, without propagating the errors to following frames. This is accomplished with MemoryLink’s Channel Look-Ahead and Programmable Channel Optimization features.
Easy Set-Up and Configuration
Because Flanger provides physical pass-through of the T1 link, no special configuration or set-up is required regardless of the T1 format (e.g., channelized, clear channel, checksum on or off, etc.). The T1 signals are precisely repeated using a clock locked to the incoming line. In case of signal interruption at the source, no additional resynchronization is required at the Flanger device level. When used with Motorola Canopy radios (Back haul Modules), Flanger provides “last mile” T1 transport across distances of up to two miles, or 20 miles using
Canopy’s passive reflectors. It is an ideal, affordable solution for connecting PBX systems in remote office and campus environments. Flanger can also supplant wire-line T1 connections to cellular towers, eliminating monthly leased landline charges as well as the associated problems.
Typical Deployments
Remote PBX
Cellular Backhaul
Network Connectivity
Flanger can be deployed across a networked environment using wireless broadband, wired Ethernet, or a combination, as long as the requirement for MAC-layer transparency is met, and there are no competing bandwidth demands. This is most easily met by a private, dedicated radio system, or by a private LAN.
Channel Look-Ahead continuously estimates the variation of arrival time; in conjunction with the Programmable Channel Optimization feature, the MemoryLink system can deliver optimum reliability under changing network conditions.
Flanger requires the full T1 bandwidth (1.544-Mbps.) plus 256-kbps of overhead in each direction, for a total of 3.6-Mbps. Flanger uses fixed bandwidth that accommodates a single fractional, channelized or clear channel T1 line. The device was not engineered for any specific wireless system. However, because it has only been tested and qualified with Motorola’s Canopy Broadband platform, MemoryLink does not specifically support use with other radios at this time. Because Flanger is deployed on a point-to-point basis, it should be used with the Canopy Back – haul modules.
Flanger is currently configured with a single Ethernet port that is used for the T1 tunneling.
However, future releases will include a second port, providing 3- to 4-Mbps. of data transmission capacity. Flanger is designed for point-to-point applications using true Ethernet MAC addressing; it does not support IP routing.
Related posts:



