1. What are the file management commands on Linux, their common methods and their related examples are shown:
Command format: command-option parameter
File processing commands
Ls
Features: View the details of the files in the directory
Syntax: LS option [-ald] [file or directory]
-a displays all files, including hidden files
-L Detailed information display
-D View Directory properties
Demonstrate:
[[email protected] mytest1]# ls
p23b p2e
[email protected] mytest]# LL
Total 0
Drwxr-xr-x. 2 root root 6 Sep 19:58 p23b
Drwxr-xr-x. 2 root root 6 Sep 19:58 p2e
Cd
Function: Switch directory
Demo: CD/(Switch to the root directory)
Cd.... (Go back to the top-level directory)
Pwd
Function: Displays the current working directory
Demonstrate:
[Email protected] mytest1]# pwd
/tmp/mytest1
Touch
Function: Create an empty file
Syntax: touch [filename]
Demonstrate:
[[email protected] mytest1]# Touch file 1
Mkdir
Function: Create a new directory
Syntax: mkdir [directory name]
Demonstrate:
[[email protected] mytest1] #mkdir AAA
Cp
Features: Copying Files or directories
Syntax: cp-r[source file or directory] [destination directory]
-R Copy Directory
Demonstrate:
1. Copy the file File1,file2 to the directory Dir1
$ CP file1file2 Dir1
2. Copy all files and subdirectories under Dir1 to Dir2
$ cp-r Dir1 Dir2
Mv
Features: Move files, rename
Syntax: MV [source file or directory] [destination directory]
Demonstrate:
1. Rename the file File1 in the current directory to File3
$ mv File2 Dir2
Rm
Features: Deleting files
Syntax: rm-r [file or directory]
-R Delete Directory
Demonstrate:
1. deleting files File3
$ RM File3
2. Delete directory Dir1
$ rm-r Dir1
Cat
Function: Display file contents
Syntax: cat[file name]
Demonstrate:
$cat/etc/services
More
Features: page display of file contents
Syntax: more[file name]
(space) or F to display the next page
(enter) to display the next line
Q or Q exit
Demonstrate:
$more/etc/services
Head
Function: Production look at the first few lines of the file
Syntax: head-n [file name]
N Displays the first few lines of a file
Demonstrate:
$head -20/etc/services
Tail
Function: View the following lines of a file
Syntax: tail-n [file name]
-N Displays the following lines of the file
-F Dynamic Display of file contents
Demo: $tail -30/etc/services
Ln
Function: Generate link file
Syntax: ln-s[source file [target file]
-S Create link
Demonstrate:
1: Create a soft link to file/etc/issue/issye.soft
[Email protected] ~]# Ln-s/etc/issue/issue.soft
[[Email protected] ~] #ls-l/
Etc/issue/issue.soft
-rw-r--r--. 1 root root, Jan 2013/etc/issue
lrwxrwxrwx. 1 root root10oct 16:01/issue.soft->/etc/issue
2. Create a hard link to the file/etc/issue/issue.hard
[Email protected] ~]ln/etc/issue/issue.hard
[[Email protected] ~]
# Ls-l/etc/issue/issue.hard
-rw-r--r--. 2 root root ll75 Jan 2013/etc/issue
-rw-r--r--. 2 root root, Jan 2013/issue.hard
The difference between a hard link and a soft link:
Soft links are similar to shortcuts under Windows, and soft links can be generated across file systems, and the purpose of soft links is meaningless when the original file is deleted.
A hard link is similar to a copy under Windows, a hard link cannot be generated across a file system, and when the original file is deleted, the hard-linked file also exists, and the hard link has the same inode as the original file, so you can synchronize the updates. Hard Link =cp+ Synchronous update
File Search Command
which
Function: Displays the directory where the system commands are located
Syntax: which [command name]
Demonstrate:
$Which ls
Find
Function Description: Find files or directories
Syntax: Find [Search Path] [search for keywords]
Demonstrate:
1. Find file init in directory/etc
$ find/etc-name Init
2. Look for files larger than 100MB in the root directory
Find/-size +204800
3. Find the file owner-sam in the root directory
Find/-user Sam
4. Find the File box directory with properties modified within 24 hours under/etc
$ find/etc-ctime-1
5. Find files larger than 80MB less than 100MB in/etc
$ find/etc-size +163840-a-size-a-204800
6. Find the Inittab file in/etc and display its details
$find/etc-nameinittab-exec ls-l{} \;
The Find parameter is detailed:
(-name file name
*: matches any character including 0 characters, init*----"Files starting with Init
*init*--"file with init in file name
-size File Size
The file size is the smallest unit of block data, with 1 data block sizes equal to 512 bytes and 512 bytes =0.5kb,1block=0.5kb.
100mb=?block
100mb=102400kb=204800block
Greater than +
Less than
-user file Owner
Search by Time: 1.ctimeatimemtime in days
2.cmin Aminmmin in minutes
c---change, indicating that the properties of the file have been modified, the owner, the owning group, and the permissions.
A---Access
M-modify modified, the contents of the file have been modified.
"-" stands for how long,-1 represents a day within the
"+" more than how long.
For example, find/etc-mmin-120 represents a file that was modified within 120 minutes.
1. Connector:-A and logic with
-O or Logical OR
-type: Query By Type
-F: Binary file
-L: Link file
D: Catalog file
Example: [Root@blog ~]# find/etc/-name init*-a-type L
/etc/init.d
[Root@blog ~]# find/etc/-name inittab-o-size+204800
/etc/inittab
2. Connector: Find ... –exec command {}\;
-ok (Inquiry confirmation)
{} Represents the result of a find query.
\ escape character, the symbolic command uses its own meaning.
; Represents the end of a statement.
-inum: Query by I node
For example: Delete the file named-abc.
[Root@blog Test] #ls-I
655336 a 655340 ab 655341-abc 655333 \-abc 655339 \-\a\b\c 655335 Issue
[Root@blog Test] #find. -inum 655341
./-abc
[Root@blog Test] #find. -inum 655341-exec RM-RF {}\;
)
Locate
Features: Find file or directory limitations: for newly created files, you cannot query with the Locate command
Syntax: locate[search keywords]
Demonstrate:
List all files related to file
$ Locate File
Grep
Function: Searches the file for string matching lines and outputs
Syntax: grep [Specify string] [source file]
Demonstrate:
#grep ftp/etc/services
File Help Commands
Mans
Function: Get help information
Syntax: Man [command or configuration file]
1. View the help information for the LS command
$ man ls
2. View the Help information for profile services
$Man Services
Info
Function: Get help information
Syntax: info [any keyword]
Demonstrate:
View Help for LS instructions
$ info ls
File Compression Decompression Command
Tar
Function: Compress or unzip the directory
Grammar:
Tar options [CVF] [contents]
-V Show More information
-f Specifies the file name after compression
-C generate. tar package file
-Z Package simultaneous compression (for compression)
Decompression (when extracting)
-X Decompression
Demonstrate:
Packaging files Peng
#tar-CF Peng.tar Peng
#ls
Peng Peng.tar
Compress directory Dir1 into a packaged and compressed file
$ TAR-ZCVF dir1.tar.gz Dir1
Unzip the dir1.tar.gz directory
$ TAR-ZXVF dir1.tar.gz
Gzip
Function:
Grammar:
Demonstrate:
Zip
Features: compressing files or directories
Syntax: Zip option [-r] [compressed filename] [file or directory]
Pros:. zip default Windows and Linux common format. When you compress a file, the original file is preserved after compression, unlike the gzip command
Demonstrate:
Compress files
$zip services.zip/etc/services
Compressed directory
$ zip-r Test.zip/test
Gunzip
Function: Unzip compressed file of. zip
Syntax: unzip[compressed file]
Demonstrate:
$ unzip Test.zip
Bzip2
Features: Compressing files
Syntax: bzip2 option [-K] [file]
-K preserves source files after generating compressed files
Demonstrate:
$ bzip2-k File1
Bunzip2
Function: Unzip
Syntax: BUNZIP2 option [-K] [compressed file]
-K preserves source files after decompression
Demonstrate:
$ bunzip2-k file1.bz2
Note: The command to extract the suffix. TAR.BZ2 packet: TAR-JXVF Package
File Rights Management Commands
Chmod
Function: Change file or directory permissions
Syntax: chmod [{ugo}[{+-=}{rwx}] [file or directory]
[mode=421]
Demonstrate:
1. Grant Write permission to the group to which the file file1 belongs
$ chmod g+w file1
2. Set directory Dir1 full permissions for all users
$ chmod 777 Dir1
Command Summary: The permissions for the file directory are r,w,x
File:
R Read---Cat, more, head, tail
W Write-echo, vim
X Execute---command script
Directory:
R Read---ls
W Write-touch, Rm,mkdir
X Execute---CD
Chown
Function: Change the owner of a file or directory
Syntax: chown [user] [file or directory]
Demonstrate:
Change the owner of the file file1 to nobody
$ chown Nobody file1
Chgrp
Function: Change the owning group of a file or directory
Syntax: chgrp [user Group] [file or directory]
Demonstrate:
Change the file File1 group to ADM
$ chgrp adm File1
2.Bash working characteristics the command execution status return value and command-line expansion are involved in the contents and examples of the demonstration
3. Use the command line expansion function to complete the following exercises
Create the A_c,a_d,b_c in the/tmp directory, b_d
[Root@server30 tmp]# mkdir a_c a_d b_cb_d
[Root@server30 tmp]# ls
A_c a_d B_c B_d
Create the/tmp/mylinux directory:
mylinux/
|-----Bin General Command Store directory
|-----Boot System boot directory, kernel file Vmlinux boot file, boot load Bootloader,grub
|------Grub Boot settings file related
|------Dev any device and interface device is stored in this directory as a file
|------ETC configuration file directory, init.d a variety of software startup scripts
|-----RC.D Store Script startup file
| |-----INIT.D Storage via Xin
|------sysconfig
|---network-scripts
|--lib Basic Shared Library
| |---Modules kernel module files
|---lib64 dedicated to the secondary shared library file location on the x86_64 system
|---proc belongs to the state of the virtual file system such as the external device, the network state, in the dynamic change
|---sbin commands that can be operated on or in single-user mode
|----SYS belongs to the virtual file system, in-memory data such as kernels, file systems, etc.
|----TMP temporary STORAGE directory, the user can access
|----USR User program
| |----local contains the user program installed from the source
| |-----bin Normal User executable directory
| |-----The executable command for Sbin superuser root
|-----file on the normal change of Var
|---lock program file
|--log Login File
|--run files generated by the operation of the software
4. What are the metadata information for a file, what does it mean, and how to view it?
The file's element information includes the file's permissions, size, owner, group, and modification time information
Use Ls-l to view
5. How do I define an alias for a command, and how do I refer to the execution result of another command in a command?
Explanation: Here are two tips for command aliases and command substitutions
Define an alias:
Demo: Alias COPY=CP
Alias xrm= ' Rm–r '
View alias Information: Alias
Remove alias: unalias copy
To reference the execution result of another command in a command: Also called command substitution
Command substitution:
Function: The output of one command as a parameter of another command
Syntax: Command 1 ' command 2 ' (Note: ' Is fluttering, is a grave accent)
Demo: Ls-l ' Whichtouch ' (this is only temporary, of course, if you want to be permanently effective)
Add Shell variable Knowledge:
$ $ Gets the file name of the currently executing shell script, including the path
$n gets the nth parameter value of the currently executing shell script, n=1....9, which represents the file name of the script when n is 0, if n is greater than 9 enclosed in curly braces {10}
$* gets all parameters of the current shell, treats all command-line arguments as a single string, equivalent to $1$2$3 .... Note with $ #的区别
$ #获取当前shell命令行中参数的总个数
[email protected] All parameters of this program ' $ ' ' $ ' $ ' ... ', this is the best way to pass parameters to other programs because he retains any whitespace embedded in each parameter
$? is the return value of the last execution result
0 is correct, not 0 is wrong
Example Demo:
Use the ping command to write a script to determine if the target host is alive
#! /bin/bash
For-N in {1 ... 20}
Do
host=192.168.0. $n
PING-C2 $host &>/dev/null
If[$?=0];
Then
Echo "$host is Up"
Else
Echo "$host is Down"
6. Show all files or directories that start with 1 in the/var directory and end with a lowercase letter with at least a number (can have other characters) in the middle
[Root@server30var]# Ls/var | grep ^1[0-9]*[a-z]$
7. Displays files or directories that start with any number in the/etc directory and end with a non-numeric
[Root@server30etc]# ls/etc | Grep^[0-9]|grep-v [0-9]$
8. Displays files or directories that start with a non-letter, followed by a letter and any character of any length, in the/etc directory
[Root@server30 var]# ls/etc |grep-v ^[a-z]|grep [a-z]*$
9. In the/tmp directory, create a file that starts with Tfile, followed by the current date and time, with a filename such as tfile-2016-05-27-09-32-22
[Root@server30 tmp]# mkdir tfile ' Date +%y-%m-%d-%h-%m-%s '
6-9 Summary of Knowledge Points: The main content of these questions is grep search keywords and date (date learning See blog one)
Keyword Content learning:
*: matches any number of characters (can be 0 or more)
? : Indicates a match for one character
[0-9]: represents any one byte in the range matching 0 to 9
[ABC]: Represents any one of the matching ABC
[^ABC]: Indicates a match other than any other in ABC
Tail-f/var/log/messages (In a tab, if you want to quit when you're done, press Ctl+c)
/etc/init.d/networkmanagerrestart (In another tab)
Show middle section
Head-n 20/etc/passwd |tail
Cat passwd |head-n20 |tail-n 10
Head-n 22/etc/passwd |tail-n 3
There are two ways to display line numbers:
Vim passwd----: Set Nu
nlpasswd
*grep
-V Reverse selection except for what
^ #表示以 # Start
$ with what end of search (^$ means space)
-A5 match keyword After 5 lines show up
-B5 Match keyword First 5 lines show up
Cat passwd |grep root filter keywords in behavioral units
grep Root passwd
*cut-d (d is the meaning of the delimiter F:from) shows the content in front of the cutoff character
Exercise 1: Display only the user name in/etc/passwd
Answer: CUT-D:-F1/ETC/PASSWD
Exercise 2: Show only the shell environment in/etc/passwd
Answer: CUT-D:-F7/ETC/PASSWD
Parsing: Because there are only 6 in each row of the/etc/passwd file: Here is the following: for the delimiter and from the 7th intercept, so it is from the last space, that is, the end of the line intercept
10. Copy all files or directories in the/etc directory that start with P and that end in non-digits to the/tmp/mytest1 directory
[Root@server30 mytest1]# find/etc-namep*[^0-9]-exec/bin/cp-rf {}/tmp/mytest1 \;
11. Copy all files or directories ending with. D in the/etc directory into the/tmp/mytest2 directory
[Root@server30 mytest2]# find/etc-name *.d-exec/bin/cp-rf {}/tmp/mytest2 \;
12. Copy all files in the/etc/directory that begin with 1 or M or N and end with. conf to the/TMP/MYTEST3 directory
[root@server30mytest3]# Find/etc-name [1MN]*.CONF-EXEC/BIN/CP {}/tmp/mytest3\;
10-12 Summary of Knowledge points: The contents of the study are find and CP (see File processing commands)
File management class commands on Linux