CentOS file management and related commands

Source: Internet
Author: User

CentOS file management and related commands
Linux Introduction

Strictly speaking, Linux is not an operating system, but a kernel in a Linux system, that is, a platform for communication between computer software and hardware. The full name of Linux is GNU/Linux, this is a real Linux system. GNU is a project organized by Richard Stallman. programmers from all over the world can modify the GNU program and follow the GPL protocol to allow any changes. However, the modified procedure must follow the GPL agreement.

Linux is a multi-user, multi-task operating system and a free software. It is fully compatible with POSIX standards and has a good user interface. It supports a variety of processor architectures and is easy to transplant.

The software that allocates system resources to programs and processes the computer's internal details is called the operating system or kernel.

The user interacts with the Linux kernel through Shell. Shell is a command line interpretation tool (software) that converts user-input commands into languages (commands) that the kernel can understand ).

In Linux, a lot of work is done through commands. To learn about Linux, you must first master Common commands.

Introduction

All data in Linux is stored in files, and all files are allocated to different directories. A directory is a tree-like structure called a file system. File System reference: http://www.cnblogs.com/0201zcr/p/4773995.html

When you use Linux, you will deal with files most of the time. Through this section, you can understand basic file operations, such as creating files, deleting files, copying files, renaming files, and creating links to files.

In Linux, there are three basic file types:

1) common files

Common files are data streams in bytes, including text files, source code files, and executable files. There is no difference between text and binary in Linux. The interpretation of common files is performed by the application that processes the file.

2) Directory

A directory can contain common files and special files. The directory is equivalent to a folder in Windows and Mac OS.

3) Device Files

Some tutorials refer to special files as a meaning. Linux communicates with external devices (such as optical drives, printers, terminals, and modern) through a file called a device file. Linux inputs and outputs to an external device are the same as input and output to a file. Before Linux communicates with an external device, a device file must exist.

For example, each terminal has its own device file for Linux to write data (displayed on the terminal screen) and read data (input through the keyboard ).

The device file is different from a common file. The device file does not contain any data.

There are two types of device files: character device files and block device files.

  • The character device file starts with the letter "c. When a character device file transfers data to a device, one character is transferred at a time. Typical devices that transmit data by character include terminals, printers, mappers, and modern. Character device files are also called "raw" device files.
  • The block device file starts with the letter "B. When a block device file transfers data to a device, it first reads or writes data from the buffer in the memory, rather than directly transfers data to the physical disk. A disk and a CD-ROMS can use either a character device file or a block device file.
View files

You can use the ls command to view files and directories in the current directory, for example:

$lsbin        hosts  lib     res.03ch07       hw1    pub     test_resultsch07.bak   hw2    res.01  usersdocs       hw3    res.02  work

You can use the-l option of the ls command to obtain more file information, for example:

$ls -ltotal 1962188drwxrwxr-x  2 amrood amrood      4096 Dec 25 09:59 uml-rw-rw-r--  1 amrood amrood      5341 Dec 25 08:38 uml.jpgdrwxr-xr-x  2 amrood amrood      4096 Feb 15  2006 univdrwxr-xr-x  2 root   root        4096 Dec  9  2007 urlspedia-rw-r--r--  1 root   root      276480 Dec  9  2007 urlspedia.tardrwxr-xr-x  8 root   root        4096 Nov 25  2007 usrdrwxr-xr-x  2    200    300      4096 Nov 25  2007 webthumb-1.01-rwxr-xr-x  1 root   root        3192 Nov 25  2007 webthumb.php-rw-rw-r--  1 amrood amrood     20480 Nov 25  2007 webthumb.tar-rw-rw-r--  1 amrood amrood      5654 Aug  9  2007 yourfile.mid-rw-rw-r--  1 amrood amrood    166255 Aug  9  2007 yourfile.swfdrwxr-xr-x 11 amrood amrood      4096 May 29  2007 zlib-1.2.3$

The meaning of each column is as follows:

  • Column 1: file type.
  • Column 2: Number of files. If it is a file, it is 1; if it is a directory, it is the number of files in the directory.
  • Column 3: The object owner, that is, the object creator.
  • Column 4: User Group of the file owner. In Linux, each user belongs to a user group.
  • Column 5: file size (in bytes ).
  • Column 6: time when the file was created or last modified.
  • Column 7: file name or directory name.


Note: Each directory has a subdirectory "." pointing to it and a subdirectory ".." pointing to its parent directory. Therefore, for an empty directory, the second column should be 2.

For files listed through ls-l, each line starts with a, d,-, or l. These characters indicate the file type:

