Linux System File Command Master Guide (top) _unix Linux

Source: Internet
Author: User
Tags chmod echo command parent directory egrep

Although GUI desktops, such as KDE and GNOME, can help users take advantage of Linux features without the functional knowledge of command-line interfaces, they often require more functionality and flexibility. Moreover, familiarity with these commands is still necessary to automate certain functions in the shell script correctly.

This article is an "accelerated tutorial" on Linux file commands, which is provided for users who have just touched the operating system or just need to supplement this knowledge. It contains a concise overview of some of the more useful commands and guidance on their most powerful applications. The information contained below-combined with some experiments-will make it easy for you to master these basic commands. (Note: When it comes to a kernel that is associated with an Oracle clustered file system (OCFS), some commands in these commands may behave slightly differently.) In this case, Oracle provides a OCFS toolset that can provide a better choice for file command applications. )

Note that all of the examples included here are tested on the SUSE Linux 8.0 Professional. Although there is no reason to believe that they will not work on other systems, you should review your documentation for possible changes if problems arise.

Background concepts

Before delving into the specification, let's review some of the basics.

Files and commands

In the Linux/unix operating system, everything is handled as a file: hardware devices (including keyboards and terminals), directories, commands themselves, and of course files. This strange practice is actually the foundation of Linux/unix's ability and flexibility.

Most (almost all) of the commands are in the form of:

command [option] [source file (s)] [target file]

Get help

One of the most useful commands is those that provide help (especially for those who learn Linux). Two important sources of information in Linux are the online reference manual, or the man page and the Whatis tool. You can use the Whatis command to access the man page of an unfamiliar command.

$ whatis Echo

To learn more about this command, you can use:

$ mans Echo

If you don't know the commands you need for a particular task, you can use Man-k (also known as apropos) and a topic to generate the possible commands. For example:

$ man-k Files

A useful but often overlooked command can provide information about the use of the man itself:

$ mans Man

You can use SPACEBAR to browse any man page, up ARROW will roll up the file. To exit, enter q,!, or ctrl-z.

User Category

Remember the famous saying, "All animals are equal but some animals are more equal than other animals"? In the Linux world, root users are in charge of everything.

The root user can log in with another user name SU (originating from "Superuser"). To perform tasks such as adding a new user, printer, or file system, you must log on as root or switch to superuser with the SU command and root user password. System files (including system files that control the initialization process) are all in the root of the user. Although normal users may be allowed to read them, the rights to edit are left to the root user for system security reasons.

BASH Shell

While other shells are provided, BASH (Bourne Again shell) is the default shell for Linux. It combines the characteristics of the Bourne shell with its namesake and the features of the Korn, C, and TCSH shells.

BASH's built-in command history the last 500 commands entered in the default record. You can view them by entering history at the command prompt. To retrieve a particular command, press UP ARROW or DOWN arrow at the command prompt, or enter its number in the History list, preceded by a number with "!", for example:

$!49

You can also execute this command from the distance from the previous item in the history list: If there are 53 events in the History list, $!-3 will execute event number 51.

Like other shells in the Unix/linux world, BASH uses special environment variables to facilitate system management. For example:

Home, user main directory

Path,linux search path used to search for executable mirrors of the command you entered

Histsize, number of historical events saved by the system

In addition to these reserved keywords, you can define your own environment variables. For example, Oracle uses Oracle_home (and other variables) to make the Oracle installation complete successfully, and you must set these variables in your environment.

You can set a variable temporarily at the prompt:

$HISTSIZE =100

Alternatively, make system-wide permanent settings in/etc/profile (requiring root user privileges), or make local permanent settings in. Profile.

You can view the value by using the Echo command and using a $ symbol to access the value of an environment variable.

$ echo $HOME
/home/bluher

You can use the ENV command to view all current environment variables.

Regular expressions and wildcard characters

Many Linux commands use the wildcard character * and? To match any number of characters or to match any single character individually; a regular pattern matching expression uses a period (.) to match any single character except the line break. In both cases, the brackets ([]) are used to match the group of characters except the "*" number. However, the * number has a similar but different meaning in all cases: although it will match one or more characters in the shell, it matches 0 or more instances of the above characters in a regular expression. Some commands, such as Egrep and awk, use a broader set of special characters for pattern matching.

File processing commands

Profiling a list of files

The LS command is used to view a list of files in any directory where the user has execute permissions, and the command has many interesting options. For example:

