[Linux] Linux tips

Source: Internet
Author: User
Tags name database

Http://www.21php.com/tutorial/tutorial.php? Tid = 89
Author: linuxforum

1. virtual console

After logging on, press Alt + F2. Then you can see the "login:" prompt, which is the second virtual console. press Alt + F1 and return to the first console, which is convenient to switch back and forth. View the/etc/inittab file to find the following lines:
# Run Gettys in standard runlevels
1: 12345: respawn:/sbin/mingetty tty1
2: 2345: respawn:/sbin/mingetty tty2
3: 2345: respawn:/sbin/mingetty tty3
4: 2345: respawn:/sbin/mingetty tty4
5: 2345: respawn:/sbin/mingetty tty5
6: 2345: respawn:/sbin/mingetty tty6
That is to say, six virtual consoles are defined during initialization. You can edit this file to add or remove virtual consoles. To use the virtual console in xwin, CTRL + ALT + F? To switch. XWindow occupies the first console after the virtual console. The default is tty7, so if you want to switch back to xwin, press Alt + F7 (when you change the number of virtual consoles, you need to change ). If the second xserver is started, it should be Alt + F8.

2. display the color directory and file list under RedHat
Use ls -- color-F, and parameter -- color to indicate color display. Parameter-F adds a character after the file/directory name to indicate its category. We recommend that you use man ls for more information. For example, you can use the-l parameter to list the vast majority of information about a file/directory (displayed in long format). Use the-a parameter to list all files, including the first character ". "implicit file; use the-n parameter to directly display the file name without processing, so that the Chinese file name can be displayed in the Chinese environment. Note that using ls with other commands can give birth to many tips (for example, "ls-L | more ").
For convenience, You can edit/etc/bashrc or. bashrc under your home directory (home) and add a line:
Alias ls = "ls -- color-F-n"
Running bash again takes effect immediately. If it is added to/etc/bashrc, it is valid for all users who use bash shell. If other shells are used, find the corresponding configuration file. Note that it is not the/etc/aliases file, it is the alias configuration file of Sendmail.
The color displayed by ls can be modified. See "colours with Linux terminals". A shell script can display all ANSI colors. You can check/etc/dir_colors or $ home/. dir_colors. Note that the two files have different scopes. You can also use the dircolors-p command to list the settings. For more information, see "Man dircolors ".

3. The method to only list sub-directories:
Ls-f | grep/$ or alias sub = "ls-f | grep/$" (Linux)
Ls-L | grep "^ d" or LS-ll | grep "^ d" (Solaris)
Find.-type D
Find.-type D | sort
See dtree

4. list all files in the current directory except for some types of files
Use ksh and ls! (*. Z) displays all files except *. Z.
This command is useful when there are many types of files in a directory.

5. Calculate the number of files and directories
The following statement can help you calculate the number of files and directories ..
Ls-L * | grep "^-" | WC-L <---- to count files
Ls-L * | grep "^ d" | WC-L <----- to count dir
You can also convert the preceding statement into a script or an alias statement.

6. Copy all files in subdirectories and subdirectories
Add the-R parameter when using CP, for example, "CP-r/mnt/floppy/*/usr/local /"
The important parameter to be referenced is-F-s.

7. Rename the file/directory
Simply use the move command to move it to the new name. For example, "mV/usr/oldname/usr/newname"

8. Delete all files in a directory and all files in subdirectories and subdirectories.
Rm-RF/tmp/Example
These are all for the lazy, and it is best for new users to use man command to learn more. If the English language is not good, wait for the Chinese release

9. quick access to some Directories
Type Cd ~ (Or CD $ home) to enter the user's home directory.
Type CD-to enter the last directory.

10. display environment variables
Echo $ home display home directory
Echo $ path: display the executable file search path
Env displays all environment variables (there may be many, it is best to use "env | more", "env | grep path", etc)
Modify environment variables, use export in bash, and use setenv in CSH. For example:
Export Path = $ path:/usr/local/bin
To know the specific usage of export, you can use the shell HELP command: help export

