Linux File processing commands

Source: Internet
Author: User
Tags compressed file types egrep

Linux
The system information is stored in the file, and the file is similar to ordinary official documents. Each file has its own name, content, storage address, and other administrative information, such as the user of the file, the size of the file, and so on. File can be
A letter, an address book, or a program's source statement, program data, and even can include executable programs and other non-body content.
Linux file system has a good structure, the system provides a lot of file processing programs. This article mainly describes the commonly used file processing commands.
File
1. The role of the content to determine the file type, the use of permissions are all users.
2. Format
File through probing text
file [options] File name
3.[options] Main parameters
-V: Displays the version information after the standard output and exits.
-Z: Detects compressed file types.
-L: Allow the connection to be met.
-F Name: Reads the list of file names to parse from the file NameFile.
4. Brief description
You can use the file command to know whether a file is a binary (elf-formatted) executable, a shell script file, or any other format. File types that can be identified include directories, shell scripts, English text, binary executables, C-language source files, text files, and DOS executable files.
5. Application examples
If we see a file with no suffix grap, you can use the following command:
$ file Grap
Grap:english text
At this point the system shows that this is an English text file. Note that the file command is not able to detect multimedia file types including graphics, audio, video, and so on.
Mkdir
2. Use
The purpose of the mkdir command is to create a subdirectory named DirName, similar to the MD command under MS DOS, which is used by all users.
2. Format
mkdir [Options] Directory name
3.[options] Main parameters
-M,--mode= mode: Set permissions, similar to chmod.
-P,--parents: Creates a top-level directory when needed, and is not considered an error if the directory already exists.
-V,--verbose: Displays information each time a new directory is created.
--version: Show version information and leave.
4. Application examples
You can set permissions for the directory at the time of catalog creation, and the parameters used are "-M". Assuming that the directory name you want to create is "tsk", so that all users have rwx (that is, read, write, execute), you can use the following command:
$ mkdir-m 777 Tsk
Grep
2. Use
The grep command allows you to specify a file to search for specific content and to output the standard rows containing the content. The grep full name is global Regular expression Print, which represents the globally regular expression version, and its use rights are for all users.
2. Format
grep [Options]
3. Main parameters
[Options] Main parameters:
-C: Outputs only the count of matching rows.
-I: Case insensitive (only for single-character).
-H: The file name is not displayed when querying multiple files.
-L: Only file names that contain matching characters are output when querying multiple files.
-N: Displays matching lines and line numbers.
-S: does not display error messages that do not exist or have no matching text.
-V: Displays all lines that do not contain matching text.
Pattern Regular Expression Main parameters:
\: Ignores the original meaning of special characters in regular expressions.
^: matches the start line of the regular expression.
$: Matches the end line of the regular expression.
\: The end of the line to match the regular expression.
[]: A single character, such as [a], a meets the requirements.
[-]: range, such as [A-z], i.e. A, B, C to Z all meet the requirements.
。 : all the individual characters.
*: There are characters, the length can be 0.
Is
The expression is a very important concept in the Linux/unix system. A regular expression (also known as "regex" or "regexp") is a pattern that can describe a class of strings
(Pattern). If a string can be described with a regular expression, we say that the character matches the regular expression (match). This and DOS users can use wildcard characters
"*" stands for any character similar. On Linux systems, regular expressions are often used to find patterns in text, and to perform "search-and-replace" operations and other functions on text.
4. Application examples
Check
Consulting the DNS service is one of the daily tasks, which means maintaining a large number of IP addresses that cover different networks. Sometimes there are more than 2000 IP addresses. If you want to see the NNN.NNN network address, but forget
The remainder of the second part is only known to have two periods, such as nnn nn ... To extract all of the nnn.nnn IP addresses, use [0-9]\{3
\}\. [0-0\{3\}\. The meaning is that any number appears 3 times, followed by a period, followed by any number 3 times, followed by a period.
$grep ' [0-9]\{3 \}\. [0-0\{3\}\ ' Ipfile
To add, the grep family also includes Fgrep and Egrep. Fgrep is fix grep, which allows to find strings instead of a pattern; Egrep is an extended grep that supports both basic and extended regular expressions, but does not support the application of \q mode scope and some of the more canonical patterns corresponding to it.
Dd
1. Role
The DD command is used to copy the file and convert and format the data according to the parameters.
2. Format
DD [Options]
3.[opitions] Main parameters
bs= bytes: Forced ibs= and obs=.
cbs= Bytes: Each conversion is specified.
conv= Keyword: Converts a file based on a comma-delimited representation of a keyword.
count= number of blocks: only the specified input data is copied.
ibs= bytes: Read each time the specified.
if= file: Reads the content rather than the standard input data.
obs= Bytes: Writes each time the specified.
of= file: Writes data instead of the standard output display.
Number of seek= blocks: The specified output data in Obs is skipped first.
skip= number of blocks: Skip the specified input data in IBS first.
4. Application examples
The DD command is often used to make Linux boot disks. Find a bootable kernel, point its root device to the correct root partition, and then use the DD command to write it to the floppy disk:
$ Rdev Vmlinuz/dev/hda
$DD If=vmlinuz of=/dev/fd0
The code above shows that using the Rdev command to point the root device in the bootable kernel vmlinuz to/dev/hda, replace "HDA" with its own root partition, and then write the kernel to a floppy disk using the DD command.
Find
2. Use
The function of the Find command is to search the directory for a file, and its use rights are for all users.
2. Format
Find [Path][options][expression]
path Specifies the directory path from which the system starts to look down the file down the directory tree. It is a list of paths that are separated from each other by spaces, and if you do not write path, the current directory is assumed.
3. Main parameters
[Options] Parameters:
-depth: Use the Depth-level lookup process to prioritize file content in a specific level of the specified directory.
-maxdepth levels: Indicates that at most find the first level subdirectory of the start directory. Level is a non-negative number, and if level is 0, it is only found in the current directory.
-mindepth levels: Indicates that at least the level subdirectory of the start directory is found.
-mount: Not found in directories and files in other file systems (such as MSDOS, VFAT, etc.).
-version: Print version.
[Expression] is a matching expression and is an expression accepted by the Find command, and all operations of the Find command are for an expression. It has a lot of parameters, and here are just a few common parameters.
-name: Supports wildcard characters * and?.
-atime N: Searches for files that have been read in the last n days.
-ctime N: Searches for files that have been modified in the last n days.
-group Grpoupname: Search for all files with Grpoupname group.
-user User name: Searches for all files that belong to the primary user name (ID or name).
-size N: The file size of the search file is n blocks.
-print: Output search results, and print.
4. Application Tips
The Find command finds several ways to locate a file:
(1) Search by file name
For example, if we want to find a file with a filename of lilo.conf, you can use the following command:
Find/-name lilo.conf
The "/" after the Find command means that the entire hard disk is searched.
(2) Quickly find files
Root
Finding files by file names can have a real problem, which is to take a long time, especially when large Linux file systems and high-capacity hard disk files are placed in deep sub-directories. If we know,
This file is stored in a directory, so you can save a lot of time simply by looking down in this directory. such as the smb.conf file, from its file suffix ". conf" can be judged as a match
file, it should be in the/etc directory, at which point the following command can be used:
Find/etc-name smb.conf
This way, you can shorten the time by using the Quick Find file method.
(3) Search method based on partial file name
Sometimes we know that only one file contains the 4 words of ABVD, then to find all the files in the system that contain these 4 characters, you can enter the following command:
Find/-name ' *abvd* '
After entering this command, the Linux system will look in the/directory for all files containing the ABVD 4 characters (where * is a wildcard character), such as Abvdrmyz and other eligible files can be displayed.
(4) Find a file using a hybrid lookup method
The find command can use a hybrid lookup method, for example, if we want to find a file that is larger than 500000 bytes in the/etc directory and modified within 24 hours, you can use-and (and) to link the two lookup parameters together into a mixed lookup.
Find/etc-size +500000c-and-mtime +1

14 Ways to use grep

All classes Linu All Class Linux systems will provide a search tool called grep (globalregular expression Print, global regular expression output). The grep command is useful in situations where a pattern-based search is performed on the contents of one or more files. A pattern can be a single character, multiple characters, a single word, or a sentence.
When the command matches the pattern specified when the command is executed, grep outputs a line containing the pattern, but does not modify the contents of the original file.
In this article, we will discuss 14 examples of GREP commands.
Example 1 finding a pattern in a file (word)
Find the word "Qiegao" in the/etc/passwd file
grep "Qiegao"/etc/passwd
Example 2 finds a pattern in multiple files.
grep "Qiegao"/etc/passwd/etc/shadow/etc/gshadow
Example 3 uses the-l parameter to list the file name of the file that contains the specified pattern.
Grep-l Qiegao/etc/passwd/etc/shadow/etc/fstab
Example 4 uses the-n parameter to find the specified pattern in the file and displays the line number of the matching row
Grep–n "Qiegao"/etc/passwd
Example 5 using the-v parameter to output rows that do not contain a specified pattern
Output all lines in the/etc/passwd file that do not contain the word "Qiegao"
Grep–v "Qiegao"/etc/passwd
Example 6 using the ^ symbol to output all lines beginning with a specified pattern
The bash script sees the ^ symbol as a special character used to specify the beginning of a line or a word. For example, all lines that start with "root" in the output/etc/passes file
grep "^root"/etc/passwd
Example 7 uses the $ symbol to output all lines ending in the specified pattern.
Output all lines in the/etc/passwd file that end with "bash".
grep "bash$"/etc/passwd
The bash script sees the dollar ($) symbol as a special character used to specify the end of a line or a word.
Example 8 using the-R parameter to recursively find a specific pattern
Grep–r "Qiegao"/etc
The above command will recursively look for "Qiegao" words in the/etc directory
Example 9 using grep to find all empty lines in a file
grep "^$"/etc/shadow
Because there are no blank lines in the/etc/shadow file, there is no output
Example 10 using the-I parameter lookup mode
The-i parameter of the grep command ignores the case of characters when looking.
Let's look at an example to find the word "Qiegao" in the paswd file.
Grep–i "Qiegao"/etc/passwd
Example 11 finding multiple modes using the-e parameter
For example, I want to find the ' Qiegao ' and ' root ' words in a grep command, and with the-e parameter, we can find multiple patterns.
Grep-e "Qiegao"-E "root"/etc/passwd
Example 12 using the-F file to specify the mode to find
First, create a search mode file "Grep_pattern" in the current directory, and I want to enter the following content in the file.
Cat Grep_m
^qiegao
Root
false$
Now, try searching using the Grep_pattern file
Grep-f grep_m/etc/passwd
Example 13 using the-c parameter to calculate the number of patterns to match
Continuing with the example above, we use the-C command in the grep command to calculate the number of matches for the specified pattern
Grep-c-F GREP_M/ETC/PASSWD
Example 14 output matches the front or back n rows of a specified pattern line
A) Use the-b parameter to output the first 4 rows of the matching row
Grep-b 4 "Qiegao"/etc/passwd
b) Use the-a parameter to output the following 4 rows of the matching row
Grep-a 4 "Qiegao"/etc/passwd
c) Use the-c parameter to output the front and back 4 rows of the matching row
Grep-c 4 "Qiegao"/etc/passwd

This article is from the "Live to learn old" blog, please be sure to keep this source http://nihaozqy.blog.51cto.com/10511002/1692846

Linux File processing commands

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.