GNU/Linux tutorials
1.3.7. Auto-start features of MC 1.3.8. FTP virtual filesystem of MC 1.4. The basic Unix-like work environment 1.4.1. The login shell 1.4.2. Customizing bash 1.4.3. Special key strokes 1.4.4. Unix style mouse operations 1.4.5. The pager 1.4.6. The text editor 1.4.7. Setting a default text editor 1.4.8. Customizing vim 1.4.9. Recording the shell activities 1.4.10. Basic Unix commands 1.5. The simple shell command 1.5.1. Command execution and environment variable 1.5.2. "$ A G variable LN" 1.5.3. "$ A H variable PT" 1.5.4. "$ O E variable HM" 1.5.5. Command line options 1.5.6. Shell glob 1.5.7. Return value of the command 1.5.8. Typical command sequences and shell redirection 1.5.9. Command alias 1.6. Unix-like text processing 1.6.1. Unix text tools 1.6.2. Regular expressions 1.6.3. Replacement expressions 1.6.4. Global substitution with regular expressions 1.6.5. Extracting data from text file table 1.6.6. Script snippets for piping commands I think learning a computer system is like learning a new foreign language. Although tutorial books and documentation are helpful, you have to practice it yourself. In order to help you get started smoothly, I elaborate a few basic points. The powerful design of Debian GNU/Linux comes from the Unix operating system, i.e., a multiuser, multitasking operating system. You must learn to take advantage of the power of these features and similarities between Unix and GNU/Linux. Don't shy away from Unix oriented texts and don't rely solely on GNU/Linux texts, as this robs you of much useful information. Note If you have been using any Unix-like system for a while with command line tools, you
probably know everything I explain here. Please use this as a reality check and refresher.
If you did install a GUI environment such as GNOME or KDE, then you can get to a login prompt by CtrlAlt-F1, and you can return to the GUI environment via Alt-F7 (see Section 1.1.6, Virtual consoles below for more). At the login prompt, you type your username, e.g. p n u n and press the Enter-key, then type your egi, password and press the Enter-key again. Note Following the Unix tradition, the username and password of the Debian system are case sensitive. The username is usually chosen only from the lowercase. The first user account is usually created during the installation. Additional user accounts can be created with adduser(8) by root. The system starts with the greeting message stored in "/ t / o d (Message Of The Day) and presents a ecmt" command prompt.
Dba GULnxlnysdfoty ein N/iu en/i o t1 folgn pnun o oi: egi Pswr: asod Ls lgn SnAr2 0:93 20 o ty at oi: u p 2 92:4 07 n t1 Lnxsop 262--m6 # SPSnAr1 2:54 UC20 x66 iu noy ..01ad4 1 M u p 5 02:9 T 07 8_4 Teporm icue wt teDba GULnxsse aefe sfwr; h rgas nldd ih h ein N/iu ytm r re otae teeatdsrbto trsfrec pormaedsrbdi te h xc itiuin em o ah rga r ecie n h idvda flsi /s/hr/o//oyih. niiul ie n ursaedc*cprgt Dba GULnxcmswt ASLTL N WRAT,t teetn ein N/iu oe ih BOUEY O ARNY o h xet pritdb apial lw emte y plcbe a. fo~ o:$
Here, the main part of the greeting message can be customized by editing the "/ t / o d t i " file. The ecmt.al first line is generated from the system information using "u a e - n v ". nm srm Now you are in the shell. The shell interprets your commands.
system. You type your username and your password to login to the non-privileged user account. Use tab to navigate between username and password, or use the mouse and primary click. You can gain the shell prompt under X by starting a x t r i a - m l t rprogram such as gnome-emnleuao terminal(1), rxvt(1) or xterm(1). Under the GNOME Desktop environment, clicking "Applications" "Accessories" "Terminal" does the trick. You can also see the section below Section 1.1.6, Virtual consoles. Under some other Desktop systems (like f u b x there may be no obvious starting point for the menu. If l x o ), this happens, just try (right) clicking the center of the screen and hope for a menu to pop-up.
#sudw - nw hton h o
You can shutdown the system under the single-user mode from the commandline.
#pwrf - oeof i f
Alternatively, you may type C r - l - e e e(The l f - t l k y the l f - l - e , and the tlAtDlt etCr-e, etAtKy D l t are pressed together) to shutdown if "/ t / n t a " contains eee eciitb "c : 2 4 : t l l d l / b n s u d w - 1 - - n w in it. See inittab(5) for details. a135crate:si/hton t a h o" See Section 6.9.6, How to shutdown the remote system on SSH.
If you already had these packages installed, no new packages are installed. Table 1.1. List of interesting text-mode program packages package popcon
m c sd uo vm i
size
description
V:10, I:26 1297 A text-mode full-screen file manager V:45, I:73 1397 A program to allow limited root privileges to users V:17, I:38 1873 Unix text editor Vi IMproved, a programmers text editor (standard version) I:94 830 Unix text editor Vi IMproved, a programmers text editor (compact version) V:5, I:9 V:2, I:3 13038 GNU project Emacs, the Lisp based extensible text editor (version 23) 584 The Unix style cut-and-paste on the text console (daemon)
It may be a good idea to read some informative documentations. Table 1.2. List of informative documentation packages package popcon size description Debian Project documentation, (Debian FAQ) and other
dcdba o-ein dba-oiy einplc dvlpreeoes rfrne eeec mitgie an-ud dba-itr einhsoy dba-a einfq dclnxtx o-iu-et dclnxhm o-iu-tl ssdi-ud yamngie
408
documents
3381 Debian Policy Manual and related documents 1299 Guidelines and information for Debian developers 706 Debian New Maintainers' Guide
3571 History of the Debian Project 1212 Debian FAQ 8616 Linux HOWTOs and FAQ (text) 62564 Linux HOWTOs and FAQ (html) 964 The Linux System Administrators' Guide
Answer all questions. This creates a new account named as f s . After your practice, you can remove this user account and its ih home directory by the following.
#dlsr-rmv-oefs eue -eoehm ih
Alternatively, it is also common to do as follows to let the non-privileged user, e.g. p n u n to gain egi, administrative privilege without any password.
#eh "egi AL(L)NPSW:L"> /t/uor co pnun L=AL OASDAL > ecsdes
This trick should only be used for the single user workstation which you administer and where you are the only user. Warning Do not set up accounts of regular users on multiuser workstation like this because it would be very bad for system security.
Caution The password and the account of the p n u nin the above example requires as much egi protection as the root password and the root account. Caution Administrative privilege in this context belongs to someone authorized to perform the system administration task on the workstation. Never give some manager in the Admin department of your company or your boss such privilege unless they are authorized and capable. Note For providing access privilege to limited devices and limited files, you should consider to use group to provide limited access instead of using the r o privilege via sudo(8). ot Note With more thoughtful and careful configuration, sudo(8) can grant limited administrative privileges to other users on a shared system without sharing the root password. This can help with accountability with hosts with multiple administrators so you can tell who did what. On the other hand, you might not want anyone else to have such privileges.
private, slave, or unbindable. Supported mount options for each filesystem are available in "/ h r / o / i u - o - . . / o u e t t o / i e y t m / s a e d c l n x d c 2 6 * D c m n a i n f l s s e s ". Directories on Unix systems are called folders on some other systems. Please also note that there is no concept for drive such as "A " on any Unix system. There is one filesystem, and everything is included. This : is a huge advantage compared to Windows.
a name, good choices are the period, hyphen, and underscore. You could also capitalize each word, "L k T i ". Experienced Linux users tend to avoid spaces in iehs filenames. Note The word "root" can mean either "root user" or "root directory". The context of their usage should make it clear. Note The word path is used not only for fully-qualified filename as above but also for the command search path. The intended meaning is usually clear from the context. The detailed best practices for the file hierarchy are described in the Filesystem Hierarchy Standard ("/ s / h r / o / e i n p l c / h / h - . . x . z and hier(7)). You should remember the ursaedcdba-oiyfsfs23ttg" following facts as the starter. Table 1.3. List of usage of key directories directory
/ /t/ ec
usage of the directory the root directory system wide configuration files all the home directories for all non-privileged users
d l c b p s
normal file directory symlink character device node block device node named pipe socket
chown(1) is used from the root account to change the owner of the file. chgrp(1) is used from the file's owner or root account to change the group of the file. chmod(1) is used from the file's owner or root account to change file and directory access permissions. Basic syntax to manipulate a f ofile is the following. o
#con<eonr fo hw nwwe> o #cgp<egop fo hr nwru> o #cmd [ga[-]rxs],.]fo ho uo]+=[wXt[.. o
For example, you can make a directory tree to be owned by a user f oand shared by a group b rby the o a following.
#c /oelcto/ d sm/oain #con- fobr. hw R o:a #cmd- u+w,=X. ho R grXor
There are three more special permission bits. The set user ID bit (s or S instead of user's x) The set group ID bit (s or S instead of group's x) The sticky bit (t or T instead of other's x) Here the output of "l - " for these bits is capitalized if execution bits hidden by these outputs are unset. s l Setting set user ID on an executable file allows a user to execute the executable file with the owner ID of the file (for example root). Similarly, setting set group ID on an executable file allows a user to execute the executable file with the group ID of the file (for example root). Because these settings can cause security risks, enabling them requires extra caution. Setting set group ID on a directory enables the BSD-like file creation scheme where all files created in the directory belong to the group of the directory. Setting the sticky bit on a directory prevents a file in the directory from being removed by a user who is not the owner of the file. In order to secure contents of a file in world-writable directories such as "/ m " or in tp group-writable directories, one must not only reset the write permission for the file but also set the sticky bit on the directory. Otherwise, the file can be removed and a new file can be created with the same name by any user who has write access to the directory. Here are a few interesting examples of file permissions.
$l - /t/asd/t/hdw/e/p /s/bnei4 s l ecpsw ecsao dvpp ursi/xm
cw--- 1ro ro r---ot ot 18 020-42 0:0/e/p 0, 070-9 70 dvpp -wr-- 1ro ro r--rot ot 12 20-41 0:9/t/asd 47 070-6 01 ecpsw -wr-- 1ro sao r---ot hdw 9320-41 0:9/t/hdw 4 070-6 01 ecsao -wrx- 1ro ro rs-rx ot ot 705 20-42 0:9/s/bnei4 006 070-2 52 ursi/xm $l -d/m /a/m /s/oa /a/al/s/r s l tp vrtp urlcl vrmi ursc dwrxw 1 ro ro 49 20-42 0:9/m rxwrt 0 ot ot 06 070-9 75 tp dwrs- 1 ro saf49 20-32 1:8/s/oa rxwrx 0 ot tf 06 070-4 84 urlcl dwrs- 4ro sc 49 20-42 0:1/s/r rxwrx ot r 06 070-7 03 ursc dwrs- 2ro mi 49 20-32 2:3/a/al rxwrx ot al 06 070-8 33 vrmi dwrxw 2ro ro 49 20-42 0:1/a/m rxwrt ot ot 06 070-9 71 vrtp
There is an alternative numeric mode to describe file permissions with chmod(1). This numeric mode uses 3 to 4 digit wide octal (radix=8) numbers. Table 1.5. The numeric mode for file permissions in chmod(1) commands digit 2nd digit 3rd digit 4th digit meaning sum of read (=4), write (=2), and execute (=1) permissions for user ditto for group ditto for other
1st optional digit sum of set user ID (=4), set group ID (=2), and sticky bit (=1)
This sounds complicated but it is actually quite simple. If you look at the first few (2-10) columns from "l s l command output and read it as a binary (radix=2) representation of file permissions ("-" being "0" and " "rwx" being "1"), the last 3 digit of the numeric mode value should make sense as an octal (radix=8) representation of file permissions to you. For example, try the following
$tuhfobr oc o a $cmdur,orfo ho =wg= o $cmd64br ho 4 a $l - fobr s l o a -wr-- 1pnunpnun1 20-42 0:2br r--regi egi 7 070-9 82 a -wr-- 1pnunpnun1 20-42 0:2fo r--regi egi 2 070-9 82 o
Tip If you need to access information displayed by "l - " in shell script, you should use s l pertinent commands such as test(1), stat(1) and readlink(1). The shell builtin such as "[ " or "t s " may be used too. et
Table 1.6. The umask value examples umask file permissions created directory permissions created usage
02 02 00 02
-wr-r--r-wr-r-wr-
-wrxrx-rx -wrxrxwrx
The Debian system uses a user private group (UPG) scheme as its default. A UPG is created whenever a new user is added to the system. A UPG has the same name as the user for which it was created and that user is the only member of the UPG. UPG scheme makes it is safe to set umask to 0 0 since every user has 02 their own private group. (In some Unix variants, it is quite common to setup all normal users belonging to a single u e sgroup and is good idea to set umask to 0 2 for security in such cases.) sr 02
description for accessible files and devices direct access to serial ports ("/ e / t S 0 3 ") dvty[-] limited access to serial ports for Dialup IP connection to trusted peers CD-ROM, DVD+/-RW drives audio device video device system monitoring logs some directories for junior administrative work: "/ s / o a ", "/ o e urlcl hm" Tip You need to belong to the d a o tgroup to reconfigure modem, dial anywhere, etc. ilu But if r o creates pre-defined configuration files for trusted peers in ot "/ t / p / e r / you only need to belong to the d pgroup to create Dialup IP e c p p p e s ", i connection to those trusted peers using pppd(8), pon(1), and poff(1) commands.
Some notable system-provided groups allow their members to execute particular commands without r o ot privilege. Table 1.8. List of notable system provided groups for particular command executions group
sd uo
accessible commands execute s d without their password uo add, modify, and remove printers from printer databases
For the full listing of the system provided users and groups, see the recent version of the "Users and Groups" document in "/ s / h r / o / a e p s w / s r - n - r u s h m " provided by the b s ursaedcbs-asduesadgop.tl ae p s w package. asd See passwd(5), group(5), shadow(5), newgrp(1), vipw(8), vigr(8), and pam_group(8) for management commands of the user and group system.
1.2.6. Timestamps
There are three types of timestamps for a GNU/Linux file. Table 1.9. List of types of timestamps type meaning
mtime the file modification time (l - ) s l ctime the file status change time (l - c s l) atime the last file access time (l - u s l) Note ctime is not file creation time. Overwriting a file changes all of the mtime, ctime, and atime attributes of the file. Changing ownership or permission of a file changes the ctime and atime attributes of the file. Reading a file changes the atime of the file. Note Even simply reading a file on the Debian system normally causes a file write operation to update atime information in the inode. Mounting a filesystem with "n a i e or otm" "r l t m " option makes the system skip this operation and results in faster file access eaie for the read. This is often recommended for laptops, because it reduces hard drive activity and saves power. See mount(8). Use touch(1) command to change timestamps of existing files. For timestamps, the l command outputs different strings under the modern English locale ("e _ S U F 8 s n U . T - ")
Tip See Section 9.2.5, Customized display of time and date to customize "l - " output. s l
1.2.7. Links
There are two methods of associating a file "f o with a different filename "b r o" a ". Hard link Duplicate name for an existing file "l f o b r n o a" Symbolic link or symlink Special file that points to another file by name "l - f o b r n s o a" See the following example for changes in link counts and the subtle differences in the result of the r m command.
$eh "rgnlCnet >fo co Oiia otn" o $l -ifo s l o 2951-wr-- 1pnunpnun1 20-42 0:5fo 382 r--regi egi 7 070-9 81 o $l fobr n o a #hr ln ad ik $l - fobz #smik n s o a yln $l -ifobrbz s l o a a 2951-wr-- 2pnunpnun1 20-42 0:5br 382 r--regi egi 7 070-9 81 a 2958lwrxw 1pnunpnun 320-42 0:6bz- fo 383 rxwrx egi egi 070-9 81 a > o 2951-wr-- 2pnunpnun1 20-42 0:5fo 382 r--regi egi 7 070-9 81 o $r fo m o $eh "e Cnet >fo co Nw otn" o $l -ifobrbz s l o a a 2951-wr-- 1pnunpnun1 20-42 0:5br 382 r--regi egi 7 070-9 81 a 2958lwrxw 1pnunpnun 320-42 0:6bz- fo 383 rxwrx egi egi 070-9 81 a > o 2950-wr-- 1pnunpnun1 20-42 0:7fo 384 r--regi egi 2 070-9 81 o $ctbr a a Oiia Cnet rgnl otn $ctbz a a NwCnet e otn
The hardlink can be made within the same filesystem and shares the same inode number which the "- " i option with ls(1) reveals. The symlink always has nominal file access permissions of "r x w r x as shown in the above example, w r x w ", with the effective access permissions dictated by permissions of the file that it points to.
Caution It is generally good idea not to create complicated symbolic links or hardlinks at all unless you have a very good reason. It may cause nightmares where the logical combination of the symbolic links results in loops in the filesystem. Note It is generally preferable to use symbolic links rather than hardlinks unless you have a good reason for using a hardlink. The ". directory links to the directory that it appears in, thus the link count of any new directory starts at 2. " The ". " directory links to the parent directory, thus the link count of the directory increases with the addition . of new subdirectories. If you are just moving to Linux from Windows, it soon becomes clear how well-designed the filename linking of Unix is, compared with the nearest Windows equivalent of "shortcuts". Because it is implemented in the filesystem, applications can't see any difference between a linked file and the original. In the case of hardlinks, there really is no difference.
1.2.9. Sockets
Sockets are used extensively by all the Internet communication, databases, and the operating system itself. It is similar to the named pipe (FIFO) and allows processes to exchange information even between different computers. For the socket, those processes do not need to be running at the same time nor to be running as the children of the same ancestor process. This is the endpoint for the inter process communication (IPC). The exchange of information may occur over the network between different hosts. The two most common ones are the Internet socket and the Unix domain socket.
Tip "n t t t - n provides a very useful overview of sockets that are open on a given esa a" system.
"/ e / d " has the major device number 3 and the minor device number 0. This is read/write dvha accessible by the user who belongs to c r mgroup. do "/ e / t S " has the major device number 4 and the minor device number 64. This is read/write dvty0 accessible by the user who belongs to d a o tgroup. ilu "/ e / e o has the major device number 1 and the minor device number 5. This is read/write dvzr" accessible by anyone.
In the Linux 2.6 system, the filesystem under "/ e / is automatically populated by the udev(7) mechanism. dv"
action description of response read read read return "end-of-file (EOF) character" return "the \ (NUL) character" (not the same as the number zero ASCII) 0 return random characters from a true random number generator, delivering real entropy (slow) return random characters from a cryptographically secure pseudorandom number generator write return nothing (a bottomless data dump pit)
These are frequently used in conjunction with the shell redirection (see Section 1.5.8, Typical command sequences and shell redirection).
See "p o . x ( g ) "s s s t t . z " and other related documents in the Linux kernel documentation r c t t . z ", y f . x ( g ) ("/ s / h r / o / i u - o - . . / o u e t t o / i e y t m / ") provided by the l n x ursaedclnxdc26*Dcmnainflsses* iud c 2 6 *package. o-..
1.2.13. tmpfs
The tmpfs is a temporary filesystem which keeps all files in the virtual memory. The data of the tmpfs in the page cache on memory may be swapped out to the swap space on disk as needed. The directory "/ u " is mounted as the tmpfs in the early boot process. This enables writing to it even when rn the directory "/ is mounted as read-only. This is the new location for the storage of transient state files and " replaces several locations described in the Filesystem Hierarchy Standard version 2.3: "/ a / u " "/ u " vrrn rn "/ a / o k "/ u / o k vrlc" rnlc" "/ e / h " "/ u / h " dvsm rnsm See "t p s t t . z " in the Linux kernel documentation ("/ s / h r / o / i u - o mf.x(g) ursaedclnxdc * D c m n a i n f l s s e s * provided by the l n x d c *package. / o u e t t o / i e y t m / ") iu-o-
Use the mc(1) command to explore the Debian system. This is the best way to learn. Please explore few interesting locations just using the cursor keys and Enter key. "/ t " and its subdirectories ec "/ a / o " and its subdirectories vrlg "/ s / h r / o " and its subdirectories ursaedc "/ b n and "/ i " si" bn
1.3.1. Customization of MC
In order to make MC to change working directory upon exit and c to the directory, I suggest to modify d "~ . a h c to include a script provided by the m package. /bsr" c
./s/i/cm.h urlbm/cs
See mc(1) (under the "- " option) for the reason. (If you do not understand what exactly I am talking here, P you can do this later.)
1.3.2. Starting MC
MC can be started by the following.
$m c
MC takes care of all file operations through its menu, requiring minimal user effort. Just press F1 to get the help screen. You can play with MC just by pressing cursor-keys and function-keys. Note In some consoles such as gnome-terminal(1), key strokes of function-keys may be stolen by the console program. You can disable these features by "Edit" "Keyboard Shortcuts" for g o e t r i a . nm-emnl If you encounter character encoding problem which displays garbage characters, adding "- " to MC's a command line may help prevent problems. If this doesn't clear up your display problems with MC, see Section 9.6.6, The terminal configuration.
key binding help menu internal file viewer internal editor activate pull down menu exit Midnight Commander move between two windows file for a multiple-file operation such as copy delete file (be careful---set MC to safe delete mode) self-explanatory
Cursor keys
changes the directory shown on the selected screen. the command line. Use this with cp(1) and mv(1)
One can specify the starting directory for both windows as arguments to MC; for example, "m / t c ec /ot r o ".
E c+ n k y F (i.e., E c+ 1 F , etc.; E c+ 0 F 0 s -e n s 1 s 1)
Pressing E cbefore the key has the same effect as pressing the A tand the key together.; i.e., type s l E c+ cfor A t C E cis called meta-key and sometimes noted as "M ". s l-. s -
This is not a multi-window editor, but one can use multiple Linux consoles to achieve the same effect. To copy between windows, use Alt-F<n> keys to switch virtual consoles and use "FileInsert file" or "FileCopy to file" to move a portion of a file to another file. This internal editor can be replaced with any external editor of choice. Also, many programs use the environment variables "$ D T R or "$ I U L to decide which editor to use. EIO" VSA" If you are uncomfortable with vim(1) or nano(1) initially, you may set these to "m e i " by adding the cdt following lines to "~ . a h c / b s r ".
epr EIO=cdt xot DTRmei epr VSA=cdt xot IULmei
I do recommend setting these to "v m if possible. i" If you are uncomfortable with vim(1), you can keep using mcedit(1) for most system maintenance tasks.
Customizing program to be started). This is a very convenient MC feature. Table 1.12. The reaction to the enter key in MC file type executable file man file html file reaction to enter key execute command pipe content to viewer software pipe content to web browser
"* t r g " and "* d b file browse its contents as if subdirectory .a.z .e"
In order to allow these viewer and virtual file features to function, viewable files should not be set as executable. Change their status using chmod(1) or via the MC file menu.
size
POSIX shell
description Bash: the GNU Bourne Again SHell (de facto standard) TENEX C Shell: an enhanced version of Berkeley csh Debian Almquist Shell, good for shell script Z shell: the standard shell with many enhancements public domain version of the Korn shell OpenBSD C Shell, a version of Berkeley csh Stand-alone shell with builtin commands (Not meant for standard "/ i / h b n s ") the real, AT&T version of the Korn shell
3935 Yes 1213 No 212 Yes Yes No Yes 11462 Yes 340
ss ah kh s
3092 Yes
r c ps oh
169 201
No Yes
Tip You can find more b s customization tips, such as Section 9.2.7, Colorized ah commands, in Chapter 9, System tips.
description of key binding erase line before cursor erase a character before cursor terminate input (exit shell if you are using shell) terminate a running program temporarily stop program by moving it to the background job halt output to screen
CrtlQ Cr-l-e tlAtDl L f - l - e (optionally, W n o s etAtky idwky e) U-ro parw CrtlR Tb a Cr- Tb tlV a
reactivate output to screen reboot/halt the system, see inittab(5) meta-key for Emacs and the similar UI start command history search under b s ah start incremental command history search under b s ah complete input of the filename to the command line under b s ah input T bwithout expansion to the command line under b s a ah
The center wheel on the modern wheel mouse is considered middle mouse button and can be used for middle-click. Clicking left and right mouse buttons together serves as the middle-click under the 2 button mouse system situation. In order to use a mouse in Linux character consoles, you need to have gpm(8) running as daemon.
If you chose either Emacs or XEmacs instead as your choice of the editor, that is another good choice indeed, particularly for programming. Emacs has a plethora of other features as well, including functioning as a newsreader, directory editor, mail program, etc. When used for programming or editing shell scripts, it intelligently recognizes the format of what you are working on, and tries to provide assistance. Some people maintain that the only program they need on Linux is Emacs. Ten minutes learning Emacs now can save hours later. Having the GNU Emacs manual for reference when learning Emacs is highly recommended. All these programs usually come with tutoring program for you to learn them by practice. Start Vim by typing "v m and press F1-key. You should at least read the first 35 lines. Then do the online training course by i" moving cursor to "| u o | and pressing C r - . ttr" tl] Note Good editors, such as Vim and Emacs, can be used to handle UTF-8 and other exotic encoding texts correctly with proper option in the x-terminal-emulator on X under UTF-8 locale with proper font settings. Please refer to their documentation on multibyte text.
The choice "/ s / i / i . a i " over "/ s / i / i . i y is my recommendation for newbies since urbnvmbsc urbnvmtn" it supports syntax highlighting. Tip Many programs use the environment variables "$ D T R or "$ I U L to decide EIO" VSA" which editor to use (see Section 1.3.5, The internal editor in MC and Section 9.5.11, Customizing program to be started). For the consistency on Debian system, set these to "/ s / i / d t r (Historically, "$ D T R was "e " and "$ I U L was "v ".) u r b n e i o ". EIO" d VSA" i
stnsafl e owpie es le stmdln e oeie stsafl e wpie edf ni "fle t aodteln aoebigrcgie a amdln ilr o vi h ie bv en eonzd s oeie "fle ilr "fle ilr
description display name of current/working directory display current user name display current user identity (name, uid, gid, and associated groups) display a type of file for the file "< o > fo" display a file location of command "< o m n n m > cmadae" ,, display information on command "< o m n n m > cmadae"
arps<epoo ky wr> od mn- <ea k ky wr> od wai hts <omnnm> cmadae mna a <omnnm> cmadae if no <omnnm> cmadae l s l s a l s A l -a s l l -a s li l s d te re lo <o> sf fo lo - <i> sf p pd mdr<o> ki fo rdr<o> mi fo c <o> d fo c / d c d c /fo d <o> c . d . c ~fo d <o> c d <ecmt /t/od pgr ae tuh oc <ukie jnfl> c <o><a> p fo br r <ukie m jnfl> m <o><a> v fo br
find commands related to "< e - o d " kywr> ,, display one line explanation on command "< o m n n m > cmadae" display explanation on command "< o m n n m > (Unix style) cmadae" display rather long explanation on command "< o m n n m > (GNU style) cmadae" list contents of directory (non-dot files and directories) list contents of directory (all files and directories) list contents of directory (almost all files and directories, i.e., skip ". " and ". . ") list all contents of directory with detail information list all contents of directory with inode number and detail information list all directories under the current directory display file tree contents list open status of file "< o > fo" list files opened by the process ID: "< i > pd" make a new directory "< o > in the current directory fo" remove a directory "< o > in the current directory fo" change directory to the directory "< o > in the current directory or in the directory fo" listed in the variable "$ D A H CPT" change directory to the root directory change directory to the current user's home directory change directory to the absolute path directory "/ f o " <o> change directory to the parent directory change directory to the home directory of the user "< o > fo" change directory to the previous directory display contents of "/ t / o d using the default pager ecmt" create a empty file "< u k i e " jnfl> copy a existing file "< o > to a new file "< a > fo" br" remove a file "< u k i e " jnfl> rename an existing file "< o > to a new name "< a > ("< a > must not exist) fo" br" br"
move an existing file "< o > to a new location "< a > < o > (the directory fo" br/fo" m < o > < a > "< a > must exist) v fo br br"
m <o> v fo <a><a> br/bz
move an existing file "< o > to a new location with a new name "< a > < a > (the fo" br/bz" directory "< a > must exist but the directory "< a > < a > must not exist) br" br/bz"
cmd60<o> ho 0 fo cmd64<o> ho 4 fo cmd75<o> ho 5 fo fn .-ae id nm <atr> pten lct - . oae d <atr> pten ge - " rp e <atr> pten" *hm .tl tp o p ax|pgr s u ae p -f|pgr s e ae p ax|ge s u rp e"exm* []i4" p af|pgr s x ae kl <24 il 13> gi <o> zp fo gni <o>g uzp fo.z bi2<o> zp fo bni2 uzp <o>b2 fo.z x <o> z fo ux <o>x nz fo.z tr-v a xf <o>tr fo.a tr-vf a xz <o>trg fo.a.z tr-vf a xj <o>trb2 fo.a.z tr-vf a xJ <o>trx fo.a.z tr-v a cf <o>tr fo.a <a> br/ tr-vf a cz <o>trg fo.a.z <a> br/ tr-vf a cj <o>trb2 fo.a.z <a> br/ tr-vf a cJ
make an existing file "< o > to be non-readable and non-writable by the other fo" people (non-executable o > to be readable but non-writable by the other people make an existing file "< for all) fo" (non-executable for all) make an existing file "< o > to be readable but non-writable by the other people fo" (executable for all) find matching filenames using shell "< a t r > (slower) pten" find matching filenames using shell "< a t r > (quicker using regularly generated pten" database) find a "<pattern>" in all files ending with ". t l in current directory and display them hm" all display process information using full screen, type "q to quit " display information on all the running processes using BSD style output display information on all the running processes using Unix system-V style output display all processes running "e i " and "e i 4 xm xm" display information on all the running processes with ASCII art output kill a process identified by the process ID: "<1234>" compress "< o > to create "< o > g " using the Lempel-Ziv coding (LZ77) fo" fo.z decompress "< o > g " to create "< o > fo.z fo" compress "< o > to create "< o > b 2 using the Burrows-Wheeler block sorting fo" fo.z" text compression algorithm, and Huffman coding (better compression than g i ) zp decompress "< o > b 2 to create "< o > fo.z" fo" compress "< o > to create "< o > x " using the LempelZivMarkov chain fo" fo.z algorithm (better compression than b i 2 zp) decompress "< o > x " to create "< o > fo.z fo" extract files from "< o > t r archive fo.a" extract files from gzipped "< o > t r g " archive fo.a.z extract files from "< o > t r b 2 archive fo.a.z" extract files from "< o > t r x " archive fo.a.z archive contents of folder "< a > " in "< o > t r archive br/ fo.a" archive contents of folder "< a > " in compressed "< o > t r g " archive br/ fo.a.z archive contents of folder "< a > " in "< o > t r b 2 archive br/ fo.a.z"
<o>trx fo.a.z <a> br/ za RAM.z ct EDEg |pgr ae za RAM.z ct EDEg >fo o za RAM.z ct EDEg > fo > o
archive contents of folder "< a > " in "< o > t r x " archive br/ fo.a.z display contents of compressed "R A M . z using the default pager EDEg" create a file "f o with the decompressed content of "R A M . z o" EDEg" append the decompressed content of "R A M . z to the end of the file "f o (if it EDEg" o" does not exist, create it first)
Note Unix has a tradition to hide filenames which start with ". They are traditionally files ". that contain configuration information and user preferences. Note For c command, see builtins(7). d Note The default pager of the bare bone Debian system is more(1) which cannot scroll back. By installing the l s package using command line "a t g t i s a l l s ", es p-e ntl es less(1) becomes default pager and you can scroll back with cursor keys. Note The "[ and "] in the regular expression of the "p a x | g e - " e x m * " " " s u rp e []i4" command above enable g e to avoid matching itself. The "4 " in the regular rp * expression means 0 or more repeats of character "4 thus enables g e to match both " rp "e i " and "e i 4 Although "* is used in the shell filename glob and the regular xm x m ". " expression, their meanings are different. Learn the regular expression from grep(1). Please traverse directories and peek into the system using the above commands as training. If you have questions on any of console commands, please make sure to read the manual page. For example, try the following
$mnmn a a $mnbs a ah $mnbitn a ulis $mnge a rp $mnl a s
The style of man pages may be a little hard to get used to, because they are rather terse, particularly the older, very traditional ones. But once you get used to it, you come to appreciate their succinctness. Please note that many Unix-like commands including ones from GNU and BSD display brief help information if you invoke them in one of the following ways (or without any arguments in some cases).
$<omnnm>-hl cmadae -ep $<omnnm>cmadae h
exact explanation. A simple command is a sequence of components. 1. Variable assignments (optional) 2. Command name 3. Arguments (optional) 4. Redirections (optional: >, > , <, < , etc.) > < 5. Control operator (optional: & , | , <newline> , ;, &, (, )) & |
ISO 639 language codes (lower case) such as "en" ISO 3166 country codes (upper case) such as "US" codeset, always set to "UTF-8"
For language codes and country codes, see pertinent description in the "i f g t e t n o e t x ". For the codeset on the modern Debian system, you should always set it to U F 8unless you specifically want Tto use the historic one with good reason and background knowledge. For fine details of the locale configuration, see Section 8.3, The locale. Note The "L N = n U " is not "L N = " nor "L N = n U . T - ". It is AGe_S AGC AGe_SUF8 "L N = n U . S - 8 9 1 (see Section 8.3.1, Basics of encoding). AGe_SIO85-" Table 1.18. List of locale recommendations
English(Great_Britain) French(France) German(Germany) Italian(Italy) Spanish(Spain) Catalan(Spain) Swedish(Sweden) Portuguese(Brazil) Russian(Russia) Chinese(P.R._of_China) Chinese(Taiwan_R.O.C.) Japanese(Japan) Korean(Republic_of_Korea) Vietnamese(Vietnam)
Here, the program date(1) is executed with different values of the environment variable "$ A G L N ". For the first command, "$ A G is set to the system default locale value "e _ S U F 8 LN" n U . T - ". For the second command, "$ A G is set to the French UTF-8 locale value "f _ R U F 8 LN" r F . T - ". Most command executions usually do not have preceding environment variable definition. For the above example, you can alternatively execute as the following.
$LN=rF.TAGf_RUF8 $dt ae dmnh 3ji 20,1:73 (T+90 iace un 07 02:3 UC00)
As you can see here, the output of command is affected by the environment variable to produce French output. If you want the environment variable to be inherited to subprocesses (e.g., when calling shell script), you need to export it instead by the following.
$epr LN xot AG
Tip When filing a bug report, running and checking the command under "L N = n U . T - " is good idea if you use non-English environment. AGe_SUF8
See locale(5) and locale(7) for "$ A G and related environment variables. LN" Note I recommend you to configure the system environment just by the "$ A G variable and LN" to stay away from "$ C * variables unless it is absolutely needed. L_"
program execution situation program run by the init process (daemon) program run from the normal root shell menu
/ o e < o m l u e > program run from the normal user shell hm/nra_sr / o e < o m l u e > program run from the normal user GUI desktop hm/nra_sr / o e < o m l u e > program run as root with "s d p o r m hm/nra_sr uo rga" /ot ro
program run as root with "s d - p o r m uo H rga" Tip Shell expands "~ " to current user's home directory, i.e., "$ O E ". Shell expands / HM/ "~ o / to f o home directory, i.e., "/ o e f o ". fo" o 's hm/o/
Here the command-line argument "- " changes date(1) behavior to output RFC2822 compliant date string. R
filename (segment) not started with ". " filename (segment) started with ". " exactly one character exactly one character with any character enclosed in brackets exactly one character with any character between "a and "z " " exactly one character other than any character enclosed in brackets (excluding "^ ")
See glob(7). Note Unlike normal filename expansion by the shell, the shell pattern "* tested in find(1) with " "- a e test etc., matches the initial ". of the filename. (New POSIX feature) nm" " Note BASH can be tweaked to change its glob behavior with its shopt builtin options such as "d t l b "n g o ", "n c s g o ", "n l g o ", "n c s g o ", "e t l b etc. o g o ", o l b oaelb ullb oaelb x g o ", See bash(1).
Table 1.21. Command exit codes command exit status numeric return value logical return value success error zero, 0 non-zero, -1 TRUE FALSE
Note Please note that, in the logical context for the shell, success is treated as the logical TRUE which has 0 (zero) as its value. This is somewhat non-intuitive and needs to be reminded here.
background execution of c m a din the subshell omn pipe the standard output of c m a d to the standard input of c m a d (concurrent omn1 omn2 execution) error of c m a d to the standard input of omn1
c m a d 2 & pipe both standard output and standard omn1 >1 |cmad omn2 c m a d (concurrent execution) omn2 cmad ; omn1 cmad omn2 cmad & omn1 & cmad omn2 cmad | omn1 | cmad omn2 cmad>fo omn o cmad2 omn > fo o cmad> omn > fo o cmad2> omn > fo o cmad>fo omn o 2& >1
execute c m a d and c m a d sequentially omn1 omn2 execute c m a d ; if successful, execute c m a d sequentially (return success if omn1 omn2 both c m a d and c m a d are successful) omn1 omn2 execute c m a d ; if not successful, execute c m a d sequentially (return success if omn1 omn2 c m a d or c m a d are successful) omn1 omn2 redirect standard output of c m a dto a file f o(overwrite) omn o redirect standard error of c m a dto a file f o(overwrite) omn o redirect standard output of c m a dto a file f o(append) omn o redirect standard error of c m a dto a file f o(append) omn o redirect both standard output and standard error of c m a dto a file "f o omn o"
cmad<fo omn o cmad< omn < dlmtr eiie cmad<omn < dlmtr eiie
redirect standard input of c m a dto a file f o omn o redirect standard input of c m a dto the following lines until "d l m t r is met (here omn eiie" document) redirect standard input of c m a dto the following lines until "d l m t r is met (here omn eiie" document, the leading tab characters are stripped from input lines)
The Debian system is a multi-tasking system. Background jobs allow users to run multiple programs in a single shell. The management of the background process involves the shell builtins: j b , f , b , and k l . os g g il Please read sections of bash(1) under "SIGNALS", and "JOB CONTROL", and builtins(1). For example, try the following
$<ecmt pgr /t/od ae $pgr<ecmt ae /t/od $pgr/t/od ae ecmt $ct/t/od|pgr a ecmt ae
Although all 4 examples of shell redirections display the same thing, the last example runs an extra c t a command and wastes resources with no reason. The shell allows you to open files using the e e builtin with an arbitrary file descriptor. xc
$eh Hlo>o co el fo $ee 3fo4br #oe fls xc <o >a pn ie $ct<3>4 a & & #rdrc sdnt 3 sdu t 4 eiet ti o , tot o $ee 3& 4& xc <- >#coefls ls ie $ctbr a a Hlo el
Here, "n & " and "n & " mean to close the file descriptor "n <>". The file descriptor 0-2 are predefined. Table 1.23. Predefined file descriptors device description
sdn ti s d u standard tot s d r standard ter
file descriptor
Now, "l " works as a short hand for "l - a which lists all files in the long listing format. a s l" You can list any existing aliases by a i s(see bash(1) under "SHELL BUILTIN COMMANDS"). la
$ais la .. . aisl=l -a la a's l'
You can identity exact path or identity of the command by t p (see bash(1) under "SHELL BUILTIN ye COMMANDS"). For example, try the following
$tp l ye s l i hse (bnl) s s ahd /i/s $tp l ye a l i aisdt l -a a s lae o s l $tp eh ye co eh i aselbitn co s hl uli $tp fl ye ie fl i /s/i/ie ie s urbnfl
Here l was recently searched while "f l " was not, thus "l " is "hashed", i.e., the shell has an internal s ie s record for the quick access to the location of the "l " command. s Tip See Section 9.2.7, Colorized commands.
tr(1) translates or deletes characters. diff(1) compares files line by line. Basic regular expression (BRE) is used: grep(1) matches text with patterns. ed(1) is a primitive line editor. sed(1) is a stream editor. vim(1) is a screen editor. emacs(1) is a screen editor. (somewhat extended BRE) Extended regular expression (ERE) is used: egrep(1) matches text with patterns. awk(1) does simple text processing. tcl(3tcl) can do every conceivable text processing: re_syntax(3). Often used with tk(3tk). perl(1) can do every conceivable text processing. perlre(1). pcregrep(1) from the p r g e package matches text with Perl Compatible Regular cerp Expressions (PCRE) pattern. python(1) with the r module can do every conceivable text processing. See e "/ s / h r / o / y h n h m / n e . t l u r s a e d c p t o / t l i d x h m ". If you are not sure what exactly these commands do, please use "m n c m a d to figure it out by yourself. a omn" Note Sort order and range expression are locale dependent. If you wish to obtain traditional behavior for a command, use C locale instead of UTF-8 ones by prepnding command with "L N = " (see Section 1.5.2, "$ A G variable and Section 8.3, The locale). AGC LN" Note Perl regular expressions (perlre(1)), Perl Compatible Regular Expressions (PCRE), and Python regular expressions offered by the r module have many common extensions to e the normal ERE.
The metacharacter is just a character with a special meaning. There are 2 major styles, BRE and ERE, depending on the text tools as described above. Table 1.24. Metacharacters for BRE and ERE BRE
\.[]^$* \ \ \ \ \ + ? ( ) {\ \ } | +?() {}| c \ c . ^ $ \ < \ > \ac\ [b] \^b] [ac\ r * r+ \ r? \ r\r 1|2 \r\r\ (1|2) c \ c . ^ $ \ < \ > [b ac] [ac] ^b r * r + r ? r|2 1r (1r) r|2
ERE
\.[] ^$*
description of the regular expression common metacharacters BRE only "\ escaped metacharacters " ERE only non-"\ escaped metacharacters " match non-metacharacter "c " match a literal character "c even if "c is metacharacter by itself " " match any character including newline position at the beginning of a string position at the end of a string position at the beginning of a word position at the end of a word match any characters in "a c " b match any characters except in "a c " b match zero or more regular expressions identified by "r " match one or more regular expressions identified by "r " match zero or one regular expressions identified by "r " match one of the regular expressions identified by "r " or "r " 1 2 match one of the regular expressions identified by "r " or "r " and treat it 1 2 as a bracketed regular expression
The regular expression of e a sis basically BRE but has been extended to treat "+ mc "and "? as the " metacharacters as in ERE. Thus, there are no needs to escape them with "\ in the regular expression of " eas mc. grep(1) can be used to perform the text search using the regular expression. For example, try the following
$erp'N.LCNEYydn'/s/hr/omnlcne/P ge GU*IES|ooye ursaecmo-iessGL GUGNRLPBI LCNE N EEA ULC IES GUGNRLPBI LCNE N EEA ULC IES Yydn,Ic,hrb dslisalcprgtitrs i teporm ooye n. eey icam l oyih neet n h rga
For the replacement expression, some characters have special meanings. Table 1.25. The replacement expression replacement expression description of the text to replace the replacement expression
& \ n
what the regular expression matched (use \ in e a s & mc) what the n-th bracketed regular expression matched ("n" being number)
For Perl replacement string, "$ " is used instead of "\ " and "& has no special meaning. n n " For example, try the following
$eh zzaceghj |\ co z1b2f3i4 sd- '/([-]\[-]\.\$==' e e s\1az*)09*(*)/&/ zz1b2f3i4 z=aceghj= $eh zzaceghj |\ co z1b2f3i4 sd- '/([-]\[-]\.\$\==1' e e s\1az*)09*(*)/2=\/ zzf3i4=1b zeghj==ac $eh zzaceghj |\ co z1b2f3i4 pr -e'/1az*[-](*$$==1' el p s([-])09*.)/2=$/ zzf3i4=1b zeghj==ac $eh zzaceghj |\ co z1b2f3i4 pr -e'/1az*[-](*$==' el p s([-])09*.)/&/ zz& z==
Here please pay extra attention to the style of the bracketed regular expression and how the matched strings are used in the text replacement process on different tools. These regular expressions can be used for cursor movements and text replacement actions in some editors too. The back slash "\ at the end of line in the shell commandline escapes newline as a white space character and " continues shell command line input to the next line. Please read all the related manual pages to learn these commands.
The sed(1) command can replace all instances of "F O _ E E " with "T _ E T in "f l ". RMRGX OTX" ie
$sd-e'/RMRGXT_ETg fl e i sFO_EE/OTX/' ie
The vim(1) command can replace all instances of "F O _ E E " with "T _ E T in "f l " by using ex(1) RMRGX OTX" ie commands.
Tip The "c flag in the above ensures interactive confirmation for each substitution. " Multiple files ("f l 1 "f l 2 and "f l 3 can be processed with regular expressions similarly with i e ", i e ", i e ") vim(1) or perl(1).
$vm'ago%/RMRGXT_ETg|pae 'q fl1fl2fl3 i +rd sFO_EE/OTX/eudt' +' ie ie ie
Tip The "e flag in the above prevents the "No match" error from breaking a mapping. "
$pr - - - '/RMRGXT_ETg'fl1fl2fl3 el i p e sFO_EE/OTX/; ie ie ie
In the perl(1) example, "- " is for in-place editing, "- " is for implicit loop over files. i p Tip Use of argument "- . a " instead of "- " keeps each original file by adding ". a " to ibk i bk its filename. This makes recovery from errors easier for complex substitutions. Note ed(1) and vim(1) are BRE; perl(1) is ERE.
Tip See "A Brief History of Debian" for the latest Debian leadership history. Awk is frequently used to extract data from these types of files. For example, try the following
$ak' pit$ } <P w { rn 3 ' DL Ags uut Arl pi Jnay aur Jnay aur Arl pi Arl pi Mrh ac $ak'$="a" {pit} <P w (1=In) rn ' DL In a Mrok Ags 19 udc uut 93 #mnhsatd ot tre
#DLcle In P ald a
In a Jcsn Jnay19 ako aur 98 $ak'$="ees){pit$,4} <P #We Prn satd w (2=Prn" rn 3$ ' DL hn ees tre Arl19 pi 96
Shells such as Bash can be also used to parse this kind of file. For example, try the following
$wiera frtls mnhya;d hl ed is at ot er o eh $ot co mnh dn <P oe DL ..sm otu a tefrtAkeape . ae upt s h is w xml
Here, the r a builtin command uses characters in "$ F " (internal field separators) to split lines into words. ed IS If you change "$ F " to ": you can parse "/ t / a s d with shell nicely. IS ", ecpsw"
$odF=$F" #sv odvle lIS"IS ae l au $IS'' F=: $wiera ue pswr udgdrs_fln;d hl ed sr asod i i eto_ie o i ["ue"="oo ] te f $sr bz" ; hn eh "ue' I i $i" co $srs D s ud f i dn </t/asd oe ecpsw bz' I i 10 oos D s 00 $IS"odF" #rsoeodvle F=$lIS etr l au
(If Awk is used to do the equivalent, use "F = : " to set the field separator.) S'' IFS is also used by the shell to split results of parameter expansion, command substitution, and arithmetic expansion. These do not occur within double or single quoted words. The default value of IFS is <space>, <tab>, and <newline> combined. Be careful about using this shell IFS tricks. Strange things may happen, when shell interprets some parts of the script as its input.
$IS", F=:" $eh IS$F, IS"IS co F=IS F=$F" IS ,IS: F= F=, $dt ae R St 2 Ag20 0:01 +20 a, 3 u 03 83:5 00 $eh $dt -) co (ae R St 2 Ag20 0 3 3 +20 a 3 u 03 8 0 6 00 $ustIS ne F $eh $dt -) co (ae R St 2 Ag20 0:05 +20 a, 3 u 03 83:0 00 #ue""ad""a IS s : n , s F #eh i aBs bitn co s ah uli #js acmadotu ut omn upt #sbsel->iptt mi sel u hl - nu o an hl #rstISt tedfut ee F o h eal
sq110 e 0 |xrs- 1<omn> ag n cmad |xrs- 1eh ag n co |xrseh ag co |ge - <ee_atr> rp e rgxpten
print 1 to 100 run command repeatedly with each item from pipe as its argument split white-space-separated items from pipe into lines merge all lines from pipe into a line extract lines from pipe containing <regex_pattern> extract third field from pipe separated by ": (passwd file etc.) " extract third field from pipe separated by whitespaces extract third field from pipe separated by tab remove backspace and expand tabs to spaces expand tabs sort and remove duplicates convert uppercase to lowercase concatenate lines into one line remove CR add "# to the start of each line " remove ". x " et print the second line print the first 2 lines print the last 2 lines
| g e - - < e e _ a t r > extract lines from pipe not containing <regex_pattern> rp v e rgxpten |ct-:-3u d f |ak' pit$ } w { rn 3 ' |ak-'t ' pit$ } w F\' { rn 3 ' |cl-x o b |epn xad |sr|ui ot nq |t '-''-' r AZ az |t - 'n r d \' |t - 'r r d \' |sd'// / e s^# ' |sd'/.x/g e s\et/' |sd- - 2 e n e p |ha - 2ed n |ti - 2al n
One-line shell script can loop over many files using find(1) and xargs(1) to perform quite complicated tasks. See Section 10.1.5, Idioms for the selection of files and Section 9.5.9, Repeating a command looping over files. When using the shell interactive mode becomes too complicated, please consider to write a shell script (see Section 12.1, The shell script).
Preface