Monday, May 17, 2010

Linux 常见目录

The Linux file system has a well-defined set of top-level directories,and some of these directories have specific purposes.

Top-level Direcotries in Linux File System

DirectoryDescription
/This root directory forms the base of the file system.All files and directories are contained logically in the root directory,regardless of their physical locations.
/binContains the executable programs that are part of the Linux operating system. Many Linux commands, such as cat, cp, ls, more, and tar, are located in /bin.
/bootContains the Linux kernel and other files that the LILO and GRUB boot managers need. (The kernel and other files can be anywhere, but placing them in the /boot directory is customary.
/devContains special files that represent devices attached to the system.
/etcContains most system configuratoin files and the initialization scripts(in the /etc/tc.d subdirectory)
/homeConverntional location of the home directories of all users. User edulaney's home directory, for example, is /home/edulaney.
/libContains library files for all programs stored in /sbin and /bin directories (including the loadable driver modules) needed to start Linux.
/lost+foundDirectory for lost files. Every disk partition has a lost+found directory.
/mediaA directory for mounting file systems on removable media, such as CD/DVD-ROM drives, floppy disks, and Zip drives. Contains the /media/floppy directory for mounting floppy disks and /media/cdroom or /media/cdroom0 directory for mounting the CD/DVD-ROM drive. if you have a CD/DVD recorder, you find a /media/cdrecorder directory instead of /media/cdrom and may also find /media/DVD.
/mntA directory for temporarily mounted file systems
/optProvides a storage area for large application software packages. For example, some distributions install the OpenOffice.org Office suite in the /opt directory.
/procA speical directory that contains various information about the processes running in the Linux system.
/rootThe home directory for root user.
/sbinContains executable files representing commands typically used for system-administration tasks and used by the root user. Commands such as halt and shutdown reside in the /sbin directory.
/srvContains data for services( such as Web and FTP ) offered by this system.
/sysA special directory that contains information about the devices, as seen by the Linux kernel.
/tmpA temporary directory that any user can use as a scratch directory, meaning that the contents of this directory are considered unimportant and usually are deleted every time the system boots.
/usrContains the subdirectories for many important programs, such as the X Window System( in the /usr/X11R6 directory ) and the online manual.
/varContains various system files(such as logs), as well as directories for holding other information, such as files for the Web server and anonymous FTP server.

Important /usr Subdirectories
SubdirectoryDescription
/usr/X11R6Contains the X Window System software
/usr/binContains executable files for many more Linux commands,including utility programs that are commonly available in Linux but aren't part of the core Linux operating system.
/usr/gamesContains some old Linux games
/usr/includeContains the header files (file names ending in .h) for the C and C++ programming languages; also includes the X11 header files in the /usr/include/X11 directory and the Linux kernel header files in the /usr/include/linux directory.
/usr/libContains the libraries for C and C++ programming languages; also contains many other libraries, such as database libraries and graphical libraries.
/usr/localContains local files. The /usr/local/bin directory, for example, is supposed to be the location for any executable program developed on your system
/usr/sbinContains many administrative commands, such as commands for electronic mail and networking.
/usr/shareContains shared data, such as default configuration files and images for many applications. For example, /usr/share/gnome contains various shared files for the GNOME desktop, and /usr/share/doc has the documentation files for many Linux applicaions (such as the bash shell, the Sawfish window manager, and The GIMP image-processing program).
/usr/share/manContains the online manual (which you can read by using the man command)
/usr/srcContains the source code for the Linux kernel (the core operating system)


Important /var Subdirectories
SubdirectoryDescription
/var/cacheStorage area for cached data for applications
/var/libContains information relating to the current state of applications
/var/lockContains locked files to ensure that a resource is used by one application only
/var/logContains log files organized into subdirectories. The syslogd server stores its log files in /var/log, with the exact content of the files depending on the syslogd configuration file /etc/syslog.conf. For example, /var/log/messages is the main system log file; /var/log/secure contains log messages from secure services (such as sshd and xinetd); and /var/log/mainlog contains the log of mail messages.
/var/mailContains user mailbox files
/var/optContains variable date for packages stored in /opt directory
/var/runContains data describing the system since it was booted
/var/spoolContains data that's waiting for some kind of processing
/var/tmpContains temporary files preserved between system reboots
/var/ypContains Network information Service(NIS) database files

Wednesday, May 12, 2010

Common Linux commands by category

Help and Abbreviations
Command NameAction
aproposFinds online manual pages for a specified keyword
infoDisplays online help information about a specified command
manDisplays online help information
whatisSimilar to apropos but searches for complete words only
aliasDefines an abbreviation for a long command
typeShows the type and location of a command
unaliasDeletes an abbreviation defined using alias

Managing Files and Directories
Command NameAction
cdChanges the current directory
chmodChanges file permissions
chownChanges file owner and group
cpCopies files
lnCreates symbolic links to files and directories
lsDisplays the contents of a directory
mkdirCreates a directory
mvRenames a file as well as moves a file from one directory to another
rmDeletes files
rmdirDeletes directories
pwdDisplays the current directory
touchUpdates a file's time stamp

Find Files
Command NameAction
findFinds files based on specified criteria,such as name and size
locateFinds files using a periodically updated filename database.(The database is created by the updatedb program.)
whereisFinds files based in the typical directories where executable (also known as binary)files are located
whichFinds files in the directories listed in the PATH enviroment variable

Processing Files
Command NameAction
catDisplays a file on standard output (can be used to concatenate several files into one big file)
cutExtracts specified sections from each line of text in a file
ddCopies blocks of data from one file to another(used to copy data from devices)
diffCompares two text files and finds any differences
expandConverts all tabs into spaces
fileDisplays the type of data in a file
foldWraps each line of text to fit a specified width
grepSearches for regular expressions within a text file
lessDisplays a text file one page at a time(can go backward,also)
lprPrints files
moreDisplays a text file,one page at a time(goes forward only)
nlNumbers all nonblank lines in a text file and prints the lines to standard output
pasteConcatenates corresponding lines from several files
patchUpdates a text file using the differences between the original and revised opy of the file
sedCopies a file to standard output while applying specified editing commands
sortSorts lines in a text file
splitBreaks up a file into several smaller files with specified size
tacReverses a file(last line first and so on)
tailDisplays the last few lines of a file
trSubstitutes one group of characters for another throughout a file
uniqEliminates duplicate lines from a text file
wcCounts the number of lines,words,and characters for another throughout a file
zcatDisplays a compressed file (after decompressing)
zlessDisplays a compressed file one page at a time(can go backward also)
zmoreDisplays a compressed file one page at a time

Archiving and Compressing Files
Command NameAction
compressCompresses files
cpioCopies files to and from an archive
gunzipDecompresses files compressed with GNU Zip(gzip)
gzipCompresses files using GNU Zip
tarCreates an archive of files in one or more directories(originally meant for archiving on tape
uncompressDecompresses files compressed with compress

Managing System
Command NameAction
bgRuns an interrupted process in the background
fgRuns a process in the foreground
freeDisplays the amount of free and used memory in the system
haltShuts down Linux and halts the computer
killSends a signal to a proces(usually used to terminate a process)
lddDisplays the shared libraries needed to run a program
niceRuns a process with lower priority(referred to as nice mode)
psDisplays a list of currently running processes
printenvDisplays the current enviroment variables
pstreeSimilar to ps bu shows parent-child relationships clearly
rebootStops Linux and then restart the computer
shutdownShuts down Linux
topDisplays a list of most processor-and memory-intensive processes
unameDisplays information about the system and the Linux kernel

Managing Users
Command NameAction
chshChanges the shell(command interpreter)
groupsPrints the list of groups that include a specified user
idDisplays the user and group ID for a specified user name
passwdChanges the password
suStarts a new shell as another user or root(when invoked without any argument)

Managing the File System
Command NameAction
dfSummarizes free and available space in all mounted
duDisplays disk usage information
fdformatFormats a diskette
fdiskPartitions a hard disk
fsckChecks and repairs a file system
mkfsCreates a new file system
mknodCreates a device file
mkswapCreates a swap space for Linux in a file or a hard drive partition
mountMounts a device(for example,the CD-ROM)on a directory in the file system
swapoffDeactivates a swap space
swaponActivates a swap space
syncWrites buffered(saved in memory) date to files
ttyDisplays the device name for the current terminal
unmountUnmounts a device from the file system

Dates and Times
Command NameAction
calDisplays a calendar for a specified month or year
dateShows the current date and time or sets a new date and time

Linux 第二天 来点开胃小菜

Combining shell commands
For example,suppose that you want to find out whether a device file name sbpcd resides in your system's /dev directory because some documentation says you need that device file for a Sound Blaster Pro CD-ROM drive.You can combin ls and grep command :

ls /dev | grep sbpcd


Controlling command input and output

Task Command Syntax
Send stdout to a file command > file
Send stderr to a file command 2> file
Send stdout and stderr to a file command > file 2>&1
Read stdin from file command < file
Read stdin from file.in and Send stdout to file.out command < file.in > file.out
Append stdout to the end of a file command >> file
Append stderr to the end of a file command 2>> file
Append stdout and stderr to the end of a file command >> file 2 >&1
Read stdin from the ketboard until the character c command << c

Another interesting use of sending stdout to a file is the use of the cat command to quickly prepare small text files.For example,suppose that you want to create a new text file where you want to keep storing lines of text you type until you type ZZ and press enter.Here is  how you can accomplish that task:

cat <<zz > input.txt


Typing less with automatic command completion
Many commands take a filename as an argument.To view the contents of the /etc/modprobe.conf text file,for example,type the following command:
cat /etc/modprobe.conf
You can use a bash feature to avoid having to type the whole filename. All you have to type is the bare minimum -- just the first few characters -- to uniquely identify the file in its directory.
To see an example,type cat /etc/mod but don't press Enter;press Tab instead.bash automatically completes the filename,so the command becomes cat /etc/modprobe.conf.Now press Enter to run the command.

Going wild with asterisks and question marks

  • The asterisk(*) character matches zero or more characters in a filename. That means * denotes all files in a directory.
  • The question mark(?) matches any single character. If you type test?, that matches any five-character text that begins with test.
  • A set of characters in brackets matches any single character from that set. The string [aB]*,for example,matches any filename that starts with a or B.
Repeating previously typed commands
To see the command history, type history. bash displays a numbered list of the old commands,including those that you entered during previous logins. bash stores up to 500 old commands.

If the command list is too long,you can limit the number of old commands that you want to see. For example,to see only the ten most recent commands, type this command:
history 10

To repeat a command from the list that the history command shows,simply type an exclamation point(!), followed by that command's number. To repeat command number 3, type !3. Also you can type !more to repeat previously recent more command.

Often, you may want to repeat the last command that you just typed,type two exclamation point as follows:
!!

Sometimes, you may want to repeat the previous command but add extra arguments to it. Sippose that ls -l shows too many files. Simply repeat that command but pipe the output through the more command as follows:
!! | more

Tuesday, May 11, 2010

Linux 第一天

第一天当然是 ls 命令啦

ls [options] [names]

List contents of directories. If no names are given,list the files in the current directory.

The most useful options include -F, -R, -l, and -a. Some options don't make sense together(e.g.,-u and -c).

Options

-F,--classify,--indicator-style=classify
Flag filenames by appending:
/ to directories
* to executable
@ to symbolic links
| to FIFOs
= to sockets

-R,--recursive
List directories and their contents recursively

-l,--format=long,--format=verbose
Long format listing(includes permissions,owner,size,modification time,etc.).
for example:


total 36
drwxr-xr-x 2 royer royer 4096 2010-03-07 19:42 Desktop
drwxr-xr-x 2 royer royer 4096 2010-03-07 19:42 Documents
drwxr-xr-x 2 royer royer 4096 2010-03-07 19:42 Downloads
-rw-r--r-- 1 royer royer 167 2010-03-07 19:36 examples.desktop
drwxr-xr-x 2 royer royer 4096 2010-03-07 19:42 Music
drwxr-xr-x 2 royer royer 4096 2010-03-07 19:42 Pictures
drwxr-xr-x 2 royer royer 4096 2010-03-07 19:42 Public
drwxr-xr-x 2 royer royer 4096 2010-03-07 19:42 Templates
drwxr-xr-x 2 royer royer 4096 2010-03-07 19:42 Videos

This listing shows considerable information about every directory entry --- each of which can be a file or another directory. Looking at a line from the right column to the left,you see that the rightmost column shows the name of the directory entry.The date and time before the name show when the last modifications to that file were made.To the left of the date and time is the size of the file in bytes.

The file's group and owner appear to the left of the column that shows the file size.The next number to the left indicates the number of links to the file.(A link is like a shortcut in Windows.)Finally,the leftmost column shows the file's permission settings,which determine who can read,write,or execute the file.

The first letter of the leftmost column has a special meaning,as the following list shows:
  • l,the file is a symbolic link to another file.In other words,it's a shortcut to something else.
  • d,the file is directory.It will appear as a folder in GUI.
  • -,the file is normal.
  • b,the file represents a block device,such as a disk drive.
  • c,the file represents a character device,such as a serial port or a terminal.
After that first letter,the leftmost column shows a sequence of nine characters, which appear as rwxrwxrwx when each letter is present.Each letter indicates a specific permission.A hyphen (-) in place of a letter indicates no permission for a specific operation on the file.Think of these nine letters as three groups of three letters (rwx), interpreted as follows:
  • the leftmost group of rwx controls the read,write and execute permission of the file's owner.Although executable programs(including shell programs) typically have execute permission,directories treat execute permission as equivalent to use permission: A user must have execute permission on a directory before he or she can open and read the contents of the directory.
  • The middle three rwx letters control the read,write and execute permission of any user belonging to that file's group.
  • The rightmost group of rwx letters controls the read,write and execute permission of all other users(collectively thought of as the world).
Thus,a file with the permission setting rwx------ is accessible only to the file's owner,whereas the permission setting rwxr--r-- makes the file readable by the world.

-a,--all
List all files,including the normally hidden files whose names begin with a period.

-A,--almost-all
List all files,including the normally hidden files whose names begin with a period.Does not include the . and .. directories.

-h,--human-readable
Print sizes in kilobytes and megabytes

Search This Blog