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