Working with Linux

Linux is not so hard as you might think it is. Trust me, you will get hooked up to it if you use it for a while. This tutorial is provides information on how to get started with Linux for novice users.

Basic Commands in Linux

I will denote the command prompt as: '$>' (without quotes). Any command that can be issued will therefore look like: '$> command_Name' Right click on the desktop and select 'Open Terminal'. This will open up a terminal (or a console) window, just like the cmd window in MS Windows. The description of most used commands at the command prompt in the terminal window is given below:
1. ls - list files
  • $> ls
    • Lists all files (and directories) in the current directory with the exception of files (and directories) whose names start with '.'
  • $> ls -la
    • Lists all files in the current working directory in long listing format showing file and directory permissions, ownership, size, time and date stamp
  • $> ls -lah
    • Same as above but the file sizes will look human readable. i.e., a file of size 10035 bytes will show as 10K, meaning approximately 10Kilobytes.
  • $> ls -lhrt
    • Lists all files iin the current working directory in long listing but they are sorted by date & time
  • $> ls -p --color
    • The '-p' option differentiates files and directories (directories have a '/' symbol at the end), and --color differentiates directories, regular files and executables with different colors

2. cd - change directory
  • $> cd /home
    • Change the current working directory to /home. The '/' indicates relative to the root location, and regardless of the directory you are currently in, you will be changed to the directory '/home'.
  • $> cd ..
    • Change to the parent directory of the current directory. If you are in the directory called: /home/users/REU and you issue the command 'cd ..', your current directory will change to: '/home/users'.
  • $> cd ~ (or)
  • $> cd
    • Change to the user's home directory. The convention at vrac for a user's home directory is usually '/home/users/user_Name'. Regardless of which directory you are in, your current directory will change to '/home/users/user_Name' if you issue 'cd ~' or just 'cd'.

3. cp - Copy files
  • $> cp source_file dest_file
    • Copy the files (or directory) source_file to the dest_file (or directory). If a file of filename dest_file already exists, this command will overwrite it.
  • $> cp -i source_file dest_file
    • You will be prompted before a file is over written with this command
  • $> cp -i /home/source_file .
    • Copy the file 'source_file' from /home/ directory to the current directory. You will be prompted for overwriting if a file already exists

4. pwd
  • $> pwd
    • Show present working directory

5. clear
  • $> clear
    • clean up your command prompt window

6. man
  • $> man ls
    • This command gives detailed information about how to use the command 'ls'. Wait, there's more.
  • $> man glVertex3f
    • The word 'glVertex3f' is not a command that can be issued at the command prompt. It is a function that is used in OpenGL . Issuing this command provides a full manual information about how to use the function 'glVertex3f' in your code.

7. nedit/gedit - text editors just like notepad in MS Windows.
  • $> nedit myprogram.cpp &
    • This command opens a blank window with a pop-up saying that such a file doesnt exist. Do you want to create one. If myprogram.cpp already exists in the current working directory, this command simply opens up nedit window loaded with myprogram.cpp
    • The '&' symbol at the end leaves the command prompt free for you. That means, nedit will run in the background while leaving you the command prompt free to enter any additional commands. If you dont use '&', you wont get to the command prompt until you do one of the following:
      1. Close nedit window and re-issue the command with '&'
      2. On the terminal, issue 'Ctrl-z' followed by 'bg'. Hitting 'Ctrl-z' will freeze the nedit process but your command prompt will be free. A following 'bg' command will unfreeze the 'nedit' program while also freeing your command prompt for any commands to be issued.
    • The text editors nedit, gedit, etc are built for writing various programs such as C,C++, etc. Features such as Syntax highlighting, line number display, etc is supported in these text editors. My personal favorite is 'nedit', although 'gedit' can be quite pleasing to use for beginners.
    • Other text editors such as 'emacs' or 'Vi' is for advanced users. I dont discourage anyone to use them, but using it is quite a pain to learn it if you are used to traditional 'Ctrl-s' for saving a file. emacs notation for saving a file is: 'Ctrl-x-Ctrl-s'. In Vi, it is: ':wq!'

8. zip/Unzip - process compressed files of type '.zip'
  • $> unzip dots.zip
    • This command unzips the file 'dots.zip' and puts the contents of the zip file in the current working directory.
  • $> zip -r dots.zip dots/
    • This command zips the entire directory 'dots/' into dots.zip. A good practice is to move to the parent directory and zip the entire folder rather than zipping the contents of a folder directly. This way, when someone unzips dots.zip, a folder called 'dots/' will be created and all individual files will be filled in this folder.
    • Here is a BAD practice to zip a file:
      • $> zip -r dots.zip dots.sln dots.vcproj glut32.dll main.cpp Makefile render.cpp render.h
      • No doubt, this command zips all the files into the zip file 'dots.zip'. However, when someone unzips it in a folder containing many other important files and folders, the unzipper will put all the components of dots into the working folder thereby creating a lot of confusion and re-work.

