All the data in Linux is stored in the file, and all the files are assigned to different directories. A directory is a tree-like structure called a file system.
When you use Linux, you spend most of your time working with files, and this section provides you with 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)Normal file
Normal files are data streams in bytes, including text files, source files, executable files, and so on. There is no difference between text and binary for Linux, and the interpretation of ordinary files is done by the application that handles the file.
2)Catalogue
Directories can contain normal files and special files, and directories are equivalent to folders in Windows and Mac OS.
3)Device Files
Some of the tutorials called special files are a meaning. Linux and external devices (such as optical drives, printers, terminals, modern, etc.) are communicated through a file called a device file . The way the Linux input and output to an external device is the same as the input and output to a file. Before Linux communicates with an external device, the device must first have a device file present.
For example, each terminal has its own device files for Linux to write data (appearing on the terminal screen) and read data (the user enters through the keyboard).
Device files are not the same as normal files, and the device files do not contain any data.
There are two types of device files: Character device files and block device files.
- The character device file begins with the letter "C". When a character device file transmits data to the device, one character is transferred at a time. Typical devices that transmit data via characters are terminals, printers, plotters, modern, and so on. Character device files are also sometimes referred to as "raw" device files.
- The block device file begins with the letter "B". When a block device file transmits data to a device, it first reads or writes data from the in-memory buffer, rather than transferring the data directly to the physical disk. Disk and CD-ROMs can use both character device files and block device files.
View Files
to view files and directories under the current directory, you can use the ls command , for example:
$lsbin hosts Lib res.03ch07 hw1 pub test_resultsch07.bak hw2 res.01 Usersdocs hw3 res.02 work
With the- l option of the ls command, you can get more file information, such as:
$ls-ltotal 1962188drwxrwxr-x 2 amrood amrood 4096 Dec 09:59 uml-rw-rw-r-- 1 amrood amrood 5341 Dec 25 08:38 uml.jpgdrwxr-xr-x 2 amrood amrood 4096 Feb 2006 Univdrwxr-xr-x 2 root root 4096 Dec 9 urlspedia-rw-r--r-- 1 root root 276480 Dec 9 2007 Urlspedia.tardrwxr-xr-x 8 root root 4096 Nov usrdrwxr-xr-x 2 300 4096 webthumb-1.01-rwxr-xr-x 1 root root 3192 Nov 2007 webthumb.php-rw-rw-r-- 1 amrood amrood 20480 Nov webthumb.tar-rw-rw-r-- 1 Amrood amrood 5654 9 yourfile.mid-rw-rw-r-- 1 amrood amrood 166255 9 2007 Yourfile.swfdrwxr-xr-x Amrood amrood 4096 may zlib-1.2.3$
Each column has the following meanings:
- First column: File type.
- Second column: Indicates the number of files. If it is a file, then it is 1; if it is a directory, it is the number of files in that directory.
- The third column: The owner of the file, the creator of the file.
- Fourth column: The user group where the file owner resides. In Linux, each user is affiliated to a user group.
- Fifth Column: File size (in bytes).
- Column Sixth: The time when the file was created or was last modified.
- Seventh column: File name or directory name.
Note: Each directory has a subdirectory "." and a sub-directory pointing to its parent directory ".", so for an empty directory, the second column should be 2.
by Ls-l The files listed, each line begins with a, d,-or L, which represent the file type:
| prefix |
Description |
| - |
Normal file. such as text files, binary executables, source code, and so on. |
| B |
Block device files. The hard disk can use block device files. |
| C |
Character device files. The hard disk can also use character device files. |
| D |
Catalog files. Directories can contain files and other directories. |
| L |
Symbolic links (soft links). You can link any normal file, similar to a shortcut in Windows. |
| P |
Named pipes. Pipelines are a communication mechanism between processes. |
| S |
A socket for interprocess communication. |
Hint: The popular talk soft connection is the Windows shortcut, the original file is deleted, although the shortcut is not working.
Metacharacters
A meta-character is a character with a special meaning. * and? are meta characters:
- * can match 0 or more of any character;
- ? Matches one character.
For example
$ls Ch*.doc
You can display all files that begin with CH and end with. Doc:
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 one character. If you want to display all files that end in. doc, you can use the
$ls *.doc.
Hide File
The first character of a hidden file is a period or dot (.), and Linux programs (including the shell) typically use hidden files to save configuration information.
Here are some common hidden files:
. Profile:bourne Shell (SH) init script
. Kshrc:korn Shell (ksh) Init script
. Cshrc:c Shell (CSH) Init script
. Rhosts:remote Shell (rsh) configuration file
Viewing hidden files requires 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 point number (.) Represents the current directory, two point numbers (..) Indicates a parent directory
Note: When entering a password, the asterisk (*) is used as a placeholder for 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 to the file. For example:
This is Linux file .... I created it for the first time ..... I ' m going to save the content in this file.
When you are finished editing, you can press the ESC key to exit edit mode or press the key combination SHIFT + ZZ to exit the file completely. This completes the creation of the file.
$ VI filename$
Edit File
The vi Editor can be used to edit text pieces. Due to space limitations, this is a brief introduction and will be explained in more detail later in this chapter.
You can open a file named filename as follows:
$ VI filename
When the file is opened, you can press the I key to enter edit mode and edit the file in your own way. If you want to move the cursor, you must first press the ESC key to exit edit mode, and then use the following keys to move the cursor within the file:
- L Key Move Right
- H Key moves left
- K Key to move up
- J Key to move Down
Using the buttons above, you can quickly position the cursor where you want to edit it. After positioning the cursor, press the I key to enter edit mode again. Press ESC to exit edit mode or press the key combination shift+zz to exit the current file after editing is complete.
View File Contents
You can use the Cat command to view the contents of a file, here is a simple example:
$ cat filenamethis is Linux file .... I created it for the first time ..... I ' m going to save the content in this file.$
line numbers can be displayed using the- B option of the Cat command , for example:
$ cat-b filename1 This is a Linux file .... I created it for the first time ..... 2 I ' m going to save the content in this file.$
Count the number of words
You can use the WC command to count the number of rows, words, and characters of the current file , and here is a simple example:
$ WC filename2 103 filename$
Each column has the following meanings:
- First column: Total number of rows in a file
- Second column: number of words
- Column three: The number of bytes in a file, that is, the size of the file
- Fourth column: File name
You can also view the contents of multiple files at once , for example:
$ WC filename1 filename2 Filename3
Copying files
You can use the CP command to copy files . The basic syntax for the CP command is as follows:
CP Source_file Destination_file
The following example will copy the filename file:
$ CP filename copyfile$
Now there is a copyfile file in the current directory that is identical to filename.
Renaming files
To rename a file, you can use the mv command with the following syntax:
MV Old_file New_file
The following example renames the filename file to NewFile:
$ MV FileName newfile$
Now in the current directory, there is only one newfile file.
The MV command is actually a command to move a file, not only to change the path of the file, but also to change the filename.
deleting files
The RM command can delete a file with the following syntax:
$ RM filename
Note: Deleting a file is a risky behavior because the file may contain useful information, and it is recommended that you combine the-i option with the RM command.
The following example will completely delete a file:
$ RM filename$
You can also delete multiple files at once:
rm filename1 filename2 filename3$
Standard Linux Streams
In general, each Linux program will create three file streams (three files) when it runs:
- Standard input stream (stdin): stdin file descriptor for 0,linux program reads data from stdin by default.
- Standard output stream (STDOUT): StdOut file descriptor for 1,linux program outputs data to stdout by default.
- standard error Stream (stderr): The stderr file descriptor for 2,linux program writes an error message to the stderr stream .
Linux Learning series 2--file system