$ Ls-liah *
22684-rw-r--r--1 bluher users 952 Dec 18:43. Profile
19942-rw-r--r--1 scalish users 3 20:00 test2.out
925-rwxr-xr-x 1 scalish users 378 SEP 2 2002 test.sh

The list above shows 8 columns:

The 1th column indicates the inode of the file because we use the-i option. The remaining columns are displayed normally through the-l option.

The 2nd column shows file types and file access rights.

The 3rd column shows the number of links, including the table of contents.

Columns 4th and 5th display the owner and group owner of the file. Here, the owner "Bluher" belongs to the group "users".

The 6th column shows the file size (in units shown, not the default number of bytes, because we use the-H option.)

The 7th column shows the date (it looks like three columns), including the Month, day and year, and the day's time.

The 8th column shows the file name. Using-A In the list of options causes the list to contain a list of hidden files, such as. Profile.

Working with files

You can move (MV), copy (CP), or delete (RM) files and directories. It is usually a good idea to use the-I option wisely to get confirmation.

$ cp-i ls.out ls2.out
Cp:overwrite ' Ls2.out '?

The MV command allows you to use the-B option, which will make a backup copy before moving the file. RM and CP accept the powerful but dangerous-r option, which is recursively executed in a directory and its files.

$ Rm-ir Test
Rm:descend into directory ' Test '? Y

You can use mkdir to create a table of contents with RmDir to remove the directory. However, it is often easier to use the RM Plus-r option because you cannot use RMDIR to delete directories that contain files.

For security reasons, all files have ownership and protection. File access rights (or file mode) contain the same 10 characters mentioned earlier:

The first character indicates the file type. The most common is-represents the file, D represents the directory, and L represents the link.

The next 9 characters are access rights for three user classes: the file owner (character 2-4), the user group (5-7) and others (8-10), where R is the Read permission, W represents the Write permission, and X indicates the execution permission on a file. Dash-If any of these nine locations are present, indicate that this action is prohibited for users of that category.

You can use the chmod command to set access permissions through character symbols or binary masks. To use a binary mask, you must convert the character representation of three permission groups to binary format and then convert to octal format:

User Category: Owner user Group Other
Character representation: rwx R-x r--
Binary representation: 111 101 100
Octal notation: 7 5 4

Give write permission to the user group, you can use:

chmod g+w test.sh or chmod 774 test.sh

Use the umask command to make a system-wide, or local file permission default setting in the/etc/init.dev file. This command indicates that the number is subtracted by 777 to obtain the default permissions:

$ umask 022

This will generate a default file permission of 755 for all new files created by the user.

You can use Chown to modify the ownership of a file:

$ chown Bluher ls.out

Here, Bluher is the new file owner. Similarly, group membership will be modified in the following manner:

$ chgrp devgrp ls.out

Here, Devgrp is the new user group.

LS does not provide information about which files are text and which files are binary. To understand this information, you can use the file * command.

Renaming files

Two popular ways to give multiple names to a file are to use the link and alias commands. The Alias can be used to recreate a more convenient name for a longer command:

$ alias ll= ' Ls-l '
$ ll

Note the use of single quotes, which allows BASH to pass the project to the alias instead of estimating it yourself. Aliases can also be used as abbreviations for longer path names:

$ alias Jdev9i=/jdev9i/jdev/bin/jdev

For more information about the alias and its counter command Unalias, see the "SHELL BUILTIN COMMANDS" sub-section of BASH's man page. In the last example, an environment variable is defined to achieve the same result.

$ Export Jdev_home=/jdev9i/jdev/bin/jdev
$ echo $JDEV _home
/jdev9i/jdev/bin/jdev
$ $JDEV _home

A link allows several file names to refer to a single source file in the following format:

ln [-S] fileyouwanttolinkto newname

A separate ln command creates a hard link to a file and uses the-s option to create a symbolic link. In short, a hard link can hardly be separated from the original file area (except that the inode for these two files will be the same). Symbolic links are easier to distinguish because they appear in a long file list and are-> to indicate the source file, L indicates the file type.

View and find Files

File filter

Commands used to read the contents of the file and perform operations on the contents of the file are sometimes referred to as 1y filters. The SED and awk commands are examples of two filters, because they are discussed in detail in previous OTN articles and will be skipped here.

