linux file management and its related instructions

File Info

Users interact with the kernel by Shell Linux. Shell is a command line interpreter tool (a software), it will command the user enters into a language (command) the kernel can understand. Under Linux, a lot of work is done by the command, to learn Linux, one must master the commonly used commands.

All data in Linux are saved in a file, all files are assigned to a different directory. A directory is a tree-like structure , known as the file system.
  When you use Linux, most of the time and will to deal with files, by this section for basic file operations, such as creating files, delete files, copy files, rename files, and create links to files.

1, view the file ls

View the current file and directory in the directory can 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

(1), by LS the -l option command file for more information

$ Ls - L 
Total 1962188 

Drwxrwxr -x 2 Amrood Amrood 4096 Dec 25 09:59 Uml
 -rw-Rw-R-- Amrood L Amrood 534l Dec 25 08:38 Umlkjpg 
Drwxr -cshr-q 2 Guava Guava 4096 Feb 15 2006 Univ 
Drwxr -cshr X-2 Root Root 4096 Dec 9 2007 Urlspedia
 -rw-r - R-- Root L Root 276480 Dec 9 2007 Urlspediaktar 
Drwxr -cshr X-8 Root Root 4096 Nov 25 2007 Usr 
Drwxr -cshr -x 2 200 300 4096 Nov 25 2007 webthumb-1.01 
-rwcshr-Cshr-x 1 root root 3192 Nov 25 2007 Webthumbkfp
-rw-Rw-R-- 1 amrood amrood 20480 Nov 25 2007 Webthumbktr
 -rw-Rw-R-- 1 amrood amrood 5654 Aug 9 2007 Yourfilekmid
 -rw-Rw-R-- 1 amrood amrood 166255 Aug 9 2007 Yourfilekswf 
Drwxr -cshr-x 11 amrood amrood 4096 May 29 2007 zlib-1.2.3

The meaning of each column is as follows:

  • The first column: file type.
  • The second column: Indicates the number of files. If the file is, so it is 1; if it is a directory, then that is the number of the files in the directory.
  • The third column: the owner of the file, that file's creator.
  • The fourth column: user group owner of the file is located. In Linux, each user belongs to a group of users.
  • Fifth column: file size (in bytes).
  • Sixth column: the file was created or last modified time.
  • Seventh column: filename or directory name.

Note : Each directory has a link to its own subdirectory of the parent directory and its subdirectories point "..", "." So for an empty directory, the second column should be 2.
By ls -l file listed in each row is d, a, - or the beginning of the L, the characters indicating the file type:

  • -: normal file . Such as text files, binary executable files, source code and so on.
  • b: block device file. Hard block device file can be used.
  • c: character device file. You can also use the hard disk character device files.
  • d: directory file . Directory can contain files and other directories.
  • l: symbolic link (soft link). Can be linked to any ordinary files, shortcuts, Windows is similar. Delete the original files, shortcuts, although it does not work.
  • p: named pipe. Pipeline is an inter-process communication mechanism.
  • s: a socket for interprocess communication.

(2) three basic file types

  1. Normal file: the data stream is a normal file in bytes, including text files, source code files, executable files and the like. There is no difference between text and binary for Linux, the common interpretation of documents by the application processes the file.
  2. Directory: The directory can contain ordinary files and special files, directories, equivalent to the Windows and Mac OS folder.
  3. Device files: Some special file called tutorial, is a meaning. Linux with an external device (e.g., optical drives, printers, terminals, etc. AND ECONOMICS) the device file is referred to by a method for communication. Linux input mode and output to the external input device to a file in the same way. Before communications and a Linux external device, the device must first have an existing device file. For example, each terminal has its own device file for Linux to write data and the read data (user input via keyboard) (appears on the terminal screen). Device files and not the same as a normal file, device file does not contain any data.
      Device files There are two types : character device file and block device file.

Character device file with the letter begins with "c" . Character device file to the device to transmit data, a transmission of a character . A typical character by transmitting data terminal equipment, printers, plotters, and other AND ECONOMICS. Character device file is sometimes called the "raw" device file.

Block device file with the letter "b" at the beginning . Block device file, when the device transmits data to start memory read or write data in the buffer, instead of transferring data directly to the physical disk . Disks and CD-ROMS can either use the character device file can also use the block device file.

(3), meta characters *?

Metacharacter is a character that has special meaning. ? * And are metacharacters:

  • * Matches zero or more arbitrary characters;
  • ? Matches one character.

E.g

$ ls ch*.doc

Show all begin with ch, ending with .doc file:

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 single character. If you want to show all files ending in .doc, you can use

$ ls *.doc

(4), hidden files ls -a

Hidden files first character periods or dot (.) , Linux program (including Shell) typically use a hidden file to save the configuration information.
Here 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

View hidden files need to use the ls command -a option:

$ 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 (.) Represents the current directory, two dots (..) represents the parent directory

Note: When you enter a password, an asterisk (*) as a placeholder for the number of characters you entered.

2, create a file, edit the file vi

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

$ vi filename

The above command creates and opens the file filename and press the i button to enter edit mode, you can write to the file. E.g:

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

When you finish editing, you can press the esc key to exit edit mode , you can press the key combination Shift + ZZ completely out of the file . This completes the creation of the file.

$ vi filename

vi editor to edit the file. as follows

You can open a file called filename:

$ vi filename

When the file is opened, you can press i to enter edit mode, edit the file in its own way. To move the cursor, you must press esc to exit the edit mode, and then use the arrow keys or below the machine to move the cursor within the file:

  • l keys to move to the right
  • h keys to move left
  • k keys to move up
  • j to move downwardly

Using the above button, you can quickly locate the cursor to where you want to edit. After locating a good cursor and press i to enter edit mode again. After editing is completed, press esc to exit edit mode or press the key combination Shift + ZZ exit the current file.

3, view the file contents cat

You can use the cat command to view the contents of the documents, the following 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.

By cat to display line numbers -b option 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.
$

4, the number of statistical word wc

The number of rows can use the wc command to count the current document, words, and characters, here's a simple example:

$ wc filename
2  19 103 filename

The meaning of each column is as follows:

  • The first column: total number of rows of file
  • The second column: the number of words
  • Third column: number of bytes of the file, i.e., the file size
  • The fourth column: the file name

Also you can view the contents of multiple files at once, for example:

$ wc filename1 filename2 filename3

5. Copy files cp

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

$ cp source_file destination_file

The following example will copy the file filename:

$ cp filename copyfile

  Now in the current directory and filename will be more of a copyfile exactly the same file.

6, rename the file mv

mv command is actually a command to move a file, not only can change the path to the file, you can also change the file name.

Rename files using mv command syntax is:

$ mv old_file new_file

The following example will rename the file filename newfile:

$ mv filename newfile

Now in the current directory, only a newfile file.

7, delete the file rm

rm command to delete a file, the syntax is:

$ rm filename

Note: deleting files is a dangerous behavior, because it may contain useful information in the file, the proposed combination - i (each prompt before deleting) option to use 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

8, standard Linux flow

In general, each program is running Linux will create three files stream (three files):

  • Standard input stream (stdin): stdin file descriptor is 0, Linux default program data read from stdin.
  • Standard output stream (stdout): stdout file descriptor 1, Linux program default data output to stdout.
  • The standard error stream (stderr): stderr file descriptor 2, Linux program will write an error message to stderr stream.

Guess you like

Origin www.cnblogs.com/springsnow/p/12192330.html