laitimes

File management for Linux basic commands

适用范围:所有主流Linux发行版系统(CentOS、Ubuntu、Debian、DeepLinux、UOS、Kali Linux、Arch Linux等等)

1. ls command: used to list files and subdirectories in a directory.

  • Usage: ls [options] [file/directory]
  • 示例:ls -l /home
  • $ ls -l /home 总用量 4 drwxr-xr-x 2 user user 4096 1月 1 1970 test
  • Explanation: This is the result of the ls -l /home command. It lists the files and subdirectories in the /home directory and displays detailed file information, including permissions, owner, size, and date modified. In this example, there is only one subdirectory named test in the /home directory.
  • Common options:
    • -l: Displays files and directories in long form (details).
    • -a: Displays all files and directories, including those with . Hidden files at the beginning.
    • -h: Displays the file size in a human-readable format.
    • -r: Displays files and directories in reverse order.
    • -t: sorted by modification time, with the latest file or directory displayed first.
    • -R: Recursively lists all files and subdirectories in the directory.
  • These are just some of the common usages and options for the ls command. You can get more details and other options by looking at the help documentation for the ls command (man ls).

2. cd command: used to change the current working directory.

  • Usage: cd [table of contents]
  • 示例:cd /var/www/html
  • $ cd /var/www/html
  • Explanation: This command changes the current working directory to /var/www/html. Now you can execute other commands in this directory.
  • The cd command is used to switch directories. Its general syntax is cd<目录路径>. You can switch to the target directory by specifying an absolute path (a full path from the root) or a relative path (a path relative to the current working directory).
  • Special directory switching operations:
    • -:- is a special directory switch symbol that indicates switching to the previous working directory. For example, if you are currently in the /var/www/html directory, executing the cd - command will switch back to the previous working directory.
    • .:. indicates the current directory. It can be used as a reference point in a relative path. For example, if you are currently in the /var/www/html directory, executing the cd .command will remain in the current directory.
    • .. :.. Indicates the directory at the next level. It can be used as a reference point in a relative path. For example, if you are currently in the /var/www/html directory, execute cd .. The command will switch to the /var/www directory.

3. pwd command: used to display the path of the current working directory.

  • Usage: PWD
  • Example: PWD
  • $ pwd /var/www/html
  • Explanation: This is the result of the pwd command, which shows the path to the current working directory. In this example, the current working directory is /var/www/html.

4. mkdir command: used to create a new directory.

  • Usage: mkdir [option] directory name
  • 示例:mkdir test
  • $ mkdir test
  • Explanation: This command creates a new directory called test. It creates a new subdirectory under the current working directory.
  • Common options:
    • -p:递归地创建目录,如果父目录不存在则一并创建。 例如,mkdir -p /path/to/new/directory将创建/path/to/new/directory目录及其所有父目录。
    • -m: Sets the permission mode for the newly created directory. For example, mkdir -m 755 new_directory will create a directory named new_directory and set its permissions to 755.
    • -v: Displays the details of each directory created.
  • These are just some of the common usages and options for the mkdir command. You can get more details and other options by looking at the help documentation for the mkdir command (man mkdir).

5. rm command: used to delete files or directories.

  • Usage: rm [option] file/directory
  • 示例:rm file.txt
  • $ rm file.txt
  • Explanation: This command attempts to delete the file named file.txt. If the file exists and you have sufficient permissions, it will be deleted. If the file doesn't exist, you'll receive an error message.
  • Common options:
    • -r: Recursively deletes the directory and its contents. If you want to delete a directory, you must use this option. For example, rm -r directory will delete the directory directory and all its contents.
    • -f: Force deletion of a file or directory without confirmation. Be careful when using this option, as deletion is irreversible. For example, rm -rf directory will forcibly delete the directory directory and all its contents without confirmation.
    • -i: Interactive deletion, confirmation will be prompted before each deletion. For example, rm -i file will prompt for confirmation before deleting the file.
    • -v: Displays the details of each deleted file or directory.
  • These are just some of the common usages and options for rm commands. You can get more details and other options by looking at the help documentation for the rm command (man rm).
  • Note that you should be careful when deleting files or directories using the rm command, as the deletion is irreversible. Make sure that the files or directories you want to delete are the ones you want to delete, and that you back up important data.

6. cp command: used to copy files or directories.

  • Usage: cp [option] source file target file/directory.
  • 示例:cp file.txt /tmp
  • $ cp file.txt /tmp
  • Explanation: This command attempts to copy the file named file.txt to the /tmp directory. If the file exists and you have sufficient permissions, it will be copied to the target directory. If the file doesn't exist, you'll receive an error message.
  • Common options:
    • -r:递归地复制目录及其内容。 如果要复制目录,必须使用此选项。 例如,cp -r directory1 directory2将递归地复制directory1目录及其所有内容到directory2目录。
    • -i: Interactive copying, confirmation will be prompted before each copy. For example, cp -i file1 file2 will prompt confirmation before copying file1 to file2.
    • -v: Displays the details of each copied file or directory.
  • These are just some of the common usages and options for cp commands. You can get more details and other options by looking at the help documentation for the cp command (man cp).
  • Note that when using the cp command to copy files or directories, be careful to ensure that no files or directories with the same name exist in the destination location to avoid overwriting the original data.

7. MV command: used to move files or directories, or rename files or directories.

  • Usage: MV [Option] Source File/Directory Target File/Directory
  • 示例:mv file.txt /tmp
  • $ mv file.txt /tmp
  • Explanation: This command attempts to move the file named file.txt to the /tmp directory. If the file exists and you have sufficient permissions, it will be moved to the target directory. If the file doesn't exist, you'll receive an error message.
  • $ mv file.txt newfile.txt
  • Explanation: Rename the file named file.txt to newfile.txt.
  • Common options:
    • -i: Interactive move, confirmation will be prompted before each move. For example, mv -i file1 file2 will prompt confirmation before moving file1 to file2.
    • -v: Displays the details of each moved file or directory.
  • These are just some of the common usages and options for mv commands. You can get more details and other options by looking at the help documentation for the mv command (man mv).
  • Please note that when using the mv command to move files or directories, be careful to ensure that no files or directories with the same name exist in the destination location to avoid overwriting the original data.

8. touch command: used to create an empty file or update the access and modification time of the file.

  • Usage: touch [options] file
  • 示例:touch file.txt
  • $ touch file.txt
  • Explanation: This command creates an empty file named file.txt. If the file does not exist, it will be created. If the file already exists, it will update the access and modification times of the file.
  • List the current directory file and subdirectories again
  • $ ls -l total 4 drwxr-xr-x 2 user user 4096 Jan 1 1970 test -rw-r--r-- 1 user user 0 Jan 1 1970 file.txt
  • Explanation: This is the result of the ls -l command, which lists the files and subdirectories in the current directory and displays detailed file information, including permissions, owner, size, and date modified. In this example, there is a subdirectory named test and a file named file.txt in the current directory.

Read on