PrefixDescription-Common files. Such as text files, binary executable files, and source code. Block B device files. You can use block device files on the hard disk. C character device file. The hard disk can also use character device files. D directory file. A directory can contain files and other directories. L Symbolic Link (soft link ). You can link any common file, similar to the shortcut in Windows. P named pipe. A pipe is a communication mechanism between processes. S is the socket used for inter-process communication.


Tip: In layman's terms, a soft connection is a windows shortcut. The original file is deleted, but the shortcut does not work.

Metacharacters

Metacharacters are special characters. * And? All are metacharacters:

  • * It can match 0 or multiple arbitrary characters;
  • ? Match a character.


For example

$ls ch*.doc

All files starting with ch and ending with. doc can be displayed:

ch01-1.doc   ch010.doc  ch02.doc    ch03-2.docch04-1.doc   ch040.doc  ch05.doc    ch06-2.docch01-2.doc ch02-1.doc c

Here, * matches any character. If you want to display all files ending with. doc, you can use

$ls *.doc

Hide a file

The first character of the hidden file is the English periods (.). Linux programs (including Shell) usually use the hidden file to save configuration information.

Below are some common hidden files:

. Profile: Bourne shell (sh) initialization script. kshrc: Korn shell (ksh) initialization script. cshrc: C shell (csh) initialization script. rhosts: Remote shell (rsh) configuration file


To view hidden files, use the-a option of the ls command:

$ ls -a.         .profile       docs     lib     test_results..        .rhosts        hosts    pub     users.emacs    bin            hw1      res.01  work.exrc     ch07           hw2      res.02.kshrc    ch07.bak       hw3      res.03$

A period (.) indicates the current directory, and two periods (.) indicates the upper-level directory.

Note: When you enter the password, the asterisk (*) is used as a placeholder, representing the number of characters you enter.

Create a file

In Linux, you can use the vi editor to create a text file, for example:

$ vi filename

The above command will create the file filename and open it. Press the I key to enter the editing mode. You can write content to the file. For example:

This is Linux file....I created it for the first time.....I'm going to save this content in this file.

After editing, you can press esc to exit the editing mode, or press Shift + ZZ to exit the file completely. In this way, the file is created.

$ vi filename$

Edit File

The vi editor can be used to edit files. Due to space limitations, we will only give a brief introduction here, which will be explained in detail in the following sections.

You can open a file named filename as follows:

$ vi filename

After the file is opened, you can press the I key to enter the editing mode and edit the file as needed. To move the cursor, you must first Press esc to exit the editing mode, and then use the following buttons to move the cursor in the file:

  • L move the key to the right
  • H key moves to left
  • K key move up
  • J key move down


You can use the buttons above to quickly locate the cursor where you want to edit it. After the cursor is located, press I to enter edit mode again. After editing, Press esc to exit the editing mode or press Shift + ZZ to exit the current file.

View File Content

You can use the cat command to view the file content. The following is a simple example:

$ cat filenameThis is Linux file....I created it for the first time.....I'm going to save this content in this file.$

You can use the-B option of the cat command to display the row number, for example:

$ cat -b filename1   This is Linux file....I created it for the first time.....2   I'm going to save this content in this file.$

Count the number of words

You can use the wc command to count the number of lines, words, and characters of the current file. The following is a simple example:

$ wc filename2  19 103 filename$

The meaning of each column is as follows:

  • Column 1: Total number of objects
  • Column 2: Number of words
  • Column 3: the number of bytes of the file, that is, the file size.
  • Column 4: File Name


You can also view the content of multiple files at a time, for example:

$ wc filename1 filename2 filename3

Copy a file

You can use the cp command to copy files. The basic syntax of the cp command is as follows:

$ cp source_file destination_file

The following example copies the filename file:

$ cp filename copyfile$

Now a copyfile file identical to filename will be added to the current directory.

Rename a file

You can use the mv command to rename a file. Syntax:

$ mv old_file new_file

The following example renames the filename file to newfile:

$ mv filename newfile$

Currently, there is only one newfile in the current directory.

The mv command is actually a command for moving files. It can not only change the file path, but also change the file name.

Delete an object

The rm command can delete files. Syntax:

$ rm filename

Note: deleting a file is dangerous because the file may contain useful information. We recommend that you use the-I option to use the rm command.

In the following example, a file is permanently deleted:

$ rm filename$

You can also delete multiple objects at a time:

$ rm filename1 filename2 filename3$

Standard Linux stream

Generally, each Linux program runs three file streams (three files ):

  • Standard input stream (stdin): The file descriptor of stdin is 0, and the Linux program reads data from stdin by default.
  • Stdout: The file descriptor of stdout is 1, and the Linux program outputs data to stdout by default.
  • Standard Error stream (stderr): The file descriptor of stderr is 2, and the Linux program writes error information to the stderr stream.

Thank you! Thank you for your patience!

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.