LinuxShell script introduction-Chapter 3 name a file

Source: Internet
Author: User

3.1 generate files of any size

1. The simplest way to create a file of a specific size is to use the dd command. The dd command clones the specified input content and writes an identical copy to the output.

2 stdin, device files, common files, etc. can be used as input, stdout, device files, common files, etc. can also be used as output

Example 3: dd if =/dev/zero of = data bs = 1 M count = 1

This command will create a 1 MB file data in the face-to-face directory. Let's take a look at the parameter. if indicates the input file, of indicates the output file, and bs indicates the block size in bytes, count indicates the number of blocks to be copied.

4. We set bs to 1 MB and count to 1, so we get a file with a size of 1 MB. If we set count to 2, we will get a file with 2 MB.

5/dev/zero is a character device that continuously returns 0 bytes (\ 0)

6 if no input parameter is specified, the input is read from stdin by default. similar to this, if no output parameter is specified, stdout is used as the output by default.

 

3.2 intersection and difference set of text files

1 comm command can be used to compare two files. It has many good options to adjust the output.

2. Example: comm fileA fileB

The first output column contains the row in fileA, the second column contains the row in fileB, and the third column contains the same row in fileA and fileB.

Some options can be formatted and output as needed

-1. Delete the first column from the output.

-2 Delete the second column from the output

-3. Delete the third column from the output.

 

3.3 find and delete duplicate files

1. duplicate files refer to files with the same name but identical content

2. the checksum is based on the content of the file. The Checksum is the same for files with the same content.

 

3.4 create a long path directory

1 mkdir command is used to create a directory. If the directory already exists, an error message is returned.

2-e is a parameter in the condition judgment []. It can be used to determine whether a file exists. in UNIX-like, a directory is also a file.

 

3.5 file permissions and ownership

1. A user is the file owner and a user group is a collection of multiple users. The system allows these users to access files in some form.

2. Run the ls-l command to list the object permissions.

For example,-rw-r -- 1 chenguolin 2497 hehehe.txt

3. The first column contains a total of 10 characters. The first character indicates the file type, '-' indicates a common file, 'D' indicates a directory file, and 'C' indicates a device.

The remaining 9 characters can be divided into three groups, each with three characters. The three characters in the first group are user permissions, the three characters in the second group are user group permissions, and the three characters in the third group are other users.

Permission sequence rwx, indicating read and write execution permissions respectively

4. The directory also has read and write permissions, but the directory is different from the normal file.

Directory read permission r allows reading the list of directory files and subdirectories

Directory write permission w. Allow creating or deleting files or directories in the directory

Directory execution permission indicates that files and subdirectories in the directory can be accessed

5. To set file permissions, run the chmod command.

Chmod u = rwx g = rw o = r filename

Here: u refers to user permissions, g refers to user group permissions, and o refers to entity permissions of others.

If you want to add permissions to files, you can add users, user groups, and other users, and use-to delete permissions.

You can also set permissions in octal. The permissions are represented by three digits. Each digit corresponds to the user, user group, and other users in order.

R corresponds to 4, w corresponds to 2, and x corresponds to 1

6. We can use chown to change the ownership of the file.

Chown user. group filename // change the file filename owner to user, and the user group is

7. You can use the following method to modify permissions of all files and subdirectories in the current directory recursively.

Chmod 777 path-R // in this example, recursively modify the permissions of all files in the path directory to 777

8 similarly, we can use-R to recursively modify the file owner.

Chown user. group path-R // This command recursively modifies the owner and user group of all files in the path directory.

 

3.6 create files that cannot be modified

1. Once a file is set as unchangeable, no user, including a Super User, can delete the file unless its unmodifiable attributes are removed.

2. You can use chattr to disable file modification.

Sudo chattr + I file // The file becomes unmodifiable.

3. If you need to make the file writable, use sudo chattr-I file to modify the permission.

3.7 generate blank files in batches

1 touch command can be used to generate a blank file. If the file exists, you can use it to modify the Time of the file.

2 touch file // if the file does not exist, create a file. Otherwise, modify the timestamp of the file directly.

3. If you only want to modify some timestamps, you can use the following options:

Touch-a only changes the file access time

Touch-m only changes the file content modification time

 

3.8 search for symbolic links and their targets

A symbolic link is just a pointer to other files. It functions like an alias in Mac OS or a shortcut in windows.

2. You can create a link as follows:

Ln-s file link // This command creates a connection link for the file

 

3.9 list statistics on file types

1. We can use the file command to print the file type.

File filename // you can print the file type of filename.

 

3.10 loop back file and mounting

1. To use the File System on the storage device, we need to mount it to directories called Mount Points.

2. You can use the dd command to create a large file. Then, you can use the mkfs command to format the device.

Mkfs. ext4/dev/sdb1 // This command is used to format/dev/sdb1 into the ext4 file type.

3. We can use mount to mount

Mount/dev/sdb1/mnt/usb // mount/dev/sdb1 to/mnt/usb

4. Use the following method to uninstall

Umount/dev/sdb1 or umount/mnt/usb

Umount is a privileged command, so it must be executed as a Super User.

 

3.11 search for file differences and fix them

1. We can use patch to change the file information.

The 2 diff command can be used to compare two files. The most common usage is diff fileA fileB.

We can also use the-u option for integrated output diff-u fileA fileB

3. In the integrated output, the newly added row starts with "+" and the row starts "-".

Diff-u fileA fileB> patch // This command redirects the difference between the two files to the patch file

4. Use the following command to fix

Patch-p1 file <patch // This command can update the file differently.

If you want to undo the modification, run the same command.

3.12 head and tail

The 1 head command always reads the first 10 lines of the head file print file of the input file.

2 seq commands can be used to quickly generate a sequence seq 11 to generate 1 ~ Sequence of 11

3. Run the tail command to print the last 10 lines of tail file.

 

3.13 only list Directories

First: ls-d * // only the above-d combination can print out the Directory

Type 2: ls-F | grep "/$" // when-F is used, a character representing the file type is added to all output items.

Type 3: ls-l | grep "^ d" // each line output by ls-l, and then use grep to find that the first character is d

Fourth: the find path-maxdepth 1-type d-print // find command specifies the maximum search depth and file type

3.14 number of lines, word count, and character count of the Statistical File

1 wc is a statistical tool, short for word count

2. Number of Statistics rows: wc-l file

Count words: wc-w file

Number of statistical characters: wc-c file

Wc file prints the number of lines of files by default, and the number of words and characters

3 wc file-L can be used to print the maximum length of a row.

3.15 print directory tree

1. The tree Command prints the main character of files and directories in a graphical tree structure. This command is usually not required in the Linux hairstyle version.

2 tree path // This command can be directly displayed in the tree

3. output directory tree in HTML Format

Tree path-H http: // localhost-o out.html

Replace http: // localhost with the URL suitable for storing the output file, replace path with the actual path, and then generate a directory tree for the web version.

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.