0% found this document useful (0 votes)
449 views100 pages

Linux - Shell.handbook by Linux Special

Linux shell handbook, 301 bash commands

Uploaded by

phobiamachine
Copyright
© Public Domain
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
449 views100 pages

Linux - Shell.handbook by Linux Special

Linux shell handbook, 301 bash commands

Uploaded by

phobiamachine
Copyright
© Public Domain
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 100
LINUX SHELL fa rN N D BOO 44 Payee Tat Gl YOUR LINUX SKILLS Vee aie Monitor processes Create custom scripts LINUX NEW MEDIA ‘The Pulse of Open Source WWW.LINUX-MAGAZINE.COMeo) sy) mel ar: VL News « Careers ° Life in Tech Skills * Resources FOSSlife.orgBASH CLASS Think like the experts; The powerful Bash shell provides a comprehensive collection of utilities for configuring and troubleshooting Linux systems, Eee N Nee MUS EUR ACen a ay a Sen iC eS Pe SEs Oe SCN COM Reo eee prefer to work from the keyboard, and many will ell you that Con Rese a Mice Me Sue Ree amt Sua fan Se Orestes oa eee Geemarc ees ema found on most Linux systems. Sea ure Cau cre anemia Crema Sse aoa Sonar MUU a cmscu TU} Tease Re) au shee Ger posite eeu hd Delp eeree Recah (ae Ores eer roe evapo? Dee ey ee ae CE Ce) Poeun ese Meee Ie Rue CAT RU er Mas eee FyGETTING AROUND 6 Introducing Bash 4 17 9 File Management _ mae, |e » manage our fle > z 12 Search Tools Handy tools like find, locate, and whereis help you chase «down files and programs on your system, Regular Expressions Regular expressions work with other tools to filter data and help you find the snippet you really need. We show you how to match characters with regular expressions. Pipes and Redirection Leam hovr to direct the output of one command to Customizing Bash Get mote productive by customizing the command-line environment. Text Manipulation Tools ‘Why slow down for a mouse? You can use some simple command-line tools to search, sor, cut, paste, join, and split your text files to zero in on the information you need. 26 Hardware Configuration 32. Configuring Filesystems Learn about some tools for configuring You can’t store files without a flesystem. Use these Bash and managing hardware on commands for easy filesystem configuration, your Linux system. » © 34 mount and fstab , disk, and parted = Most Linux distros mount storage media automatically, Use these handy disk but someday you might need a classic command-line tool partitioning utilities to configure your hard drive 36 Time Tools before you install a new operating These utilities let you set and keep time. system. > ‘Beat ination #llensne> Figure 2: Finding the Terminal in Ubuntu. LINUX SHELL HANDBOOK - 2022 EDITION Introducing Bash GETTING AROUND 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 permissions 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 lam later in this article, you can type man or ino, followed by the command, for information on syntax and usage. For example, to learn the var ious options for the mkdir 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 ean save time by using the history forthe current account. Stored in the bash_histony file in your home di rectory is alist of commands you have run, with the oldest numbered 1. You can use the arrow keys to move up and down or use te 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 to una previous command in the his tory Inumber rans the command with that numbee Similaty, number sets the number of previous commands to revert, to, and fring rans the frst command that inludes that sting When you are ether vey certain of what you ae doing or willing to live dangerously, you can enter string* string2 to repeat the last command but replace the frst string of characters with the second. Another rick isto add ho remove the last element ofthe path in the command ort to remove the first el ement. However, if you are uncertain of the results, you ean add po prin theBread Bash Commands 1s Ustcontents of the current directory fed Change directory [pwd Show current working directory mkdir Make directory cp Copyii m___ Remove file(s) rmdir 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), but 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 GETTING AROUND Introducing Bash PA Description General commands ‘System calls 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 case, ‘then only the head ofthe path is preserved Inthe second. press Curl#Z followed by Curl+C to re turn to the command line, When you are doing deeper research, consider using apropos followed by topic to receive a list of all the applicable ‘man 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 to you. By contrast, if all you need is a brief snippet of information, use whatis fol lowed by the command. For example, if you enter whats fdisk, you receive the line ftsk (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 fle. For several decades, man pages have been the standard help form. However, ‘more than a decade ago, the GNU Pro} ect made info its official help format But, instead of replacing man, info has simply become an alternative (Figure 5) Although some man pages today stress that the full help file is only available through info, in praet ce, many develop: =a ee) i Clibrary functions ‘Special files (usually devices found in fdov) and drivers 3 File formats and conventions 6 (Games and screensavers 7 ‘Miscellanea’ @ ‘System administration com ‘mands and daemons ers simply maintain both info and man, focusing on the command structure in the man pages and on basic instruction in the info pages. SUill, It can never hurt to check both in the hope of finding the most complete information, Digging Deeper {As experts will be quick to note, these comments provce only the barestout- line of subjects that have filled entire books. Read on for more about working inthe Bash shel, For adlitiona 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 next othe text, s0that you can try commands as you lear about them. i I] Bash Reference Manual hattpnnwgnuorgtsoftware/bash/ ‘manualbashrof htm Figure 4: The man page for the Is comman Figure 5: The info page for the Is command. LINUX SHELL HANDBOOK ~ 2022 EDITIONLearning file management commands FILEPOWER We give you an overview of commands for moving, editing UR Nv/tinux wets evertingasa Geen beamong yout is pts These conan se yt emenbe be eae the anes te ulster tncot he acne for canes Dasa fle manaenent commands faint tne eter dey and fe movement argon an edting (an ptetsty more dangers) hn tel estoy equals, an aly tolotbng orth ast standard pater oreo mallee Moving and Editing Directories and Files “The most basic commang for moving di rectores and files ip. Its sructre is simple: cp «ilesourefile> ‘target>. By default, cp overwrites any files of the same name in the target rectory, but you can be cautious and use the-b option to back up any files tha are overwritten or the -u option to overwrite only files that are newer than the ones in the target directory (Figure 1) ‘Aso, you cana ~preserve= mode to choose to preserve file attributes, such as Figure t: The ep command alos you to be both cautious and flexible. He STW nae a lee a} a ano) ‘owner or timestamp, or ~no-preserve ‘= mode to have them changed in the files’ new location, Whether or not you preserve attributes is especialy 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 to waste hard drive space on multiple cop- ies of the same file, in which case you ‘might prefer to use in» file link to create a symbolic link, or pointer, to the or nal ile, which takes up much less space (Figure 2). Later, if you copy these sym- bolic links to a backup, you can use cp -L to 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 cp. Also, you can use mp to rename a ile, 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-yarsen sgardens.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~ octal permissions to set permissions for the new directory or Pores ‘ensures that fles with the same name as those being copied are not overwritten and that the ‘owner ofthe files does not chane LINUX SHELL HANDBOOK - 2022 EDITION GREE CLS La create the directories immediately above it by adding the -p (parent) option. ‘Todelete, 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 isto create a special folder and move files to it instead of using rm or rmdir. By default, mm works only on files. To delete directories with it, you have to use the -r option. As you might imagine, m--rean remove key system files when used thoughtlessly; thus, some users prefer to add ~preserve-oot when run- ning the command anywhere near the root directory. In comparison, rmdir 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 utility that copies bytes or blocks rather than files. Used mainly by administra tors, dé has a non-standard syntax. Briefly, dd can be used for such tasks as creating an ISO image from a CD/DVD, wiping a disk by filling it with random data, and duplicating a partition 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 if you are inattentive, For more Information on di, see the “dd and gen- isoimage” chapter. Navigating and Editing Directories and Files You probably already know that you move around the directory ree withthe command ed ~ a command so simple that it has no options. You might not know, however, that cd has several shortcuts: cd. moves tothe rectory immediately above the current ‘one; e- returns you tothe previous di rectory; and ed ~ returns you to your home directory (Figure 5). Combined withthe command history ina virtual terminal, these shortcuts are enough to give you the equivalent ofthe back and forward buttons in a web browser. ‘Once you are ina directory, use ls to view the contents. In many distributions, you will ind that (sis actually an alias of |b -color, which displays different types of files in different colors. Sometimes, it is an alia of ls ~color-~lassify, which adds the use of symbols such as / to in-GETTING AROUND File Management $n screenshot. Figure 2: Creating a symbolic ink with In sa space-saving way of having the same file in two places atthe same time. Figure Cie arg Pst OMT cree e mv command does double-duty, both moving files and r waming them. Brn WN Ee ceca] Figure 4: The rmdir command is much safer to use than rm, because it can't delete directo- Hes that stl have files in them. Rim ki kee Option -amin -atime ‘Action Minutes/days since a fle was accessed, ‘min Minutes/days since a fil’s status was changed. ‘mmin Minutes/days since a fle was modified ‘group Files that belong to a particular user group. user Files that belong to a particular user. ‘maxdlepth ‘The maximum love of sub-directories in which to search, ‘mindapth ‘The minimum level of sub-directories in which to search ‘perm Designated permissions. "The

You might also like