The Linux Foundation has published the Filesystem Hierarchy Standard (FHS), which specifies what directories should be in Linux directories and what data should be placed in those directories.

  • pwd, returns the current working directory
  • cd, to switch the working directory, change directory
    • ., the current working directory
    • ., upper level directory
    • -, the previous working directory
    • ~, the user’s home directory
    • ~admin, the home directory of user admin

  • mkdir, create a directory, make directory
    • mkdir -p /tmp/dirlevel01/dirlevel02To create a nested directory
  • rmdir, remove directory, but please note,rmdirOnly non-empty directories can be deleted
  • rm, delete the file, remove
    • rm demo.shTo delete a single file
    • rm hello.sh test.sh, delete multiple files
    • rm -R dir01/Or,rm -r dir01/Or,rm --recursive dir01/By default, the system asks the user whether to delete the entire directory and all files in the directory and deletes the directory only after receiving a positive reply from the user
    • rm -fR dir01/.--forceTo delete the entire directory and all files in the directory forcibly without user confirmation.

  • touch, create a file
    • touch testCreate an empty file without any content

  • lsThe list,
    • ls -a, lists all files that contain hidden files
    • ls -lTo list the files in long file format
      • ls -l hello.sh
    • ls -alList all files, including hidden files, in long file format
    • The file type
      • d, directory files
      • f, common file
      • c, character device file
      • bBlock device file
      • lSymbolic link file

  • chmodTo modify file access permissions, such as file owner permissions, file subgroup permissions, and other user permissions.
  • chown
    • chown root hello.sh, modify the filehello.shThe owner of the
    • chown :root hello.sh, modify the filehello.shIs the subordinate user group of
    • chown root:root hello.sh, modify the filehello.shThe owner and subordinate user group of

  • cpCopy, copy
    • cp /tmp/hello.sh ~That will behello.shCopy to the user’s home directory
    • cp /tmp/hello.sh ~/test.shThat will behello.shCopy it to the user’s home directory and rename it astest.sh
    • cp /tmp/hello.sh /tmp/test.sh ~, add multiple files (hello.sh.test.sh) to the user’s home directory, separate multiple files with Spaces.
    • cp -r /tmp/dir01 ~That will bedir01Copy the directory to the user’s home directory, includingdir01All files in the directory.
    • \cp -f /tmp/hello.sh /tmp/dir01/hello.sh, the use ofcpIf the target directory contains a file with the same name, the system asks the user whether to overwrite the file by default. If the user agrees,cpThe command overwrites these files; If the target directory contains files with the same name in batches, use\cp -fCommand to force all files with the same name to be overwritten.
    • cp -p /tmp/hello.sh /tmp/dir01/hello.sh, the use ofcpWhen a file is copied, its attributes (owner, slave group, and access permission bit) may change. As a result, the copied file cannot be modified or accessed. Therefore, if you want to copy the file in its original form, you can use options-p.

  • mvMove, move
    • mv dir02/demo.sh dir01/, move files
    • mv dir02/hello.sh dir02/test.sh dir02/, move multiple files separated by Spaces
    • mv /tmp/dir01 ~, move directory
    • mv -f hello.sh dir01If the destination directory contains files with the same name, run the-fOption to force overwriting files with the same name
    • mv dir01 dir02There is no rename command in Linux, butmvYou can implement the renaming function.

  • ln -sSymlinks are similar to shortcuts in Windows
    • ln -s hello.sh hello_slink, to the source filehello.shCreate a symbolic link with the link namehello_slinkOnce the source file is deleted, the symbolic link will break.
    • ls -il, view the fileinode-number

  • ln, hard links
    • ln hello.sh hello_hardlink, to the source filehello.shCreate a hard link namedhello_hardlink.

Hard links and symbolic links, although they both have the word “link” in their names, are actually quite different, not only because they exhibit different characteristics, but also because they work in very different ways.

To understand hard links, you first need to understand how files are stored in Linux.

In Linux, when a disk is partitioned and formatted, the partition is divided into two parts: Inode table and Data Block. Each file in Linux is also divided into two parts, one of which is a unique identifier for the fileinode-numberAnd other properties. The other part is the actual data of the file. Each file must have an inode, which holds all file information except the name of the file, including the fileinode-numberThe inode table contains all information contained in the inode, and the actual Data of the file is stored in the Data Block. For more informationThis article.

