The Linux Command Line: A Complete Introduction free book http://linuxcommand.org/tlcl.php 13.07 July 6, 2013 Second Internet Edition, 537 pages 18 months after the Kindle Edition 480 pages good reviews on http://www.amazon.com/The-Linux-Command-Line-Introduction-ebook/dp/B006X2QEQS read this book with flash cards # Part 1 – Learning The Shell 1 ## 1 – What Is The Shell? 2 When we speak of the command line, we are really referring to the shell Operating system shells use a command-line interface (CLI) or a graphical user interface (GUI) The shell passes commands to the operating system Almost all Linux distributions supply a shell program from the GNU Project called bash Terminal Emulators 2 terminal gives access to shell, which passes commands to the OS Your First Keystrokes 2 $ is user prompt # is superuser prompt Command History 3 arrow up-down Cursor Movement 3 arrow left-right Ending A Terminal Session 5 $ exit ## 2 – Navigation 7 Understanding The File System Tree 7 Hierarchical directory structure starts at root The system administrator mounts storage devices at various points on the tree The Current Working Directory 7 Each user account is given its own home directory home is the only place a regular user is allowed to write files $ pwd Listing The Contents Of A Directory 8 $ ls Changing The Current Working Directory 9 $ cd Absolute Pathnames 9 Absolute pathnames are from root directory to destination Relative Pathnames 9 Relative pathnames are from the working directory to destination "." is the working directory, ".." is the parent directory $ cd bin "./" is implied, same as $ cd ./bin Some Helpful Shortcuts 11 Do not embed spaces in filenames, use underscores instead Important Facts About Filenames 11 Change working directory to your home directory $ cd Change working directory to previous working directory $ cd - Change the working directory to the home directory of user_name $ cd ~user_name ## 3 – Exploring The System 13 More Fun With ls 13 specify directory to list $ ls /usr specify multiple directories to list $ ls ~ /usr longform $ ls -l /usr Options And Arguments 14 command options are single dash and char, where chars can be strung together long options are two dashes and word Table 3-1: Common ls Options OPTION LONG OPTION DESCRIPTION -a --all List all files, even hidden files -A --almost-all Like the -a option above except it does not list . (current directory) and .. (parent directory). -d --directory Ordinarily, ls lists the contents of directory, not the directory itself. Use this option in conjunction with the -l option to see details about the directory rather than its contents. -F --classify This option appends an indicator character to the end of each listed name. / = directory, * = executable, @ = symbolic link -h --human-readable Use this option in conjunction with the -l option to display file sizes in human readable format rather than in bytes. -l Display results in long format. -r --reverse Display the results in reverse order. Normally, ls displays its results in ascending alphabetical order. -S Sort results by file size. -t Sort by modification time. A Longer Look At Long Format 16 $ ls -la -rw-r--r--. 1 wolfv wolfv 4323 Jun 28 22:55 .vimrc where columns describe file's -rw-r--r--. access rights (Chapter 9 – Permissions) 1 number of hard links wolfv owner wolfv group 4323 size in bytes Jun 28 22:55 date last modified .vimrc name Determining A File's Type With file 17 $ file picture.jpg picture.jpg: JPEG image data, JFIF standard 1.01 Viewing File Contents With less 17 many configuration files are text files less is a text-file viewer (better than more) $ less filename Table 3-3: less Commands COMMAND ACTION Page Up b Scroll back one page Page Down space Scroll forward one page Up Arrow k Scroll up one line Down Arrow j Scroll down one line G Move to the end of the text file g Move to the beginning of the text file /characters Search forward to the next occurrence of characters n Search for the next occurrence of the previous search h Display help screen q Quit less If a command output is more than a screen full, you can navigate in less $ ls | less less can be navigated like vi http://www.thegeekstuff.com/2010/02/unix-less-command-10-tips-for-effective-navigation/ Or you can scroll with gnome-terminal scroll buffer (not good with nav-layer key) Shift+PgUp/PgDn/Home/End or scroll wheel A Guided Tour 19 Good to know where the files are when configuring or trouble shooting the OS As we go about our tour, try the following: 1. navigate into a given directory $ cd 2. List the directory contents $ ls -lF | less 3. If you see an interesting file, determine its contents double click on a filename to copy $ file [paste filename] 4. If it looks like it might be text, try viewing it $ less [paste filename] DIRECTORY COMMENTS / The root directory. Where everything begins. /bin Contains binaries (programs) that must be present for the system to boot and run. /boot Contains the Linux kernel, initial RAM disk image (for drivers needed at boot time), and the boot loader. Interesting files: /boot/grub/grub.conf or menu.lst, which are used to configure the boot loader. /boot/vmlinuz, the Linux kernel. /dev This is a special directory which contains device nodes. “Everything is a file” also applies to devices. Here is where the kernel maintains a list of all the devices it understands. /etc The /etc directory contains all of the system-wide configuration files. It also contains a collection of shell scripts which start each of the system services at boot time. Everything in this directory should be readable text. Most interesting files: /etc/crontab, a file that defines when automated jobs will run. /etc/fstab, a table of storage devices and their associated mount points. /etc/passwd, a list of the user accounts. /home In normal configurations, each user is given a directory in /home. Ordinary users can only write files in their home directories. This limitation protects the system from errant user activity. /lib Contains shared library files used by the core system programs. These are similar to DLLs in Windows. /lost+found Each formatted partition or device using a Linux file system, such as ext3, will have this directory. It is used in the case of a partial recovery from a file system corruption event. Unless something really bad has happened to your system, this directory will remain empty. /media On modern Linux systems, the /media directory will contain the mount points for removable media such as USB drives, CD-ROMs, etc. that are mounted automatically at insertion. /mnt On older Linux systems, the /mnt directory contains mount points for removable devices that have been mounted manually. /opt The /opt directory is used to install “optional” software. This is mainly used to hold commercial software products that may be installed on your system. /proc The /proc directory is special. It's not a real file system in the sense of files stored on your hard drive. Rather, it is a virtual file system maintained by the Linux kernel. The “files” it contains are peepholes into the kernel itself. The files are readable and will give you a picture of how the kernel sees your computer. /root This is the home directory for the root account. /sbin This directory contains “system” binaries. These are programs that perform vital system tasks that are generally reserved for the superuser. /tmp The /tmp directory is intended for storage of temporary, transient files created by various programs. Some configurations cause this directory to be emptied each time the system is rebooted. /usr The /usr directory tree is likely the largest one on a Linux system. It contains all the programs and support files used by regular users. /usr/bin /usr/bin contains the executable programs installed by your Linux distribution. It is not uncommon for this directory to hold thousands of programs. /usr/lib The shared libraries for the programs in /usr/bin. /usr/local The /usr/local tree is where programs that are not included with your distribution but are intended for system- wide use are installed. Programs compiled from source code are normally installed in /usr/local/bin. On a newly installed Linux system, this tree exists, but it will be empty until the system administrator puts something in it. /usr/sbin Contains more system administration programs. /usr/share /usr/share contains all the shared data used by programs in /usr/bin. This includes things like default configuration files, icons, screen backgrounds, sound files, etc. /usr/share/doc Most packages installed on the system will include some kind of documentation. In /usr/share/doc, we will find documentation files organized by package. /var With the exception of /tmp and /home, the directories we have looked at so far remain relatively static, that is, their contents don't change. The /var directory tree is where data that is likely to change is stored. Various databases, spool files, user mail, etc. are located here. /var/log contains log files, records of various system activity. These are very important and should be monitored from time to time. The most useful one is /var/log/messages. For security reasons on some systems, you must be the superuser to view log files. also on https://en.wikipedia.org/wiki/Unix_filesystem#Conventional_directory_layout Symbolic Links 23 Directory listings with an arrow are symbolic links (aka soft link): lrwxrwxrwx 1 root root 11 2007-08-11 07:34 foo -> foo-2.6 Sym links are useful to reference multiple files with one name e.g. install foo-2.6 and create symlink to it symlink foo -> foo-2.6 programs that depend on foo can find file foo-2.6 when foo is upgraded to version foo-2.7, delete the symlink and create a new one symlink foo -> foo-2.7 this solves the problem of the version upgrade, and allows us to keep both versions if foo-2.7 has a bug, we can revert to foo-2.6 Hard Links 24 Hard links also allow files to have multiple names, but they do it in a different way ## 4 – Manipulating Files And Directories 25 While it is easy to perform simple file manipulations with a graphical file manager, complicated tasks can be easier with the command line programs. Wildcards 25 WILDCARD MEANING * Matches any characters ? Matches any single character and more tables of patterns in book mkdir – Create Directories 28 $ mkdir directory... "..." means the argument can be repeated cp – Copy Files And Directories 28 $ cp item1 item2 $ cp item... directory "item" can be a file or directory Useful Options And Examples 29 interactive option prompts user for before overwriting file $ cp -i item1 item2 see cp tables in book mv – Move And Rename Files 30 $ mv item1 item2 $ mv item... directory if second parameter exists, move, else rename if both item1 and item2 are files, overwrite $ mv file1 file2 safer to mv with -i Useful Options And Examples 30 see mv tables in book rm – Remove Files And Directories 31 $ rm item... Useful Options And Examples 31 see rm tables in book tip: Before you use wildcards with rm, test the wildcard first with ls. Then press the up arrow key to recall the command and replace the ls with rm. ln – Create Links 33 The ln command is used to create either hard or symbolic links Hard Links 33 $ln file link Hard links have two important limitations: 1. A hard link cannot reference a file outside its own file system. This means a link cannot reference a file that is not on the same disk partition as the link itself. 2. A hard link may not reference a directory. A hard link is indistinguishable from the file itself i.e. all files are pointed to by hard links Hard links are the original Unix way of creating links. Modern practice prefers symbolic links. Symbolic Links 33 $ ln -s item link Symbolic links were created to overcome the limitations of hard links. Symbolic links work by creating a special type of file that contains a text pointer to the referenced file or directory. A file pointed to by a symbolic link, and the symbolic link itself are largely indistinguishable from one another. However when you delete a symbolic link, only the link is deleted, not the file itself. If the file is deleted before the symbolic link, the link is broken. ls command will display broken links in a distinguishing color. Let's Build A Playground 34 Create a playground in home directory to play with file manipulation commands. Creating Directories 34 $ cd $ mkdir playground $ cd playground $ mkdir dir1 dir2 Copying Files 34 $ cp /etc/passwd . $ ls -l $ cp -v /etc/passwd . $ cp -i /etc/passwd . Moving And Renaming Files 35 rename passwd to fun $ mv passwd fun move fun to each directory $ mv fun dir1 $ mv dir1/fun dir2 restore directory structure $ mv dir2/fun . stack the directories $ mv fun dir1 $ mv dir1 dir2 $ ls -l dir2 $ ls -l dir2/dir1 restore directory structure $ mv dir2/dir1 . $ mv dir1/fun . overwrite with mv $ touch sad $ mv fun sad $ less sad restore directory structure $ mv -i sad fun Creating Hard Links 37 $ ls -l notice that fun has one hard link $ ln fun fun-hard $ ln fun dir1/fun-hard $ ln fun dir2/fun-hard $ ls -l notice that fun now has four hard links use -i option to list inode numbers (first field) $ ls -li inodes 37 I added this inodes section because the author is vague about inodes the following is a traditional Unix file system; some of the newer files systems are more complex inode is a structure that stores the file's metadata metadata from POSIX standard: The size of the file in bytes Device ID User ID of the file Group ID of the file The file mode that determines the permissions and type (regular, directory, symlink,..) Additional system and user flags to further protect the file Timestamps for inode and file content changes A link counter that lists how many hard links point to the inode Pointers to the disk blocks that store the file’s contents the disk blocks are file content and block addresses that point to more file content directory is a file containing a list of paired file-names and inode numbers FILE_CONTENT (content of a file that happens to be a directory) file name1, inode number file name2, inode number file name3, inode number / INODE (inode of file name3) metadata, disk blocks / \ / BLOCK_ADDRESSES / / \ FILE_CONTENT FILE_CONTENT FILE_CONTENT http://www.linux-mag.com/id/8658/ http://www.linux4windows.com/Articles/linux_concepts_for_beginners5.html https://en.wikipedia.org/wiki/Inode https://en.wikipedia.org/wiki/Inode_pointer_structure http://unix.stackexchange.com/questions/193465/what-file-mode-is-a-symlink http://www.unix.com/tips-and-tutorials/19060-unix-file-permissions.html Creating Symbolic Links 38 Removing Files And Directories 39 Creating Symlinks With The GUI 40 Summing Up 41 Further Reading 41 to: bshotts@users.sourceforge.net subj: The Linux Command Line erratum The Linux Command Line, Second Internet Edition, page 23, "have a file referenced by multiple names" is inverted. Should read "reference multiple files with one name". Nice book, and fewer erratum than most books :) Thank you for writing such a beautiful book. ## 5 – Working With Commands 42 What Exactly Are Commands? 42 Identifying Commands 43 type – Display A Command's Type 43 which – Display An Executable's Location 43 Getting A Command's Documentation 44 help – Get Help For Shell Builtins 44 --help – Display Usage Information 45 iiman – Display A Program's Manual Page 45 apropos – Display Appropriate Commands 47 whatis – Display A Very Brief Description Of A Command 47 The Most Brutal Man Page Of Them All 48 info – Display A Program's Info Entry 48 README And Other Program Documentation Files 49 Creating Your Own Commands With alias 50 Summing Up 52 Further Reading 52 ## 6 – Redirection 53 Standard Input, Output, And Error 53 Redirecting Standard Output 54 Redirecting Standard Error 55 Redirecting Standard Output And Standard Error To One File 56 Disposing Of Unwanted Output 57 /dev/null In Unix Culture 57 Redirecting Standard Input 57 cat – Concatenate Files 57 Pipelines 59 The Difference Between > and | 60 Filters 61 uniq - Report Or Omit Repeated Lines 61 wc – Print Line, Word, And Byte Counts 62 grep – Print Lines Matching A Pattern 62 head / tail – Print First / Last Part Of Files 63 tee – Read From Stdin And Output To Stdout And Files 64 Summing Up 65 Linux Is About Imagination 65 ## 7 – Seeing The World As The Shell Sees It67 Expansion67 Pathname Expansion68 Pathname Expansion Of Hidden Files69 Tilde Expansion69 Arithmetic Expansion70 Brace Expansion71 Parameter Expansion72 Command Substitution73 Quoting74 Double Quotes75 Single Quotes76 Escaping Characters77 Backslash Escape Sequences77 Summing Up78 Further Reading78 ## 8 – Advanced Keyboard Tricks79 Command Line Editing79 Cursor Movement79 iiiModifying Text80 Cutting And Pasting (Killing And Yanking) Text80 The Meta Key81 Completion81 Programmable Completion83 Using History83 Searching History84 History Expansion86 script86 Summing Up86 Further Reading87 ## 9 – Permissions88 Owners, Group Members, And Everybody Else89 Reading, Writing, And Executing90 chmod – Change File Mode92 What The Heck Is Octal?93 Setting File Mode With The GUI95 umask – Set Default Permissions96 Some Special Permissions98 Changing Identities99 su – Run A Shell With Substitute User And Group IDs99 sudo – Execute A Command As Another User101 Ubuntu And sudo101 chown – Change File Owner And Group102 chgrp – Change Group Ownership103 Exercising Our Privileges103 Changing Your Password106 Summing Up107 Further Reading107 ## 10 – Processes108 How A Process Works108 Viewing Processes109 Viewing Processes Dynamically With top111 Controlling Processes113 Interrupting A Process114 Putting A Process In The Background114 Returning A Process To The Foreground115 Stopping (Pausing) A Process116 Signals117 Sending Signals To Processes With kill117 Sending Signals To Multiple Processes With killall120 More Process Related Commands120 Summing Up121 # Part 2 – Configuration And The Environment123 # Part 3 – Common Tasks And Essential Tools # Part 4 – Writing Shell Scripts