Linux file management

All data in Linux is kept in files and all files are allocated to different directories. A directory is a tree-like structure called a file system.

When you use Linux, you will be working with files most of the time, use this section to learn about basic file operations such as creating files, deleting files, copying files, renaming files, and creating links to files, etc.

In Linux, there are three basic file types :
1) Ordinary files

Ordinary 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, the interpretation of a normal file is done by the application that processes the file.
2) Directories

Directories can contain common files and special files, and directories are equivalent to folders in Windows and Mac OS.
3) Device file In

some tutorials, it is called special file, which is a meaning. Linux communicates with external devices (such as optical drives, printers, terminals, modern, etc.) through a file called a device file. Linux input and output to an external device is the same as input and output to a file. Before Linux can communicate with an external device, the device must first have a device file present.

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

Unlike ordinary files, device files do not contain any data.

There are two types of device files: character device files and block device files.
Character device files begin with the letter "c". A character device file transfers data to the device one character at a time. Typical devices that transmit data through characters are terminals, printers, plotters, moderns, and so on. Character device files are also sometimes referred to as "raw" device files.
Block device files start with the letter "b". When a block device file transfers data to the device, it first reads or writes data from the buffer in memory, rather than directly transferring the data to the physical disk. Disks and CD-ROMS can use either character device files or block device files.
View files

To view files and directories in the current directory, use the ls command, for example:
$ls

bin hosts lib res.03
ch07 hw1 pub test_results
ch07.bak hw2 res.01 users
docs hw3 res.02 work
Through the -l option of the ls command , you can get more file information, for example:
$ls -l
total 1962188

drwxrwxr-x 2 amrood amrood 4096 Dec 25 09:59 uml
-rw-rw-r-- 1 amrood amrood 5341 Dec 25 08:38 uml.jpg
drwxr-xr-x 2 amrood amrood 4096 Feb 15 2006 univ
drwxr-xr-x 2 root root 4096 Dec 9 2007 urlspedia
-rw-r--r-- 1 root root 276480 Dec 9 2007 urlspedia.tar
drwxr-xr-x 8 root root 4096 Nov 25 2007 usr
drwxr-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.swf
drwxr- xr-x 11 amrood amrood 4096 May 29 2007 zlib-1.2.3
$
The meaning of each column is as follows:
First column: file type.
The second column: Indicates the 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.
Third column: The owner of the file, the creator of the file.
Fourth column: The user group of the file owner. In Linux, every user belongs to a user group.
Fifth column: file size in bytes.
Column 6: The time the file was created or last modified.
Seventh column: file name or directory name.

Note: Every directory has a subdirectory "." that points to itself and a subdirectory ".." that points to its parent directory, so for an empty directory, the second column should be 2.

For files listed by ls -l, each line begins with a, d, - or l, the characters indicating the file type:
prefix description
- Normal documents. Such as text files, binary executable files, source code, etc.
b block device file. Hard disks can use block device files.
c character device file. Hard disks can also use character device files.
d directory file. Directories can contain files and other directories.
l Symbolic links (soft links). Any normal file can be linked, similar to a shortcut in Windows.
p named pipe. A pipe is a communication mechanism between processes.
s Socket for interprocess communication.
Tip: In layman's terms, soft link is the shortcut of Windows. The original file is deleted, and the shortcut is in effect, but it does not work.
Metacharacters

Metacharacters are characters with special meaning. Both * and ? are metacharacters:
* matches zero or more arbitrary characters;
? matches one character.

For example,
$ls ch*.doc
shows all files starting with ch and ending with .doc:
ch01-1.doc ch010.doc ch02.doc ch03-2.doc
ch04-1.doc ch040.doc ch05.doc ch06- 2.doc
ch01-2.doc ch02-1.doc c
Here, * matches any character. If you want to display all files ending in .doc, use
$ls *.doc.
Hidden

files The first character of a hidden file is an English period or a period (.), and Linux programs (including Shell) usually use hidden files to save configuration information.

Here are some common hidden files:
.profile: Bourne shell (sh) init
scripts.kshrc: Korn shell (ksh) init scripts.cshrc
: C shell (csh) init
scripts.rhosts: Remote shell (rsh) configuration files

View hidden The file 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 dot (.) indicates the current directory, two The dot (..) indicates the parent directory.

Note : When entering the password, the asterisk (*) is used as a placeholder to represent 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 edit mode, and 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.
When you are done editing, you can exit edit mode by pressing esc, or you can press the combination The keys Shift + ZZ exit the file completely. This completes the creation of the file.
$ vi filename
$
edit file

The vi editor can be used to edit files. Due to space limitations, it is only briefly introduced here, and will be explained in detail in the following chapters.

You can open a file named filename as follows:
$ vi filename
When the file is opened, you can press 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 esc to exit edit mode, and then use the following keys to move the cursor within the file:
l key to move right
h key to move left
k key to move up
j key to move down

Using the above keys, you can Quickly position the cursor where you want to edit. After positioning the cursor, press the i key to enter the edit mode again. After editing, press the esc key to exit the editing mode or press the key combination Shift+ZZ to exit the current file.
To view the file content

You can use the cat command to view the file content, here is a simple example:
$ cat filename
This is Linux file....I created it for the first time.....
I'm going to save this content in this file.
$
can display line numbers with the -b option of the cat command, for example:
$ cat -b filename
1 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 in the current file, the following is a simple example:
$ wc filename
2 19 103 filename
$
The meaning of each column is as follows:
the first column: the total number of lines in the file The
second column: the number of words
The third column: the number of bytes of the file, that is, the size of the file The
fourth column: the file name

You can also view the contents of multiple files at a time, For example:
$ wc filename1 filename2 filename3
Copying files

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 will copy the file filename:
$ cp filename copyfile
$
There will now be an extra copyfile file with the same filename in the current directory.
To rename a file

, use the mv command, the syntax is:
$ mv old_file new_file
The following example will rename the file filename 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. It can not only change the path of the file, but also change the file name.
Delete files

rm command can delete files, the syntax is:
$ rm filename
Note: Deleting a file is a dangerous behavior, because the file may contain useful information, it is recommended to use the rm command with the -i option.

The following example deletes a file completely:
$ rm filename
$
You can also delete multiple files at once:
$ rm filename1 filename2 filename3
$
Standard Linux streams

Normally, each Linux program runs three file streams (three file):
standard input stream (stdin): the file descriptor of stdin is 0, and Linux programs read data from stdin by default.
Standard output stream (stdout): The file descriptor of stdout is 1, and Linux programs output 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.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327014164&siteId=291194637