11. display file type
Use the file command, such as "file/bin/Mount", "file xxx.gz", "file Netscape", "file initrd. IMG", and so on.
The related configuration file is in/etc/magic or/usr/share/magic

12. Find the path of the command file
For example, to find the specific path of the mount command, you can use
Type Mount
Whereis mount command can also find files related to this command file, such as manuals page.

13. Search for files
Use the find command, for example:
Find/usr-name xf86 * find the file named xf86 with the first four letters under the/usr directory (including subdirectories)
Find.-Name Netscape-print find the file named Netscape in the current directory (including subdirectories)
Find/home-nouser searches for files without the user owner in the/Home Directory (the user account is deleted but there are legacy files)
Find/var-newer test searches for files updated on the date of the test file in the/var directory.
The find command has many parameters. Use the man command to view the parameters.
You can use locate to quickly search for files. Before using locate for the first time, you can use updatedb to update the file/directory name database.
It writes the file/directory information to the database, and it will be very fast to search later. Updatedb is placed in crontab and the default configuration is automatically executed in the middle of the night. Locate has minor security issues because common users can use it to obtain information about directories/files that they cannot access. Now there is a secure locate, which saves UID/GID to the database. The user must first compare the permissions when locate.

14. Back-to-volume screen on the console
How can I view information that has been rolled over the screen? Try these two key combinations: Shift + Pageup and SHIFT + Pagedown
It is valid for xterm/nxterm/cxterm/xxgdb. The kvt of KDE has been well done.

15. Special Methods for viewing File Content
I believe you are familiar with the basic cat and less. What if you have special requirements:
If you only want to view the first five lines of the file, you can use the head command, for example:
Head-5/etc/passwd
If you want to view the last 10 lines of the file, you can use the tail command, such:
Tail-10/etc/passwd
Tail-F/var/log/messages
The parameter-F enables tail to constantly read the latest content, which has the effect of real-time monitoring,
It is not convenient to use PPP dialing!
View the middle part of the file. You can use the SED command, for example:
Sed-n'5, 10p'/etc/passwd
In this way, you can only view rows 5th to 10th of the file.

16. Remove ^ m characters
When you FTP some DoS Files to Unix, you will often see a nasty ^ m character behind each line of files,
There are two simple ways to cancel it.
Open the file with "Vi" and input the following in command mode:
: % S/^ V ^ m/g
Or, in the Unix shell:
Sed's/^ V ^ m // G' Foo> Foo. New

17. Use the find command to change ownership
To change the ownership of all files in the current directory, you can:
Find.-exec chown owner. [group] {}; (Solaris)
Find.-exec chown-r owner. [group] {}; (Linux)

18. Change the font size in xterm/cxterm
At the same time, press Ctrl and right-click to bring up a menu for you to choose, you can also specify the font when starting xterm,
For example, "xterm-FN 8x16 &"
Tip: Don't you want to try Ctrl + left mouse button ?!

19. when starting a program, let it immediately enter the background Running Mode
After reading the previous bash introduction, we should understand the usage of Ctrl + z/BG/FG. Some programs work very time-consuming. Add an & symbol next to it, for example, "updatedb &", "Netscape. In this way, the shell will be released for other jobs. Note that the parent process of the program is still the shell. If you exit the shell, these programs will also be killed (unless this program can be specially handled by itself ). We recommend that you run the jobs command to view background jobs before exiting shell.

20. Use the nohup command (do not suspend)
If you want to execute a process after you exit the system, you can use the nohup command, for example:
% Nohup tar-CF/dev/tape/home &
If you log out and then log on again, run the 'ps' command to see that the process is still running.
Note: To run the command in the background, nohup must be used together with & operation.
Some software can handle sighup signals themselves, such as wget, so they do not need to use nohup.
If you are willing to use your brains, you can also use nohup to implement PPP callback: first dial up and then execute a self-made script with nohup, such as "sleep 60; PPP-start-script and so on. Finally, log out and stop the call.

21. Quickly call up History commands
After reading the bash introduction in the previous article, I must be familiar with history ,!,!! These usages are very familiar. In fact, you can also use the up/down cursor key to call up History commands, and then use the left/right cursor key to move and edit the command line.

