5 Basic utilities

45
5 Basic utilities When a user logs in to the Linux operating system the directory that they will start in is their home directory. – Most users will have /home/userid as their home directory. – The root user has /root as it’s home directory.

description

5 Basic utilities. When a user logs in to the Linux operating system the directory that they will start in is their home directory. Most users will have /home/ userid as their home directory. The root user has /root as it’s home directory. ls. Lists the names of files - PowerPoint PPT Presentation

Transcript of 5 Basic utilities

Page 1: 5 Basic utilities

5 Basic utilities

When a user logs in to the Linux operating system the directory that they will start in is their home directory.

– Most users will have /home/userid as their home directory.

– The root user has /root as it’s home directory.

Page 2: 5 Basic utilities

ls

• Lists the names of files– To view the contents of the current

directory you can type:

ls –l• The –l option says to give a long list which

includes file permissions, owner, group, size (in bytes), date created, and filename.

ls –la• You can view the hidden system files by

including the –a option:

Page 3: 5 Basic utilities

You can include a path instead of just a filename to list. For example to view the contents of the / directory you can type:

ls /Notice the –l option was not used so the

directory will display only the file and directory names, not the properties.

To view the contents of the /var/log directory type:

ls /var/log

Page 4: 5 Basic utilities

cat

• Displays a Text File

Page 5: 5 Basic utilities

rm

• Deletes a filerm filename

Rm –i filenamehave to hit y to remove

To remove a directory use the rmdir command. rmdir scripts

You must have write permission to remove a directory.

Page 6: 5 Basic utilities

To remove a directory that has other files within it use the rm command with the –r option (recursive).

To remove the /etc/skel/cron directory and any files and directories that may be within it type:

rm –r /etc/skel/cron

To remove one or more files use the rm command. To remove the file Welcome from the /etc/skel directory type:

rm /etc/skel/Welcome

Page 7: 5 Basic utilities

less

• Display a text file one screen at a time

Page 8: 5 Basic utilities

hostname

• Displays the system name

Page 9: 5 Basic utilities

Copying a file from the CLI

Copy files with the cp command. – When you copy a file you duplicate the file

and it’s contents to another file.

– Filenames must be unique so if you copy the file to the same directory it must have a different name.

– To copy /root/myFile to the / directory type:

cp /root/myFile /myFile

Page 10: 5 Basic utilities

Copying a file from the CLI

Copy files with the cp command.

– To copy /root/myFile to the / directory with the name myFile2 type:

cp /root/myFile /myFile2

– You can use wildcards with filenames as you did with the ls command. To copy /myFile and /myFile2 to the /root directory type:

cp /myfile* /root

– If you are replacing an existing file you will be prompted if it can be overwritten.

Page 11: 5 Basic utilities

Moving a File from the CLI

To move a file use the mv command. When you move a file to another file in the same directory you are essentially renaming the file.

– To move /root/myFile to /root/myFile2 type:

mv /root/myFile /root/myFile2

– To move /root/myFile2 to /myFile type:

mv /root/myFile2 /myFile

– To move /myFile to /root/myFile type:

mv /myFile /root/myFile

You must have read and write permissions to move a file.

Page 12: 5 Basic utilities

lpr

• Prints a file

Page 13: 5 Basic utilities

grep allows user to search for occurrence of a string of characters in a file

Page 14: 5 Basic utilities

Head and Tail

• head– Display the beginning of a file (the first 10

lines)

• Tail– Display the end of a file (the last ten lines)

Page 15: 5 Basic utilities

sort displays the lines of a file in order

Page 16: 5 Basic utilities

uniq removes duplicate lines

Page 17: 5 Basic utilities

echo copies the command line (but not the word echo) to the screen

Page 18: 5 Basic utilities

Compressing and Archiving

• bzip2– bunzip2

• gzip

• tar

Page 19: 5 Basic utilities

who lists who is logged in

Page 20: 5 Basic utilities

finger: lists who is logged in

Page 21: 5 Basic utilities

