Linux file command proficiency Guide

Source: Internet
Author: User
Tags month name superuser permission

For file commands, we have discussed some simple and intuitive management commands in detail here. It only describes the functions of other complex commands, so that you can know the existence and purpose of these commands when you need to use them. This article does not discuss in detail the basic principles behind each command, but we will discuss the most basic and useful commands, these commands are used to monitor and manage Printer services, file systems, disks, and processes.

More information about Super Users

Many of the commands discussed here can be executed by regular users in their own files and processes. However, some commands, such as fdisk, must have the root permission to run.

To ensure the integrity of your system, we recommend that you do not directly log on to the system as "root. On the contrary, it is generally better to log on as a regular user, and then run the su or sux command from X window to run the system management function. (Sux provides you with the X window environment and root permissions .) When you log on as a root user, you always know that you are in this status because your prompt changes to "#".

Super Users can use sudo to selectively assign permissions that are usually reserved by root users or other special users to a different regular user. The root user can define actual and valid user and group ID uid and gid to match the expected user ID. This expected user can be (but does not have to) the user who is allowed to run the command.

In other words, a Super User can allow other users to execute a command, but it can also determine that any result file generated by the process should belong to the root user or any other user specified by the Super User. Once the Sudo permission is assigned, you only need to use your own password for verification, instead of the root user password.

Common commands

Technically, Uname, diff, date, sort, and uniq should be considered as file commands, but they are more meaningful here because they are often used to diagnose and maintain the system management of routines.

Uname

Uname provides general system information. When the-a option is specified, all available information is displayed:

$ Uname-
Linux linux 2.4.18-4 GB #1 Wed Mar 27 13:57:05 UTC 2002 i686 unknown

Here we can see the general system information from left to right. Each piece of information has its own options (in the parentheses below), which can be displayed separately:

The operating system name is Linux (-s ).

The network node name of the local machine is linux (-n ).

The OS version is 4.5.18-4 GB (-r ).

The operating system version is #1 Wed Mar 27 13:57:05 UTC 2002 (-v ).

The machine type is i686 (-m ).

Here, the processor type (-p) is unknown.

Similarly, stty displays information about your terminal settings:

$ Stty-
Speed 38400 baud; rows 40; columns 103; line = 0;
Intr = ^ C; quit = ^ \; erase = ^ ?; Kill = ^ U; eof = ^ D; eol =; eol2 =; start = ^ Q;
Stop = ^ S; susp = ^ Z; rprnt = ^ R; werase = ^ W; lnext = ^ V; flush = ^ O; min = 1; time = 0;
-Parenb-parodd cs8-hupcl-cstopb cread-clocal-crtscts-ignbrk-brkint-ignpar-parmrk-inpck
-Istrip-inlcr-igncr icrnl ixon-ixoff-iuclc-ixany-imaxbel
Opost-olcuc-ocrnl onlcr-onocr-onlret-ofill-ofdel nl0 cr0 tab0 bs0 vt0 ff0
Isig icanon iexten echo echoe echok-echonl-noflsh-xcase-tostop-echoprt echoctl echoke

The second and third lines show the special characters and key sequences that will be called in the command line. For example, "^ Z" means the CNTRL-Z sends a terminal break in the system. The exit symbol is sent as "^ \", that is, CNTRL-\, and we can change it to "^ X", as shown below:

$ Stty quit ^ X

In this case, you must enter the ^ symbol instead of the CNTRL key. This change only takes effect in the current window. To make this change take effect permanently, enter this statement in your. bashrc file.

Diff and Date. As the name suggests, Diff compares two files. It returns the rows that lead to different files and the output with obscure meanings about how to make the files the same:

$ Diff test. out test2.out
0a1
> Another test.

Here we are told that we need to add the line "Another test." To test. out so that it is similar to test2.out. The first character "0" is in test. "a" indicates that the row should be added to test. to match the first line "1" of test2.out. If we need to change test. the second character can also be "c. the second character is "d ".

You may want to make a file similar to another file to patch some code. Diff is often used as an analysis tool to verify whether the output of a program or script, such as sed script, is the expected output.

The date command can be used to display or set a date. If the user has the superuser permission, he or she can set the date by providing a numeric string in the following command:

$ Date 0101010004