Superficially, we find the file name and open the file. In Linux, there are actually three steps to this process:

  1. The system finds the file by nameinode-number;
  2. According to theinode-numberFind the corresponding inode;
  3. Based on the information in the inode, find the block where the file is located and read the data.

Ok, now that you know how files are stored in Linux, hard linking is easy to understand. Hard links are multiple file names that correspond to oneinode-number.

  • ls -il.-i, view the fileinode-number

  • tar, pack the files,tarIs an ancient command whose original purpose was to back up files to tape,tape archive
    • tar -cf sample.tar demo.sh hello.sh test.sh
      • -f, specifies the package file name, and suffixes the package file name.tar“, so that when others see the file, they know it is a packaged file
      • -cAnd the same--createTo create a package file
    • tar -tf sample.tar
      • -tAnd the same--list, lists all file names in the package file
    • tar -f sample.tar --delete test.sh
      • --deleteDelete the specified file from the package file. Note that this is the long option, so use two hyphens--
    • tar -f sample.tar -r file01
      • -rAnd the same--appendAppend a new file to the package file
    • tar -f sample.tar -A sample01.tar
      • -AMerge the two package files
    • tar -xf sample01.tar
      • -xAnd the same--extractBy default, the package is unpacked to the current directory
    • tar -xf sample01.tar -C /tmp/dir01
      • -CUnpack the package to the specified directory

tarThe principle of a command is very simple. It is to concatenate multiple files into one large file,tarFiles will not be compressed.

The resulting package file is shown belowsample.tar, which is larger than the sum of all the files before packaging becausetarThe command packages the file with additional information, which proves ittarCommand does not compress files.

  • gzip, compressed files
    • gzip sample.tar, the compressed file replaces the source file
    • gzip -d sample.tar.gz
      • -dAnd the same--decompressor--uncompressDecompress the file. The decompressed file replaces the compressed file

gzipCommand can provide efficient compression, but does not provide packaging, sogzipYou can only compress files individually, not multiple files into a file or a directory. As a result,gzipAnd oftentarCommand used together,tarCommands provide special options to invokegzipCommand.

  • tar -czf sample.tar.gz file01 file02 file03That will befile01,fil02,file03These three files are packaged and then compressed.
    • -zAnd the same--gzip
  • tar -xzf sample02.tar.gzThat will besample01.tar.gzThe package is unpacked and then unpacked
    • -zAnd the same--ungzip

