Basic Unix Linux commands
The following article describes very basic commands to be used on the Linux or FreeBSD command line, which is the way you interact with your VPS or server if you chose not to install any control panel. To interact with your VPS or server in this way, first you need to login vian SSH or Secure VNC, SSH being the preferred method. The scope of this article is limited to very basic functions. For more details you can read the man or info pages of any command. Some of the examples below are abstract. To make things clear, we have setup another article where we give a real life example of some of the most used commands.
Throughout this tutorial you will see a $ or a # sign preceeding all the commands. That's just the prompt that you will see in the command line and you it is not to be written as part of that command. The $ sign means a command issued as a [[Your_account#Your_Linux_or_FreeBSD_username|normal user, while the # means a command issued as the root user.
The directories on a Unix system follow certain conventions and have a specific hierarchy.
- For example: the first directory (called the root directory) is identified by /
Below that directory, every directory is separated by another /.
- For example: the home directory for a user is /home/user meaning that the directory home is below root and the directory user is below home
Directories are mainly read by their name, but some have simple shortcuts that save the typing. Some examples are:
- ~ is the home directory (for example /home/user).
- . is the present directory
- .. is the higher directory in the hierarchy
- - is the previous working directory
- You do not need to write the complete commands and file names: You can type just the first letters and then press the TAB button. If there is no ambiguity on the filename or command, it will be completed automatically. If there is ambiguity you will be shown the options you have:
you will be shown:
fdformat fdisk $ fdi[TAB]
will complete the fdisk command
- Other example:
$ ls /home/u[TAB]
If there is no other directory that starts with the letter u, it will be auto-completed:
$ ls /home/user
- If you press the UP arrow, you will be able to navigate through the commands that you have already typed.
- To type a command that you have used before just press the UP arrow. When you find the desired command, press ENTER
- If you want to go back to more recent commands just press the DOWN arrow.
- If you want to see all the history of the commands you have used, use the following command:
You will see a list of all the commands you have used preceeded with an ID number. To execute a command from that list, just append the ID number to a ! sign:
and that will execute the command with the ID 24.
Getting rid of the clutter in the screen
To clear the screen execute the following command:
Getting around Directories
ls See the files and directories on the present dierctory
- To display the contents of the present directory:
- To display the contents of the present directory in alphabetical order, plus size (in bytes), users, gruopus, permisions, modification date:
$ ls -al
- The same as before but making the file size more human-readable (ie megabytes, gygabytes):
$ ls -alh
- To display the contents of a specific directory:
$ ls /usr/bin
- To display the contents of the home directory:
$ ls /home/user
$ ls ~
- To display the contents of the previous directory on the directory hierarchy:
$ ls ..
- To show the files that match a specific string:
$ ls some*
will show all the files that start with "some"
$ ls *some*
will show the files that have the "some" string on them
mkdir Create a directory
- To create a directory under the present directory:
$ mkdir somedir
- To create a directory under other directory:
$ mkdir /home/user/documents/somedir
- To create a directory under the previous directory on the directory hierarchy:
$ mkdir ../somedir
rmdir remove an empty directory
- To remove an empty directory (please note that the same conventions apply for this as for the previous topic so they will not be repeated).
$ rmdir somedir
rm -rf Remove a non-empty directory including all of its files (use with caution)
- If you issue the following command:
$ rmdir somedir
and the directory is not empty you will encounter the following error: rmdir: failed to remove ‘prueba/’: Directory not empty
- So, to remove that directory including all its files you have to issue the following command:
$ rm -rf somedir
Please issue the above command with caution: all the files under that directory will be removed and cannot be recovered by conventional means.
You could wipe your entire system with the rm -rf command if executed as root. Make sure you know what you are removing
cd Go to another directory
- Go to a specific directory:
$ cd /home/user/documents
- Go to the home directory
$ cd /home/user
- or, to go to your home directory:
$ cd ~
- Go to the previous directory on the directory tree
$ cd ..
- Go to the previous working directory
$ cd -
mkdir Create directory
- To create a directory on the working directory
$ mkdir somedir
- To create a directory under a specific path:
$ mkdir /home/user/somedir
pwd See which directory you are in
- To see where you are:
and the result will be:
cp Copy a file (or files)
- Copy a file from the present directory to another directory:
$ cp somefile /some/dir
- Copy a file from the present directory to another directory giving it another name:
$ cp somefile /some/dir/someotherfile
- Copy a file that resides on other directory to the present directory:
$ cp /some/dir/otherdir/somefile .
- Copy a file that resides on the previous directory to the present directory:
$ cp ../somefile .
- Copy a directory and all its contents:
$ cp -r /home/user/somedir /home/user/someotherdir
- Copy from a path below the home directory: Issuing:
$ cp /home/user/somedir/somefile .
is the same as issuing:
$ cp ~/somedir/somefile .
- Copy files following a specific string (wildcards)
$ cp some* ~/somedir
will copy all the files that start with "some"
$ cp *some* ~/somedir
will copy all the files that have the "some" string on them
mv Move or rename a file (or files)
- To move a file from the present directory to other directory
$ mv somefile /some/dir
Nothe that the original file disappears.
- To rename a file:
$ mv somefile someotherfile
- To rename a file moving it to another directory:
$ mv /some/dir/somefile /some/dir/otherdir/someotherfile
- To move a directory to another place:
$ mv /some/dir/somedir /some/other/dir
- To rename a directory:
$ mv somedir someotherdir
In the previous two commands the contents of the directory stay untouched inside that directory.
touch Create an empty file
- To create an empty file:
$ touch newfile
- To change the creation date and time of a file to the present time without modifying its contents:
$ touch oldfile
rm Remove a file
- To remove a file:
$ rm somefile
- To remove a file following a path:
$ rm /some/dir/somefile
- To remove all the files in a directory (USE WITH CAUTION!)
$ rm /some/dir/*
- To remove all the files in a directory and the directory itself (USE WITH CAUTION!)
$ rm -rf /some/dir
Please issue the above command with caution: all the files under that directory will be removed and cannot be recovered by conventional means. You can wipe your entire system with this command if executed as root. Make sure you know what you are removing
cat Display the contents of a file
- To see the contents of a file just issue the cat command:
$ cat somefile
will display on the screen the contents of that file.
Some useful cat examples:
- To see the CPU information of your system:
$ cat /proc/cpuinfo
- To see the RAM information of your system:
$ cat /proc/meminfo
- To see the mount points of your system:
$ cat /etc/fstab
less Display the contents of a file one screen at a time
- If the file you want to see covers more than one screen the cat command is not very useful since you will miss some information.
- For those cases, use the less command. You will be able to move with the up and down arrow keys.
- To quit just press the q key.
$ less /etc/mysql/my.cnf
Some useful less examples:
- To see the kernel messages from your system:
$ dmesg | less
In this case, we issue the command dmesg to see all the kernel logs and we pipe it through less (see below for pipe explanation).
head Display the first lines of a file
- Display the first 10 lines of a file:
$ head somefile
- Display a specified number of lines of a file
$ head -n5 somefile
will display the first 5 lines of the file.
tail Display the last lines of a file
- Display the last 10 lines of a file:
$ tail somefile
- Display a specified number of (last) lines of a file
$ tail -n5 somefile
will display the last 5 lines of the file.
- Watch a file while it is being changed (log files for example)
$ tail -f somefile
will display the last part of a file and you will be able to see the changes as they take place.
- Example: to see the email logs as they change (for example as sending or receiving emails)
$ tail -f /var/log/maillog
grep Searching for contents of a file
- See a the line containing a specific string inside a file (it will not look for a word. It will look for a specific string, so if we type part of a word it will find the whole word)
$ grep someword somefile
will display the whole line containing the specified word in the specified file
- See a the line containing a specific string (word) inside a file.
- Note that the grep command is case-sensitive, so if we want to find the word "Dog" inside the file "animals" this won't work:
$ grep dog animals.txt
We would have to issue:
$ grep Dog animals.txt
To solve this, we can tell the grep command to do its search on a non-case-sensitive way:
$ grep -i dog animals.txt
- The following example will work, since grep will look for the matching letters fo that word:
$ grep og animals.txt dog Dog
ln Link a file
- To create a symbolic link to a specific target with the link name:
$ ln -s /home/user/pitcures /home/user/images
Will create a symbolic link from the directory pictures to the directory images.
$ ln -s /usr/local/bin/somecommand /usr/bin
will create a symbolic link from the command on the directory /user/local/bin to the directory /usr/bin
Manipulating the contents of a file
- To create a file inserting some text in it:
$ echo "this is some text" > somefile
To see that the text is inserted:
$ cat somefile this is some text
- To insert some text in a file deleting any other text that is inside of that file:
$ echo "this is some other text" > somefile $ cat somefile this is some other text
- To append a string of text at the end of a file without removing its original contents:
$ echo "appending some more text" >> somefile $ cat somefile this is some other text appending some more text
- To insert the contents of a file into a new file (following the previous example):
$ cat somefile > someotherfile $ cat someotherfile this is some other text appending some more text
- Now lets assume that the file named someotherfile already has some content:
$ cat someotherfile this is the original content $ cat somefile > someotherfile $ cat someotherfile this is some other text appending some more text
If you notice, this command has deleted the original content of someotherfile. If we wanted to keep the original content and append the new text at the end of the file we would issue:
$ cat somefile this is some other text appending some more text $ cat someotherfile this is the original content $ cat somefile >> someotherfile this is the original content this is some other text appending some more text
To "filter" a command under some other commands we use pipes which are identified by the symbol | Examples:
- See the contents of a specific file:
$ cat animals.txt Dog is the best friend Cat is very clean Horse is beautiful Donkey is stubborn
- Filter the line containing a specific word through the grep command
$ cat animals.txt | grep dog
(we don't see anything because grep is case-sensitive)
$ cat animals.txt | grep -i dog Dog is the best friend
- The last example is redundant because we can just grep into the file without the need to use the cat command, but the example is given to show how we can use pipes. Another more useful command would be:
$ ls -al | grep animal -rw-r--r-- 1 someuser users 24 May 5 18:16 animals.text
shows the result of the ls -al command but here we are filtering only the lines containing the string "animal" (note that grep checks for strings, not for full words. Thats why while grepping for animal showed animapls.txt)
File Permissions and ownerships
chmod Changing permissions of a file
- Change the permissions of a file:
$ chmod 777 somefile
will make the file executable for everybody.
- Change the file permissions of a directory recusrsively (the directory and all of the files and subdirectory inside it)
$ chmod -R 777 somedir
This examples only show the command to change the permissions. The explanation of the various permissions is outside of the scope of this article. See the chmod manual page for that.
chown Changing ownerships of a file
- Change the ownership of a file:
$ chown someuser:somegroup somefile
will make the user "someuser" the owner of the file and will make the users belonging to the group "somegroup" able to access the file.
- Change ownership of a directory recusrsively (the directory and all of the files and subdirectory inside it)
$ chown -R someuser:somegroup somedir
Display the active processes
- Show all the processes in the system including owner, process number, command, etc.
$ ps -aux
- Pipe a certain process through grep:
$ ps aux | grep bash someuser 2288 0.0 0.0 16128 2728 pts/2 Ss 18:54 0:00 /bin/bash someuser 2297 0.0 0.0 10664 1104 pts/2 S+ 18:55 0:00 grep bash
Note that our grep command is also shown. Since we are only interested in seeing the process that is occupying bash and not the one from our grep command, you can issue the following:
$ ps aux | grep [b]ash
will show only the information that we are interested in:
someuser 2288 0.0 0.0 16128 2728 pts/2 Ss 18:54 0:00 /bin/bash
Kill a process
- Following the previous example, lets assume that we want the bash process we saw to terminate. We can see on the second line that the process ID is 2288 so we can issue:
$ kill 2288
If the process "refuses to die":
$ kill -9 2288
That will kill the process we are interested in. Now lets assume we want to kill all the processes for mysql. We would issue:
$ killall mysql
That will terminate all the processes that mysql is occupying.
This article only describes how to use the commands but does not explain the meaning of them or how they work, since we want to give just an idea of how to resolve certain tasks when using your servers or any Linux or Unix machine.
It only scratches the surface and shows the most basic functions of those commands.
To get more detailed information you can see the manual pages or the info pages of those commands. There are also tons of information in forums, blogs and wikis with detailed information.
To display the manual page of a command issue the following:
$ man command
To display the info page:
$ info command
To display a brief information about what a command does:
$ whatis command
$ whatis cd cd (1p) - change the working directory