Linux File Management

Source: Internet
Author: User

Linux File Management

Content outline:
# Basic concepts of file system and Shell
# File and directory operation commands
# VI usage
# Manage software packages

I. Basic concepts of file system and Shell
1. File System Description
A file system is a system software used to manage and organize data stored on a disk drive.
2. Linux File System
Linux uses Virtual File System Technology (VFS), inverted tree structure, and VFS to make Linux support the following file systems:
EXT2: Secondary Extension
EXT3: Three-Step Extension
SWAP: SWAP File System
FAT, FAT32
NTFS (not supported by default, requires a specific module)
VFAT: Virtual FAT
SYSV: Unix File System
HPFS: OS/2 File System
ISO9660: Optical Disc File System
NFS: Network File System
3. file classification in Linux
(1) common files
Text Files: use ASCII encoding, editable, and modifiable
Binary: cannot be viewed, cannot be modified
(2) Directory files
The stored content is the file name and subdirectory name in the directory.
(3) device files (/dev)
Used to access physical devices, including Block devices and character device files.
(4) Link file
Soft link file: the target file and the linked file can span the index point, which is equivalent to the file shortcut
Deleting the original file makes the symbolic link meaningless.
Deleting a symbolic link file does not affect the original file.
Hard-link file: Links files in the same index point, equivalent to copies of Files
The two files direct to the same bucket with the same content and length
Deleting a file does not affect other files.
(5) MPs queue files
FIFO cache queue
4. Main Shell functions
~ Command Line explanation
Interaction Mode
Background running
~ Command Execution Sequence
~ Command replacement ('')
~ I/O redirection (>,>,<,<)
~ MPs Queue (|)
~ Wildcard (*,?, [], [!])
~ System Environment Maintenance
~ Shell programming
~ History)
~ Auto-completion (TAB)
5. Shell Command interpretation process

