Linux manipulates files via the command line, including viewing, editing, copying, moving, deleting, creating, searching, matching, compressing, and uncompressing files, and it’s important to learn how to use the documentation.
Viewing
cat
1 | $ # Output the entire contents of the FILE file |
cat
is short for concat*enate. The actual function of the cat
utility is to concatenate multiple files and then output them. When there is only one file, cat
will output that file directly, so the most common use of cat
is to output a single file.
less
1 | $ # Output the contents of a FILE file in an interactive window |
cat
prints the entire contents to the terminal at once and exits, while less
displays one page at a time and supports forward/backward scrolling, searching, etc. If you want to find a part of a large file, less
is usually much more convenient than cat
.
Similar commands to less
: more
, most
.
Editing
Nano is a command-line text editor that comes with many machines and is more novice-friendly than vim and emacs.
1 | $ nano FILE # Use nano to edit FILE files |
Copying
1 | $ # Copy the SOURCE file to the DEST file, the resulting file is DEST |
Hard links and soft links
Hard links have the same inode as the source file and both point to the same location on disk. Deleting one of them does not affect the other.
Soft links have a different inode than the source file. Soft links save the path to the source file, and when accessing a soft link, the accessed path is replaced with the path to the source file, so accessing a soft link is also equivalent to accessing the source file. However, if the source file is deleted, the path saved by the soft link is invalid, and the soft link is therefore invalid as well1.
Moving
1 | $ # Move the SOURCE file to the DEST file |
Similar to “cut” in Windows.
Deleting
1 | $ # Delete FILE files, FILE can be multiple files. |
Creating
Creating directories
1 | $ # Create a directory named DIR_NAME |
Creating files
1 | $ # Create a file named FILE_NAME |
The stat
command displays information about a file’s attributes.
What touch
actually does is modify the access time (atime) and modification time (mtime) of the file, which can be treated as touching the file, causing its access and modification times to change. When the file does not exist, touch
creates a new file.
Searching
1 | $ # Search for files in PATH based on EXPRESSION |
Pattern matching
The matching pattern for bash is called glob.
1 | $ # e.g. |
Be sure to make sure you type correctly before using wildcards, otherwise serious consequences may occur (e.g. rm -rf *
will delete all files in the current directory).
Compressing and uncompressing
Using tar to archive and compress files
1 | $ # The command format is as follows, please refer to the following sample usage to understand how to use |
As with most Linux commands, the tar
command can combine parameters. For example, the following command is equivalent:
1 | $ tar -c -z -v -f target.tar test/ |
The suffix name does not determine the file type, but it can help one to identify the possible file types of this file and thus choose the appropriate opening method.
Documentation
man commands
1 | $ # Call up the documentation for the tar and ls commands |
It shows the meaning of each parameter of the software, the meaning of the exit value, the author, etc. It is large and comprehensive. But generally less with the use of sample, you need to splice the software parameters according to their own needs, not conducive to quickly get started.
A few simple examples allow users to quickly understand how to use the software.
1 | $ tldr COMMAND |
Note that if this error is reported:
1 | mkdir:Unable to create directory "/home/lzy/.tldr": file already exists |
The solution is:
1 | $ sudo apt-get update |
Comments