Table of contents
1. Operation directory
1.1 Directory switching
cd /home
Go into the '/home' directory.cd /
enter the root directorycd ..
Return to the previous directory.cd ../..
Returns the directory two levels above.cd
Go to your personal home directory.cd ~
Go to your personal home directory.cd -
Return to the last directory you were in.
1.2 Browse the directory
pwd
Displays the working path.ls
View the files in the directory.ls -F
View the files in the directory.ls -l
Display details of files and directories.ls -a
Show hidden files.ls *[0-9]*
Displays file and directory names that contain numbers.tree
Displays a tree structure of files and directories starting with the root directory.lstree
Displays a tree structure of files and directories starting with the root directory.
1.3 Directory Creation
mkdir dir1
Create a directory called 'dir1'.mkdir dir1 dir2
Create two directories at the same time.mkdir -p /tmp/dir1/dir2
Create multi-level directories
1.4 Directory deletion
rmdir dir1
Delete a directory called 'dir1'.rm -rf dir1
Delete a directory called 'dir1' and recursively delete subdirectories and files in the directory.rm -rf dir1 dir2
Delete both directories and their contents at the same time.rm -ri dir1
: Ask to confirm deletion before deleting
1.5 Copy directory
cp dir/* .
Copy all files in a directory to the current working directory.cp -a /tmp/dir1 .
Copy a directory to the current working directory.cp -a dir1 dir2
Copy dir1 to dir2.cp -r [目录1] [目录2]
Recursively copy all files and folders in directory 1 to directory 2.cp -ri [目录1] [目录2
Ask the user before overwriting the original directory when performing a copy operation
1.6 Moving or Renaming a Directory
mv dir1 new_dir
Rename/move a directory.
2. Manipulating files
2.1 Find files
find / -name file1
Searches for files and directories into the root filesystem starting at '/'.find / -user user1
Search for files and directories belonging to user 'user1'.find /home/user1 -name \*.bin
Searches for files ending in '.bin' in the directory '/home/user1'.
2.2 View file information
stat file1
Look at the value of the file creation/modification time 'Change'.du -h 文件名
Check the file size.
2.3 View file content
cat -n file1
View the entire contents of the file starting from the first byte and displaying line numbers.tac file1
View the contents of a file in reverse, starting from the last line.head -2 file1
View the first two lines of a file.tail -2 file1
View the last two lines of a file.tail -f /var/log/file1
See what's being added to a file in real time.tail -f -n 100 日志文件名
View the latest 100 lines of content in real time.tail -n500 -f ./file.log | grep "exception"
Query 500 lines of log information, and query the log information that is changing, and use the pipe character to filter (for example, filter exception information).wc -l file1
View file line count.
2.4 Create file
touch file1.txt
Create a file.echo "hello shyu" > shuyu.txt
Create a file and write "hello shuyu".
2.5 File modification-vim
Basic operation:
vim file1
Enter the file1 file editing page, the default is "command mode".i
Enter edit mode.ESC
Switch from edit mode to command mode.:
Switch to the bottom line command mode and enter commands on the bottom line.
Bottom line command mode:
/word
Look under the cursor for a string named word.?word
Look for a character string named word above the cursor.n
Repeat the previous search action.:n1,n2s/word1/word2/g
Find the word1 string between lines n1 and n2, and replace the string with word2, for example: 100,200s/shuyu/SHUYU/g:w
Write the edited data to the hard disk file (commonly used).:w!
If the file attribute is "read-only", the file is forced to be written.:q
Leave vi (used).:q!
If you have modified the file and don't want to save it, use ! to force you to leave without saving the file.:wq
Save and exit.
2.6 Delete files
rm -f file1
Delete a file called file1'
2.7 Copying and renaming files
mv file1 new_file
rename/move a filecp -a /tmp/file .
copy a file to the current working directorycp -a file1 file2
copy a file
3. Compress or decompress files or directories
3.1 Compression
tar -zcvf filename.tar.gz filename
: Compress filename into a package in .tar.gz formattar -zcvf filename.tgz filename
: Compress filename into a package in .tar formattar -jcvf filename.tar.bz2 filename
: Compress filename into a package in .tar.bz2 formatzip filename.zip filename
: Compress filename into a package in .zip formatzip -r file1.zip file1 file2 dir1
: Simultaneously compress several files and directories into a compressed package in zip formatgzip -d filename.gz filename
: Compress filename into a package in .gz formatrar -a filename.rar filename
: Compress filename into a package in rar formatrar a file1.rar file1 file2 dir1
: Simultaneously compress 'file1', 'file2' and directory 'dir1'
3.2 Decompression
tar -tf archive.tar
: Display the contents of a packagetar -xvf filename.tar -C /tmp
: Decompress a tar format compressed package to the /tmp directorytar -zxvf filename.tar.gz
: Decompress a tar.gz format compressed package to the current filetar -zxvf filename.tgz
: Decompress a tgz format compressed package to the current filetar -jxvf filename.tar.bz2
: Decompress a tar.bz2 format compressed package to the current filetar -xZvf filename.tar.Z
: Decompress a tar.Z format archive to the current fileunrar e filename.rar
: Decompress a rar format compressed package to the current fileunrar x filename.rar [path]
: Decompress a rar format compressed package to the specified pathunzip filename.zip -d [path]
: Decompress a compressed package in zip format to the specified pathxz -d filename.tar.xz > tar -xvf filename.tar
: Decompress a tar.xz format compressed package to the current file