Commands such as cat, more, and less allow you to view the contents of a text file from the command line without invoking the editor. Cat is the abbreviation for "concatenate", which displays the contents of the file on the standard output (display) by default. One of the most interesting options available with Cat is the-n option, which displays the contents of the file with numbered output lines.

$ cat-n Test.out
1 This is a test.

Because cat prints all the rows in a file at once, you might prefer to use more and less, because they all output one screen of file content at a time. Less is an enhanced version of more, which allows you to enhance file viewing with key commands from the VI text editor. For example, d rolls forward, b rolls backward (if n is specified before d or b). The value entered for N becomes the default value for the subsequent D command. The man page utility uses less to display the contents of the usage instructions.

Redirection and piping

Redirection allows the command output to be redirected to a file instead of standard output, or, similarly, to redirect input. REDIRECT Standard symbol > Create a new file. The >> symbol adds output to an existing file:

$ more Test2.out
Another test.
$ cat Test.out >> test2.out
$ cat Test2.out
Another test.
This is a test.

Standard input to a file can be redirected with the < symbol:

$ Cat < Test2.out

Error messages are redirected and added with 2> and 2>>, in the following format:

$ command 2> name_of_error_file

To avoid accidentally overwriting an existing file, use BASH's built-in command set:

$ set-o Noclobber

You can use >! between the command and output files Symbol to overload this attribute. To turn off this feature, replace-O with +o.

Redirects work between a command and file, or between files and files. One of the redirected statements must be a file.

The pipe uses the | symbol and works between the commands. For example, you can send the output of a command directly to the printer in the following ways:

$ ls-l * | Lpr

You can quickly find one of the commands in the History list in the following ways:

$ history | grep cat

More Filters

Grep, Fgrep, and egrep both display rows that match one pattern. All three commands search the file for the specified pattern, which is useful if you don't remember the name of a desired file. The basic format is:

grep [options] pattern [FILE ...]
$ Grep-r ' Subject ' Nsmail
Ctrl-z will terminate the above or any other order.

The most useful option for grep may be-S. If you search for system files in any identity other than the root user, you will generate an error message for each file that you do not have access to. The order forbids those messages.

Fgrep (also called grep-f) finds only fixed strings, not regular expressions that grep receives. Egrep receives a pattern that contains a larger range of special characters, such as "|", which indicates the condition OR operator.

$ Egrep ' Subject|mailto ' *

Find Files

The GNU version of the Find command is very powerful, flexible, and more inclusive than the classic version that exists on UNIX systems. It is useful for tasks that involve the directory structure, including locating and executing commands on files. The basic format of the Find command is:

$ find startdirectory options Matchcriteria [Actionoptions]

If you know the name of the file, or even part of the name, but do not know the directory in which it is in, you can do this:

$ find.-name ' test* '
./test
./jdevhome/mywork/empws/empbc4j/test

Unlike a classic UNIX system, there is no need to add a-print operation at the end of Linux, because if no other action option is specified, the operation is assumed to be performed. A point (.) at the start of the directory will cause find to start the search in your working directory. Two points, ..., start the search in the parent directory. You can start the search in any directory.

Note that you can use wildcards as part of your search criteria (just enclose the entire item in single quotes).

$ find.-name ' test* '-print
./test.out
./test2.out

Displays a series of files that have an. out extension:

$ Find/home-name ' *.out '

But remember, unless you're running this command as a superuser, you'll probably get a lot of "Permission denied" error messages.

One of the most powerful search tools is the-exec operation that is used with grep:

$ find.-name ' *.html '-exec grep ' mailto:foo@yahoo.com ' {} \;

Here, we let the find command begin execution in the current directory,., look for an HTML file, *.html, and execute (-exec) grep command on the current file, {}. A semicolon is required when using the-exec operation, which is used to perform some other action when using the Find command. Backslashes and quotes are required to ensure that these items are passed through BASH so that they are interpreted by commands rather than shells.

Master the Linux File command immediately

There are a lot of useful commands and powerful ways to take advantage of them in Linux, which is not explained here. In addition, there are often several ways to accomplish many tasks.

We have seen some of the most commonly used and most enlightening Linux file commands. Mastering these basics but critical tools will make your Linux learning a fast track. Using the man page you have on hand, and experimenting with enthusiasm, you now have enough information to start exploring the power of Linux file operations.

In my next article, I will provide a similar description of the Linux system commands.

Related Article

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.