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
man
andinfo
pages 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 hier
gives description of the filesystem hierarchy. A few examples:/
This is the root directory. This is where the whole tree starts./bin
This directory contains executable programs which are needed in single user mode and to bring the system up or repair it./home
On 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./tmp
This directory contains temporary files which may be deleted with no notice, such as by a regular job or at system boot up./usr
This 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/bin
This 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/share
This 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/learnbyexample
absolute path../design
relative 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/shells
to know which shells are availableecho "$SHELL"
to know which is your login-shell
### Command Help
man man
get help about manual pages- usually displayed using
less
command, pressq
key to quit the man page andh
key to get help - for GNU/Linux commands, the
info
command has more detailed documentation
- usually displayed using
man bash
manual page forbash
gvim <(man bash)
open the manual page in text editor instead of displaying in terminal
man -k printf
search manual pages forprintf
man -k
is equivalent forapropos
command
type
Display information about command typetype cd
cd is a shell builtintype sed
sed is /bin/sedtype ls
ls is aliased tols --color=auto
- Use
help
for builtin commandshelp help
help page onhelp
commandhelp -m cd
display usage in pseudo-manpage format forcd
commandhelp -d compgen
use -d option to output short description for each topichelp
display all shell commands that are defined internally
whatis
display one-line manual page descriptionswhatis grep
print lines matching a pattern
whereis
locate the binary, source, and manual page files for a commandwhereis awk
awk: /usr/bin/awk /usr/bin/X11/awk /usr/share/awk /usr/share/man/man1/awk.1.gz
history
Display 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
clear
clear the terminal screentop
display Linux processes
command with options
ls -l
list directory contents, use a long listing formatdf -h
report file system disk space usage, print sizes in human readable format (e.g., 1K 234M 2G)
command with arguments
mkdir project
create directory named 'project' in current working directoryman sort
manual page forsort
commandwget http://s.ntnu.no/bashguide.pdf
download file from internet
command with options and arguments
rm -r project
remove '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 -h
calculate 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.txt
writes/overwrites to filegrep -i 'error' *.log >> errors.txt
appends to file
Redirecting input
wc -l < file.txt
in this case, useful to get just the number of lines, without displaying file name
Redirecting error
xyz 2> cmderror.log
assuming 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.txt
multiple commands can be grouped in()
and redirected as if single command output
Command substitution
sed -i "s|^|$(basename $PWD)/|" dir_list.txt
add 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