This command sets the date to (0101), January 1, 0100 ).

However, any user can use the required format to display the date. Note that a plus sign "+" is required before the format sequence ". If the expected output of this command is not displayed, try to include it in single quotes:

$ Date + '% H % M % s'
204748

You can include spaces in the sequence. In this case, you need to use double quotation marks:

$ Date + "% H % M % S"
20 47 51

There are over 30 different format sequences listed on the date homepage. These sequences can be combined as needed to get your final output format.

Date output can also be used in scripts, such:

$ Date-d '2 Day'
Thu Jan 29 19:02:02 EST 2004

The-d option indicates that date displays the time described in the subsequent string. In the above example, we asked to display the date two days from now on (January 1, January 27, 2004. We can also ask to display the date three days ago. The command is:

$ Date-d '-3 Day'
Sun Jan 25 20:56:59 EST 2004

Another example of multiple functions of date is the ability to output strings to it, providing a convenient way to generate a unique file name.

$ Ls-l | cat >$ (date + ls _ % d % B % y % H % M. out)
$ Ls _ *. out
Ls_04Feb041633.out

Here, we send the output of a long file list to cat, which transfers our output to a file named ls_04Feb041609.out. Note that we use a variable for the file name, represented by the dollar sign "$. Sort "ls _" and ". out "is appended to the date, in the format of day (% d) in the month, abbreviation of local month name (% B), last two digits of the Year (% y), hour (% H) and minute (% M), thus generating this file name.

Sort and Uniq. You can perform the sort operation on the lines of text files from the MPs queue or from the files specified by the command line. Unless otherwise specified, sort operates on the first column of Multi-column output. By default, numeric columns are sorted alphabetically unless the-n option is used.

The Sort column can be specified in two different formats. If the-k option is used, the column position starts from 1. You can also use the + POS1 format to numbered a column starting from the first column number to zero. The following two commands generate the same output:

$ Ls-l *. out | sort + 5-7
$ Ls-l *. out | sort-k 6, 8
-Rw-r -- 1 bluher users 375 Dec 5 2002 fdisk. out
-Rw-r -- 1 bluher users 2447 Dec 28 ls. out
-Rw-r -- 1 bluher users 590 Jan 4 greps. out
-Rw-r -- 1 bluher users 78 Jan 4 21:25 sgrep. out
...

By default, the column Separator of the sort command is space and tab. You can use the-t SEP option to specify other separators, where SEP is the separator you specified. The default sequence of sort is from the lowest to the highest, and the order can be reversed using the-r option.

Sort can be used for many different tasks. For example, it can be used to sort the output of files (such as/etc/passwd), or sort the output of du as we can see below. When the-m option is used, sort merges the files without sorting them. However, if you want to sort them, you should sort them before file merging.

Sort is often used with the uniq command to delete identical lines from sorted files. The uniq command is often followed by the sort command in the pipeline. You can also use the-c option to calculate the number of occurrences of a line, or use the-d option to only report identical rows:

$ Sort-m test. out test2_sort.out | uniq-c
1 Another test.
1 Testing testing
2 This is a test.

Here we merge two files: test. out and test2_sort.out. Then we use the-c option to transmit the output to uniq, so that the final result is a list in alphabetical order, and the identical rows are deleted, it also contains a report on the frequency of occurrence of each line.

Printer Control

In most cases, the use of printer commands is relatively clear. You can specify a file name after the lpr command to send the job to the printer. If no file name is provided, standard input is used. Use The lpq command to obtain status reports about jobs sent to the printer. When you specify a job number, Lprm deletes the job from the print queue of the row printer background.

$ Lpr grep2.out
$ Lpq
Printer: lp @ linux 'lpdfilter drv = upp method = auto color = yes'
Queue: 1 printable job
Server: pid 1929 active
Unspooler: pid 1930 active
Status: waiting for subserver to exit at 21:24:14. 637
Rank Owner/ID Class Job Files Size Time
Active A 928 grep2.out 779 21:24:14
Done bluher A 877 (STDIN) 117258 13:09:53
$ LPR 928
Checking perms 'cfa928linux. local'
Dequeued 'cfa928linux. local'

Here, when we send a job to the printer, We query its status. Lpq shows us that the job number 877 has been completed, and the job number we just sent is 928. Run lprm 928 to delete the active job from the queue.

Disk Information

Whether you maintain your system or support enterprises with many users, monitoring disk space is one of the more important responsibilities of the system administrator. The du, df, and fdisk commands provide different views of available disk space.

The du command evaluates the usage of the file space in each directory. By default, the unit of measurement is the number of blocks. The block size is usually determined when you install your system.

In the following example, the system uses a 1024-byte block for installation. If no directory is specified, du starts to calculate the current directory. The-h option provides the size value in an easy-to-understand form:

$ Du-h Documents ents
105 k Documents/jj
573 k Documents/John
682 k Documents

You can use the-a option to change the output. This option indicates the number of files and directories displayed by du. Option -- block-size = SIZE indicates that du displays the output based on the number of blocks in the specified byte SIZE.

$ Du -- block-size = 512 Documents ents
209 Documents/jj
1146 Documents/John
1363 Documents

By default, du recursively prints information for each sub-directory. You can use the max-depth = N option to limit this operation, where N is equal to the maximum level reported:

$ Du -- max-depth = 2.
$ Du -- max-depth = 2 | sort-n
4./. xemacs
4./public_html
105./Documents/jj
573./Documents/Ian
682./Documents
2420./jdevhome/system
8350./jdevhome/system9.0.3.1035
13483./jdevhome/mywork
24252./jdevhome
...

Du reports space usage of directories and subdirectories, while df reports disk space usage of installed file systems:

$ Df
Filesystem 1k-blocks Used Available Use % Mounted on
// Dev/hda6 4195632 1983656 2211976 48%/
/Dev/hda5 14607 3778 10075 28%/boot
/Dev/hda9 937372 494028 443344 53%/home
/Dev/hda8 6297248 3876880 2420368 62%/opt
Shmfs 256220 0 256220 0%/dev/shm

Here, df reports the status of the five file systems. Similar to du, df uses the-h option to provide human-readable output:
$ Df-h
Filesystem Size Used Avail Use % Mounted on
/Dev/hda6 4.0G 1.9G 2.1G 48%/
/Dev/hda5 14 M 3.7 M 9.8 M 28%/boot
/Dev/hda9 915 M 476 M 440 M 52%/home
/Dev/hda8 6.0G 3.7G 2.3G 62%/opt
Shmfs 250 M 0 250 M 0%/dev/shm

The df command also allows you to use the-block-size = SIZE Option to specify the block size.

The fdisk command is a partition table Operation Command in Linux. Unlike du and df, this command cannot be run without the root user permission. If the-l option is specified, it can be used only for reporting purposes. It provides a complete view of your entire partition table, not just the extended Linux partition that you see using df:

# Fdisk-l

Disk/dev/hda: 240 heads, 63 sectors, 1940 cylinders.
Units = cylinders of 15120*512 bytes

Device Boot Start End Blocks Id System
/Dev/hda1 1 286 2162128 + c Win95 FAT32 (LBA)
/Dev/hda2*288 1940 12496680 5 Extended
/Dev/hda5 288 289 15088 + 83 Linux
/Dev/hda6 290 844 4195768 + 83 Linux
/Dev/hda7 845 983 1050808 + 82 Linux swap
/Dev/hda8 984 1816 6297448 + 83 Linux
/Dev/hda9 1817 1940 937408 + 83 Linux

Process Monitoring

In a busy multi-user system, process monitoring is also an important responsibility of the Linux system administrator. In this regard, ps and top are two of the most useful commands.

The ps command provides a snapshot of the currently running process.

The following is the simplest form of ps:

$ Ps
PID TTY TIME CMD
3884 pts/1 00:00:00 bash
3955 pts/2 00:00:00 more
3956 pts/5 00:00:05 sqlplus

PID is the ID of a process.

TTY is the terminal console to which the process belongs.

The TIME column is the total cpu time used by the process.

The CMD column lists the command lines being executed.

Use the ps with the-ef option to return the complete list of all processes of all users in the system. If you send the result of this ps command to grep, the result is easier to view. For example:

$ Ps-ef | grep oracle
UID PID PPID C STIME TTY TIME CMD
Oracle 1633 1 0? 00:00:00 ora_pmon_ora1.
Oracle 1635 1 0? 00:00:00 ora_dbw0_ora1.
Oracle 1637 1 0? 00:00:01 ora_lgwr_ora1.
Oracle 1639 1 0? 00:00:02 ora_ckpt_ora1.
Oracle 1641 1 0? 00:00:02 ora_smon_ora1.
Oracle 1643 1 0? 00:00:00 ora_reco_ora1.
Oracle 1645 1 0? 00:00:00 ora_cjq0_ora1.
Oracle 1647 1 0? 00:01:18 ora_qmn0_ora1.
Oracle 1649 1 0? 00:00:00 ora_s000_ora1.
Oracle 1651 1 0? 00:00:00 ora_d000_ora1.

Note: When you send ps output to the grep command, the column title is not displayed normally as it is in use by the ps command itself. For ease of instruction, the title is located above the output so that the contact is clearly displayed.

Although uid is usually an exponential identifier, the user name specified in the first column is marked as UID.

PPID is the ID of the parent process. For an Oracle process, the id here is 1-it is the id of the init process (the parent process of all processes, because Oracle installed in the system is started as part of the login process.

The column labeled C is a factor used by the CPU to calculate the execution priority.

STIME refers to the start time of the process.

The question mark indicates that these processes do not belong to any TTY because they are started by the system.

The following is another example of the ps command with some different options. Note: Many of the columns are the same as those when executing the ps command with the-ef option:

$ Ps aux
User pid % CPU % MEM VSZ RSS TTY STAT START TIME COMMAND
Bluh 4024 0.0 0.2 2240 pts/1 S su bluher
Bluh 4025 0.0 0.3 2856 1668 pts/1 S bash
Bluh 4051 0.0 0.2 2488 1504 pts/1 R p.m. ps aux
Bluh 4052 0.0 0.1 1636 pts/1 S grep bluher

In addition to providing important diagnostic information, the ps command is often used to identify the PID of a runaway process. For example, one of your users enters a Cartesian join in the SQL * Plus session. You can run the following ps command to find its PID:

$ Ps-ef | grep bluher
...
Bluher 4068 4064 0 00:00:00 pts/6 less
Bluher 4112 3945 0 00:08:42 pts/5 sqlplus
...

Then you can use the following command to terminate it:

$ Killed 4112

Regular users can see all system processes, but they can only terminate their own processes.

Ps only provides snapshots of the current process. To view the most active processes in real time, use top. Top provides process information in real time. It also has an Interactive status, allowing users to enter commands, such as numbers 5 or 10 after n. The result indicates that top displays 5 or 10 of the most active processes. Top continues until you press "q" to exit top.

The following figure shows the top parts: up 51 min, 7 users, load average: 0.02, 0.03, 0.05
96 processes: 87 sleeping, 2 running, 0 zombie, 7 stopped
CPU states: 6.4% user, 3.5% system, 0.0% nice, 89.9% idle
Mem: 512440 K av, 392288 K used, 120152 K free, 0 K shrd, 55668 K buff
Swap: 1050800 K av, 0 K used, 1050800 K free 236128 K cached

Pid user pri ni size rss share stat % CPU % MEM TIME COMMAND
1289 root 18 0 78008 11 M 1796 R 3.7 2.2 X
1965 bluher 20 0 12244 11 M 10456 S 1.9 0: 01 kdeinit
1986 bluher 18 0 996 996 R 768 1.5 0: 00 top
1803 bluher 20 0 16960 16 M 13508 S 0.9 0: 03 kdeinit
1804 bluher 20 0 16964 16 M 13508 S 0.9 0: 03 kdeinit
1800 bluher 19 0 12260 11 M 10076 S 0.1 0: 04 kdeinit
1 root 20 0 220 220 S 188 0.0 init
...

Close

It seems that we should end this discussion with the shutdown command. Like many commands discussed in this article and the previous article, there is usually a GUI-based alternative that can shut down your system. However, you can use the following command to disable it immediately:

$ Shutdown-h now

You can use the following command to manually restart the system:
$ Shutdown-r now

Note that both commands must specify the time. You can specify "now" or use the-t option with a time in seconds.

We have discussed a lot in these two articles, but we cannot cover all the content that Linux may have. We have not discussed many other commands, but we have not seen more Command Options. However, here we want you to find at least the minimum tools required to start managing Linux systems.

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.