Wednesday, May 12, 2010

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

No comments:

Search This Blog