You are on page 1of 100
LINUX SHELL a A N |») | ={e) fe) K Ayre TCH Tart | YOUR LINUX SKILLS Vice a ie Monitor processes Create custom scripts LINUX NEW MEDIA “The Pulse of Open Source WWW.LINUX-MAGAZINE.COM FOSSLIFE Open ys All a OhsSS1T (Rel ge] BASH CLASS Maca Sowa susan aes ee of ufilities for configuring and troubleshooting Linux systems, Caer eeu eu Uh None aR ua N eg scan iC ey managed and interacted with their systems from the command line. Many advanced users still prefer to work from the keyboard, and many willtell you that Cron RUP Rane Een Tice a Mee 8 Ream sate Seu fea ee Linux Shell Handbookis a thorough primer on Ger eee ere ES ues proteome nto sce SRM Me muM yo work with regular expressions, and Ciena e isan a Iona (ane Some) DeRose Cd groups, managing processes, and installing software — and we'll even Gerais Nace (oe Onesie ok Oe UME eee Keep the Linux Shell Handbook DS ee) PeEUeUe ese Meee) Ea Ruck Marci ap er sta eee 3 am ee 6 Introducing Bash ‘The Bash command shell isn’t magic ~ anyone can learn it. This Linux Shell special edition provides a comprehensive look at the world inside the terminal window. 4 ” 9 File Management We show you how to navigate directories and manage your file 9 2 Search Tools Handy tools like find, locate, and whereis help you chase down files and programs on your system, ee Tate eva ty, Regular Expressions Regular expressions work with other tots to filter data and help you find the snippet you really need. We show You how to match characters wit regular expressions. Pipes and Redirection Leam how to direct the output of one command to another command. Customizing Bash Get more productive by custo environment. ing the command-line Text Manipulation Tools ‘Why slow down for a mouse? You can use some simple comman¢-line tools to search, sort, cut, paste, join, and split your text files to zero in on the information you need. 26 Hardware Configuration Learn about some tools for configuring and managing hardware on your Linux system, 3 Ne 34 Bdisk, and parted Use these handy disk paritioning uilities to configure your hard drive before you installa new operating system, 36 Configuring Filesystems You can't store files without a filesystem. Use these Bash commands for easy filesystem configuration. mount and fstab ‘Most Linux distros mount storage media automatically, but someday you might need a classic command-line tool Time Tools ‘These utilities let you set and keep time, LINUX SHELL HANDBOOK ~ 2022 EDITION LINUX SilelLl HANDBOOK 0 Berar COMMUNICATION 39 Users, Groups, and Permissions 67 Networking Tools ‘The shell comes with some simple commands for We show you some Bash utilis for configuring and imanaging users and granting access to system troubleshooting networks resources. 7p SSH 43 suand sudo Secure remote connections ‘Why log in to the root account i you can avoid it? 7B Rsync Synchronize your data 80 Wireless Wireless networking requires some special tools face eee AUTOMATION stopping, and managing services in Linux 84 cron and at erp ‘Automate and schedule common tasks. proces nnnng on Your bdeseeeclttd preset annng ‘We show you how to start writing your own Bash script. 58 Package Management Tools Linux package tools help you install and manage software. We show you some package tools in Debian and RPN-based systems 91 Images and PDFs Use these pieture perfect command-line tools to convert and adapt your digital images. 65 dd and mkisofs/genisoimage/xorrisofs Create backups and bootable CDs with these handy disk utes LINUX SHELL HANDBOOK 2022 EDITION 5 Beyond all the splash screens, screen savers, and vivid rock-star wallpaper is the simple yet powerful Bash shell. BY BRUCE BYFIELD any desktop users approach the command line as though armed with a magic spell. They have a command - complete with options - to type or paste to get the desired results, but they are unclear what else might be going on. This approach is understand: able; however, if you take the time to understand something ofthe structure of the command line, you can increase control over your computing. By default, most Linux distributions sun Bash (the Bourne Again Shell), Bash is a command-line interpreter a pro- gram that runs macros and other utilities. These macros and utilities are the com- ‘mands that you enter atthe prompt. They include those built into Bash, such as cd, and many others that are external, includ sng most of the commands that you run, However, from the end user's perspective, the difference between internal and exter: nal commands is unimportant Like other shells, Bash can run inter- actively or nom-interactively. When act ing as a login shell for your account, Bash runs non-interactively, reading in structions from the .bashprofile file in your home directory. In many cases, commands give you the option to create a file and run it non- interactively. Most of the time, though, Bash runs as an interactive shell, meaning that you can enter commands and scripts using the keyboard, and Bash will process your input and display output. You can also fine-tune how Bash runs with a set of options similar to any commands, ‘These options can be entered in a termi nal profile orin a script that you run when opening a command line. One of Bash's most common options is +7, which places Bash in restrictive mode. In restrictive mode, some actions, such as using the cd command or chan ing environment variables, are disabled. Some administrators place Bash in re strictive mode in the hope of limiting the damage that rash users can cause on a network, but, more often, restricted shells are used to sandbox - that is, 0 run a command in isolation for test pur LINUX SHELL HANDBOOK ~ 2022 EDITION poses, The option ~ debugger is also used to log debugging information, Getting Around at the Prompt In the old days, the command prompt was the primary means of interacting with Linux, but most contemporary Linux systems open up in some form of graphical user interface. To reach the command prompt on a GUT-based Linux system, you'll need to open a terminal window. Systems that use the Gnome desktop environment typically include the Gnome Terminal applica tion. On Ubuntu, you'll find the Termi: nal application by searching the dash for "Terminal" (Figure 2). KDE-based openSUSE systems, on the other hand, include the Konsole terminal program, which you will find in the 4p; ‘System menu. Several other terminal programs are also available for Linux systems. Consult your vendor docu- mentation for more on finding your way to a command prompt. While in the shell, you can forget about your mouse, but you can copy and paste, as the Edit menu shows. To communicate with your system ications itn You can modify how Bash operates by using its builtin commands. For instance, the umask command changes the default permissions usad when creating a file, whereas the aliascormand can be used tochange the name used to run a specific ‘command —for example, my Debian sys- tem comes with fe -color=autoaliased to /s,s0 that directories and different file types are all colored. ‘Another way to modify Bash is through the shoptbuil.in Figure 1). The shopt command includes a number of interest- ing, ifseldom used, possibilties. Forex rrr irises ees Figure t: shopt is a command built in to Bash that provides many interesting features. Here, the edspell option automatically co rects errors when you type directory names. eed | through the keyboard, type a line, then press Enter. Of course, modern tools, like Konsole or the Gnome Terminal are not terminals in the old sense but are actually terminal emulators. You can clase or minimize the terminal ‘window as you would any other win- dow on your Linux system. ‘This handbook assumes you have some basic knowledge of how to move around in the Bash shell. If you are look- {ng for a very basic crash course, a few simple commands will help you get fa- miliar with the command prompt. ‘Most likely, the terminal will open in your home directory. Type 1s to list the contents of the directory. You can use the ad (change directory) command to move 10 another directory. You'll also need to ‘mention the path to the target directory 4 08 /nome/berney Msc Bash shells let you use a dot () in the path to represent the current directory. In other words, a user named berney could move from his home directory to the Music subdirectory by typing: $04 asic AA double dot means “go up one level in the directory path,” so if berney wanted to go from /home/berney/Music back to /home/berney, he could type: ample, shopt-s cdspell enables Bash to correct minor misspelings in ts default directories when you use the cd com. mand. Similarly, shopt-s checkjobs lists any stopped jobs that remain when you close the shell ‘These few examples of what you can do with Bash should be sufficient to show ‘that Bash is far from the passive recipient ‘of your commands. Instead, lke the com. mands that itruns, Bash is ful of options land can be customized to sult your needs. ‘You'l earn more about customizing the Bach environment late in this issue, tea ‘The tilde character (~) represents the home directory, so wherever you are, you ‘ean return to your home directory with: you start to get lost when you are navi gating around in the directory structure, you can always enter the pwd command (print working directory) ta display the ‘name of the current directory. To create a new directory, enter the ‘mkdir command and give the name of the new directory 4 kale fone /bemey/Masic/Beat es Or, if user berney were already in his -Musie directory, he could just type: 4 kale ./Beaties ‘The ep command lets you copy files. The syntax is as follows cp 3 Es Ly} ie} Ly La] kd Figure 2: Finding the Terminal in Ubuntu. LINUX SHELL HANDBOOK 2022 EDITION [ier aS AAR GL The default is to look in the current di- rectory, however, you can include a path with the source or destination to copy from or to a different directory. Of course, you must have the neces sary petmissions to access the direc: tory. To delete a file, use the rm com- mand, and to delete a directory, use the rm--r or rmdir command. (Need- less to say, be careful how you use these commands.) ‘A summary of these basic commands appears in Table 1. Each of these com. mands includes additional options that you can enter at the command line. As you will learn later in this artic can type mar or nfo, followed by the command, for information on syntax and usage. For example, to learn the var- fous options for the mk command, you would enter: In later articles, you will learn about other Bash commands for modifying text, managing users, overseeing processes, and troubleshooting net works. History Ifyou are doing repetitive commands in Bash, you can save time by using the history forthe current account, Stored in the bash_fistory file in your home di- rectory is lis of commands you have rin, with the oldest numbered 1. You can use the arrow keys to move up and down or use the plain command history to see a complete list of what is stored in your history. Ifyou are somewhat more adventur- ous, you can use a number of shortcuts torn a previows command in the his- tory, Inumber runs the command with that number. Similay, number ses the number of previous commands to revert to, and ‘sing rans the frst command that includes that tring ‘When you are either very certain of what you are doing ot wiling olive dangerously, you can enter *string!® string to repeat te last command but replace the fist string of characters with the second. Another trick sto add to retnove the last element of the path in the command ort to remove the first e: ement. However, if you are uncertain of the results, you can add; to print the Dena Bash Commands ls Listcontents of the current directory jed___Change directory [pwd Show currant working directory ‘mkdir Make diestory cp Copy filels) [rm Remove filets) ‘mdi Remove directory ‘command that you find but not run it (Figure 3), Documentation ‘Bash and the individual commands as. sociated with it add up to a lotto learn Fortunately, you don’t have to remember everything. Like other Unix-type sys tems, GNU/Linux includes a number of different help systems. ‘The most basic form of help is the ‘man page (Figure 4). Man pages are di vided into eight sections (see Table 2), Dut most of the time, you only need to type the command man followed by the command, file, or concept about which you want information However, some topics have entries in several sections. To go to the specific section, place the number of the section between the man command and the topic, Thus, man man takes you to the basic page about the man command in section 1, but man 7 man takes you to a section about the collection of macros used to create man pages. Either way, when you are finished reading, you can Figure 4: The man page for the Is command. Figure 3: You can use several keyboard shortcuts to run commands in the history with slight changes, Here, the string “bruce” Is replaced with “trish” in the frst ca ‘then only the head ofthe path is preserved Inthe second. press Ctri4Z, followed by Ctrl+C to re- turn to the command line, When you are doing deeper research, consider using apropos followed by a topic to receive a list of all the applicable rman pages. The one drawback to apro- pos is that, unless you are very specific, you could get dozens of pages, only a few of which are relevant 10 you. By contrast, if all you need isa brief snippet of information, use whats fal lowed by the command, For example, if you enter whats fdisk, you receive the line flsk (8) ~ Partition table manipula tor for Linux. The (8) refers to the man section where detailed information is available. Similarly, if you need to iden tify a file type, use type then the file. For several decades, man pages have been the standard help form. However, ‘more than a decade ago, the GNU Pro} lect made info its official help format Bul, instead of replacing man, injo has simply become an alternative (Figure 5). Although some man pages today stress that the fll help file is only available through info, in practice, many develop- CTE Ge are ee Geer Ae ee ‘Section Description 1 General commands 2 ‘System calls 3 library functions: + ‘Special files (u ay devices | found in dev) and drivers File formats and conventions (Games and screensavers ‘Miscellanea ee | ers simply maintain both info and man, focusing on the command structure in the man pages and on basic instruction in the info pages. Still, tcan never hut to check both in the hope of finding the ‘most complete information, Digging Deeper As experts wil be quick to note, these comments provide only the barest out line of subjects that have filled entie books. Read on for more about working in the Bash shel, For additional informa tion, a good place to start isthe man pages. Another important reference is the online Bash Reference Manual [1]. Read this material with a Bash shel ‘open nest othe text, s0 that you can try ‘commands as you lear about them. a Bash Reference Manual: bttp:/wnau.gnu.ora/softwarorbash/ ‘manualbashret. html Figure 5: The info page for the Is command, LINUX SHELL HANDBOOK ~ 2022 EDITION g file management commands FILEPOWER eC eae mpr Su/tinos eats eventhing as be among yar pie Tae Gonmens tee ofeenber be Catster sl) area tnsol th acns for canes tens ean ale ina Tose fl mangement commands fino te xo: ery an te movement ntgatin and eng ancompreston Command al ee cages te Wp moe power (an pte mor dang han ther eso etvatent hanks mali tolling orth aset standart pater ovo mle Moving and Editing Directories and Files ‘The most basic command for moving di- rectories and files is cp. Its structure is simple: cp . By default, cp overwrites any files ofthe same name inthe target di rectory, but you can be cautious and use the -b option to back up any files that are overwritten or the - option to overwrite only files that are newer than the ones in the target directory (Figure 1) ‘Also, you can add ~preserve=mode to choose to preserve file atrbutes, such as econ eae meen ing, and generally manipulating files. BY BRUCE BYFIELD Een ‘owner or rimestamp, or ~no-preserve ‘= mode to have them changed in the files’ new location, Whether ot not you preserve attributes is especially important ‘when you are logged in as root and mov- Ing around files owned by another user ~ say, fora backup of the /home directory. Sometimes, you might not want 10 ‘waste hard drive space on multiple cop- ies ofthe same file, in which case you might prefer to use in-s lle link to create a symbolic link, or pointer, to the or nal file, which takes up much less space (Figure 2). Later, if you copy these sym- bolic links to a backup, you can use cp Lo ensure that the original file, not the link, is used. Alternatively, you might prefer to move a file with mv, which takes many of the same options as ¢p. Also, you can ‘use mv to rename a file, giving it the same directory path but a different final ‘name (Figure 3). For example, to change the name of the file garden. png while keeping it in the same directory, you could use mv /garden,png../sun-yatsen- gardens. png, ‘As you copy or move files, you might want to create a new directory with mkdir. Although this isa relatively straightforward command, you can fine- tune it with ~mode=ctal-permissions to set permissions for the new directory or media/disk Figure t: The ep command allows you to be both cautious and flexible. Here the root user ‘ensures that files with the same name ‘owner ofthe fies does nat change. those being copied are not overwritten and that the LINUX SHELL HANDBOOK 2022 EDITION File Management GETTING AROUND create the directories immediately above itby adding the -p (parent) option. To delete, use rm (remove) for files and directories and rmdir for directories. Don't forget that, unlike the desktop, the Bash shell has no Trash folder. The clos- est you can get is to create a special folder and move files to it instead of using rm or rmdir. By default, rm works only on files. To delete directories with it, you have to use the option. As you might imagine, ‘m-rean remove key system files when used thoughtlessly; thus, some users prefer to add ~preserveoot when run- ning the command anywhere neat the rvot directory. In comparison, rar is a much safer option, because it works only on empty directories (Figure 4). ‘Acompletely different approach to file ‘management is taken by dd, an old Unix uty that copies bytes or blocks rather than files. Used mainly by administa- tors, dd has a non-standard syntax. Briefly, dt can be used for such tasks as czeating an 1SO image from a CD/DVD, wiping a disk by filling it with random data, and duplicating a partion or mas- ter boot record. Just remember to con- struct your dd command carefully and double-check it. Even more than rm, the ‘dd command can be hazardous to your system ifyou ae inattentive. For more information on da, see the “dd and gen: isoimage” chapter. Navigating and Editing Directories and Files You probably already know that you move around the directory tree with the command ed - a command so simple that it has no options. You ‘might not know, however, that ed has several shortcuts: cd... moves to the di- rectory immediately above the current fone; cd returns you to the previous di- rectory; and ed » returns you to your hhome directory (Figure 5). Combined with the command history in a virtual terminal, these shortcuts are enough to give you the equivalent of the back and forward buttons in a web browser, Once you are in a directory, use is 0 view the contents. In many distributions, you will find that fs is actually an alias of Is -color, which displays different types of files in different colors, Sometimes, it is an alias of ls ~color ~classify, which Adds the use of symbols such as / to in- eo Des Figure 2: Creating a symbolic ink with In sa space-saving way of having the same file in two places at the same time. Cc Me eee ee) Figure: e mv command does double-duty, both moving files and renaming them. brucegnanday:~$ rmdir ./download eR Macy WC eC totes Figure 4: The rmdir command is much safer to use than rm -r, because it can't delete directo- Hes that still have tls in them. ee ‘ation Option amin Moa Minutos/days since a file was accessed. fee ‘min Minutos/days since a files status was changed. ‘min eminsi-mtime edays> Minutes/days since a file was modified “group Files that belong toa particular user group. ser Files that belong toa particular user ‘maxdlepth ‘The maximum level of subdirectories in which to search. ‘mindopth ‘The minimum level of sub-directories in which to search. ‘porm Designated permissions. and-nis

You might also like