Author: Cheryl Karis
A crash course for beginners who are unfamiliar with Linux file commands.
Although GUI desktops (such as KDE and GNOME) can help users take advantage of Linux features without knowledge of command-line interface functions, they usually need more functions and flexibility. In addition, basic familiarity with these commands is still necessary to correctly automate certain functions in shell scripts.
This article is a "crash course" on Linux file commands, which is provided for users who are not familiar with this operating system or just need to supplement this knowledge. It contains a brief overview of some of the more useful commands, as well as guidance on their most powerful applications. The following information-combined with some experiments-will make it easy for you to master these basic commands. (Note: Some of these commands may behave slightly differently when it comes to the kernel combined with Oracle Cluster File System (OCFS). In this case, Oracle provides OCFS toolset, which can provide a better choice for file command application. )
Please note that all the examples contained here have been tested on SUSE Linux 8.0 Professional. Although there is no reason to believe that they will not work on other systems, if there is a problem, you should check your documentation for possible changes.
Background concept
Before we go into the specification, let's review some basic knowledge.
Files and commands
In Linux/UNIX operating system, everything is regarded as files: hardware devices (including keyboards and terminals), directories, commands themselves, and of course files. This strange convention is actually the basis of the ability and flexibility of Linux/UNIX.
Most (almost all) commands take the following form:
Command [Options] [Source File] [Target File]
ask for help
One of the most useful commands is those that are helpful (especially for those who study Linux). Two important sources of information in Linux are online reference manuals, or manual pages and tools. You can use the whatis command to access the man pages of unfamiliar commands.
What is an echo?
To learn more about this command, you can use:
$ man echo
If you don't know the commands required for a specific task, you can use man -k (also known as apropos) and a topic to generate possible commands. For example:
$ TERM-k file
A useful but often overlooked command can provide information about using man itself:
$ man man
You can use the space bar to browse any manual page; The up arrow will scroll the file up. . To exit, please enter q,! Or ctrl-z.
users' category
Remember the famous saying that "all animals are equal, but some animals are more equal than others"? In the Linux world, the root user is in charge.
The root user can log in with another user name su (from "super user"). To perform tasks such as adding a new user, printer, or file system, you must log in as root, or switch to superuser with su command and root password. System files (including files that control the initialization process) are ultimately owned by users. Although ordinary users may be allowed to read them, for system security reasons, the right to edit them will be left to the root user.
BASH shell
Although other Shells are provided, BASH(Bourne Shell) is the default shell of Linux. It combines the characteristics of Bourne Schell with those of Cohen, C and tcsh· Schell.
By default, BASH's built-in command history records the last 500 commands. You can view them by entering history at the command prompt. To retrieve a specific command, you can press the up arrow or down arrow at the command prompt, or enter its number in the history list and add "!". Before it. , for example:
$ ! Forty nine
You can also execute the command according to its distance from the top item in the history list: If there are 53 events in the history list, $! -3 will execute event number 5 1.
Like other shell in the UNIX/Linux world, BASH uses special environment variables to promote system management. For example:
Home page, user home directory
PATH, the search path that Linux uses to search the executable image of the command you entered.
HISTSIZE, the number of historical events saved by the system.
In addition to these reserved keywords, you can also define your own environment variables. For example, ORACLE uses ORACLE_HOME (and other variables), which must be set in your environment for the Oracle installation to complete successfully.
You can temporarily set variables at the prompt:
$HISTSIZE= 100
Alternatively, make a system-wide permanent setting in /etc/profile (root permission is required) or make a local permanent setting in. Profile.
You can access the value of the environment variable through the echo command and use the $ sign to view the value.
$ echo $HOME
/home/bluher
You can use the env command to view all current environment variables.
Regular expressions and wildcards
Many Linux commands use wildcards * and? Match any number of characters or any single character respectively; Regular pattern matching expressions use periods (. ) matches any single character except "newline". In both cases, square brackets ([]) are used to match character groups other than "*". However, in each case, the * symbol has a similar but different meaning: although it will match one or more characters in the shell, it will match zero or more instances of the above characters in the regular expression. Some commands, such as egrep and awk, use a wider range of special characters for pattern matching.
File processing command
Parse file list
The ls command is used to view a list of files in any directory for which the user has execution permission. This command has many interesting options. For example:
$ ls -liah *
22684 -rw-r-r-1 bluher user 9521February 28th 18:43. outline
19942-rw-r-r-1scalish user301March 3rd at 20:00 test2.out
925 -rwxr-xr-x 1 scalish User 378 Tested on September 2nd, 2002. sh
The list above shows eight columns:
The column 1 represents the inode of the file, because we used the -i option. The remaining columns are usually displayed with the -l option.
The second column shows the file type and file access rights.
The third column shows the number of links, including the directory.
Columns 4 and 5 show the file owner and group owner. Here, the owner "bluher" belongs to the group "users".
The sixth column shows the file size (the unit is the displayed unit, not the default number of bytes, because we used the -h option.
The seventh column shows the date (it looks like three columns), including month, day, year and time of day.
The eighth column shows the file name. Using -a in the options list will make the list contain a list of hidden files (for example. Introduction).
Process file
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- 1ls . out ls2 . out
cpverwrite `ls2.out '?
The mv command allows the -b option, which will make a backup copy before moving the file. Rm and cp accept the powerful but dangerous -r option, which will be recursively executed on directories and their files.
$ TERM-IR test
Rm: Enter the directory "Test"? y
You can use mkdir to create a directory and rmdir to delete a directory. However, since you cannot use rmdir to delete directories containing files, it is usually more convenient to use the rm plus -r option.
For security reasons, all files are owned and protected. The file access right (or file mode) contains the same 10 character mentioned above:
The first character indicates the file type. The most common ones are-for files, d for directories, and l for links.
The next nine characters are the access rights of three user categories: file owner (characters 2-4), user group (5-7) and others (8- 10), where r stands for read permission, w stands for write permission and x stands for execute permission on files. Dash-If it appears in any of these nine positions, it means that the user in this category prohibits the operation.
You can use the chmod command to set access rights through character symbols or binary masks. To use binary masks, you must convert the character representations of the three permission groups to binary format and then to octal format:
User Category: Owner User Group Other
Character representation: rwxr-xr-
Binary representation:11101100.
Octal notation: 7 5 4
To grant write permission to a user group, you can use:
Chmod g+w test.sh or chmod774test.sh.
Use the umask command to set system-wide default file permissions in the /etc/init.dev file or. Configuration file. This command instructs to subtract this number from 777 to obtain the default rights:
$ umask 022
This will generate a default file license 755 for all new files created by the user.
Chown can be used to modify the ownership of a file:
$ chown bluher has quit.
Here, Brueux is the new file owner. Similarly, the group membership will be modified as follows:
$ TERM chgrp development group ls.out
Here, devgrp is the new user group.
Ls does not provide information about which files are text files and which files are binary files. To understand this information, you can use the file * command.
Rename file
Two popular ways to give multiple names to a file are to use the link and alias commands. Alias can be used to rename long commands with more convenient names:
$ alias ll='ls -l '
Ll dollars
Pay attention to the use of single quotation marks, which makes BASH pass items to aliases instead of estimating them himself. Aliases can also be used as abbreviations for longer path names:
$ alias jdev 9i =/jdev 9i/jdev/bin/jdev
For more information about alias and its countercommand unalias, please refer to the "SHELL Built-in Commands" section of the BASH man page. In the last example, an environment variable is defined to achieve the same result.
$ export JDEV _ HOME =/JDEV 9i/JDEV/bin/JDEV
$ echo $JDEV_HOME
/jdev9i/jdev/bin/jdev
$ $JDEV_HOME
Linking allows multiple file names to refer to a single source file in the following formats:
In the [-s] file, you want to link to the new name.
The separate ln command creates a hard link to a file, while the -s option is used to create a symbolic link. In short, a hard link is difficult to distinguish from the original file (except that the inode of the two files will be the same). Symbolic links are easier to distinguish because they appear in a long file list and are marked with->; ; To represent the source file, l represents the file type.
View and find files
File filter
Commands used to read the contents of a file and perform operations on it are sometimes called. & filter. The Sed and awk commands are examples of two filters, because they have been discussed in detail in previous OTN articles and will be skipped here.
Commands like cat more or less allow you to view the contents of a text file from the command line without calling an editor. Cat is the abbreviation of "concatenate". By default, the file content will be displayed on the standard output (display screen). One of the most interesting options provided by 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 outputs all the lines in the file at once, you may prefer to use more and less because they both output the contents of the file one screen at a time. Less is an enhanced version of more, which allows keyboard commands in the vi text editor to enhance file viewing. For example, d scrolls forward and b scrolls back by n lines (if n is specified before d or b). The value entered for n becomes the default value for subsequent d commands. The man page utility uses less to display the contents of the description.
Redirection and pipeline
Redirect allows you to redirect command output to a file instead of standard output, or similarly, you can redirect input. Standard symbol of redirection >; ; Create a new file. & gt; & gt; Symbol for adding output to an existing file:
$ more tests 2.out
Another test.
$ cat test.out & gt; & gt; test2.out
$ cat test2.out
Another test.
This is a test.
The standard input of the file can be used
$ cat & lttest2.out
Error message with 2 >; ; And 2 >; ; & gt; Redirect and add, the format is as follows:
$ command 2 & gt; The name of the error file
To avoid inadvertently overwriting existing files, use BASH's built-in command set:
$ set -o noclobber
You can use >:; ! Symbol overloads this function. To turn off this feature, use +o instead of-o.
Redirection works between commands and files or between files. One of the redirection statements must be a file.
Pipes use the | symbol and work between commands. For example, you can send the output of the command directly to the printer in the following ways:
$ ls -l * | lpr
You can quickly find commands in the history list in the following ways:
$ history | grep cat
More filters
Grep, fgrep, and egrep all display rows that match a pattern. All three commands search for the specified pattern in the file, which is very useful if you don't remember the name of the required file. The basic format is:
Grep[ Options] mode [File ...]
$ grep -r' theme' nsmail'
CTRL-Z will terminate the above or any other command.
Perhaps the most useful option for grep is-s. If you search for system files as anyone other than root, an error message will be generated for each file to which you do not have access rights. This order prohibits this information.
Fgrep (also called grep -F) only looks for fixed strings, not regular expressions received by grep. On the other hand, Egrep receives patterns containing more special characters, such as "|", which represents the conditional OR operator.
$ egrep 'Subject|mailto' *
Find file
The GNU version of the find command is very powerful, flexible and more inclusive than the existing classic version on UNIX systems. It is very useful for tasks involving directory structure, including finding and executing commands on files. The basic format of the find command is:
$ find startdirectory option matching conditions [actionoptions]
If you know the name of the file, or even part of it, but don't know the directory where the file is located, you can do this:
$ find。 -Name "Test *"
. /test
. /jdevhome/my work/EmpWS/EMP BC 4j/test
Unlike the classic UNIX system, there is no need to add the -print operation at the end in Linux, because this operation will be assumed if no other operation options are specified. A dot (. ) will cause find to start searching in your working directory. At two o'clock, ..., start searching in the parent directory. You can start your 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
Display with. External extension:
$ find /home -name '*。 "Go out"
But remember, unless you run this command as superuser, you may receive many "permission denied" error messages.
One of the most powerful search tools is the -exec operation used with grep:
$ find。 -Name' *. html '-exec grep ' mailto:foo @ Yahoo . com ' { } \;
. , found an html file. Html, and execute (-exec) grep command on the current file. When using the -exec operation, a semicolon is required; This is used to perform other operations when using the find command. You need to use backslashes \ and quotation marks to ensure that these items pass BASH so that they are interpreted by commands rather than shell.