Linux commands review!!!

In this post I will bring you the key commands in linux that are important to know if you have some kind of problems when you compile software in the raspberry pi.

These commands are the ones I have used to learn about Linux operting systems. It is possible that some commands don’t work in the raspbian operating system but the most of them will work perfectly and they will bring you the skills to handle the raspberry pi in a correct way.

The commands will work because raspbian operating system is very similar to ubuntu or other linux operating systems with a debian distribution.

Also this draft will bring you the knowledge to see what you are doing when entering in the terminal the specified commands that I tell you in the posts under the category: Raspberry pi setup.

I have installed and ubuntu disk partition on my laptop to get used to the operating system and I practised there all the commands before going to the raspberry pi. There are a lot of videos on how to do a dual-booting in youtube. For example in my case I am dealing with a dual-booting system with windows 7 and ubuntu 12.04 LTS.

There is a video on how to do it: http://www.youtube.com/watch?v=0W7XYAB4cLc

But there is no a problem to play directly in the raspberry pi, because even if you make something wrong and the system crushes, you can remove all from the SD card and download another raspbian image there to get the system working well again.

Another option is to install a virtual machine and download and ubuntu image from their website. But the problem about this option is that you can’t access in the lower levels of the software when programming and that the virtual machine will get the CPU busy.

If you think that doing a dual-booting is such a difficult thing that could not be done by yourself I recommend you an easy windows installer that does the disk partition and all the work for you. I have just discovered this kind of installer and I think it’s a really interesting option for the people which are starting to deal with all of these kind of things.

And this is the software installer for Windows to download: http://www.ubuntu.com/download/desktop/windows-installer

There are the instructions to follow: http://www.ubuntu.com/download/desktop/install-ubuntu-with-windows

So if you are planning to use ubuntu afterwards I recommend the disk partition, but if you are just training for the rapsberry pi, I recommend the virtual machine option. But you must consider the installer if you will use linux sometime in the future.

COMMANDS:

ls               To see all the directories and files that are located inside the actual directory.

ls -a           To see all the directories and files that are located inside the actual directory and the ones that begins with a point, which are the not visible files.

ls “filename”         To see all the information of a file.

ls –l  “filename or directory”   gives more information about a directory or a file.

ls – lg  “filename or directory”     for more information about the directory or the file.

cd “linux”          It goes to a directory called “linux” that have to be placed inside the directory where you are placed now.

cd “/home/user/linux”          It goes to a directory called “linux” that is placed inside home and inside user folders.

cd .     Remains in the same directory.

cd..      It brings you to the directory where that includes the folder you are situated.

cd ~     It brings you to your home directory.

pwd    gives the path of the directory you are placed.

fig1

In the following commands, everywhere that you can see the name “file” have to be replaced by the path of the file if you are not in the directory where the file is, if you want to operate with the specified file or files:

cp “file1” “file2”              copies the file named “file1” of the directory where we are situated to this same directory but with the name “file2”, if you want to copy the file to another directory, you only have to specify the path.

mv “file1” “file2”              same command as before but now the file is moved not copied, it means that when the process finalizes there will be only one file while in the command above there will be two identical files.

rm “file1”          removes a file named “file1”.

mkdir “music”           Creates a directory named “music”.

rmdir “directory name”          removes an empty directory.

clear               removes the commands and output of the terminal.

cat “file1”          display the content of “file1” on the screen.

less “file1”         display one page of the “file1” content. Q quit of the mode.  Space-bar  to pass the page.

/word             to search the word “word” and then N  to pass to the next word “word”.

head “file1”       displays the first ten lines of the file.

head -5 “file1”       displays the first five lines of the file.

tail “file1”         displays the last ten linesof the file.

tail -5 “file1”     displays the last five lines of the file.

grep “word” “file1”         displays the line containing the word “word” of the document “file1”.

grep –i “word” “file1”     does the same but ignores the uppercase.

grep –i ‘sentence’ “file1”  or grep ‘sentence’ “file1”    display the sentence that is inside the single quotes(‘’).

