Unix is a family of computer operating systems that derive from the original Unix from Bell Labs.
This is the Unix philosophy: 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.
Expect the output of every program to become the input to another, as yet unknown, program. Don't clutter output with extraneous information. Avoid stringently columnar or binary input formats. Don't insist on interactive input.
Locations in home
$HOME/bin | Local binaries |
---|---|
$HOME/etc | System configuration for local binaries |
$HOME/games | Local game binaries |
$HOME/include | Local C header files |
$HOME/lib | Local libraries |
$HOME/lib64 | Local 64-bit libraries |
$HOME/man | Local online manuals |
$HOME/sbin | Local system binaries |
$HOME/share | Local architecture-independent hierarchy |
$HOME/src | Local source code |
Primitives
ls | List files in the directory |
cd | Change directory |
rm | Remove file or directory(-r) |
cp | Copy file or directory(-r) |
mv | Move file or directory |
wc | Count words in file |
man | Read the manual |
cat | Reads content of files sequentially |
mkdir | Make new directory |
date | Show system date |
grep | Searches text for matches of a regular expression |
tail | Displays the tail end of a file or data |
Cheatsheet
Copy a file
$ cp readme.txt documents/
Duplicate a file
$ cp readme.txt readme.bak.txt
Copy a directory
$ cp -a myMusic myMedia/
Duplicate a directory
$ cp -a myMusic/ myMedia/
Move a file
$ mv readme.txt documents/
Move a directory
$ mv myMedia myMusic/
Rename a directory
$ mv myMedia/ myMusic/
Merge directories
$ rsync -a /images/ /images2/
Create a new file
$ touch 'new file'
Create a new directory
$ mkdir 'untitled folder'
Show file/directory size
$ du -sh node_modules/
Show file/directory info
$ stat readme.md
Open a file with the default program
$ xdg-open file # on Linux
Zip a directory
$ zip -r archive_name.zip folder_to_compress
Unzip a directory
$ unzip archive_name.zip
Peek files in a zip file
$ unzip -l archive_name.zip
Remove a file
$ rm my_useless_file $ rm -r my_useless_folder
List directory contents
$ ls my_folder # Simple
Tree view a directory and its subdirectories
$ tree
Find a stale file
$ find my_folder -mtime +5
View content of a file
$ cat apps/settings.py
Search for a text
$ grep -i "Query" file.txt
Make applications in ~/bin/ available from anywhere
Edit ~/.bashrc
, when finished run source ~/.bashrc
.
export PATH=$PATH:$HOME/bin
Pipes
ls > foo | Send output from ls into file foo |
wc < foo | Receive input from file foo into wc |
ls | wc | Connect output of ls into input of wc |
Aliases
An alias let you define your own command names, so you can customize the command line, and make it work the way you want it to work. You can see the list of aliases with:
alias
To create an alias that will be there for you every time you start your computer, open ~/.bash_profile and add a line like:
alias left='uxnemu ~/roms/left.rom'
When finished, run the following line to apply your changes:
source ~/.bash_profile
You have just created a new alias called left.
From each according to their stdout to each according to their stdin
Usage
A usage statement is a printed summary of how to invoke a program from a shell prompt. It includes a description of the possible command-line arguments that the program might take. If a program is called with incorrect command-line arguments, it should print a usage statement to the terminal.
usage: ls [-a] [-F]
This program is called ls and can be called with
no arguments, with one optional argument (ls -a
or
ls -F
) or with two optional arguments
(ls -a -F
). In this particular case, the
-a
optional argument says to list hidden files as
well as normal ones, and -F
changes the way the
output of the program is formatted.
Usage statements should be printed to "standard error" stderr
, and not to stdout
. Your usage statement should contain:
- The name of the program
- Every non-optional command-line argument your program takes
- Every optional command-line argument your program takes
- Any extra descriptive material that the user should know about.
Formatting guidelines
- The usage statement should always begin with the literal word
usage
in lower case, followed by a colon and space, followed by the rest of the usage message. So, the usage message starts withusage:
. - Every command-line argument in the usage statement should be a single word, with no spaces.
- The word representing a particular command-line argument should be descriptive. It should say what the command-line argument is supposed to represent, at least in general terms. Do not separate successive command-line arguments with commas or semicolons; just separate them with single spaces.
Optional arguments should always be surrounded by square brackets. Do not use square brackets for any other purpose in usage messages. Don't use anything but square brackets for this purpose. Square brackets mean that an argument is optional, always.
Optional "flags" (arguments that change the way the program works) should start with a dash. Very often (but not always) they will have a name which is a dash followed by a single letter, which identifies what it is.
Note that each optional argument gets its own set of square brackets. If an optional "flag" argument itself has arguments, put them inside the square brackets.
Notes
You shouldn't hard-code the program name into your program's source code.
It won't make any difference in how it's displayed, but if you change the
name of your program and don't change your usage message the usage message
will be invalid. Instead, you should write your code so that the program
name gets inserted into the usage message before printing. In C, the program
name is argv[0]
(the first element of the
argv
array) whereas in python it's
sys.argv[0]
.