Skip to main content

Basic Unix Commands quick reference

This quick reference lists commands, including a syntax diagram and brief description. […] indicates an optional part of the command.

  • For more detail, use: man command


1. Files
1.1. Filename Substitution
Wild Cards                ? *
Character Class (c is any single character) [c…]
Range                 [c-c]
Home Directory         ~
Home Directory of Another User ~user
List Files in Current Directory ls [-l]
List Hidden Files                 ls -[l]a
1.2. File Manipulation
Display File Contents         cat filename
Copy                 cp source destination
Move (Rename)         mv oldname newname
Remove (Delete)         rm filename

1.3. File Properties
Seeing Permissions                ls -l filename
Changing Permissions        chmod nnn filename
                       chmod c=p…[,c=p…] filename


  • n, a digit from 0 to 7, sets the access level for the user (owner), group, and others (public), respectively. c is one of: u–user; g–group, o–others, or a–all. p is one of: r–read access, w–write access, or x–execute access.


Setting Default Permissions umask ugo

  • ugo is a (3-digit) number. Each digit restricts the default permissions for the user, group, and others, respectively.


Changing Modification Time touch filename
Making Links         ln [-s] oldname newname
Seeing File Types         ls -F

1.4. Displaying a File with less
Run less         less filename
Next line         RETURN
Next Page SPACE
Previous line k
Previous Page b
At end G
Go up U
Quit         q
Search down /
Search up ?

1.5. Directories
Change Directory         cd directory
Make New Directory mkdir directory
Remove Directory         rmdir directory
Print Working (Show Current) Directory pwd

2. Commands
2.1. Command-line Special Characters
Join Words                "…"
Suppress Filename, Variable Substitution '…'
Escape Character                  \
Command Separation          ;
Command-Line Continuation (at end of line) \

2.2. I/O Redirection and Pipes
Standard Output >
(overwrite if exists) >!
Appending to Standard Output >>
Standard Input <
Standard Error and Output >&
Standard Error Separately
( command > output ) >& errorfile
Pipes/Pipelines command | filter [ | filter]

Filters
Word/Line Count wc [-l]
Last n Lines         tail [-n]
Sort lines                 sort [-n]
Multicolumn Output pr -t

2.3. Searching with grep
grep Command grep "pattern" filename
                command | grep "pattern"
Search Patterns
beginning of line         ^
end of line         $
any single character .
single character in list or range […]
character not in list or range [^…]
escapes special meaning \

3. C-Shell Features.
3.1 History Substitution
Repeat Previous Command !!
Commands Beginning with str !str
Commands Containing str !?str[?]
All Arguments to Prev. Command !*
Word Designators
All Arguments :*
Last Argument :$
First Argument :^
n'th Argument         :n
Arguments x Through y :x-y
Modifiers
Print Command Line :p
Substitute Command Line :[g]s/l/r/
3.2 Aliases
alias Command alias name 'definition'


  • definition can contain escaped history substitution event and word designators as placeholders for command-line arguments.


3.3. Variable Substitution
Creating a Variable set var
Assigning a Value set var = value
Expressing a Value $var
Displaying a Value echo $var


  • value is a single word, an expression in quotes, or an expression that results in a single word after variable, filename and command substitution takes place.


Assigning a List set var = (list)

  • list is a space-separated list of words, or an expression that results in a space-separated list.

Selecting the n'th Item $var[n]
Selecting all Items $var
Selecting a Range $var[x-y]
Item Count $#var
3.4 foreach Lists
Start foreach Loop foreach var (list)

  • foreach prompts for commands to repeat for each item in list (with >), until you type end. Within the loop, $var stands for the current item in list.

3.5. Command Substitution
Replace Command with its Output on Command Line `…`
3.6 Job Control
Run Command in the Background &
Stop Foreground Job         CTRL-Z
List of Background Jobs          jobs
Bring Job Forward                 %[n]
Resume Job in Background         %[n] &
4. Processes
Listing ps [-[ef]]
Terminating kill [-9] PID

  • Timing time command time is a number up to 4 digits. script is the name of a file containing the command line(s) to perform.


5. Users
Seeing Who is Logged In who
                w
Seeing Your User Name whoami

6. Managing Files
6.1. Looking Up Files
Standard Commands whereis file
Aliases and Commands which command
Describe Command whatis command
Searching Out Files find dir -name name -print

  • dir is a directory name within which to search. name is a filename to search for.


6.2. Finding Changes
Comparing Files diff leftfile rightfile

  • diff prefixes a less-than (<) to selected lines from leftfile and a greater-than (>) to lines from rightfile.


6.3. Automating Tasks
Create a Makefile pico Makefile

  • A makefile consists of macro definitions and targets.

Test Makefile make -n [target]
Run make make [target]
6.4. Managing Disk Usage
Check Quota quota -v
Seeing Disk Usage df
        du -s
6.5. Combining and Compressing Files
Create a tarfile tar cf file.tar file1 file2 … fileN

  • tar combines files but does not compress 

Create a zipfile zip filename / gzip filename
Unzip a file unzip filename / gunzip filename
7. Printing
7.1 Formatting Output for Printing
Paginate with Page Headers pr filename
in n columns pr -n filename

8. Miscellaneous
8.1 Miscellaneous Commands
List Commands for Subject man -k subject
Display Current Date and Time date
Log off         exit
Display Documentation man command
8.2 Control Keys
Abort Program CTRL-C
Backspace (Delete Last Character) CTRL-H
Pause Display on Screen CTRL-S
Resume Display after CTRL-S CTRL-Q
Send Job to Background CTRL-Z followed by bg

Comments

Popular posts from this blog

Optimal Binary Search using Dynamic Programming

An optimal binary search tree is a binary search tree for which the nodes are arranged on levels such that the tree cost is minimum. If the probabilities of searching for elements of a set are known from accumulated data from past searches, then Binary Search Tree (BST) should be such that the average number of comparisons in a search should be minimum. eg. Lets the elements to be searched are A, B, C, D and probabilities of searching these items are 0.1, 0.2, 0.4 and 0.3 respectively. Lets consider 2 out of 14 possible BST containing these keys. Figure 1 Figure 2 Average number of comparison is calculated as sum of level*probability(key element) for each element of the tree. Lets the level of tree start from 1. Therefore, for figure 1 -     Average number of comparison = 1*0.1 +2*0.2 +3*0.4 +4*0.3  = 2.9                                  ...

Job Sequencing with Deadlines

Given a set of n jobs Each job i has an integer deadlines di>=0 and a profit pi>0 All jobs requires only one unit time to complete Only one machine is available for processing jobs For job i the profit pi is earned if the job is completed by its deadline.

Knapsack Problem and Solution using Dynamic Programming

The knapsack problem or rucksack problem is a problem in combinatorial optimization: Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible Given a knapsack of capacity m and number of items n of weight w1, w2, w3 ... , wn with profits p1, p2, p3..., pn. Let x1,x2,...,xn is an array that represents the items has been selected or not. If the item i is selected, then xi = 1 If the item i is not selected then x i = 0 In 0/1 knapsack, the item can be selected or completely rejected. The items are not allowed to be broken into smaller parts. The main objective is to place the items into the knapsack so that maximum profit is obtained or find the most valuable subset of items that fits into the knapsack. Constraints: The weight of the items chosen should not exceed the capacity of knapsack. Obj...