Lab Assessment

Nobody
Week6LABPart2LinuxCommands.pdf

Executing the Commands in Terminal

We will be trying out the commands in Ubuntu Terminal, to bring it up, click the search icon and type

‘terminal’ in the search box as shown in the following image. Then open the Terminal program (the

first one).

UBUNTU COMMANDS

Linux File System Organization:

Files on a Linux system are arranged in what is called a hierarchical directory structure. This means that they are organized in a tree-like pattern of directories (called folders in other systems), which may contain files and other directories. The first directory in the file system is called the root directory, and is denoted by “/”. The root directory contains files and subdirectories, which contain more files and subdirectories and so on and so on.

One important difference between the legacy operating system and Unix/Linux is that Linux Does Not employ the concept of drive letters.

Directory Description

/ The root directory where the file system begins. In most cases the root directory only contains subdirectories.

/boot This is where the Linux kernel and boot loader files are kept. The kernel is a file called vmlinuz.

/etc The /etc directory contains the configuration files for the system. All of the files in /etc should be text files. Some

important Sub-Directories are:

/etc/passwd The passwd file contains the essential information for each user. It is here that users are defined.

/etc/fstab The fstab file contains a table of devices that get mounted when your system boots. This file defines

your disk drives.

/etc/hosts This file lists the network host names and IP addresses that are intrinsically known to the system.

/etc/init.d This directory contains the scripts that start various system services typically at boot time.

/bin, /usr/bin

These two directories contain most of the programs for the system. The /bin directory has the essential programs that

the system requires to operate, while /usr/bin contains applications for the system's users.

Directory Description

/sbin,

/usr/sbin

The sbin directories contain programs for system administration, mostly for use by the superuser.

/usr The /usr directory contains a variety of things that support user applications. Some highlights are:

/usr/share/X11 Support files for the X Windows system

/usr/share/doc Various documentation files in a variety of formats.

/usr/share/man The man pages are kept here.

/usr/src Source code files. If you installed the kernel source code package, you will find the entire Linux kernel

source code here.

/usr/local /usr/local and its subdirectories are used for the installation of software and other files for use on the local

machine. What this really means is that software that is not part of the official distribution (which usually goes

in/usr/bin) goes here.

/var The /var directory contains files that change as the system is running. This includes:

/var/log Directory that contains log files. These are updated as the system runs. You should view the files in this

directory from time to time, to monitor the health of your system.

/home /home is where users keep their personal work. In general, this is the only place users are allowed to write files.

/root This is the superuser's home directory.

/tmp /tmp is a directory in which programs can write their temporary files.

/dev The /dev directory is a special directory, since it does not really contain files in the usual sense. Rather, it contains devices that are available to the system. In Linux (like

Unix), devices are treated like files. You can read and write devices as though they were files. For example /dev/fd0 is the first floppy disk drive, /dev/sda(/dev/hda on older systems) is

the first IDE hard drive. All the devices that the kernel understands are represented here.

Directory Description

/proc The /proc directory is also special. This directory does not contain files. In fact, this directory does not really exist at all. It is entirely virtual. The /proc directory contains little

peep holes into the kernel itself. There are a group of numbered entries in this directory that correspond to all the

processes running on the system. In addition, there are a number of named entries that permit access to the current configuration of the system. Many of these entries can be viewed. Try viewing /proc/cpuinfo. This entry will tell you

what the kernel thinks of your CPU.

/media, /mnt

Finally, we come to /media, a normal directory which is used in a special way. The /media directory is used for

mount points. The different physical storage devices (like hard disk drives) are attached to the file system tree in various

places. This process of attaching a device to the tree is called mounting. For a device to be available, it must first be

mounted.

note: Linux commands are case sensitive

pwd

To find the name of the working directory, use pwd; shows the directory you are currently in.

$ pwd /dev

cd

To change your working directory (where you are standing in the maze) you use the cd command. To do this, type cd followed by the pathname of the desired working directory.

$ cd /dev