Figure 5-19 Entering text with vim

Page 22: 5 Basic utilities

Figure 5-20 The main vim Help screen

Page 23: 5 Basic utilities
Page 24: 5 Basic utilities

Figure 5-21 Help with insert commands

Page 25: 5 Basic utilities

File system

When a user logs in to the Linux operating system the directory that they will start in is their home directory.

– Most users will have /home/userid as their

home directory. – The root user has /root as it’s home

directory.

Type pwd to display the path

Page 26: 5 Basic utilities

Linux File System Structure

The file system structure used by Linux is a hierarchal list of directories called a directory tree. The top of the tree is the root directory indicated by a /. The upper directories are called parent directories and the directories beneath them are called child directories.

Page 27: 5 Basic utilities

Figure 6-5 Absolute pathnames

Page 28: 5 Basic utilities

Figure 6-6 Relative pathnames

Page 29: 5 Basic utilities
Page 30: 5 Basic utilities

Changing and Viewing Directories from the CLI

Although you can view the contents of any directory from anywhere in the file system hierarchy you can also change to the individual directories. To change the current directory to the / directory type:

cd /– To change to jsmith’s Documents directory within his

home directory type:cd /home/jsmith/Documents

There are shortcut commands you can also use. For example, you can use the shortcut .. change to the parent directory of the current directory by typing:

cd ..

Page 31: 5 Basic utilities

Changing and Viewing Directories from the CLI

You can change to your home directory with the shortcut ~ by typing:

cd ~ (or just cd without any options)– The root user goes back to /root with this

command.

You can display the current directory by typing the pwd command.

Page 32: 5 Basic utilities

Linux File System StructureIn order to find files within the file system, a standard exists that identifies where certain types of files go.

– This standard is the file system Hierarchy Standard (FHS) maintained by the file system Hierarchy Standard Group.

– The standard contains a set of requirements and guidelines for file and directory placement under UNIX-like operating systems.

– Use of the FHS provides interoperability of applications, system administration tools, development tools, and scripts.

– The FHS defines many common directories used in UNIX-like operating systems.

Page 33: 5 Basic utilities

Linux File System Structure / - The root directory is the top of the file system hierarchy. The contents of the root file system must contain all the files needed to boot, restore, recover, and repair the system.

/bin - The /bin directory contains commands used by the system administrator and other users. These are required commands when no other filesystems are mounted.

/boot - The /boot directory contains all files, except configuration files, needed to boot the operating system. The /boot stores data that is used before the kernel begins executing user-mode programs.

Page 34: 5 Basic utilities

Linux File System Structure/dev - The /dev directory contains file system entries which represent devices that are part of the system.

/etc - The /etc directory contains local configuration files. No binaries are contained within /etc.

/home - The /home directory contains user directories. The /home directory is optional. Typically, the /home directory contains a subdirectory for each user added to the system.

Page 35: 5 Basic utilities

Linux File System Structure

/lib - The /lib directory contains shared library images need to boot the system and run the commands in the root filesystem.

/opt - The /opt directory contains add-on application software packages.

/proc - The /proc file system is used to handle storage and retrieval of process information.

Page 36: 5 Basic utilities

Linux File System Structure

/root - The /root directory is the home directory of the root user.

/sbin - The /sbin directory contains utilities used for system administration. These utilities are executed after /usr is known to be mounted and there are no problems.

/tmp - The /tmp directory contains temporary files. These files are lost if the system restarts.

Page 37: 5 Basic utilities

Linux File System Structure/usr - The /usr directory contains files that can be

shared across all hosts. This directory is mounted read-only and contains the following subdirectories or symbolic links to directories:

– /usr/bin – contains most user commands

– /usr/include – contains header files included by C programs

– /usr/lib – contains libraries

– /usr/local – local hierarchy

– /usr/sbin – contains non-vital system binaries

– /usr/share – contains architecture-independent data

Page 38: 5 Basic utilities

Linux File System Structure/var - The /var directory contains variable data files.

