Learning Linux for Beginners 2
30 April 2008
Peace to you all.
In the second article, we will navigate to many commands here.
The cd command
The cd command is used to change the current directory (i.e., the directory in which the user is currently working) in Linux and other Unix-like operating systems. It is similar to the CD and CHDIR commands in MS-DOS.
cd’s syntax is
cd [option] [directory]
The items in square brackets are optional. When used without specifying any directory name, cd returns the user to the previous current directory. This provides a convenient means of toggling between two directories.
When a directory name is provided, cd changes the current directory to it. The name can be expressed as an absolute pathname (i.e., location relative to the root directory) or as a local pathname (i.e., location relative to the current directory). It is usually more convenient to use a local pathname when changing to a subdirectory of the current directory.
As an example, the following would change the current directory, regardless of where it is on the system (because it is an absolute path), to the root directory (which is represented by a forward slash):
cd /
Likewise, the following would change the current directory, regardless of its location, to the /usr/sbin directory (which contains non-vital system utilities that are used by the system administrator):
cd /usr/sbin
If a user currently in the directory /usr/local/share/man/ desired to change to the directory /usr/local/share/man/man2, which is a subdirectory of the current directory, it would be possible to change by using the absolute pathname, i.e.,
cd /usr/local/share/man/man2
However, it would clearly be much less tedious to use the relative pathname, i.e.,
cd man2
On Unix-like operating systems the current directory is represented by a single dot and its parent directory (i.e., the directory that contains it) is represented by two consecutive dots. Thus, it is possible (and often convenient) to change to the parent of the current directory by using the following:
cd ..
Another convenient feature of cd is the ability for any user to return directly to its home directory by merely using a tilde as the argument. A home directory, also called a login directory, is the directory on a Unix-like operating system that serves as the repository for a user’s personal files, directories and programs. It is also the directory that a user is first in after logging into the system. A tilde is a short, wavy, horizontal line character that represents the home directory of the current user. That is, any user can return immediately to its home directory by typing the following and then pressing the Enter key:
cd ~
This is easier than typing the full name of the user’s home directory, for instance, /home/josephine in the case of a user named josephine. (And it is just one of the numerous shortcuts that help make the command line on Unix-like operating systems so easy to use.)
When followed by a space and then a hyphen, cd both returns the user to the previous current directory and reports on a new line the absolute pathname of that directory. This can further enhance the already convenient toggling capability of cd. Toggling is particularly convenient when at least one of the two directories has a long absolute pathname, such as /usr/local/share/man/man2
The chmod command
We use the chmod command to change the access mode of a file. This command comes in many flavors, but we’ll be talking primarily about one of them.
chmod who=permissions filename
This gives “who” the specified permissions for a given filename.
Who
The “who” is a list of letters that specifies whom you’re going to be giving permissions to. These may be specified in any order.
| Letter | Meaning |
|---|---|
u |
The user who owns the file (this means “you.”) |
g |
The group the file belongs to. |
o |
The other users |
a |
all of the above (an abbreviation for ugo) |
Permissions
Of course, the permissions are the same letters that you see in the directory listing:
r |
Permission to read the file. |
w |
Permission to write (or delete) the file. |
x |
Permission to execute the file, or, in the case of a directory, search it. |
Note: Do not put blanks around the equal sign, or your command will not work!
Syntax
chmod [OPTION]… MODE[,MODE]… FILE…
chmod [OPTION]… OCTAL-MODE FILE…
chmod [OPTION]… –reference=RFILE FILE…
-c, –changes like verbose but report only when a change is made
–no-preserve-root do not treat `/’ specially (the default)
–preserve-root fail to operate recursively on `/’
-f, –silent, –quiet suppress most error messages
-v, verbose output a diagnostic for every file processed
–reference=RFILE use RFILE’s mode instead of MODE values
-R, –recursive change files and directories recursively
–help display this help and exit
–version output version information and exit
Permissions
u – User who owns the file.
g – Group that owns the file.
o – Other.
a – All.
r – Read the file.
w – Write or edit the file.
x – Execute or run the file as a program.
Numeric Permissions:
CHMOD can also to attributed by using Numeric Permissions:
400 read by owner
040 read by group
004 read by anybody (other)
200 write by owner
020 write by group
002 write by anybody
100 execute by owner
010 execute by group
001 execute by anybody
Examples
The above numeric permissions can be added to set a certain permission, for example, a common HTML file on a Unix server to be only viewed over the Internet would be:
chmod 644 file.htm
This gives the file read/write by the owner and only read by everyone else (-rw-r–r–).
Files such as scripts that need to be executed need more permissions. Below is another example of a common permission given to scripts.
chmod 755 file.cgi
This would be the following 400+040+004+200+020+100+010+001 = 775 where you are giving all the rights but the capability for anyone to edit your file.cgi (-rwxr-xr-x).
Finally, another common CHMOD permission is 666, as shown below, which is read and write by everyone.
chmod 666 file.txt
Additional information
Below is an example of how a file may be listed when typing ( ls -l ) at the prompt as well as information on how to interpret it.
-rw-rw-r– 1 hope 123 Feb 03 15:36 file.txt
- rw rw- r– 1 hope 123 Feb 03 15:36 file.txt
File owner group everyone else links owner size mod date file name
Install Software & Programs
To install software or programs write the following command in the termianl
yum install [software_name]
The yum command searches for the software supplied to it and look forward to find it in the repositories then installs it.
All yum commands cause yum to take the following actions:
* The first time yum is called, as root, it downloads all the header files in all repositories in its yum.conf and puts them in a cache on your system. Subsequently it only downloads ones that have changed since the last time the cache was refreshed.
* It then reads your RPM database (which should be consistent, see note above) to determine what packages are installed.
* Determine which yum command has been invoked, and process the command.
* If the command is run interactively (the default) yum will prompt the user for permission to proceed before actually updating or installing files.
* If yum is run with the -y option, yum will assume a “yes” answer to all interactive questions. This is typically used in a script or to process “known safe” installs or updates.
* If yum is run with the -d 0 option, it will run as quietly as possible. -y -d 0 are typically invoked together in a scripted update where only serious errors are to be reported.
* There are many other yum action modifiers; read the man page for a synopsis.
Yum is very conservative. It will never perform the equivalent of a -force install. It was designed, as noted above, by systems administrators to facilitate the consistent and scalable maintenance of systems, and thus will balk at any action that might cause your system to enter “package hell” (a state of package inconsistency). If yum refuses to do something, then the thing it refuses to do is probably a bad idea for most people to think about. If you are skilled enough to know that it is really OK on YOUR system, you are skilled enough to use rpm itself to override yum.
But do you really want to? Perhaps what you really should be doing is rebuilding the RPMs in question, or fixing some apparent conflict instead of ignoring it. In a sense, yum is the ultimate sanity check. If yum is happy, your repository is consistent, your RPM database is consistent, and your system is happy. If yum is not happy, then chances are pretty good that at least some part of your system could break, which would cost you a lot more time to set right than just making everything consistent to start with.
The following is a brief synopsis of the commands:
yum install package1 [package2 package3...]: Yum checks to see if package1 is already installed and is the latest version. If not, it downloads package1 and all its dependency packages (saving them in its cache directory) and installs them.
yum update [package1 package2...: If there is a basic, essential yum command this is it. Without arguments, yum compares each installed package to packages available on the repository list. If a newer package is available in the repository, it downloads it (saving it in the cache directory) and updates the old one. If a list of packages is given, it only checks for updates of those packages (and their dependencies).
Note that RPM updates generally preserve configuration information and restart daemons and so forth, so your system will generally not require a reboot, although updating the kernel is an obvious exception. Kernel updates are a special case and systems administrators will generally set a policy on this in /etc/yum.conf. In any event, yum will not reboot your system even after a kernel update.
yum remove package1 [package2 package3...]: Yum will first check to see if removing the package breaks anything else (that is, if anything else already installed depends on this package). It will then offer to remove the package and all the packages that depend on it. Yum will generally refuse to leave “dangling” packages that won’t work anyway because a critical library or component has been removed. Note that running remove with the -y option is probably a really stupid idea for most people, unless they are certain that they know all of the possible side effects.
yum list [ ,available,installed,updates,extras] [package list]: This is one of yum’s most useful commands. Run without arguments or a list of packages, yum lists all packages available to be installed on all repositories. Run with a list of packages (which can contain file wildcard globs, e.g. yum list kernel
to list all kernel package) it shows both the installed packages and the available packages that match the expanded list! The installed, available, and updates options show only installed packages, available packages, or packages that will be updated by the next yum update command. Note Well that when run interactively from a shell, wildcard characters need to be escaped from the shell itself or the command will have unexpected consequences.
yum info [ ,available,installed,updates,extras] [package list]: This command extracts the description and summary from all matching packages and prints it out. It takes the same options as list, but provides different information. yum info > /tmp/packages thus provides you with a shopping list in /tmp/packages of every package either already installed on your system or available on its set of repositories! This can be very useful indeed!
The extras option lists all packages that are installed but not available in the repositories. These are packages you may have installed by hand, for example. A good practice is to create a repository for these local RPMs (which can be nothing but a local directory on the system) and add the repository to the yum.conf file for the system, following the recipe above.
yum search keyword: Searches for packages matching the keyword string in the description, summary, packager and package name fields. This is useful for finding a package you do not know by name but know by some word related to it. The keyword string can contain wildcard file globs. Be sure to escape these from the shell as noted above if run interactively.
yum provides [feature|file]: Find package(s) that provide some feature or file. Accepts file wildcard globs, which again should be escaped from the shell if run interactively.
yum clean: Cleans up yum’s cache file. On systems with small disks or limited /var resources, the RPMs that yum caches can fill up the disk. Running yum clean periodically can free up the resources, but it can also mean that yum runs more slowly when doing certain things. Clean takes various options: see the man page for details.
yum group[list,install,update]: Yum has recently acquired the ability to perform the list, install, and update functions on package groups defined for a system, with otherwise similar arguments. This makes it relatively easy to install an entire block of software all at once, on systems that have package groups defined.
Entry Filed under: Linux. Tags: cd command, chmod command, Files permissions in Linux, Learning Linux For Beginners, Linux, Linux permissions, The cd command, yum, yum install, yum list, yum remove, yum update.
Trackback this post | Subscribe to the comments via RSS Feed