Linux Command Line introduction
Command Line introduction
For any thing that is repetitive or programmable, there likely is a relevant command. Ask your peers or search online before you start writing a script. Just remember that Unix was first introduced in late 1960s - there is likely to be a command for what you need
Starting trouble with command line (for those accustomed to GUI) is the sudden trouble of interacting with the computer using just text commands. After using for a week or so, things will seem very systematic and GUI feels ill suited for frequent tasks. With continuous use, recalling various commands becomes easier. Short-cuts, history, aliases and tab-completion help in the process
If you’ve used a scientific calculator, you’d know that it is handy with too many functionalities cramped into tiny screen and plethora of multi-purpose buttons. Commands and short-cuts pack much more punch than that on a terminal.
- Commands presented here are GNU/Linux specific and generally behave similarly across distros
- Commands in GNU/Linux usually have a few different options and syntax than POSIX specification
manandinfopages of commands come in handy
- If any command is not found in a particular distro, either it has to be manually installed or not available for that distro
- The bash shell version 4+ is used throughout this material
File System
Before we dive into ocean of commands, lets get a brief on Linux file system. If you’ve used Windows, you would be familiar with C: D: etc.
In Linux, directory structure starts with / symbol, which is referred as the root directory
man hiergives description of the filesystem hierarchy. A few examples:/This is the root directory. This is where the whole tree starts./binThis directory contains executable programs which are needed in single user mode and to bring the system up or repair it./homeOn machines with home directories for users, these are usually beneath this directory, directly or not. The structure of this directory depends on local administration decisions./tmpThis directory contains temporary files which may be deleted with no notice, such as by a regular job or at system boot up./usrThis directory is usually mounted from a separate partition. It should hold only sharable, read-only data, so that it can be mounted by various machines running Linux./usr/binThis is the primary directory for executable programs. Most programs executed by normal users which are not needed for booting or for repairing the system and which are not installed locally should be placed in this directory./usr/shareThis directory contains subdirectories with specific application data, that can be shared among different architectures of the same OS. Often one finds stuff here that used to live in /usr/doc or /usr/lib or /usr/man.
Absolute and Relative paths
Quoting wikipedia
An absolute or full path points to the same location in a file system regardless of the current working directory. To do that, it must contain the root directory.
By contrast, a relative path starts from some given working directory, avoiding the need to provide the full absolute path. A filename can be considered as a relative path based at the current working directory. If the working directory is not the file’s parent directory, a file not found error will result if the file is addressed by its name.
/home/learnbyexampleabsolute path../designrelative path- ~/Documents is a relative or an absolute path?
Further Reading
Command Line Interface
Command Line Interface (CLI) allows us interact with computer using text commands
For example: opening a Terminal, typing ls and pressing Enter key - the ls command lists the contents of a directory. To do the same thing in GUI, you double-click on the directory to view its content
Shell and Terminal are sometimes interchangeably used to mean the same thing - a prompt where user enters and executes commands. They are quite different
- Shell command line interpreter
- Terminal text input/output environment
cat /etc/shellsto know which shells are availableecho "$SHELL"to know which is your login-shell
Command Help
man manget help about manual pages- usually displayed using
lesscommand, pressqkey to quit the man page andhkey to get help - for GNU/Linux commands, the
infocommand has more detailed documentation
- usually displayed using
man bashmanual page forbashgvim <(man bash)open the manual page in text editor instead of displaying in terminal
man -k printfsearch manual pages forprintfman -kis equivalent foraproposcommand
typeDisplay information about command typetype cdcd is a shell builtintype sedsed is /bin/sedtype lsls is aliased tols --color=auto
- Use
helpfor builtin commandshelp helphelp page onhelpcommandhelp -m cddisplay usage in pseudo-manpage format forcdcommandhelp -d compgenuse -d option to output short description for each topichelpdisplay all shell commands that are defined internally
whatisdisplay one-line manual page descriptionswhatis grepprint lines matching a pattern
whereislocate the binary, source, and manual page files for a commandwhereis awkawk: /usr/bin/awk /usr/bin/X11/awk /usr/share/awk /usr/share/man/man1/awk.1.gz
historyDisplay or manipulate the history list
Further Reading
- Excellent resource: How do I use man pages to learn how to use commands?
- explainshell write down a command line to see the help text that matches each argument
- example: find . -type f -print0
- ch similar functionality from command line, doesn’t have all features of explainshell though
- which, whatis, whereis examples
Do one thing and do it well
Write programs that do one thing and do it well.
Write programs to work together.
Write programs to handle text streams, because that is a universal interface.
Examples given below are for demonstration purposes only, more detail in later chapters
Command Structure
only the command
clearclear the terminal screentopdisplay Linux processes
command with options
ls -llist directory contents, use a long listing formatdf -hreport file system disk space usage, print sizes in human readable format (e.g., 1K 234M 2G)
command with arguments
mkdir projectcreate directory named ‘project’ in current working directoryman sortmanual page forsortcommandwget http://s.ntnu.no/bashguide.pdfdownload file from internet
command with options and arguments
rm -r projectremove ‘project’ directoryfind . -name '*log*'print files in current directory (and its sub-directories) whose name contains the string ‘log’
single quotes vs double quotes
- single quotes preserves the literal value of each character within the quotes
- double quotes preserves the literal value of all characters within the quotes, with the exception of ‘$’, ‘`’, ‘', and, when history expansion is enabled, ‘!’
- Difference between single and double quotes
Example:
$ echo '$SHELL'
$SHELL
$ echo "$SHELL"
/bin/bash
Command Network
Redirecting output of a command
- to another command
du -sh * | sort -hcalculate size of files/folders, display size in human-readable format which is then sorted
- to a file (instead of displaying on terminal)
grep -i 'pass' *.log > pass_list.txtwrites/overwrites to filegrep -i 'error' *.log >> errors.txtappends to file
Redirecting input
wc -l < file.txtin this case, useful to get just the number of lines, without displaying file name
Redirecting error
xyz 2> cmderror.logassuming a non-existent commandxyz, it would give an error and gets redirected to specified file
Redirecting output of command as input file
comm -23 <(sort file1.txt) <(sort file2.txt)allows to create named pipes, effectively avoiding need to create temporary files
Combining output of several commands
(head -5 ~/.vimrc ; tail -5 ~/.vimrc) > vimrc_snippet.txtmultiple commands can be grouped in()and redirected as if single command output
Command substitution
sed -i "s|^|$(basename $PWD)/|" dir_list.txtadd current directory path and forward-slash character at the start of every line- Note the use of double quotes
stdin, stdout and stderr
<or0<is stdin filehandle>or1>is stdout filehandle2>is stderr filehandle- read more
More detailed discussion in Shell chapter