Ii. File Operation commands
1. pwd
Function: displays the current directory.
Instance:
? # Pwd
2. cd
Function: changes the current directory.
Instance:
? # Cd/usr/sbin (enter/usr/sbin)
# Cd ../u1 (enter the u1 directory under the parent directory)
# Cd .. (returns the parent directory)
# Cd (go to the user's home directory)
# Cd ~ (Go to the user's home directory)
3. mkdir
Function: create a directory
Format: mkdir [parameter] <Directory Name>
Parameter:-m attribute value: Specifies the directory attributes (r, w, x, 4, 2, 1)
-P: create a directory cyclically
Instance:
# Mkdir d1 (create a d1 subdirectory in the current directory)
# Mkdir/d1/d2/d1/d3/d1/d4 (create subdirectories d2, d3, and d4 in the/d1 directory at the same time)
# Mkdir-p/d1/d2 (create the d2 subdirectory in the/d1 directory and create d1 first)
4. rmdir
Function: delete an empty directory.
Format: rmdir [parameter] <Directory Name>
Parameter:-p: delete cyclically
Instance:
# Rmdir a1 (delete the a1 subdirectory in the current directory)
# Rmdir/etc/a2 (delete the a2 subdirectory under/etc)
# Rmdir-p/a1/a2
5. ls
Function: displays the names of files and subdirectories in a directory.
Format: ls [parameter] [file name]
Parameter:-a: displays all files (including hidden files. Files starting with "." are hidden files)
-L: display the file name and directory name in long format (display the detailed information of the file)
-F: displays the file name and type.
-R: displays the file structure of all files in the directory and subdirectories.
-T: sorted by the modification time
-S: displays the file size, in block units.
-S: sorted by file size
-M: display file names by Column
Instance:
# Ls
# Ls/bin/
# Ls grub. conf: Check whether grub. conf exists in the directory.
# Ls-l
# Ls -- help: displays the Detailed help information of ls.
# Ls-
6. cp
Function: copy a file.
Format: cp [parameter] <Source Path> <target path>
Parameter:-f: if the object exists in the target path, it is overwritten directly.
-I: if the object exists in the target path, the system prompts whether to overwrite the object.
-R: Copy all content and structure in a specified object.
-B: generate backup for overwriting files
-A: keep original file attributes
Instance:
# Cp/etc/*/a1/copy all files in/etc to the a1 directory
# Cp/bin/ls/dir1/bin/ls file to/directory named dir1
# Cp-f/sbin/*/exe/directly add all files in/sbin to the/exe directory
# Cp-r/soft/abc/
7. rm
Function: delete an object.
Format: rm [parameter] <File Name>
Parameter:-f: You are not prompted to confirm deletion.
-I: The system prompts you to confirm deletion.
-R: recursive Deletion
Instance:
# Rm/m1/f1 delete/f1 In the m1 directory
# Rm-f/m1/* Delete all objects in the m1 directory
# Rm-rf/m1 force delete a directory
8. mv
Function: Move or rename a file
Format: mv [parameter] <Source Path> <target path>
Parameter:-I: if there is a file with duplicate names, the system prompts whether to overwrite
-B: Create backup for overwriting files
-F: if there is a file with duplicate names, it will be overwritten directly.
Instance:
# Mv/m1/f1/m2/move the f1 file under the/m1 directory to the/m2 directory
# Mv f1 f2 rename the f1 file in the current directory f2
# Mv-f/d1/*/d2/move all files in/d1 to the/d2 directory
9. file
Function: detects the file type.
Format: file [parameter] <file Name>
Parameter:-z: detects the compressed package file.
-L: Follow the Symbolic Link
Instance:
# File/etc/lilo. conf
# File-z 1.gz
10. display text file content
(1) cat
Format: cat [parameter] <File Name>
Parameter:-n indicates the row number.
-B: the row number is displayed, but the blank row is not.
Instance:
# Cat/etc/inittab
# Cat-n/d1/f1/d1/f2> txt1
(2) more
Format: more [parameter] <File Name>
Instance:
# Ls | more: input the ls display result to more.
# More/etc/rc. d/rc display the content of the/etc/rc. d/rc file
Note: You can use the Enter key to wrap the line down. Press the Space key to change the page down and press the Q key to exit more.
(3) less
Instance:
# Ls | less: input the ls display result to less.
# Less/var/named/localhost. zore
Note: The Page up/Page Down keys scroll up and Down, and the Q key exits.
(4) head and tail
Instance:
# Head-20/a1: display the first 20 rows in a1
# Tail-20/a1: the last 20 rows after a1
(5) cut
Function: displays a specified number of characters, bytes, or fields in a file.
Format: # cut parameter numeric File
Parameter:-c in characters
-B is in bytes.
-F is a field.
-D specifies the delimiter
Instance:
# Cut-c 3/etc/passwd
# Cut-f 1-6-d:/etc/passwd
# Cut-B-8/etc/passwd
11. Search for files
(1) find
Format: find search path Search Condition
Condition:-name "file name": Find the specified name file
-Lname "file name": searches for all connected files of the specified file.
-User name: searches for Files Owned by the specified user
-Group name: searches for Files Owned by the specified group.
-Links n: searches for files with n links.
-Mtime n: searches for files modified N days ago.
-Atime n: searches for files that have been accessed N days ago.
-Type d/f/B/l/p: searches for files of the specified type.
-S n [bcwkMG]: searches for files of the specified size.
-Empty: searches for empty files.
-Perm mode: searches for files with specified properties.
-Exec command {}; find the specified file and execute the specified command
-Newer file name: searches for new files than the specified file
Instance:
# Find/-name "named *"
# Find/-user root
# Find./-type d-exec chmod 755 {};
# Find.-empty
# Find.-perm 644
# Find.-newer oldfile! -Newer newfile
(2) whereis
Function: Find binary executable, manual, and source files
Instance:
# Whereis umount
(3) locate
Function: search for a specified file in the database.
Format: # locate [parameter] File Name
Parameters:
-U/-U: Create a database.-u starts from the root directory, and-U can specify the start location.
-E <directory 1…> Exclude specified directories from the search range
-L <0/1> set the security level. 1. Enable the security mode. 0. disable the security mode.
-F <file type> exclude specific types of files from the database
-N: display up to n outputs
-R: Conditions for searching using regular expressions
-O specifies the data inventory name.
-D specifies the database path
Instance:
# Locate-u
# Locate chdrv
# Locate-n 100 a. out
(4) which
Function: search for a specified command file.
Format: # which file name
Instance:
# Which mount
12. grep
Function: Search for matched strings in a text file.
Format: # grep [parameter] "string" File
Parameter :-? At the same time, the upper and lower lines of matching rows are displayed? Line
-B prints the block number of the row before the matching row.
-C: only the number of matched rows is printed, and the matching content is not displayed.
-F File: Extracts templates from files. Empty files contain 0 templates, so none of them match.
-H when multiple files are searched, the matching file name prefix is not displayed.
-I ignore case differences
-If q is not displayed, only the exit status is returned. 0 indicates that the matched row is found.
-L print the file list matching the Template
-L print the list of files that do not match the Template
-N: print the row number before the matched row
-S does not display error messages about nonexistent or unreadable files
-V reverse search: Only unmatched rows are displayed.
-If w is referenced by <and>, the expression is used as a word search.
Regular Expression metacharacters:
^ Start of the anchor row: '^ grep' matches all rows starting with grep.
$ The End Of The Anchor row is as follows: 'grep $ 'matches all rows ending with grep.
. Match a non-linefeed character such as: 'gr. P' match gr followed by any character, followed by p
* Match zero or multiple previous characters, for example, '* grep'. Match All one or more spaces followed by the grep line. . * Represents any character together
[] Matches a character in a specified range, for example, '[Gg] rep' matches Grep and grep
[^] Match a character that is not within the specified range, for example, '[^ A-FH-Z] rep' match a line that does not start with a letter that does not contain the A-R and T-Z, followed by the rep
(..) Mark matching characters, such as '(love)', love is marked as 1
<Pin the start of a word, for example, '<grep' matches a row that contains a word starting with grep.
> Pin the end of a word, for example, 'grep> 'matches the row containing the word ending with grep.
X {m} repeated characters x, m times, such as: '0 {5} 'matching rows containing 5 o
X {m,} repeated characters x, at least m times, such as: 'O {5,} 'matching rows with at least 5 o
X {m, n} repeated characters x, at least m times, no more than n times, such as: 'O {5, 10} 'matching rows of 5-10 o
W matches text and numeric characters, that is, [A-Za-z0-9], for example, 'gw * P' matches with G followed by zero or multiple characters or numbers, followed by p
The inverse form of W, matching one or more non-word characters, such as periods and periods
B word lock, for example, 'bgrepb' only matches grep
+ Match one or more previous characters. For example, '[a-z] + able' matches one or more lower-case letters followed by able strings, such as loveable, enable, and disable.
? Matches zero or multiple previous characters. For example, 'gr? P' matches gr followed by one or no characters, and then the row of p.
A | B | c matches a, B, or c. For example, grep | sed matches grep or sed.
() Grouping symbols, such as: love (able | rs) ov + matches loveable or lovers and matches one or more ov.
X {m}, x {m ,}, x {m, n} act the same as x {m}, x {m ,}, x {m, n}
Instance:
# Ls-l | grep '^'
Filter the ls-l output content in the MPs queue and display only the rows starting with.
# Grep 'test' d *
Display all the lines containing test in files starting with d.
# Grep 'test' aa bb cc
The row Matching test is displayed in the aa, bb, and cc files.
# Grep '[a-z] {5} 'aa
Display All rows of strings that contain five consecutive lowercase characters.
# Grep 'W (es) t. * 1 'aa
If west is matched, es is stored in the memory, marked as 1, and any characters (. *). These characters are followed by another es (1). If they are found, the row is displayed. If you use egrep or grep-E, you do not need to escape it by using the "" sign. You can directly write it as 'W (es) t. * 1.
13. File compression
(1) gzip (*. gz)
Function: compresses a single file.
Format: gzip [parameter] compression (decompression) file name
Parameter:-d: used for decompression
-T: Check whether the compressed file is damaged.
-L: displays the compression ratio of compressed files.
-R: recursively searches for the specified directory and compresses all the files or decompress them.
-Num: Use the specified numeric num to adjust the compression speed.-1 or -- fast indicates the fastest compression method (low compression ratio)
Instance:
? # Gzip/a1
# Gzip-d/a1.gz
# Gzip-dv/*. gz
# Gzip-r/www
(2) zip
Function: compress a file.
Format: # zip [parameter] file or directory name
-D. Delete the specified file from the compressed file.
-F tries to repair damaged compressed files.
-T <mmddyy> sets the date of the compressed file to the specified date.
-U Replace the newer file into the compressed file.
-V displays the command execution process or version information.
-Z adds comments to the compressed file.
-<Compression efficiency> the compression efficiency is a value ranging from 1 to 9.
-P password: Use the specified password to encrypt the compressed package
-M: add the file to the ZIP package.
Instance:
? # Zip abc.zip f1 f2 f3
# Zip-r-P 123 soft.zip/soft/
# Zip-d soft.zip soft/wb
# Zip-u soft.zip abc. log
# Zip-m soft.zip install. log
(3) unzip
Function: Decompress the zip file.
Format: # unzip. ZIP file]
-L displays the files contained in the compressed file.
-P <password>
-D <directory> specifies the directory to be stored after the file is decompressed.
Instance:
? # Unzip f1.zip-d/f1
# Unzip-l f1.zip
(4) tar (*. tar)
Function: Creates a backup for a file or directory.
Format: tar [parameter] File/directory name
Parameter:-t: List Files in the compressed package (*. tar)
-X: extract (*. tar)
-Z: compressed files using gzip
-C: Create a compressed package
-F: Specifies the file name.
-J: compressed files using bzip
-V: displays operation information.
-C: Specifies the decompression path (the default path is the current path)
-R: Add a file to the compressed package
-U: updates files in the compressed package.
-K: During file restoration, the same file is not overwritten.
-M: During file restoration, the modification time is the current time.
Instance:
? # Tar-cvf all.tar *. jpg
# Tar-uvf all.tar logo.gif
# Tar-rf all.tar *. gif
# Tar-cvfz etc.tar.gz/etc
# Tar-xvf all.tar
# Tar-xzvf etc.tar.gz-C/soft
In addition to the above compression tools, there are also compress (uncompress), bzip2 (unbzip2)
14. ln
Function: creates a link file.
Format: ln [parameter] <Source File> <link File>
Parameter:-s: Creates a soft link file.
-I: Indicates whether to overwrite the target file.
-F: directly overwrite the existing target file
Instance:
? # Ln/etc/abc
# Ln-s/a1/etc/a1
15. touch
Function: specifies the time when an empty file is created or the file is modified.
Format: # touch [parameter] File Name
Parameter:-r: the time when the file is modified is the date and time of the specified file.
-D <yymmdd> specifies the object date and time as yymmdd.
-T <yymmddhhmm> specifies the object date and time as yymmddhhmm.
-C. Do not create the specified file
Instance:
? # Touch/f1/f2
# Touch-r/f1/f2
# Touch-d "6:03 pm 05/06/2000" file
16. sort
Function: sorts text files.
Format: sort [parameter] [file]
Parameter:-o file name: Save the sorting result to the specified file.
-U remove duplicate rows
Instance:
? # Sort/etc/passwd
# Sort-o/etc/oldpass/etc/passwd
17. paste
Function: merge text files.
Instance:
? # Paste f1 f2> f3
18. cmp
Function: Compares two files.
Format: # cmp [parameter] File 1 file 2
Parameter:-l indicates different bytes.
-S does not show any difference. Only the returned status is displayed.
Instance:
? # Cmp f1 f2
# Cmp-l f1 f2
18. diff
Function: Display differences between files.
Format: # diff [parameter] File 1 file 2
Parameter:-c: show the full text and mark the differences
-U merge and display, and mark the differences
Instance:
? # Diff f1 f2
# Diff-c f1 f2
19. wc
Function: counts the number of lines, words, and characters in a file.
Format: # wc [parameter] File Name
Parameter:-c
-W count words
-L number of statistics rows
Instance:
? # Wc/etc/passwd
# Wc-c/etc/passwd
Note:
1. Command auto-completion
Press the TAB key to automatically expand
2. Command history
Linux uses the. history File to store command history. The following operations are related to history:
! N: Call the n commands in the forward direction.
!! : Call the previous command
History: View command history
History-c: Clear history
History-w file name: Save the history to the specified file
3. Command redirection
">": Saves the command output result to the specified file. If the file does not exist, create it first and overwrite it if it exists.
">": Saves the command output result to the specified file. If the file is not created before it is saved, it overwrites it if it exists.
"<": Enter the specified command for the file content
"<": Enter the text of a pair of separators to the specified command.
Instance:
? # Ls> abc.txt
# Ls/etc> abc.txt
# Wc </etc/passwd
# Wc <!
4. Command pipeline (the output of the previous command serves as the input of the next command)
Instance: ls | more
5. Command replacement (obtain the result of the specified command)
Instance: # cat 'ls abc'
6. Multiple command execution sequence
";": Multiple commands are executed sequentially.
"|": There is a "logic or" Relationship Between the execution of the preceding and following commands. Only after | the execution of the preceding command fails will the subsequent commands be executed.
"&": There is a "logical and" Relationship Between the execution of commands before and after the command is executed successfully. Only after the successful execution of the command above & is executed
Instance:
? # Ls/etc; cd/etc
# Ls/d1/f1 | touch f1
7. Command alias
Instance: # alias [alias = 'COMMAND ']
Iii. vi editor usage
1. vi Definition
Vi is a text editing software in Unix/Linux systems.
2. Three vi Modes
Command mode: Delete characters and typeset
Insert mode: insert, delete, and modify characters
Last line mode: use commands to operate vi software
3. Use of vi
(1) Start VI
# Vi [parameters] [file name]
(2) Common VI commands
~ Move cursor
Move one character to the left: Press h
Move one character to the right: press l
Move one row down: Press j
Move one row up: Press k
Move to the beginning of the row: Press ^
Move to the end of the row: Press $
Move to the top of the file: Press H
Move to the end of the file: press L
Move to the center of the file: By M
Flip one screen: press ctrl + f
Flip the screen: press ctrl + B
Front Half screen: press ctrl + d
Half screen flip: press ctrl + u
Move the cursor to the specified row: input: Number
~ Insert text
Insert text to the right of the cursor: press
Add text at the end of A line: press
Insert text to the left of the cursor: press I
Insert text at the beginning of the row: press I
Insert a new line in the next line of the row where the cursor is located: Press o
Insert a new row in the row where the cursor is located: Press O
~ Undo and repeat operations
Undo the previous operation: press u
Undo changes to the row where the cursor is located: press U
Repeated operation: Enter "."
~ Delete text
Delete current character: Press x
Delete a word: by dw
Delete a row: By dd (CUT)
Delete part of the row content: Press D (Delete content on the right of the cursor) or press d0 (Delete content on the left of the cursor)
Delete to the end of the file: Press dG
DL: Delete content from the last line on the screen
DH: Delete the content until the first line on the screen
DG: Delete content that ends in the working cache area (Text tail)
D1G: Delete content that begins in the work Cache
: N, md: delete m rows from row n
~ Copy and paste
Copy a line of content: Press yy
Paste: Press p
Search and replace commands
/String: forward query string
? String: returns a string.
N: continue the last search
Shift + n: searches in the opposite direction of the previous Query
: % S/string 1/string 2/g: replace string 1 with string 2 in the whole text
N, MS/string 1/string 2/g: replace string 1 in n to m rows as string 2
~ Save and exit command
: W: Write Buffer
: W file name: Write the buffer to the specified file
: Wq: Save and exit
: Q! : Do not save and exit
: Wq! : Save and exit. Read-Only permission is ignored for the owner.
E! : Cancel the modification made since the last save
ZZ: Save and exit
~ Modify text commands
Cl: change the current character
Cw: to the end of a word
C3w: change to the end of the third word.
Cb: change to the start position of a word
C0: change to the end of a row
C): Modify to the end of a statement.
C4): To the end of the fourth statement.
C}: change to the end of a paragraph
C2 {: Modify the position of the first paragraph before the starting position of the current paragraph
Ctc: modify the content between the current row and the position where the next character c appears
C: Modify to the end of a row.
Cc: modify the current row
~ Custom session commands
: Set: Display vi Variables
: Set all: display all possible vi variables and their current values
: Set nu: displays the row number.
: Set nonu: Hide row number
: Set showmode: displays the current operation mode.
: Set noshowmode: Hide the current operation mode
: Set ai: Automatic Alignment
: Set dir = Directory: set the buffer location
4. Advanced Application of VI
(1) edit multiple files
# Vi [-o] File 1 file 2 .........
-O: open multiple files in the same window
: Next: edit the next file
: Prev: edit the previous file
: Args: displays the names of all open files.
Ctrl + ww: switch window
: E file name: Read another file
(2) Advanced Replication
: Start, end cp dest: copy the content from start to end to below the dest row
: Start, end m dest: Move the content from start to end to below the dest row
NY: Save the content of n rows starting from the row where the cursor is located
: [M], [n] w <File Name>: save m to n to a specified file.
(3) other advanced applications
: X enter password: Save it with password encryption
: Ctrl + s: Lock VI (ctrl + q unlock)
: R! <Command>: Execute the specified command and insert the command result to the current file.
: J: Merge the upper and lower rows
Iv. Software Package Management
1. Main Types of Linux software packages
(1) RPM package
A software packaging method developed by RedHat can be used in multiple Linux systems.
RPM package name format: Software name-version number. runtime platform. rpm (foo-1.2.0-3.i386.rpm)
Linux uses a dedicated database to record the RPM package installation.
(2) TAR package
The TAR package encapsulates the software source code and uses gzip or other methods for secondary compression.
2. RPM package management
(1) install the RPM package
# Rpm-ivh [detailed options] RPM package name
Detailed options:
-- Nodeps ignore dependency
-- Rplacepkgs forcibly overwrites an existing RPM package
Instance:
? # Rpm-ivh grub-0.93-7.i386.rpm
(2) Upgrade the RPM package
? # Rpm-Uvh [-- nodeps/-- replacepkgs] package name
Instance:
? # Rpm-Uvh bind-9.2.20-8.i385.rpm
(3) query the RPM package
# Rpm-q [a/f file name/l/] [software name]
Instance:
# Rpm-q bind (query the software package named bind)
# Rpm-qa (query system for all installed RPM packages)
# Rpm-qf/boot/grub. conf (query the RPM package to which the grub. conf file belongs)
# Rpm-ql bind (query the software package named bind and display the package content)
(4) uninstall the RPM package
# Rpm-e Software name
Instance: # rpm-e bind
(5) Check the RPM package
# Rpm-V software name
Instance: # rpm-V bind
3. Manage the TAR package
(1) install the TAR package
Extract
Configure installation parameters
Compile and install
Instance:
# Tar-xzvf bind-9.2.0.tar.gz
# Cd bind-9.2.0
#./Configure -- prefix =/usr/named
# Make
# Make install
(2) uninstall the TAR package
Delete Installation directly
Instance:
# Rm-rf/usr/named

Articles you may be interested in
  • Linux Command file directory management cat command
  • Linux File Content statistics command wc command usage details
  • How many lines of files can be viewed in linux?
  • Linux File Creation (touch) command details
  • Summary of linux Command for viewing File Content
  • Create a folder (mkdir) command in linux
  • Prohibit web page right-click, copy, save as, View Source file and other functions to implement web page source code protection
  • Linux chmod (file or folder permission setting) command parameters and Usage Details

Related Article

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.