options of grep:

                   -v display those lines that do NOT match.

                   -n precede each matching line with the line number.

                   -c print only the total count of matched lines.

wc –w “file1”    counts the words of the file.

wc –l “file1”      counts the rows of the file.

fig2

REDIRECT THE OUTPUT:

cat > “list1 ”      redirects the output to the file named list1.

cat >> “file1”     appends more items to the file named file1.

control d        to finalize the file.

cat “list1” “list2” “list3” > “biglist”    concatenates the three lists into a unique one list named biglist.

REDIRECT THE INPUT:

sort     sorts a list of things that we enter.

sort < “list1”     redirects the input and take it from a file called list1.

sort  < “biglist” > “newlist”        redirects the input from biglist, sort the information and finally redirects the output to newlist file.

PIPES:

whoami         to know with which user you are logged in.

who               gives a list of the users that are inside the system at that moment.

who|sort       combines the two options in one command.

who | wc –l   counts how many users are logged in.

——————————————————————————————————————–

EXERCISE:
Using pipes, display all lines of list1 and list2 containing the letter ‘p’, and sort the result.

SOLUTION:

Cat list1 list2|grep p | sort

SOLUTION 2:

Cat list list2 |Grep p list1 list2 | sort

——————————————————————————————————————–

fig3

fig4

CHANGE THE PERMISSIONS OF FILES AND DIRECTORIES:

chmod go-rwx “file1”              to change the permissions on file1 depending on the kind of users, the kind of groups that belongs to, etc.

chmod 777 “file1”               gives the permissions to write(w), read(r) and execute(x) to the user that owns the file, which is the first 7, to the group of users that belongs the root, which is the second 7, and to every other user, which is the third 7.

Number meanings:

0 – no permission

1 – execute

2 – write

3 – write & execute

4 – read

5 – read & execute

6 – read & write

7 – all

Thats is the most important thing you must remember when using chmod:

D(if it’s a directory)wrx(for the user that owns the file)wrx(for the group of users that correspond the root)wrx(for everyone else).

fig5

fig6

fig7

chown user “mydoc.txt”         changes the owner of the file mydoc.txt and the owner will be a user named “user”.

chgrp users “mydoc.txt”         changes the group of users named “users” as the owners of file mydoc.txt (it can be done with the command chown user.group “mydoc.txt”)

ps             to list all the processes runnning in our system with the data of the users, I mean the permissions, the PID of the process, etc.

ps -e        to list all the process that runs in the background or the foreground and all the processes that are not typed by you and that initializes in their own.

background  “name of the process”                  this command will execute a process in the background, this is useful for tasks that takes a lot of time to execute.

sleep “number”                        the prompt sleeps the number of seconds specified.

sleep “number” &        the prompt does the same in a background format, it displays the PID of the process and the number in brackets. The PID is the identifier of the process.

control + Z        to finalize a process.

bg                to put the previous typed process into a background format if it is suspended.

fg or fg %number     puts the last process in the foreground format/status or the process specified by the number.

control  + C       to kill a process in the foreground.

kill %number            to kill a process in the background or suspended.

kill “PID_number”                  to kill a process of the list of running processes.

kill -9 “PID_number”              if it refused to be killed with the command above.

fig8

top, htop                it is a version of ps that gives us a list of the last processes.

sudo apt-get install “quota”           to download and install the quota command, “apt-get” does all of this work automatically and you will have not to compile anything. The word “sudo” in the first part will execute the command as the root user to have more permissions.

quota –v        to view what part of the disk you are using.

df .                 to know how much space is free and how much is used.

du  -s *           to show how many kilobytes has every directory.

gzip “file1”        compress file1.

 
GZIP is the most common method to compress files in GNU/Linux. Gzip compresses all types of files, even though it works better with   text files.

There are some examples of commands that you can use related with  GZIP command:

1) To compress a file: gzip "file1"    

2) To unzip a file previously compressed by gzip: gunzip "file1"          
3) To compress a file of "tar" extension: tar cvzf "file1.tar" "name of the file to compress"

4) To unzip a file of "tar" extension: tar cvzf "name of the compressed file.tar" "name of the file when extracted"

gunzip “file1”        unzip file1.

zcat “file1.gz”         displays the content of file1 if it is compressed.

zcat “file1.gz” | less     displays the content in another window, you can exit using q quit.

file *    sort the files by alphabetical order and it gives information about the type of files they are.

diff “file1” “file2”         gives the differences between the contents of file1 and file2.

find                   it has many options, but displays the files of a given characteristics of the current and subsequent directories.

history           to show the commands’ list.

These commands recall and execute the command you are looking for:

!!         recall the last command.

!-3         recall the 3 last commands.

!5           recall 5th command in the list.

!grep    recall the last command starting with grep.

set                   to see all the shell variables.

unset              to unset the shell variables.

set history=100         to increase the size of the history list.

set history=200         to increase the size of the history list.

echo $history             to prove that it has been gone well.

gedit  “file1”               to open a file with gedit text editor.

nano  “file1”               to open a file with nano text editor.

source .cshrc             to read the file named “cshrc” and execute it.

lsb_release –a            to know the ubuntu version you have installed. (this will not work with the raspberry pi)

man “command”        to view the command options that you can use.

——————————————————————————————————————–

EXAMPLE OF COMPILING A PACKAGE:

The simplest way to compile a package is:

  1. cd to the directory containing the package’s source code.
  2. Type ./configure to configure the package for your system.
  3. Type make to compile the package.
  4. Optionally, type make check to run any self-tests that come with the package.
  5. Type make install to install the programs and any data files and documentation.
  6. Optionally, type make clean to remove the program binaries and object files from the source code directory.

First of all go to the web browser like mozilla and download this file: http://www.ee.surrey.ac.uk/Teaching/Unix/units-1.74.tar.gz

We are going to compile a little terminal program that transforms units. You have to go to the directory where you have downloaded the file and follow the next steps.

Commands to compile the package:

gunzip units-1.74.tar.gz       unzip the file.

tar –xvf units-1.74.tar           to extract the file of the format “tar”, which means tarball.

cd units-1.74                          to enter the directory.

mkdir  ~/units174                  creates a subdirectory inside the one you are now, called units174.

./configure –prefix=$HOME/units174       creates a file named Makefile with the necessary options to say the machine to install the program in the units174 directory.

make              creates the executables.

make check        check if all has gone well.

make install       installs the program into the units174 directory if the check command has been succesfull.

make install-strip      if we want a reduced file.

cd ~/units174             to change the directory.

./units                         to execute the compiled program.

info –file=units.info  in the info directory to read all the units program information.

strip units                  reduces the size of the program units.

file units                    shows the size of the file units.

——————————————————————————————————————–

By agreement:

1)      Environment variables = uppercase:

printenv         to show all the values of these variables (or env).

setenv            to set environmental variables.

unsetenv        to unset environmental variables.

2)      Shell variables = lowercase.

——————————————————————————————————————–

That is just an intorduction of some important commands you will have to use when playing with your raspberry pi.

I have extracted some of these commands and parts of my tutorial for these other websites that I recommend to read and pay attention because they give an interesting view of how is distributed the ubuntu operating system, which is similar to raspbian:

How are files distributed: http://linuxnewbieguide.org/tutorials/files-directories-and-the-linux-filing-system/

Some helpful tutorials: http://www.ee.surrey.ac.uk/Teaching/Unix/

An overview of all Ubuntu operating system: http://doc.ubuntu-es.org/Documentaci%C3%B3n

I wish this tutorial might be helpful for you to handle the rapsberry pi in a better way.

Anuncis

Deixa un comentari

Fill in your details below or click an icon to log in:

WordPress.com Logo

Esteu comentant fent servir el compte WordPress.com. Log Out / Canvia )

Twitter picture

Esteu comentant fent servir el compte Twitter. Log Out / Canvia )

Facebook photo

Esteu comentant fent servir el compte Facebook. Log Out / Canvia )

Google+ photo

Esteu comentant fent servir el compte Google+. Log Out / Canvia )

S'està connectant a %s