22. Generate a file with the suffix of the current day
File = 'date' + % m % d' | touch todayis. $ File
Or make a shell script:
#! /Usr/bin/sh
File = 'date' + % m % d''
Touch todayis. $ File
Exit 0
Note that the definition of "'" in the file variable is not "'", but the value at the top left corner of the number key 1 (left single quotes ).

23. save man/info as a text file
Take man tcsh as an example:
1) MAN tcsh | col-B> tcsh.txt
2) nroff-man tcsh. Man | col-B> tcsh.txt
Taking info Emacs as an Example
Info emacs-O emacs.txt-S
NOTE: If KDE is installed, it is very convenient to use kdehelp to view man page and info page.
If KDE is too big, tkman can also be selected. It is also a graphic/hypertext method, with a search engine and better functionality.
New visitors will visit man and info more!

24. Tips on RPM
1) The progress is displayed when the RPM package is installed.
Use-VL parameters such as rpm-IVH ipchains-1.3.6-1.i386.rpm
You do not need to use glint or xrpm.
2) directly install the RPM package through FTP
Such as rpm-I ftp://ftp.xxx.xxx
3) Check all RPM packages to find the lost files
Rpm-va
4) Find the RPM package of a file
Example: rpm-qf/usr/bin/WHO
5) list the descriptions of an RPM package
Example: rpm-QPI mon-0.37j-1.i386.rpm
6) list the file information of an RPM package
Example: rpm-qpl mon-0.37j-1.i386.rpm

25.rapid development xxx.tar.gz/xxx. tgz compressed package
Tar zvfx XXX. tgz expand this package in the current directory
Tar zvft XXX. tgz only test not to expand
Tar zvfx XXX. tgz-C/new-DIR is expanded and put in the/new-Dir directory.
Tar zvfc test. tgz/test package all the files and subdirectories in the/test directory into test. tgz.
There are many parameters for tar, so you need to learn it carefully.

26. copy and paste text
If you have started the deamon program GPM, you can use the left mouse button to select the text, and then paste it to the desired place. If it is a two-Key mouse, the configuration is to select the anti-real 3 key mouse, and then press the left and right key to simulate the middle key. Right-click a text terminal and paste it.

27. quickly find the PID Number/shutdown in a program running
For example, to search for inetd: PS aux | grep inetd
If you want to find the most memory-consuming process: PS aux | sort + 4n
After finding the PID of a process, you can use kill to send a signal to it, or force it to kill, for example, kill-9 XXX (pid of a process)
In Linux, A killall command is also provided. You can directly use the process name instead of the PID Number, such as killall-hup inetd.
Another example is killall xdm. Killall sends the signal to all processes whose names are running.
Use killall-L to list all available signals.
I can't help but tell you about QPS. I used the graphic interface written by QT to integrate the features of free, top, PS, and kill. I can also see the socket/memory map/files information of the process. There is also a ktop written for KDE, which lists the relationship between parent and child processes.
Shutdown-R now kills all processes and immediately restarts
Shutdown-H now kill all processes and immediately stop
Shutdown-H + 5 "Warning: after 5 minutes I'll shutdown" sends a message to the user and stops working 5 minutes later.
Related Commands include reboot, halt, poweroff, and init.
To turn off the power of the software, your PC must be in The ATX structure, then configure and compile the kernel, open
Charactor device/Advanced Power Management Support/power off on shutdown.

28. Search for the font information under XWindow, especially the character ing table (internal code)
Xlsfonts lists all fonts
Xfontsel category lists all fonts and displays the demo text
XFD displays all characters and inner code in a font file. For example, you can use XFD-FN hanzigb16fs to check the Chinese inner code.
Use XFD-FN cclib16st to check whether the internal code of the cxterm font is different from that of xf86 (note the highest bit ).

