Catalog
-
Linux Basic Learning 2
- File directory structure
- File Naming Specification
- File System Structure
- Composition of linux applications
- Absolute and relative paths
- Directory name and base name
- Switch directories
- Switch to Home Directory
- Switch to the last directory
- Show current working directory
- List directories or files
- Catalog type under linux
- View file status
- touch
- File wildcards
- Create directory
- display directory tree
- Delete Directory
- Copy files and folders
- Move, Rename
- delete
- link
- View File Types
- Input and output
- I/O Redirection
- Save standard input separately from error input
- Merge all outputs
- Import stdin from a file
- Send multiple lines to stdin
- The Conduit
Linux Basic Learning 2
File directory structure
- Files and directories are organized into an inverted tree structure
- File system starts at root,'/'
- File names are case sensitive
- Hidden files begin with'..
- Path is delimited by'/'
File Naming Specification
- File characters up to 255 characters
- The maximum number of file names, including paths, is 4095
- Color Representation
- Blue File-->Directory
- Green File-->Executable File, Executable Program
- Red File-->Compressed or Package File
- Light Blue File --> Link File, mainly files built with ln command
- White Files-->General Files such as Text Files, Configuration Files, Source Files, etc.
- Flashing red: problems with linked files
- Yellow: for device files
- Gray Files-->Other Files
- All characters except slash and NULL can be used
- Case sensitive
File System Structure
- /boot boot boot file location, the kernel file, boot loader are in this directory
- /bin Commands available to all users
- /sbin Management Class Commands
- /lib Basic library file used by program at startup.so end
- /lib64 specially stores auxiliary library files on X86_64 system
- /etc Store Profile
- /home/USERNAME Ordinary User's Home Directory
- /root Administrator's Home Directory
- Mount Point for/media Portable Mobile Devices
- /mnt temporary file mount point
- /dev device files and special files
- Installation location for/opt third-party applications
- /tmp temporary file storage location
- /usr Store Installer
- /var stores frequently changing files, such as logs
- /proc holds virtual files related to kernel startup and processes
- /sys Output hardware-related files on the current system
- Data used by allowed services on/srv systems
Composition of linux applications
- Binary file
- /bin
- /sbin
- /usr/bin
- /usr/sbin
- /usr/local/bin
- /usr/local/sbin
- Library Files
- /lib
- /lib64
- /usr/lib
- /usr/lib64
- /usr/local/lib
- /usr/local/lib64
- configuration file
- /etc
- /etc/name
- /usr/local/etc
- Help Files
- /usr/share/man
- /usr/share/doc
- /usr/local/share/man
- /usr/local/share/doc
Absolute and relative paths
- Absolute path
- Start with root
- Full file location
- Can read any file or folder
- Relative Path
- Do not start with root
- Determine relative to current position
- Can be short to represent a file or folder
- Current directory
- ..Parent directory
Directory name and base name
[root@localhost log]#basename /etc/sysconfig/network-scripts/ifcfg-ens33 ifcfg-ens33 [root@localhost log]#basename /etc/sysconfig/network-scripts network-scripts [root@localhost log]#dirname /etc/sysconfig/network-scripts /etc/sysconfig [root@localhost log]#dirname /etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts
Switch directories
cd switch directory change directory
- Relative paths can be used
- Absolute paths are available
Switch to Home Directory
[root@localhost log]#cd [root@localhost ~]#
Switch to the last directory
[root@localhost ~]#cd /etc/sysconfig/network-scripts/ [root@localhost network-scripts]#cd - /root [root@localhost ~]#cd - /etc/sysconfig/network-scripts
Show current working directory
pwd print working directory
[root@localhost network-scripts]#pwd /etc/sysconfig/network-scripts # View the real directory of the link [root@localhost /]#cd lib [root@localhost lib]#pwd -P /usr/lib
List directories or files
ls list
Command format: Usage: ls [OPTION]... [FILE]...
ls -a Show all files [root@root ~]# ls -a . .cshrc file15 file3 .passwd.swo .. file1 file16 file4 .tcshrc anaconda-ks.cfg file10 file17 file5 teaching_plan.zip ls -l Show file list in long format [root@root ~]# ls -l total 8 -rw-------. 1 root root 1414 Aug 22 2019 anaconda-ks.cfg -rw-r--r--. 1 root root 0 Mar 1 18:28 file1 -rw-r--r--. 1 root root 0 Aug 22 17:21 10 //Permission Hard Drive References Owned by Master Group Size Access Time File Name ls -R Display directories recursively [root@root ~]# ls -R .: anaconda-ks.cfg file12 file16 file2 file5 file9 file1 file13 file17 file20 file6 teaching_plan.zip ls -d Show the catalog itself [root@root ~]# ls -d . ls -1(Number1) File Branch Display [root@root ~]# ls -1 anaconda-ks.cfg file1 file10 file11 file12 ls -S Installation File Size Sorting [root@root ~]# ls -S teaching_plan.zip file11 file15 file19 file4 file8 ls -r Reverse Display [root@root ~]# ls -r teaching_plan.zip file6 file20 file17 file13 file1 ls -t Sort by time [root@root ~]# ls -t file18 file11 file15 file3 file7 teaching_plan.zip ls -lh Show human readable ways [root@root ~]# ls -1h anaconda-ks.cfg file1 file10 file11 file12 file13 ls -d */ Show folders in the current directory l. Show only hidden files
Catalog type under linux
- -used to represent files
- d is used to represent a directory
- b Block Device
- c character device
- l Represents a symbolic link file
- s socket socket
View file status
File: 'anaconda-ks.cfg' Size: 1747 Blocks: 8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 33574992 Links: 1 Access: (0600/-rw-------) Uid: ( 0/ root) Gid: ( 0/ root) Context: system_u:object_r:admin_home_t:s0 Access: 2019-08-22 12:09:03.288000381 +0900 # Access time Modify: 2019-08-22 11:47:12.262947345 +0900 # Modification Time Change: 2019-08-22 11:47:12.262947345 +0900 # Change time atime Access time to view file content changes mtime Modification time changes content ctime Changing time metadata changes
touch
Create empty file and refresh time, refresh time if file exists, create file if file does not exist
-a Modify only atime and ctime
-m only modifies mtime and ctime
File wildcards
- *All
- What?Matches any single character
- ~Indicates the user's home directory
- One of [123]
- [^123] Reverse
- [0-9] denotes a number
- file[a-z] letter (pitted) missing Z
- file[A-Z] letter (pitted) missing a
- [: lower:] lower case letters
- [: upper:] uppercase letters
- [: alpha:] All letters a-zA-Z
- [: alnum:] Represents letters and numbers
- [: digit:] denotes a number
Create directory
mkdir Create Directory
- -p Recursive Creation
- -v Show Detailed Procedures
display directory tree
Installation: Yum install-y tree
tree Displays Catalog tree
[root@root ~]# tree . ├── anaconda-ks.cfg ├── file1 ├── file10 ├── file11 ├── file12 ├── file13 ├── file14 ├── file15 ├── file16 ├── file17 ├── file18 ├── file19 ├── file2 ├── file20 ├── file3 ├── file4 ├── file5 ├── file6 ├── file7 ├── file8 ├── file9 └── teaching_plan.zip 0 directories, 22 files
-d Display folders only
[root@root ~]# tree -d . 0 directories
-L #Show only #Layers
[root@root ~]# tree -L 1 . ├── anaconda-ks.cfg ├── file1 ├── file10 ├── file11 ├── file12 ├── file13 ├── file14 ├── file15 ├── file16 ├── file17 ├── file18 ├── file19 ├── file2 ├── file20 ├── file3 ├── file4 ├── file5 ├── file6 ├── file7 ├── file8 ├── file9 └── teaching_plan.zip 0 directories, 22 files
Delete Directory
rmdir can only delete non-empty directories
-p Remove empty parent directory recursively
-v Displays the deletion process
Rm-rf Delete non-empty directories
Copy files and folders
cp copy is an alias by default and the original command itself is not prompted to overwrite
Usage: cp [OPTION]... [-T] SOURCE DEST or: cp [OPTION]... SOURCE... DIRECTORY or: cp [OPTION]... -t DIRECTORY SOURCE... -i Show prompt information -n Not Covered -r -R Recursive replication -d Copy only linked files, not source files -a File -v Show process -b Back up the original file --backup=number Backup files plus numbers -p Keep original attributes
- If the source file is a file
- Target is file
- If the target file does not exist, create a new target file and write the contents to it
- If the target file exists, the original command is to overwrite it directly. It is recommended that -i be used to prompt the user
- Target is a folder
- Create a new file with the same name in the folder and write the contents to the new file
- Target is file
- If the source file is more than one file
- Target must be a folder, the folder must exist, everything else will go wrong
- If the source file is a folder
- The target file is a file: no
- The target file must be a folder and the -r option must be used
- If the destination file does not exist: create the destination folder directly and copy all the data from the source folder to the destination folder
- If the target file exists:
- Error if file
- If it is a folder: create a folder with the same name in the destination folder and copy all the data to the new folder
Move, Rename
mv move
Usage: mv [OPTION]... [-T] SOURCE DEST or: mv [OPTION]... SOURCE... DIRECTORY or: mv [OPTION]... -t DIRECTORY SOURCE... -i Tips -f force -b backups --backup=number Add a number after the backup -v Show process
delete
rm remove
Usage: rm [OPTION]... FILE... -i Tips -r -R recursively -f Force Delete rm -rf Use with caution rm -rf /* cd / rm -rf *
link
Soft Link
- A shortcut equivalent to windows
- Create command Ln-S source file target file
- You can make soft links to directories
- The path to another file or directory, the size being the character of the path's length
- No effect on disk references
- Can cross partition
- Source file changes, soft links change
Source file deleted, soft link not accessible
Hard Link
- The number of disk references changes
- Pointing to the same area on the hard disk
- The number of references to the disk increases with the number of hard links
- Directories cannot be hard-linked
- Cannot span partitions
- The source file changes, and the hard links change
Hard links are accessible when the source file is deleted
View File Types
file
Input and output
- Standard input defaults to stdin 0 from the keyboard
- Standard Output Default Output to Terminal Window stdout 1
- Standard error output default output to terminal window stderr 2
I/O Redirection
>Overlay
- >Redirect standard output to file
- 2>Redirect error output to file
- &>Redirect all output to a file
Prohibit, Allow Overlay
- Prohibit overwriting set-C
- Allow overwriting set +C
>Append
> Append the standard output to the file
2 > > Append error output to file
&>> Append all outputs to the file
Save standard input separately from error input
[root@localhost ~]#ls f 45yuio > log.log 2> error.log [root@localhost ~]#cat log.log f [root@localhost ~]#cat error.log ls: cannot access 45yuio: No such file or directory
Merge all outputs
- &>Override redirection
- & >> Acceleration Orientation
- command > file 2>&1
- command >> file 2>&1
- (): Combine output from multiple files
- /dev/null black hole
Import stdin from a file
tr character substitution
-t truncation -d delete -s Compression, Weight Removal -c Reverse [root@localhost ~]#tr 'a-z' 'A-Z' < /etc/issue \S KERNEL \R ON AN \M [root@localhost ~]#tr 'a-z' 'A-Z' qwertyy QWERTYY 12345678 12345678 ASDFGHJ ASDFGHJ qwertyuio QWERTYUIO ^C [root@localhost ~]#tr ab 12 ab 12 abb 122 asdfghjkl 1sdfghjkl ^C [root@localhost ~]#tr abc 12 ab 12 abc 122 abc 122 ^C [root@localhost ~]#tr ab 123 ab 12 abb 122 avc 1vc qbc q2c abc 12c [root@localhost ~]#tr -t abc 12 abc 12c ab 12 [root@localhost ~]#tr -d abc qwertyui qwertyui an^H^H n abc artyibrtyuiocrtyuiop rtyirtyuiortyuiop ^C [root@localhost ~]#tr -d abc < /etc/issue \S Kernel \r on n \m [root@localhost ~]#cat /etc/issue \S Kernel \r on an \m [root@localhost ~]#tr -s a abc abc aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabc abc ^C [root@localhost ~]#tr -sc a aasdaaaaaaa aasdaaaaaaa ^[[A^H^H^C [root@localhost ~]#tr -sc a aaaaaaaaabbbbbbbbbbbbbccccccccddddddddddd aaaaaaaaabcd [root@localhost ~]#tr -dc a aaaaaaaaaaaabbbbbbbbbb asdfghjkqwertyuiozxcvbnmxcvbnm,. aaaaaaaaaaaaa ctrl+d End [root@localhost ~]#tr -dc "a\n" asdfghjk a wertyujk;l' asdfghj a [root@localhost test]#Tr-d a < issue > issue cannot be written to the source file after processing is complete. Write to a new file [root@localhost test]#seq 1 10 > b [root@localhost test]#cat b 1 2 3 4 5 6 7 8 9 10 [root@localhost test]#tr -d "\n" < b 12345678910[root@localhost test]#tr -d "\n" < b [root@localhost test]#tr "\n" " " <b 1 2 3 4 5 6 7 8 9 10 [root@localhost test]#tr "\n" " " <b >c [root@localhost test]#cat c 1 2 3 4 5 6 7 8 9 10 [root@localhost test]#tr " " "\n" <c 1 2 3 4 5 6 7 8 9 10
Send multiple lines to stdin
# First way [root@localhost test]#cat > f1 qwert wertyui wertyui wertyuiopasdfghjk sdfghjkl sdfyhjkl;sdfghjkl;xcvb # Second way [root@localhost test]#cat > f2 <<EOF > qwerty > qwertyu > wertyui > qwertyu > EOF EOF Not necessarily, as long as two are identical
The Conduit
Pipeline is represented by'|'
Command 1|Command 2|Command 3
- Use the output of command 1 as the output of Command 2 and Command 2 as the input of command 3
- By default, pipes can only transfer standard output
- If error output needs to be passed along, then |&
- Commonly used to combine multiple commands
- Some commands do not accept pipelines
[root@localhost test]#ls f1|tr 'a-z' 'A-Z' F1 [root@localhost test]#ls f ls: cannot access f: No such file or directory [root@localhost test]#ls f|tr 'a-z' 'A-Z' ls: cannot access f: No such file or directory [root@localhost test]#ls f|&tr 'a-z' 'A-Z' LS: CANNOT ACCESS F: NO SUCH FILE OR DIRECTORY [root@localhost test]#echo file{1..20}|touch touch: missing file operand Try 'touch --help' for more information.