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:
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.
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]
Display files in current directory.
Ls -l display detailed information.
Ls -a display hidden files.
Ls -t sort by date modified
Change directory. . is current directory. .. is top directory. ~ is home directory
Ex: cd ~ go back to home directory.
Displays current working directory, the directory you are in.
Write something to the screen with carriage return. Prints to standard output.
Ex: echo "Hello, World!"
Create a file.
Ex: touch file.txt
Create a directory.
mkdir [dirname]
Copy a file. Use -r for recursive copy (copy a directory).
Ex: cp [src] [dst]
Delete a file / a directory.
Display a file.
Ex: cat file.txt
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
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].
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
wc -l : print number of lines.
wc -m: print number of characters.
Ex: wc -l file.txt
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].
Change permissions.
Ex: chmod +x file. Gives execute permission to the file.
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
Print to screen. Print variables similar to C.
Ex: var = hello; printf “variable: %s” $var;
List the running processes on the machine with their PID.
ps -e or ps -A : list all processes.