29. Search for files on a certain day
A = 'Find ~ -Print '| LS-l -- full-time $ A 2>/dev/null | grep "Jun 27" | grep 1998
Linux commands are amazing!
I wrote the following script for you, which is easy to use. You can save it as a file with any name and set it to the X attribute.
#! /Bin/sh
# Copyright by Ming Gong (Gong min) for news: // news.freesoft.cei.gov.cn/chinese.com.linux
# GPL V2, Jun 30,199 8
# The right of usage, distribution and modification is here by granted by the author.
# The author deny any responsibilities and liabilities related to the code.
#
OK = 0
A = 'Find $1-print'
If expr $3 = 1>/dev/NULL; then M = Jan; OK = 1; FI
If expr $3 = 2>/dev/NULL; then M = Feb; OK = 1; FI
If expr $3 = 3>/dev/NULL; then M = MAR; OK = 1; FI
If expr $3 = 4>/dev/NULL; then M = APR; OK = 1; FI
If expr $3 = 5>/dev/NULL; then M = May; OK = 1; FI
If expr $3 = 6>/dev/NULL; then M = Jun; OK = 1; FI
If expr $3 = 7>/dev/NULL; then M = Jul; OK = 1; FI
If expr $3 = 8>/dev/NULL; then M = Aug; OK = 1; FI
If expr $3 = 9>/dev/NULL; then M = Sep; OK = 1; FI
If expr $3 = 10>/dev/NULL; then M = Oct; OK = 1; FI
If expr $3 = 11>/dev/NULL; then M = Nov; OK = 1; FI
If expr $3 = 12>/dev/NULL; then M = Dec; OK = 1; FI
If expr $3 = 1>/dev/NULL; then M = Jan; OK = 1; FI

If expr $ OK = 1>/dev/NULL; then
Ls-l -- full-time $ A 2>/dev/null | grep "$ M $4" | grep $2;
Else
Echo usage: $0 path year month Day;
Echo example: $0 ~ 1998 6 30;
Fi

30. use mobile media such as a floppy disk or a CD
A friend who just switched from death to death, does not know how to do it with a floppy disk/CD in his hand, because Linux does not have the concept of a disk D. this is also the case when I first started learning Linux (I have never done root :-before :-). If you have a book about Linux, read the relevant chapters on devices and file systems carefully. To put it simply, you need to install the file system on a floppy disk/CD device to a point on the Linux directory tree, which is called a mount point, which is usually a directory. After Linux is installed, there will be a default installation point/mnt, under which there are/mnt/Floppy and/mnt/CDROM. You can use
Mount-T ext2/dev/fd0/mnt/Floppy <-- install a floppy disk in Linux File System Format
Mount-T msdos/dev/fd0/mnt/Floppy <-- install a floppy disk in the DOS file system format
Mount-T vfat/dev/fd0/mnt/Floppy <-- vfat supports 95/98 long file names
Mount/dev/HDB/mnt/CDROM <-- install the disc that is attached to the optical drive on the first ide port in the form of a slave disk.
Mount/dev/CDROM/mnt/CDROM <--/dev/CDROM is a connection to the optical drive device.
Mount/mnt/CDROM <-- this can be used because it has been defined in/etc/fstab.
Umount/mnt/Floppy <-- detach a floppy disk before detaching it.
Umount/mnt/CDROM <-- the disc cannot be obtained without uninstalling it.
Of course, you can also create some installation points by yourself, such as "mkdir/mnt/SMB", "mkdir/SCD" and
Mount-T iso9660/dev/sp_0/SCD <--/dev/cd0: SCSI Optical Drive
Smbmount // fjj/download/mnt/SMB
And so on.
You can use mount to display the installed file system, or view the/etc/mtab file.
Beginners often ask how to format a disk. in Linux, the concept is somewhat different. It is called Make filesystem)
Mkfs command should be used. Because the current Linux uses the ext2 format, it is best to use mke2fs directly, such as mke2fs/dev/fd0
Mke2fs/dev/hdb2 and so on. For low-level formatting of a floppy disk, you can use fdformat, while the hard disk has been formatted at the factory,
Low-level formatting is not required (that is dangerous ). Check the file system with e2fsck, such as e2fsck-Av/dev/fd0.
Ha, this is not a tips at all, but some beginners do not like reading books and thinking, and even ask how to add/delete users.