In addition, the compression ratio can be set during compression. The compression ratio is an integer in the range of1 ~ 9In between. Among them,1Is the lowest compression ratio, meaning that the compressed file is the largest, compression speed is the fastest;9Is the highest compression ratio, meaning that the compressed file is the smallest and the compression speed is the slowest. If the compression ratio is not set, the default compression ratio is6.

  • gzip -1 file01
  • gzip file02
  • gzip -9 file03

  • locate, find the file
    • locate .tar, find the name contained in.tarCharacter for all files

    locateThe lookup is fast because instead of traversing the hard disk, it looks up the database./var/lib/mlocate/mlocate.db, this database records all file names in the Linux system, but this database is not updated in real time, the default is updated every day, solocateYou may encounter the following problems when searching for files: Deleted files or newly created files cannot be found.

    • updatedb, can be updated/var/lib/mlocate/mlocate.dbThis database

    For the first time to uselocateWhen searching, you may encounter problems:Locate: can't perform the stat () '/ var/lib/mlocate/mlocate db' : don't have that file or directory.updatedbCan solve this problem.

  • find, find the file
    • Find files by filename
      • find / -name hello.shIn the root directory, find the file by the name of the file.-name, you must use the full filename to search,find / -name helloNot to be foundhello.sh.
    • Find files by file type
      • find /bin -type lListed,/binAll character link files in the directory
    • Find files based on time attributes
      • find /tmp/test -mtime -1 -lsListed,/tmp/testAll files (including directory files) whose contents have been modified within one day
      • find /tmp/test -mmin -10 -lsListed,/tmp/testIn this directory, all files whose contents have been modified within 10 minutes
      • find /tmp/test -ctime -1 -lsListed,/tmp/testIn the directory, all files whose file properties have been modified within 1 day
      • find /tmp/test -cmin -5 -lsListed,/tmp/testAll files whose file properties have been modified in the directory within 10 minutes
      • find /tmp/test -cmin -1 -lsListed,/tmp/testIn the directory, all files whose file properties have been modified within 1 minute
      • find /tmp/test -atime -1 -lsListed,/tmp/testAll files that have been read or executed within a day
      • find /tmp/test -amin -10 -lsListed,/tmp/testAll files that have been read or executed in the last 10 minutes
    • Find files by file size
      • find /tmp/test -size -2k -lsListed,/tmp/testAll files in a directory whose file size is smaller than 2k bytes
      • find /tmp/test -size -5k -lsListed,/tmp/testAll files in the directory whose file size is smaller than 5K bytes
      • find /tmp/test -size +100c -lsListed,//tmp/testAll files larger than 100 bytes in a directory
      • find /tmp/test -size 10M -lsListed,/tmp/testAll files in the directory whose file size is 10 MB
        • cAnd bytes.
        • kAnd the same1024c
          • +2k, the file size is greater than2k
          • -2kThe file size is smaller than2k
          • 2k, the file size is equal to2k
        • MAnd the same1024k
        • GAnd the same1024M
    • Search for files based on the file owner and user group
      • find /tmp/test -user admin -lsListed,/tmp/testThe owner of the directory isadminAll files of
      • find /tmp/test -group root -lsListed,/tmp/testThe directory is subordinate torootAll files of the user group
      • find /tmp/test -uid 1000 -lsListed,/tmp/testThe user ID in the directory is1000All files of
      • find /tmp/test -gid 0 -lsListed,/tmp/testThe ID of the subordinate user group under the directory is0All files of
    • Find files based on file permissions
      • find /tmp/test -perm 764 -lsListed,/tmp/testAll files in the directory with permission 764
      • find /tmp/test -perm 644 -lsListed,/tmp/testAll files in the directory with permission 644

As mentioned above, you can search for files based on file name, file type, time attribute, file size, file owner, subordinate user group, and file permissions. Of course, you can also search for files by combining these criteria with (-a,and), or (-o,or,or,or!), such asfind /tmp/test \( -size +1k -a -size -10M -a -type f \) -lsListed,/tmp/testThe file size in the directory is between1k~10MBetween all ordinary files.

  • find -execFind the file and perform the action. For details, see the video
    • find /tmp/test \( -size +1k -a -size -10M -a -type f \) -exec rm -rf {} \;
      • {}It’s going to be filled withfind /tmp/test \( -size +1k -a -size -10M -a -type f \)The results of the
      • \;Represents the end of the action
    • find /tmp/test \( -mmin -10 -a -type d \) -exec tar -cf {}.tar {} \;That will be/tmp/testPackage all directory files that have been modified in the last 10 minutes with the same name as the original directory.

  • The wildcard
    • *Represents a string of arbitrary length
      • rm -rf /tmp/test/*.txt, delete the/tmp/testThe file name is.txtSuffix for all files.
    • ?Represents any single character
      • ls -l /bin/?? shListed,/binUnder the directory, the file name contains four characters and starts withshAll files at the end.
    • []
      • [c1-c2], an ordered sequence of characters to match any single character. Like an ordered sequence of numbers,[1-4], including1,2,3,4; Like an ordered sequence of English letters,[a-c], includinga,b,c.
        • cp /tmp/test/file[1-4] /tmp/demoThat will be/tmp/testUnder thefile1,file2,file3,file4Copied to the/tmp/demodirectory
      • [c1,c2,c3...cn], a list of characters to match any single character.
        • rm -f /tmp/test/script[1,a,d].sh
    • {}, such as{string1,string2,string,... stringn}A list of strings to match any single string.
      • rm -f {script,file}*.shAnd delete thescript,fileAt the beginning of,.shAll files at the end
    • !, take the
      • tar -cf file.tar /tmp/test/file[!0-9].txt, in addition tofile0.txt,file1.txt,file2.txt,file3.txt,file4.txt,file5.txt,file6.txt,file7.txt,file8.txt,file9.txtOutside,/tmp/testUnder the name tofileAll files at the beginning are packaged tofile.tar.