09 – Basic Unix/Linux Commands & Concepts
December 5, 2009 – 10:16 pmPlease note… This information no longer exists at the referenced locations. This is only a copy of what was available in 2003.
Basic Linux Training™
Basic Unix/Linux Commands & Concepts
Gary Leaf
Table of Contents
- Filesystem
- Prompt
- Listing Directories
- Permissions
- chmod, chown, chgrp
- Linking files
- Wildcards
- Processes
- Assignments
Filesystem
If you are comfortable at the DOS prompt, you should see a lot of similarities in Linux; if most of your experience has been in Windows, the command line may seem a little awkward at first. Remember from an earlier lesson that Linux has an hierarchical, unified filesystem (directories within directories; and files, directories, and device drivers are treated as files), supports 256-character filenames (avoid symbols and punctuation except for the dot (.) and note that you can have more than one nonadjacent dots in the filename – e.g. this.is.okay).
All command line entries are case sensitive. Also note that Linux uses the slash (/) rather than the backslash (\) you’ve been using in DOS.
There is extensive online help available on any command on your system called the man pages (or manual pages); type in man CommandInQuestion to get a summary of what the command does and a brief summary of the options. Unfortunately, using the man pages is not an efficient or effective way to learn Unix commands, but is often helpful with syntax. Linux, like all Unix, assumes you know what you’re doing and that you do not make typographical errors. Linux, like all Unix, will execute whatever command you give it – all that matters is that it’s a valid command. If it’s not what you intended, that’s your mistake, so don’t blame it on the operating system! This operating system allows you to do your work without constantly nagging you. There are four types of files:
- ordinary files – text files (plain vanilla ASCII), data files (contain special characters not contained in the ASCII set you are familiar with), command text files (shell scripts), and executable files (binaries);
- directories;
- links (we’ll get into this below);
- special device files (physical hardware)
The main subdirectories (and contents) are:
- /bin – binary files;
- /boot – information need to boot the system;
- (/cdrom) – your CD-ROM drive by the name you assigned to it;
- (/dos) – your DOS partition by the name you assigned to it;
- /dev – device files;
- /etc – miscellaneous files (mostly system administration);
- /home – home directories for users;
- /lib – programming libraries;
- /sbin – superuser and system startup binaries;
- /tmp – temporary files;
- /usr – commands;
- /var – files that define the system.
(There may be some slight differences between distributions.)
Prompt
If you login as root, the prompt will be machinename:~# and if you login as user, the prompt will be machinename:~$. The tilde character (~) represents the home directory; appended to the end of a filename, it means a backup of a file that has been edited (the file as it existed before it was last edited, and if your configuration is set up to make backups).
Listing Directories
If you are using bash – the Bourne Again Shell (this is the default shell for most Linux distributions) – you can get a listing with ls. The default is to list files in alphabetical order (capitals and numbers first). Some of the most useful options with ls are:
- -a – lists all files, including hidden files;
- -A – lists all files, except the current and parent directory;
- -c – sorts file by time (oldest first);
- -d – lists only the name of a directory, not its contents;
- -l – lists in long format (showing permissions and other details);
- -r – lists in reverse order;
- -t – sorts files by time (newest first);
- -x – lists all files across the page instead of in columns.
Reading from the far right you have the links, filename, date and time the file was last modified, the file size (in bytes), the name of the group and owner. The number to the immediate left of the owner is the number of links to the file. (Links will be discussed below, so for the time being think of it as a way for one copy of a file to appear to be in several locations.) The long group of letters and hyphens on the left are the permissions. One of the first things you should learn is about navigating through the directories. As in DOS, the current directory is represented by a single dot (.); the parent directory is represented by double dots (..).
The command for change directory is cd so let’s go to a more interesting directory such as /etc for some examples.
- cd /etc
- ls -l
- (NOTE: the “l” is a lowercase L)
This directory is mostly system administration files, but run ls -l and one of the first things you’ll notice is that the listing is very long. (To page back through the listing use Shift+PageUp; to page down use Shift+PageDown.) Near the middle of the listing is localtime with a hyphen and greater than symbol (->; sort of like an arrow) and /usr/share/zoneinfo indicating a link to that location; also notice that the permissions begin with an l, while most begin with a hyphen (-) indicating a file, and some permissions begin with a d indicating a directory.
let’s go to the /dev directory.
- cd /dev
- ls -l
This directory lists device files that allow user programs to access hardware devices on the system through the kernel (NOTE: Be careful not to delete or edit these files in any way, unless you “really” know what you are doing!). They are not actual “files”, but look like files from the program’s point of view. When the program you’re running accesses such a device “file”, the kernel recognizes the I/O (input/output) request and passes it to an actual device driver, which performs the operation. Notice that some permissions begin with a b indicating a block device, while most begin with a c indicating a character device, and a few permissions begin with the familiar l indicating a link. Typically, symbolic links in this directory will be set at installation time for your hardware peripherals, such as: modem, mouse, floppy, and cdrom.
If your system has the kernel source software loaded, you can review the /usr/src/linux/Documentation/devices.txt file for further explanation of these device files.
Let’s move on to the /bin directory.
- cd /bin
- ls -l
All the files here are binaries and end with an asterisk (*).
Let’s move to your home directory.
- cd ~
- ls -a
There are hidden files beginning with a dot (.). They are usually your system setup user preference settings configuration files, and/or files derived from user program settings configuration. Examples may include: .profile, .bashrc, .bash_history, and .lynxrc (Remember the tilde character (~) is just shorthand for the home directory and that the home directory will change if you are logged in as root user.) The “rc” at the end of these files indicates it’s a resource configuration.
Permissions
For security reasons, all Unix systems including Linux have file permissions which allow you to control access to directories – who can read, write, or execute a file or command in a directory. In the extreme left is either a d or hyphen (-) indicating whether this is a directory or a file (occasionally you will also see an l indicating a link). Then you see three groups of the same three letters in the same order: r for read, w for write, x for execute, and the hyphen (-) for no permission given in that type. The first group of three letters is for the owner, the second group for the group, and the third the world. Whoever creates the file is the owner, and if more than one person is working on a project or needs access to this file they are given permission as a group, and finally how the file is open to anyone who has access to the system (the world).
chmod, chown, chgrp
The command to change file permissions is chmod (change mode). There are two ways for doing this: the numeric system and the symbolic system. The numeric system uses numbers to track permissions. Using the table below you add together the numeric equivalent for the permissions you want.
400 - owner has read permission 200 - owner has write permission 100 - owner has execute permission 040 - group has read permission 020 - group has write permission 010 - group has execute permission 004 - world has read permission 002 - world has write permission 001 - world has execute permission
Thus chmod 764 SomeFile gives the owner permission to read, write, and execute SomeFile; the group has permission to read and write; the world permission to read only. The other method for changing modes is the symbolic method. With this method, you have to know the existing permissions because the commands are added or removed relative to how permissions are currently set. The plus sign (+) adds a permission, the minus sign (-) removes a permission.
u - user (owner) g - group o - other (world) a - everyone - user, group, and other r - read permission w - write permission x - execute permission t - sticky bit
Thus chmod g+x SomeFile gives permission to the group to execute SomeFile. In the past Unix crackers used to get around the permissions by messing around with entire directories. The way to prevent this is to set restrictive permissions for the directory using the sticky bit, which makes the directory accessible only to the owner and root without affecting how the individual file permissions are set.
- chmod -t TheDirectory
You can also change the owner with the chown command, and change the group with chgrp.
Linking Files
Rather than having multiple copies of a file, Linux uses linking to one file to save disk space and administrative headaches trying to keep multiple copies up to date and synchronized. Linux supports two types of links, hard links and symbolic links. Hard links are set with the command
- ln FileName /NewDirectoryLocation
The problem with hard links is that Linux treats all hard links equally, and before you can delete the original file, you have to remove all hard links. On the other hand, symbolic links don’t need to be physically removed in order to delete the file. (There are some other differences between hard links and symbolic links, but irrelevant to this discussion; consult the man pages.)
Wildcards
Linux has three types of wildcards – the question mark (?), which is used to match a single character, the same as in DOS; the asterisk (*), which is much more expansive than anything in DOS because it can be used to return any number of letters at the beginning or end of an expression; and the final wildcard used to return specific characters as defined within brackets ([ ]).
Processes
A shell acts as the intermediary between the user and the operating system, interpreting your commands into a form the operating system can understand. The shell has the capacity to run multiple commands at one time, and can run commands in the background using the ampersand (&) after the command. Multiple requests to the shell are called processes. As these requests are made, beginning with init during boot, the shell numbers them. These numbers are important if you want to stop a process: use the command ps to to see a list of current processes, then the command kill and the number of the process you want to stop.
Assignments
Terms and Concepts:
Define and add these to your glossary:
- .dir_colors or DIR_colorS
- awk
- cat
- cd
- chdir
- chgrp
- chmod
- chown
- echo
- ed
- export
- fg
- forking
- fs
- kill
- ls
- MANPATH
- more
- PATH
- ps
- pwd
- rm
- rmdir
- sed
- set
- setenv
- sort
- tr
- vi
- virtual console
- virtual desktop
Online:
- http://blt.basiclinux.net/files/unixhelp/ – UNIXhelp for Users
Copyright © 1997-2003 Henry White. Copyright © 2003 Gary Leaf. All Rights Reserved.
Reproduction or redistribution without prior written consent is strictly prohibited. Address comments and inquiries to info@basiclinux.net.
Sorry, comments for this entry are closed at this time.