Linux File Content Manage
# Linux File and Directory Management \\n\\nWe know that the Linux directory structure is tree-like, with the root directory `/` at the top.\\n\\nOther directories can be added to the tree by mounting them and removed by unmounting them.\\n\\nBefore starting this tutorial, we need to understand absolute paths and relative paths.\\n\\n* **Absolute Path:**\\n\\nThe path is written starting from the root directory `/`, for example: the directory `/usr/share/doc`.\\n\\n* **Relative Path:**\\n\\nThe path is not written starting from `/`. For example, to go from `/usr/share/doc` to `/usr/share/man`, you can write: `cd ../man`. This is the relative path notation.\\n\\n* * *\\n\\n## Common Commands for Managing Directories\\n\\nNext, let's look at some common commands for managing directories:\\n\\n* `ls` (list files): Lists directory contents and file names.\\n* `cd` (change directory): Changes the current directory.\\n* `pwd` (print work directory): Displays the current working directory.\\n* `mkdir` (make directory): Creates a new directory.\\n* `rmdir` (remove directory): Deletes an empty directory.\\n* `cp` (copy file): Copies files or directories.\\n* `rm` (remove): Deletes files or directories.\\n* `mv` (move file): Moves files and directories, or renames them.\\n\\nYou can use `man ` to view the documentation for each command, for example: `man cp`.\\n\\n### ls (List Directory)\\n\\nIn Linux systems, the `ls` command is probably one of the most frequently used.\\n\\nSyntax:\\n\\n[root@www ~]# ls Directory name\\n[root@www ~]# ls [--color={never,auto,always}] Directory name\\n[root@www ~]# ls Directory name\\n\\nOptions and Parameters:\\n\\n* `-a`: Lists all files, including hidden files (files starting with `.`). (Commonly used)\\n* `-d`: Lists only the directory itself, not its contents. (Commonly used)\\n* `-l`: Lists in long format, including file attributes and permissions. (Commonly used)\\n\\nList all files in a directory (including attributes and hidden files):\\n\\n[root@www ~]# ls -al ~\\n\\n### cd (Change Directory)\\n\\n`cd` is an abbreviation for Change Directory. It is used to change the current working directory.\\n\\nSyntax:\\n\\ncd \\n\\nExample: Use the `mkdir` command to create a `` directory.\\n\\n[root@www ~]# mkdir \\n\\nUse an absolute path to switch to the `` directory.\\n\\n[root@www ~]# cd /root//\\n\\nUse a relative path to switch to the `` directory.\\n\\n[root@www ~]# cd .//\\n\\n`~` represents returning to your home directory, which is `/root`.\\n\\n[root@www ]# cd ~\\n\\n`..` represents going to the parent directory of the current directory, which is the parent of `/root`.\\n\\n[root@www ~]# cd ..\\n\\nAfter practicing a few times, you should have a good understanding of the `cd` command.\\n\\n### pwd (Print Working Directory)\\n\\n`pwd` is an abbreviation for **Print Working Directory**, which displays the current working directory.\\n\\n[root@www ~]# pwd \\n\\nOptions and Parameters:\\n\\n* `-P`: Displays the actual physical path, not the symbolic link path.\\n\\nExample: Simply display the current working directory:\\n\\n[root@www ~]# pwd\\n/root\\n\\nExample: Display the actual working directory, not just the name of the symbolic link.\\n\\n[root@www ~]# cd /var/mail\\nNote,/var/mailis a link file\\n[root@www mail]# pwd\\n/var/mail\\nList the current working directory\\n[root@www mail]# pwd -P\\n/var/spool/mail\\nWhat happened? Did you add -P Very differentο½\\n[root@www mail]# ls -ld /var/mail\\nlrwxrwxrwx 1 root root 10 Sep 4 17:54 /var/mail -> spool/mail\\n# You should know why after seeing this, because /var/mail is a link file, linked to /var/spool/mail\\n# So, add pwd -P After the option, instead of showing the link file data, it shows the correct full path!\\n\\n### mkdir (Make Directory)\\n\\nIf you want to create a new directory, use `mkdir` (make directory).\\n\\nSyntax:\\n\\nmkdir Directory name\\n\\nOptions and Parameters:\\n\\n* `-m`: Configure file permissions directly, without relying on the default permissions (umask).\\n* `-p`: Helps you create nested directories recursively, including parent directories.\\n\\nExample: Try creating several new directories under `/tmp`:\\n\\n[root@www ~]# cd /tmp\\n[root@www tmp]# mkdir test\\nCreate a new directory named test\\n[root@www tmp]# mkdir test1/test2/test3/test4\\nmkdir: cannot create directory `test1/test2/test3/test4': No such file or directory\\nCannot create this directory directly!\\n[root@www tmp]# mkdir -p test1/test2/test3/test4\\n\\nWith the `-p` option, it can create multiple levels of directories for you!\\n\\nExample: Create a directory with permissions **rwx--x--x**.\\n\\n[root@www tmp]# mkdir -m 711 test2\\n[root@www tmp]# ls -l\\ndrwxr-xr-x 3 root root 4096 Jul 18 12:50 test\\ndrwxr-xr-x 3 root root 4096 Jul 18 12:53 test1\\ndrwx--x--x 2 root root 4096 Jul 18 12:54 test2\\n\\nIf you don't use `-m` to force the permissions, the system will use the default attributes.\\n\\nIf we use `-m`, as in the example above with `-m 711`, we give the new directory the permissions `drwx--x--x`.\\n\\n### rmdir (Remove Empty Directory)\\n\\nSyntax:\\n\\nrmdir Directory name\\n\\nOptions and Parameters:\\n\\n* `-p`: Deletes multiple levels of empty directories starting from the specified directory.\\n\\nDelete the `` directory:\\n\\n[root@www tmp]# rmdir /\\n\\nDelete the directories created in the `mkdir` example (under `/tmp`):\\n\\n[root@www tmp]# ls -l\\nSee how many directories exist?\\ndrwxr-xr-x 3 root root 4096 Jul 18 12:50 test\\ndrwxr-xr-x 3 root root 4096 Jul 18 12:53 test1\\ndrwx--x--x 2 root root 4096 Jul 18 12:54 test2\\n[root@www tmp]# rmdir test\\nCan be directly deleted, no problem\\n[root@www tmp]# rmdir test1\\nCannot delete because there is still content!\\nrmdir: `test1': Directory not empty\\n[root@www tmp]# rmdir -p test1/test2/test3/test4\\n[root@www tmp]# ls -l\\nLook, test and test1 are gone in the output below!\\ndrwx--x--x 2 root root 4096 Jul 18 12:54 test2\\n\\nUsing the `-p` option, you can immediately delete `test1/test2/test3/test4` all at once.\\n\\nHowever, note that `rmdir` can only delete empty directories. You can use the `rm` command to delete non-empty directories.\\n\\n### cp (Copy File or Directory)\\n\\n`cp` is for copying files and directories.\\n\\nSyntax:\\n\\n[root@www ~]# cp Source file(source) Target file(destination)\\n[root@www ~]# cp source1 source2 source3 .... directory\\n\\nOptions and Parameters:\\n\\n* `-a`: Equivalent to `-pdr`. See the explanations for `p`, `d`, and `r` below. (Commonly used)\\n* `-d`: If the source file is a symbolic link, copy the link's attributes, not the file itself.\\n* `-f`: Force. If the destination file exists and cannot be opened, remove it and try again.\\n* `-i`: If the destination file already exists, prompt before overwriting. (Commonly used)\\n* `-l`: Create a hard link instead of copying the file itself.\\n* `-p`: Copy the file along with its attributes, rather than using the default attributes (commonly used for backups).\\n* `-r`: Recursively copy, used for copying directories. (Commonly used)\\n* `-s`: Create a symbolic link (shortcut) instead of copying the file.\\n* `-u`: Update the destination only if the source is newer.\\n\\nAs root, copy `.bashrc` from the root directory to `/tmp` and name it `bashrc`:\\n\\n[root@www ~]# cp ~/.bashrc /tmp/bashrc\\n[root@www ~]# cp -i ~/.bashrc /tmp/bashrc\\ncp: overwrite `/tmp/bashrc'? n\\nnNot overwrite, y to overwrite\\n\\n### rm (Remove File or Directory)\\n\\nSyntax:\\n\\nrm Filesor directory\\n\\nOptions and Parameters:\\n\\n* `-f`: Force. Ignores nonexistent files and does not show warning messages.\\n* `-i`: Interactive mode. Prompts the user before deletion.\\n* `-r`: Recursive deletion! Most commonly used for deleting directories. This is a very dangerous option!!!\\n\\nDelete the `bashrc` file created in the `cp` example:\\n\\n[root@www tmp]# rm -i bashrc\\nrm: remove regular file `bashrc'? y\\n\\nIf you add the `-i` option, it will actively prompt you, preventing accidental deletion of the wrong file!\\n\\n### mv (Move File and Directory, or Rename)\\n\\nSyntax:\\n\\n[root@www ~]# mv source destination\\n[root@www ~]# mv source1 source2 source3 .... directory\\n\\nOptions and Parameters:\\n\\n* `-f`: Force. If the destination file exists, it will overwrite without prompting.\\n* `-i`: If the destination file already exists, it will prompt before overwriting.\\n* `-u`: Update. Only overwrite the destination if the source is newer.\\n\\nCopy a file, create a directory, and move the file into the directory:\\n\\n[root@www ~]# cd /tmp\\n[root@www tmp]# cp ~/.bashrc bashrc\\n[root@www tmp]# mkdir mvtest\\n[root@www tmp]# mv bashrc mvtest\\n\\nTo move a file into a directory, do it like this!\\n\\nRename the directory to `mvtest2`:\\n\\n[root@www tmp]# mv mvtest mvtest2\\n\\n* * *\\n\\n## Viewing File Contents in Linux\\n\\nThe following commands are used to view file contents in Linux:\\n\\n* `cat`: Displays file content starting from the first line.\\n* `tac`: Displays content starting from the last line. Notice that `tac` is `cat` spelled backwards!\\n* `nl`: Displays content with line numbers.\\n* `more`: Displays file content one page at a time.\\n* `less`: Similar to `more`, but better because it allows scrolling backward!\\n* `head`: Displays only the first few lines.\\n* `tail`: Displays only the last few lines.\\n\\nYou can use `man ` to view the documentation for each command, for example: `man cp`.\\n\\n### cat\\n\\nDisplays file content starting from the first line.\\n\\nSyntax:\\n\\ncat \\n\\nOptions and Parameters:\\n\\n* `-A`: Equivalent to the combined options `-vET`. Lists some special characters instead of just spaces.\\n* `-b`: Numbers non-blank lines only. Blank lines are not numbered!\\n* `-E`: Displays the line-ending character `$`.\\n* `-n`: Numbers all lines, including blank lines. Different from the `-b` option.\\n* `-T`: Displays `` characters as `^I`.\\n* `-v`: Lists some non-printable special characters.\\n\\nView the content of `/etc/issue`:\\n\\n[root@www ~]# cat /etc/issue\\nCentOS release 6.4 (Final)\\nKernel r on an m\\n\\n### tac\\n\\nThe `tac` command is the reverse of `cat`. It displays file content starting from the last line. Notice that `tac` is `cat` spelled backwards! For example:\\n\\n[root@www ~]# tac /etc/issue\\nKernel r on an m\\nCentOS release 6.4 (Final)\\n\\n### nl\\n\\nDisplays line numbers.\\n\\nSyntax:\\n\\nnl Files\\n\\nOptions and Parameters:\\n\\n* `-b`: Specifies the method for line numbering. There are two main types:\\n * `-b a`: Numbers all lines, whether blank or not (similar to `cat -n`).\\n * `-b t`: Does not number blank lines (default).\\n* `-n`: Specifies the format for line numbers. There are three main types:\\n * `-n ln`: Line numbers are displayed at the far left of the screen.\\n * `-n rn`: Line numbers are displayed at the far right of their column, without leading zeros.\\n * `-n rz`: Line numbers are displayed at the far right of their column, with leading zeros.\\n* `-w`: Specifies the width of the line number field.\\n\\nExample 1: Use `nl` to list the contents of `/etc/issue`:\\n\\n[root@www ~]# nl /etc/issue\\n 1 CentOS release 6.4 (Final)\\n 2 Kernel r on an m\\n\\n### more\\n\\nScrolls through content page by page.\\n\\n[root@www ~]# more /etc/man_db.config\\n## Generated automatically from man.conf.in by the\\n# configure script.\\n## man.conf from man-1.6d\\n....(Middle omitted)....\\n--More--(28%)\\n\\nThe key is on this line! Your cursor will also wait here for your command\\n\\nWhile the `more` program is running, you can use the following keys:\\n\\n* Spacebar: Scrolls down one page.\\n* Enter: Scrolls down one line.\\n* `/string`: Searches forward for `string` in the displayed content.\\n* `:f`: Immediately displays the filename and the current line number.\\n* `q`: Exits `more` immediately and stops displaying the file content.\\n* `b` or `-b`: Scrolls backward one page. This action only works for files, not for pipes.\\n\\n### less\\n\\nScrolls through content page by page. The following example outputs the content of the `/etc/man.config` file:\\n\\n[root@www ~]# less /etc/man.config\\n## Generated automatically from man.conf.in by the\\n# configure script.\\n## man.conf from man-1.6d\\n....(Middle omitted)....\\n:\\n\\nHere you can wait to enter your command!\\n\\nCommands you can input while `less` is running:\\n\\n* Spacebar: Scrolls down one page.\\n* ``: Scrolls down one page.\\n* ``: Scrolls up one page.\\n* `/string`: Searches forward for `string`.\\n* `?string`: Searches backward for `string`.\\n* `n`: Repeats the previous search (related to `/` or `?`!).\\n* `N`: Repeats the previous search in the opposite direction (related to `/` or `?`!).\\n* `q`: Exits the `less` program.\\n\\n### head\\n\\nDisplays the first few lines of a file.\\n\\nSyntax:\\n\\nhead Files\\n\\nOptions and Parameters:\\n\\n* `-n`: Followed by a number, specifies how many lines to display.\\n\\n[root@www ~]# head /etc/man.config\\n\\nBy default, it displays the first 10 lines! To display the first 20 lines, do this:\\n\\n[root@www ~]# head -n 20 /etc/man.config\\n\\n### tail\\n\\nDisplays the last few lines of a file.\\n\\nSyntax:\\n\\ntail Files\\n\\nOptions and Parameters:\\n\\n* `-n`: Followed by a number, specifies how many lines to display.\\n* `-f`: Continuously monitors the specified file. Press `-c` to stop monitoring.\\n\\n[root@www ~]# tail /etc/man.config\\n# By default, it shows the last 10 lines! To show the last 20 lines, do this:\\n[root@www ~]# tail -n 20 /etc/man.config
YouTip