This includes spool directories, log files, and temporary files. Some directories within /var are sharable and some are unsharable. The following directories are required in /var:

– /var/cache – contains application cache data– /var/lib – contains variable state information– /var/local – contains data for /usr/local– /var/lock – contains lock files– /var/log – contains log files and directories– /var /opt - contains variable data for /opt– /var/run – contains data relevant to running processes– /var/spool – contains application spool data– /var/tmp – contains temporary files preserved between

system reboots

Page 39: 5 Basic utilities

Permissions• Every file or directory has a set of permissions

that determines who can access and what they can do to the file. – Permissions are represented by the characters r, w,

and x which represent read, write, and execute. • The r, w, and X have a different meaning depending if it

is assigned to a file or a directory. – r with a file means the file can be displayed or copied,

with a directory means it can be listed. – w with a file means the file can be changed, with a

directory means it can be created or deleted. – x means a file can be executed (script or program),

with a directory allows files within it to be copied or moved.

Page 40: 5 Basic utilities

Permissions

-rw-rw-r--

-rw-rw-r-- 1 dengle dengle 0 Sep 15 15:59 mydocumentdrwxrwxr-x 2 dengle dengle 4096 Sep 15 15:59 my-files

File Type

Owner

Group

Others

-rw-rw-r--

-rw-rw-r-- 1 dengle dengle 0 Sep 15 15:59 mydocumentdrwxrwxr-x 2 dengle dengle 4096 Sep 15 15:59 my-files

File Type

Owner

Group

Others

For example:

-rwx------ indicates a file that has read, write, and execute permissions for the owner only.

-r-xr-xr-x indicates a file that has read and execute permissions for the owner, group, or anyone else.

drwxr-x—x indicates a directory that allows read, write, and execute permission for the owner, read and execute for the group, and execute only for everyone else.

Page 41: 5 Basic utilities

Permissions• Permissions can also be viewed as octal numbers.

– r permission is represented by 4– w permission is represented by 4– x permission is represented by 1

• Permissions 777 means r, w, and x permissions are enabled for the user, the user’s groups, and all others

• Permissions 541 means the user has r, and x permissions, the user’s group has only r permission, and all others have just x permissions.

Page 42: 5 Basic utilities

• The permission list, -rwxr-xr-x is treated as a block of binary digits, a bitfield. Translating this into octal, we would get 0755, which is 0111101101 base 2.

• We can then represent each permission for each user type by a single octal digit (plus an extra digit for the first bit).

• 400 -r-------- read by owner • 040 ----r----- read by group • 004 -------r-- read by anybody (other) • 200 --w------- write by owner • 020 -----w---- write by group • 002 --------w- write by anybody • 100 ---x------ execute by owner • 010 ------x--- execute by group • 001 ---------x execute by anybody • 4000 s--------- suid • 2000 s--------- sgid • 1000 t--------- sticky • These permissions on their own may not be very useful, but we can add the octal numbers to

get a combination of permissions. For example, 4, which corresponds to r-- (for some user type), and 2, which corresponds to -w- (for some user type), can be added to get 6, 110 in binary, which corresponds to rw-, read and write. Note also that in a group of three octal digits, the "place" of the digit signifies where we assign the permission. 600 octal, in binary is 0110000000, so we are setting read and write permissions for us, but not the group or other users. 060 octal, in binary however is 0000110000, and sets it for the group only, and likewise.

• So, for example, if we want to set read and write for ourselves and the group, and have no permissions whatsoever for the other users, we could use the permission 660.

Page 43: 5 Basic utilities

Changing Permissions with the CLI

• Change permissions for group and others to read and execute using octal numbering:

chmod 755 IT250• You can also use chmod to add or remove specific permissions

chmod u+w IT250 would add w permission for the user.

chmod a-x IT250 would remove x permission for all (a) users.

Page 44: 5 Basic utilities

Links

• A pointer to a file

Page 45: 5 Basic utilities

Hard Links

• A pointer to a file – the directory entry points to the inode