3.2. Basic Linux


Linux commands

Type a command into a terminal window after the prompt. Always end a command with the Enter key. The following is a list of some useful commands.

gedit&

Open a text editor (gedit). The & symbol at the end of the command tells the terminal not to wait for gedit to finish. That way, you can keep using the terminal without closing gedit. The gedit text editor is simple and easy to use.

If you start gedit this way, you will get error messages that do not mean anything in the terminal. Ignore them, If you start gedit from a desktop menu, those messages will not show up.


firefox&

Start the firefox browser.

cd dir

A directory is the same as a folder. It holds files and other directories. You always have a current working directory. Any files that you mention are assumed to be in the current working directory. The cd command sets the current working directory to a given directory.

pwd

Show the current working directory.

ls

List the names of the files and directories in the current directory. (The first letter of this command is a lower case ell.)

Use

  ls -l
to get a listing that shows details about the files and directories. Use
  ls -F
to get a listing that shows a / after the name of each directory.

Normally, the ls command does not show files whose names begin with a dot. To see all files, use

  ls -a
Add multiple options to get the effect of all of them.


Paths

You refer to a file by saying how to find it, either starting in the current working directory or starting in the main directory of the computer (called the root directory). Use / to separate directories, or to separate a directory from a file name. Do not use \.
  1. A path that starts with / starts in the root directory. For example, /bin/ls is a file called ls in the directory called bin in the root directory. (It is the executable code for the ls command.)

  2. A path that does not start with / starts in the current working directory. For example, if your current working directory is your home directory, 2530/test/trial.cpp describes file test.cpp in directory test that is in directory 2530.

Your home directory is /home/INTRA/your id. But you can abbreviate that as ~. For example, ~/2530 is the directory or file called 2530 in your home directory.

Each directory has two special directories in it that are created automatically. File . (just a dot) refers to the directory itself. File .. refers to the directory that contains this directory. For example, if the current directory has a file called assn1.cpp, then you can write path ./assn1.cpp to say explicitly to look in the current directory. Command

  cd ..
sets the current working directory to its parent, the one that contains it. For example, if your current directory as ~/2530/assn1 and you do command cd .., then your current directory becomes ~/2530. Another cd .. makes the current directory be ~.


mkdir dir

Create directory dir. For example,
  mkdir 2530
creates a directory called 2530.

cp old new

Make a copy of file old, and call the copy new. Both old and new are paths. For example,
  cp assn1a.cpp assn1b.cpp
creates a copy of assn1a.cpp and calls the copy assn1b.cpp.

If new is an existing directory, then cp creates a file in that directory with the same name as old. So if 2530 is a directory inside the current directory, then

  cp foo.cpp 2530
makes a copy of foo.cpp in directory 2530.

Use cp -R … to copy an entire directory. For example,

  cp -R 2530 2530.bak
creates a copy of directory 2530 and everything in it, and calls the copy 2530.bak.


mv old new

Move file or directory old to new. If new does not already exist, then this is just a renaming. For example, command
  mv assn1.cpp assn1a.cpp
renames assn1.cpp to assn1a.cpp.

If new is an existing directory then the mv command just moves old into directory new


rm path

Remove file path. If you want to remove a directory and everything in it, use
  rm -r dir
Be careful. The rm command does a permanent removal. You cannot get the file back.

rmdir dir

Remove directory dir, but only if it is empty. (A directory is defined to be empty if it only contains . and ...

man command

Read the manual entry for command. For example,
  man ls
shows the manual entry for ls in the terminal. It will show one page at a time. To go to the next page, press the space bar.

!!

Redo the previous command.


The PATH environment variable

You can set environment variables that control how Linux works for you. One environment variable tells Linux where to look for the executable files that perform commands. Its value is a string that is a sequence of directories separated by colons. For example, a setting of PATH =

  .:/bin:/usr/local/bin
says to look first in the current directory (.), then in /bin (where many commands are kept) and then in /usr/local/bin (where a few commands are put). In a command, you refer to path by $PATH. Command
  echo $PATH
shows you the current value of PATH. Command
  export PATH=.:/bin:/usr/local/bin
sets PATH to be .:/bin:/usr/local/bin. Typically, you put commands into a file such as ~/.bashrc (which is read each time the bash command processor starts).


Exercises

  1. How to you enter commands? Answer

  2. How can you create a C++ file using Linux? Answer

  3. How do you create a directory called dir? Answer

  4. How can you set your current working directory to dir? Answer