9. gzip/gunzip - process compressed files of type '.gz'. These files are similar to '.zip' files except that it is a better compression algorithm than .zip. Typical .gz files are smaller than .zip files.
  • $> gunzip dots.tar.gz
    • This command unzips the file 'dots.gz'
  • $> gzip dots.tar
    • This command zips a tar file into dots.tar.gz

10. tar - 'I understand about '.gz', but what on earth is tar? tar files are uncompressed enclosures for file(s) or folder(s). The usual procedure to archive files is to first tar it, and then zip it:
  • $> tar -cvf dots.tar dots/
    • This creates an uncompressed archive called 'dots.tar'
  • $> gzip dots.tar
    • This creates a compressed archive called 'dots.tar.gz'
    • Use the procedure in 9 to unzip a compressed archive.

11. bzip2/bunzip2 - This is similar to zip and gz except that it is a better compression algorithm than '.gz'. The procedure to handle .bzip2 files is similar to 8 & 9.

Linux Usage Tips

  1. Pressing up arrow at $> will display the previous command issued at the command prompt.
  2. If you have a long directory name and you want to change into that directory, you can type in a partial name of the directory and press the 'tab' key. The rest of the directory name shows up automatically at the prompt.
  3. Ctrl-shift-t (Ctrl-T) will open a new tab in your terminal window.
  4. Ctrl-shift-n (Ctrl-N) will open a new window
  5. Ctrl-Page-up/Ctrl-Page-down will switch between various tabs in a terminal window or an nedit or a mozilla firefox window when multiple tabs are present.
  6. Alt-tab switches between various open windows.

Compiling code in Linux

1. gcc/g++ - Compiler to compile C/C++ programs in Unix. As a rule of thumb, gcc is for 'C' programs and g++ is for C++ programs.
  • $> g++ myprogram.cpp
    • This command compiles the program 'myprogram.cpp'.
    • If there are any errors in your code, they will be printed at the command prompt.
    • Once successfully compiled, it creates an executable called 'a.out' in your working directory.
  • $> a.out
    • This will run the executable at the command prompt.

2. Issuing just the command above will not compile all programs. What if you have multiple C++ codes for a project or for an assignment? What if you have external software libraries to include while compiling the code. For this purpose, we use something called as 'makefile' or 'Makefile'.
  • A makefile is a script that tells the gcc or g++ compiler all the libraries, include header files needed for your code to compile. Understanding Makefiles is a big topic and there are certain rules to write a makefile. For all the assignments and templates available for you to download, I have included a 'Makefile' where the source code is located. Unless you add more .cpp files to the assignment/project, you can issue the following at the command prompt to compile all the sources and build an executable:
    • $> gmake
      • This will compile your codes and create an executable of name defined in the first line of the Makefile. If there are no compile errors, just type in 'executable_Name' or './executable_Name' at the command prompt and you should be able to run the program.

3. You might now ask, how am I able to compile the same source code on both windows and Linux. The template code I am providing you is cross-platform compatible. That means, you need to compile the code on a platform once and run the created executable on that platform without altering the source code. For example, you can open up dots.sln in .NET, compile it and run it on windows. You can then use the same source code on Linux by running gmake to compile and create an executable called 'dots' for Linux.

Walk through of compiling & executing dots code in Linux

  1. Download the zip file and save it to say, '/home/users/REU1/graphicsProgramming' directory.
  2. Unzip dots.zip
  3. A folder is created called 'dots' in /home/users/REU1/graphicsProgramming folder. Let's denote the directory: /home/users/REU1/graphicsProgramming/dots as DOTS_HOME to make things easier.
  4. cd into DOTS_HOME
  5. Issue the command:
    1. $> gmake
  6. The code will compile and will create an executable called 'dots' in DOTS_HOME
  7. Issue the command:
    1. $> ./dots
    2. This will execute the dots program
  8. To edit the dots program and recompile, use the following procedure:
    1. $> gedit dots.cpp render.cpp render.h &
    2. This command will open all the three files in three tabs. Edit whatever you want here and save each of them individually using 'Ctrl-s'.
  9. The code needs to be re-compiled since there are changes. Again, issue the command:
    1. $> gmake
    2. $> ./dots

Have a great time developing code in Linux.

-- VijayKalivarapu - 10 Jun 2008

Topic revision: r1 - 2008-06-10 - 15:00:51 - VijayKalivarapu
 
This site is powered by the TWiki collaboration platformCopyright &© by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback