Top Linux Commands

Here is a list of basic Unix/Linux commands for the terminal as well as scripts. This list is good for beginners who want to learn Unix.

This, is a terminal:

picture of a linux terminal

The terminal, also called shell, or command prompt on Windows, allows you to input commands for the computer to follow. In the old days, back when there was no GUI (Graphic User Interface) all computers worked this way.

The commands are followed by their options, preceded by a -, and then their argument, which can be a file name.


1. MAN

Although many commands display a help witht the --help option, man cmd might give more in-depth information. It refers to the manual.

Ex: man [command]


2. LS

Display files in current directory.

Ls -l display detailed information.

Ls -a display hidden files.

Ls -t sort by date modified


3. CD

Change directory. . is current directory. .. is top directory. ~ is home directory

Ex: cd ~ go back to home directory.


4. PWD

Displays current working directory, the directory you are in.


5. ECHO

Write something to the screen with carriage return. Prints to standard output.

Ex: echo "Hello, World!"


6. TOUCH

Create a file.

Ex: touch file.txt


7. MKDIR

Create a directory.

mkdir [dirname]


8. CP

Copy a file. Use -r for recursive copy (copy a directory).

Ex: cp [src] [dst]


9. RM / RMDIR

Delete a file / a directory.


10. CAT

Display a file.

Ex: cat file.txt


11. The pipe operator or |

While not a command this is a useful operator. It is used to pipe the output of one command into the input of another command.

Ex: cat file.txt | grep hello


12. GREP

Search for a string. Returns the line containing the string.

grep -i ignore case

grep -v display all lines that do not match the pattern

Ex: echo hello | grep e

Ex: cat file.txt | grep hello

grep makes use of REGEX, or regular expressions to work. For more detail, see the following article [under construction].


13. The > operator, or write to or redirect

Redirects the output of a command to a file. The difference between pipe | and redirect > is that pipe redirects to a command, whereas > redirects to a file.

Ex: echo "hi" > file.txt

Good to know:
- stdin= 0 standard input
- stdout= 1 standard output
- stderr= 2 for error messages

Redirect all errors to a file: >2 means redirecting stderr

command >2 file.txt

Redirect errors to stdout. (this will display the errors to output)

command 2>&1

Redirect to /dev/null. This will erase all the output by redirecting to a special file

command >2 /dev/null -> this will suppress all error messages from the command


14. WC

wc -l : print number of lines.

wc -m: print number of characters.

Ex: wc -l file.txt


15. HEAD and TAIL

Shows the first n lines of a program. Shows the n last lines of a program.

Ex: head -n 10 -> Shows the ten first lines of a program.

You can pipe head and tail into each other. This might be useful if you want to show the lines between two numbers, as show in the link: [under construction please bookmark to stay updated].


16. CHMOD

Change permissions.

Ex: chmod +x file. Gives execute permission to the file.

How does chmod work?

When doing ls -l, you will notice a line that looks like this: -rw-r-xr--. These are the file permissions. The first three digits refer to the user, the second three digits to the group, and the last three digits to others. The three digits have the values of r, w or x.

- r: read permission
- w : write permission
- x : execute permission

If there is a dash instead of the letter this means the permission is not granted. Note that there is a dash in front which means the object is a file, this can also be a d which means it is a directory.

Now, how to get the permission integer? So, for each group of three letters, a number between 0 and 7 is assigned, giving a three digit number, such as 777, for each three letters group.

Here is how the number is calculated:

- r is 4
- w is 2
- x is 1

You can then calculate the number. Let's say you have a rw- group: that is r which is 4 plus w which is 2 which gives a total of 6. So six is your number for the group. It's really pretty simple. There are three groups of three letters, giving a three digit number.

Ex: chmod 607 file.txt


17. PRINTF

Print to screen. Print variables similar to C.

Ex: var = hello; printf “variable: %s” $var;


18. PS

List the running processes on the machine with their PID.

ps -e or ps -A : list all processes.