To return one step up (that is, the parent directory), use cd .. (there's a space before '..').

ls

The ls command is used to list the contents of a directory.

$ ls $ ls -l ; long description $ ls -a ; include hidden files in the list [ '.' is used for hidden files, ex → .config ] $ ls -t ; sort the list according to modification time [ newest first ] $ ls -1 ; list 1 file per line

Fig: Breakdown of descriptions obtained in the long format

du

du used to check the information of disk usage of files and directories on a machine.

$ du -h /home ; directory size information inside home in human readable form $ du -ah /home ; files + directory size info inside home in human readable form $ du -sh /home ; shows total size of home directory

$ du -h /home/asif/Desktop/LinCom.doc ; shows size of LinCom.doc $ du -h /home/asif/Desktop/*.doc ; lists with size all the doc files inside Desktop $ du -ch /home/asif/Desktop/*.doc ; adds a summary with the list

A Look at the ‘Home’ and other Relevant Directories

When you open the Terminal, by default you are in your HOME directory (you can try pwd

command to verify), and the Terminal Prompt looks like the following:

You can see all your folders by typing the ls command as shown below:

Now if you want to navigate to Folders such as Desktop or Documents, execute the

command cd Desktop (Note: commands are case sensitive)

You can visually inspect the File System by opening it through the marked icon - shown below

Navigating to Desktop from ANY Directory

cd /home/asif/Desktop ; replace ‘asif’ as appropriate

Note: All

directories in

Linux are under

‘/’ … called root

Creating a text file in the Desktop

Use the Touch command as shown below. Afterwards use the ls command to confirm its

creation. You can also just look at the Desktop area in the GUI to see the file.

touch test.txt

Using Administrative Privilege

If for some reason, you want to use the administrative privilege to do certain task, such as

installing new programs, the sudo command can be used as shown below:

sudo -s

The administrator user is called the ‘Root’ user. You will be asked for the password. Just

provide the password that you have created while creating this VM (halifaxpc in this case).

You will not see the password as you type, just type it out and press enter. After successful

execution, note how the prompt has changed.

To get out of ‘root’ mode, type ‘exit’ and press enter. The prompt will change accordingly

and you will be back to your actual user mode.

Setting Access Permissions

• Note: for the following chmod command (setting file permission), create a text file in

the desktop and carry out the experimentation on it.

• Though we will change the file permission from command line, but you can visually

inspect the permissions by right clicking the file > Properties > Permission tab.

• Owner = user (file creator), Group = the group in which the owner belongs, as every

user\owner in Linux must belong to a group. Others = other users of the system.

chmod

Changes the file permission. drwxr-xr-x ….....

r = read ….... w = write ….... x = execute * user is the owner of the file

7 = read/write/execute [4 + 2 + 1] 6 = read/write [4 + 2 ] 5 = read/execute [4 + 1] 4 = read 3 = write/execute [2 + 1] 2 = write 1 = execute

Method 1:

$ chmod 175 some_file ; --x rwx r-x [ Only Execute, no Read or Write for Owner, Everything for the group, Only Read and Execute for the rest, no Write ]

$ chmod 644 some_file ; rw- r-- r-- [ Read and Write for Owner, Only Read for the group, no Write and Execute Only Read for the rest, no Write and Execute ]

Method 2:

chmod X@Y file1 file2 … [no space among X,@ and Y]

Where:

X is any combination of 'u' (for owner), 'g' (for group), 'o' (for others), 'a' (for all [ugo]);

@ is either '+' to add permissions, '-' to remove permissions, or '=' to absolute assign;

Y is any combination of 'r', 'w', 'x'.

Examples:

chmod u=rx file ; give the owner rx permissions, not w

chmod go-rwx file ; deny rwx permission for group, others

chmod g+w file ; give write permission to the group

chmod a+x file1 file2 ; give execute permission to everybody

chmod g+rx, o+x file ; OK to combine like this with a comma

cp

The cp program copies files and directories. It can copy single or multiple files to destination:

$ cp File1 File2 testDir $ cp File1 File2 /tmp

use '-i' flag to show overwriting confirmation and use '-n' flag to cancel overwriting.

mv

The mv can be used to move a file along with renaming it [first argument is the source]:

$ mv test.doc /tmp/renamed_and_moved_test.doc ; cut and paste with new name $ mv test.doc /tmp ; cut and paste without renaming $ mv test.doc renamed.doc ; only rename, no move

mkdir

$ mkdir NewFolder ; NewFolder has been created in PWD

The Process List

Use the ps command to view the current process list. The first column, PID, is the process

ID.

Kill

kill ProcessNumber ; to terminate a process, use PID for the process number

man

Manual Pages, example:

man ps

man netstat