31. Add/delete users
Use useradd username (or adduser username) to add a user named username, and then use passwd username to specify the password. User information is recorded in the/etc/passwd file. Since each user can read the file (and must be able to), the password of other users can be crack, currently, shadow is used for security considerations. In/etc/passwd, the user's password becomes X. The real password ciphertext is recorded in/etc/shadow, and this file can be read only with root permission. Shadow is not directly used in versions earlier than RedHat 5.1. You need to use pwconv for conversion. If you want to change back, you can use pwunconv. Tip: in the/etc/skel directory, record a basic file that the user should have (LS-A/etc/skel/To see what it has ), useradd will automatically copy these files to the user's home directory and set the appropriate permissions (unless the-M parameter: useradd-m xxx is used when the user is added ). If you press a public_html directory under this directory, the users added later will automatically have a directory with the same name under its home directory, in this way, each user has a "Personal Homepage ".
To delete a user, you can use userdel usrname or userdel-r username (some releases may be deluser). The-R parameter deletes the user's home directory, however, the user information may exist elsewhere in the system. Therefore, before deleting a user, you 'd better find the user information by using the find/-User Username method.
If you want to temporarily disable a user, you can add a * sign before the first character of the user's password in the/etc/passwd file.
If you change the password field to null, the user can enter the system without the password. When you forget the root password, you can start the system with a floppy disk to install the original root password, directly modify the passwd file. I remember that the password is not required for single-user startup.
Long user names have been well performed in the RedHat release, and spaces are allowed in the user names. If the release version is not supported, change ut_namedsize to 32 in/usr/include/utmpbits. h to recompile the relevant program.
Another intuitive Configuration tool is linuxconf, for example, using it to configure pppuser.
Here we can only provide some clues. For more information, see man useradd, man shadow, man group, man userdel "......
Once again, it is recommended that new users read more books and read more man pages; otherwise, it is difficult to find out what/etc/skel is doing.

32. List the space occupied by a directory
Du, Du-S, or Du-K
Du-S | sort-N can quickly find that the directory is the largest.
DF shows the size of the installed file system and the remaining space.
Quota-V allows you to view your disk space. If you use quota to limit the size of your disk space.

33. Command completion (a wonderful use of the tab key)
Many shells have this function.
1) You only need to enter the first few characters of the file or directory name, and then press the tab key. If there is no duplicates, the complete file name will automatically appear on the command line immediately, press the tab key to list all names starting with these characters in the current directory. for example
CD/mnt/CD <Tab key> is automatically set to CD/mnt/CDROM
2) In the command line, you only need to enter "M" and press the tab key twice consecutively. The system will list all commands starting with "M, (including user-defined bshell command functions), which are used to find unclearly remembered commands. for example, if you enter "ftp", you will find FTP, ftpcount, ftpwho, ftpshut, and other unfamiliar commands.

34. There is no setenv in redhat5.0, And the set variable cannot be read in the program using getenv.
Which function can be used to read environment variables set with set?
Set env; export env
Then, use getenv to read the data.

35. Use grep to search for files in all subdirectories.
For example, search for "chinput" in all original C codes under zhxwin (try to cancel the downstream-Q Parameter ):
Find/download/zhxwin-name *. C-exec grep-Q-s chinput {};-print
To search for all files, you can use:
Find.-Type F-exec grep-s Chinese {};-print
If you like the graphic interface, Kde's kfind is good.

36. view the log information at startup
First, check the/etc/syslog. conf file and find the corresponding file.
(For more details, see "Man syslogd" "Man syslog. conf" and "Man klogd ")
Generally, slackware is/var/adm/messages, and RedHat is/var/log/messages.
These files may be large and must be backed up and deleted.
As mentioned above, you can use tail-F to view these files. In addition, there is also a logtail tool, which is quite good.

We recommend that you read the Linux tips howto and subscribe to Linux Gazette.
When collecting and organizing these documents, you can see a new site: Linux Tips & tricks
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.