Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the Linux commands related to the operation of 5 file directories

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

The editor today takes you to understand how to understand the Linux commands related to the operation of 5 file directories. The knowledge points in the article are introduced in great detail. Friends who feel helpful can browse the content of the article together with the editor, hoping to help more friends who want to solve this problem to find the answer to the problem. Let's follow the editor to learn more about how to understand Linux commands related to the operation of 5 file directories.

Pwd command

Use the pwd command in Linux to see the full path to the "current working directory". To put it simply, every time you operate on the terminal, you will have a current working directory.

When the current location is uncertain, pwd is used to determine the exact location of the current directory within the file system.

1. Command format:

Pwd [options]

2. Command function:

View the full path to the current working directory

3. Common parameters:

Normally, there are no parameters.

If the directory is a link:

Format: pwd-P shows the actual path instead of using the link path.

4. Common examples:

Example 1: use the pwd command to view the full path of the default working directory

Command:

Pwd

Output:

[root@localhost ~] # pwd/root [root@localhost ~] #

Example 2: use the pwd command to view the specified folder

Command:

Pwd

Output:

[root@localhost ~] # cd / opt/soft/ [root@localhost soft] # pwd / opt/soft [root@localhost soft] #

Example 3: when a directory connects to a link, pwd-P shows the actual path instead of using the link path; pwd shows the connection path

Command:

Pwd-P

Output:

[root@localhost soft] # cd / etc/init.d [root@localhost init.d] # pwd/etc/init.d [root@localhost init.d] # pwd-P/etc/rc.d/init.d [root@localhost init.d] #

Instance 4:/bin/pwd

Command:

/ bin/pwd

Options:

When the-L directory connects to the link, output the connection path

-P output physical path

Output:

[root@localhost init.d] # / bin/pwd / etc/rc.d/init.d [root@localhost init.d] # / bin/pwd-- help [root@localhost init.d] # / bin/pwd-P/etc/rc.d/init.d [root@localhost init.d] # / bin/pwd-L/etc/init.d [root@localhost init.d] #

Example 5: the current directory is deleted, but the pwd command still shows that directory

Output:

[root@localhost init.d] # cd / opt/soft [root@localhost soft] # mkdir removed [root@localhost soft] # cd removed/ [root@localhost removed] # pwd/opt/soft/removed [root@localhost removed] # rm.. / removed-rf [root@localhost removed] # pwd/opt/soft/removed [root@localhost removed] # / bin/pwd/bin/pwd: couldn't find directory entry in ".." With matching i-node [root@localhost removed] # cd [root@localhost ~] # pwd/root [root@localhost ~] #

Cd command

Linux cd command can be said to be the most basic command statement in Linux. Other command statements to operate are based on the use of cd command.

Therefore, to learn the common commands of Linux, we must first learn the skills of using cd commands.

1. Command format:

Cd [directory name]

two。 Command function:

Switch the current directory to dirName

3. Common examples

Example 1: enter the system root directory

Command:

Cd /

Output:

[root@localhost ~] # cd /

Description:

Enter the system root directory. After the above command is executed, take a look at the ls command. The current directory has already reached the system root directory.

Command:

Cd.. Or cd.. / /

Output:

[root@localhost soft] # pwd/opt/soft [root@localhost soft] # cd.. [root@localhost opt] # cd.. / / [root@localhost /] # pwd/

Description:

If you enter the root directory of the system, you can use "cd.." to go straight back to the root directory.

Command:

Cd.. /.. / /

Output:

[root@localhost soft] # pwd/opt/soft [root@localhost soft] # cd.. /.. / / [root@localhost /] # pwd/ [root@localhost /] #

Description: use the cd command to enter the parent directory of the current directory.

Example 2: use the cd command to enter the current user's home directory

The current user home directory and the system root directory are two different concepts. There are two ways to enter the current user's home directory.

Command 1:

Cd

Output:

[root@localhost soft] # pwd/opt/soft [root@localhost soft] # cd [root@localhost ~] # pwd/root

Command 2:

Cd ~

Output:

[root@localhost ~] # cd / opt/soft/ [root@localhost soft] # pwd/opt/soft [root@localhost soft] # cd ~ [root@localhost ~] # pwd/root

Example 3: jump to the specified directory

Command:

Cd / opt/soft

Output:

[root@localhost ~] # cd / opt/soft [root@localhost soft] # pwd/opt/soft [root@localhost soft] # cd jdk1.6.0_16/ [root @ localhost jdk1.6.0_16] # pwd/opt/soft/jdk1.6.0_16 [root@localhost jdk1.6.0_16] #

Description:

Jump to the specified directory, starting from the root directory, the directory name is preceded by /, and the subdirectories in the current directory can write the name directly.

Example 4: return to the directory where you were before you entered this directory

Command:

Cd-

Output:

[root@localhost soft] # pwd/opt/soft [root@localhost soft] # cd-/ root [root@localhost ~] # pwd/root [root@localhost ~] # cd-/ opt/soft [root@localhost soft] #

Example 5: use the parameter of the previous command as the cd parameter.

Command:

Cd! $

Output:

[root@localhost soft] # cd! $cd-/ root [root@localhost ~] # cd! $cd-/ opt/soft [root@localhost soft] #

Ls command

The ls command is the most commonly used command under linux. The ls command is the abbreviation of list  by default ls is used to print out the list of the current directory  if ls specifies another directory  then it will display a list of files and folders in the specified directory. Through the ls command, you can view not only the file  contained in the linux folder, but also the file permissions (including directories, folders, file permissions)  to view directory information, and so on. The ls command is often used in daily linux operations!

1. Command format:

Ls [option] [directory name]

two。 Command function:

Lists all subdirectories and files in the target directory.

3. Common parameters:

-a,-all lists all the files in the directory, including. The implied file at the beginning

-An is the same as-a, but not listed "." (represents the current directory) and ".." Represents the parent directory of the current directory.

-c fit-lt: sort by ctime and display ctime (time when the file status last changed) match-l: show ctime but sort by name otherwise: sort by ctime

-C list items from top to bottom in each column

-color [= WHEN] controls whether color resolution files are used. WHEN can be one of 'never',' always', or 'auto'

-d,-directory displays the directory like a file, not the file under it.

-D,-dired produces results suitable for the use of dired mode for Emacs

-f does not sort the output files,-aU option takes effect,-lst option is invalid

-g is similar to-l, but does not list the owner

-G,-no-group does not list any information about the group

-h,-human-readable lists file sizes in an easy-to-understand format (for example, 1K 234m 2G)

-si is similar to-h, but the file size is to the power of 1000 instead of 1024

-H,-dereference-command-line indicates the real destination using symbolic links on the command line

-indicator-style= mode specifies that each project name is followed by an indicator: none (default), classify (- F), file-type (- p)

-I,-inode prints the inode number of each file

-I,-ignore= style does not print any items that conform to shell universal characters

-k, or-block-size=1K, represents the file size in k bytes.

-l in addition to the file name, the file permissions, owner, file size and other information are listed in detail.

-L,-dereference when displaying the file information of the symbolic link, displays the object indicated by the symbolic link rather than the information of the symbolic link itself

-m all items are separated by commas and fill the entire line width

-o similar to-l, displays the details of the file except for the group information.

-r,-reverse in reverse order

-R,-recursive lists all subdirectory layers simultaneously

-s,-size lists the size of all files in block size

-S sorts by file size

-sort=WORD the following are the available WORD and the corresponding options they represent:

Extension-X status-c

None-U time-t

Size-S atime-u

Time-t access-u

Version-v use-u

-t sort by file modification time

-u match-lt: displays the access time and sorts by access time

Match-l: display access time but sort by name

Otherwise: sort by access time

-U does not sort; items are listed in the original order of the file system

-v sort by version

-w,-width=COLS specifies the screen width instead of the current value

-x list items line by line instead of column by column

-X sorts by extension

-1 only one file is listed per line

-help displays this help and leaves

-version displays version information and leaves

4. Common examples:

Example 1: list the details of all files and directories under the / home/peidachang folder

Command:

Ls-l-R / home/peidachang

In the use of ls command should pay attention to the format of the command: after the command prompt, the first is the command keyword, followed by the command parameters, before the command parameters to have a short dash "-", all the command parameters have a specific role, you can choose one or more parameters, after the command parameters is the command operation object. In the above command "ls-l-R / home/peidachang", "ls" is the command keyword, "- l-R" is the parameter, and "/ home/peidachang" is the operator of the command. In this command, two parameters are used, "l" and "R". Of course, you can also use them together, as shown below:

Command:

Ls-lR / home/peidachang

The result of this form is exactly the same as that of the above command form. In addition, if the operating object of the command is located in the current directory, you can operate on the operating object directly; if it is not in the current directory, you need to give the full path to the operating object, for example, in the above example, my current folder is the peidachang folder, and I want to operate on the peidachang file under the home folder. I can type ls-lR peidachang directly or use ls-lR / home/peidachang.

Example 2: to list the details of all directories starting with "t" in the current directory, you can use the following command:

Command:

Ls-l t *

You can view information about all files in the current directory whose file names begin with "t". In fact, in the command format, the contents in square brackets can be omitted. For the command ls, if you omit the command parameters and operands and type "ls" directly, a list of the contents of the current working directory will be listed.

Example 3: list only the subdirectories under the file

Command: ls-F / opt/soft | grep / $

List the subdirectories under the / opt/soft file

Output:

[root@localhost opt] # ls-F / opt/soft | grep / $jdk1.6.0_16/subversion-1.6.1/tomcat6.0.32/

Command:

Ls-l / opt/soft | grep "^ d"

List the details of the subdirectories under the / opt/soft file

Output:

[root@localhost opt] # ls-l / opt/soft | grep "^ d" drwxr-xr-x 10 root root 4096 09-17 18:17 jdk1.6.0_16drwxr-xr-x 16 1016 1016 4096 10-11 03:25 subversion-1.6.1drwxr-xr-x 9 root root 4096 2011-11-01 tomcat6.0.32

Example 4: list all files whose names begin with s in the current working directory. The newer the files, you can use the following command:

Command:

Ls-ltr s*

Output:

[root@localhost opt] # ls-ltr s*src: total 0script: total 0soft: total 350644drwxr-xr-x 9 root root 4096 2011-11-01 tomcat6.0.32-rwxr-xr-x 1 root root 81871260 09-17 18:15 jdk-6u16-linux-x64.bindrwxr-xr-x 10 root root 4096 09-17 18:17 jdk1.6.0_16-rw-r--r-- 1 root root 205831281 09-17 18:33 apache-tomcat-6.0 .32.tar.gz-rw-r--r-- 1 root root 5457684 09-21 00:23 tomcat6.0.32.tar.gz-rw-r--r-- 1 root root 4726179 10-10 11:08 subversion-deps-1.6.1.tar.gz-rw-r--r-- 1 root root 7501026 10-10 11:08 subversion-1.6.1.tar.gzdrwxr-xr-x 16 1016 1016 4096 10-11 03:25 subversion-1.6.1

Example 5: list all files and directories under the current working directory; add "/" to the name of the directory, and "*" after the name of the executable file.

Command:

Ls-AF

Output:

[root@localhost opt] # ls-AFlog/ script/ soft/ src/ svndata/ web/

Example 6: calculate the number of files and directories in the current directory

Command:

Ls-l * | grep "^ -" | wc-l-number of files

Ls-l * | grep "^ d" | wc-l-number of directories

Example 7: list the absolute path of the file in ls

Command:

Ls | sed "s: ^: pwd/:"

Output:

[root@localhost opt] # ls | sed "s: ^: `pwd` /:" / opt/log/opt/script/opt/soft/opt/src/opt/svndata/opt/web

Example 8: lists the absolute paths of all files (including hidden files) in the current directory without recursion to the directory

Command:

Find $PWD-maxdepth 1 | xargs ls-ld

Output:

[root@localhost opt] # find $PWD-maxdepth 1 | xargs ls-lddrwxr-xr-x 8 root root 4096 10-11 03:43 / optdrwxr-xr-x 2 root root 4096 2012-03-08 / opt/logdrwxr-xr-x 2 root root 4096 2012-03-08 / opt/scriptdrwxr-xr-x 5 root root 4096 10-11 03:21 / opt/softdrwxr-xr-x 2 root root 4096 2012-03-08 / opt/srcdrwxr-xr-x 4 root root 4096 10-11 05:22 / opt/svndatadrwxr -xr-x 4 root root 4096 10-09 00:45 / opt/web

Example 9: recursively lists the absolute paths of all files (including hidden files) in the current directory

Command:

Find $PWD | xargs ls-ld

Example 10: specify the file time output format

Command:

Ls-tl-time-style=full-iso

Output:

[root@localhost soft] # ls-tl-- time-style=full-iso Total 350644drwxr-xr-x 16 1016 1016 4096 2012-10-11 03VV 25purl 58.000000000 + 0800 subversion-1.6.1

Ls-ctl-time-style=long-iso

Output:

[root@localhost soft] # ls-ctl-- time-style=long-iso Total 350644drwxr-xr-x 16 1016 1016 4096 2012-10-11 03:25 subversion-1.6.1

Extend:

\ 1. Display a list of color directories

Open / etc/bashrc and add the following line:

Alias ls= "ls-color"

The next time you start bash, you can display a color directory list as in Slackware, where the meaning of the color is as follows:

\ 1. Blue-> directory

\ 2. Green-> executable file

\ 3. Red-> compressed file

\ 4. Light blue-> linked file

\ 5. Gray-> other files

Mkdir command

The linux mkdir command is used to create a directory with the specified name, requiring that the user who created the directory have write permission in the current directory, and that the specified directory name cannot be an existing directory in the current directory.

1. Command format:

Mkdir [options] directory …

2. Command function:

The mkdir command enables you to create a folder or directory named after DirName (the specified file name) at a specified location. The user who wants to create a folder or directory must have write permission to the parent folder of the folder you create. Also, the folder (directory) created cannot have the same name as the file name in its parent directory (that is, the parent folder), that is, it cannot have the same name (case-sensitive) in the same directory.

3. Command parameters:

-m,-mode= mode, setting permissions (similar to chmod) instead of rwxrwxrwx minus umask

-p,-parents can be a path name. At this point, if some directories in the path do not already exist, if you add this option, the system will automatically establish those directories that do not already exist, that is, multiple directories can be established at a time.

-v,-verbose displays information every time a new directory is created

-help displays this help and exits

-version outputs version information and exits

4. Command example:

Example 1: create an empty directory

Command:

Mkdir test1

Output:

[root@localhost soft] # cd test [root@localhost test] # mkdir test1 [root@localhost test] # ll Total 4drwxr-xr-x 2 root root 4096 10-25 17:42 test1 [root@localhost test] #

Example 2: recursively create multiple directories

Command:

Mkdir-p test2/test22

Output:

[root@localhost test] # mkdir-p test2/test22 [root@localhost test] # ll Total 8drwxr-xr-x 2 root root 4096 10-25 17:42 test1drwxr-xr-x 3 root root 4096 10-25 17:44 test2 [root@localhost test] # cd test2/ [root@localhost test2] # ll Total 4drwxr-xr-x 2 root root 4096 10-25 17:44 test22 [root@localhost test2] #

Example 3: create a directory with permissions of 777

Command:

Mkdir-m 777 test3

Output:

[root@localhost test] # mkdir-m 777 test3 [root@localhost test] # ll Total 12drwxr-xr-x 2 root root 4096 10-25 17:42 test1drwxr-xr-x 3 root root 4096 10-25 17:44 test2drwxrwxrwx 2 root root 4096 10-25 17:46 test3 [root@localhost test] #

Description:

The permission of test3 is rwxrwxrwx

Example 4: create a new directory to display information

Command:

Mkdir-v test4

Output:

[root@localhost test] # mkdir-v test4mkdir: created directory "test4" [root@localhost test] # mkdir-vp test5/test5-1mkdir: created directory "test5" mkdir: created directory "test5/test5-1" [root@localhost test] #

Example 5: a command creates the directory structure of the project

Reference: http://r6e.cn/gRMc

Command:

Mkdir-vp scf/ {lib/,bin/,doc/ {info,product}, logs/ {info,product}, service/deploy/ {info,product}}

Output:

[root@localhost test] # mkdir-vp scf/ {lib/,bin/,doc/ {info,product}, logs/ {info,product}, service/deploy/ {info Product}} mkdir: created directory "scf" mkdir: created directory "scf/lib" mkdir: created directory "scf/bin" mkdir: created directory "scf/doc" mkdir: created directory "scf/doc/info" mkdir: created directory "scf/doc/product" mkdir: created directory "scf/logs" mkdir: created directory "scf/logs/info" mkdir : created directory "scf/logs/product" mkdir: created directory "scf/service" mkdir: created directory "scf/service/deploy" mkdir: created directory "scf/service/deploy/info" mkdir: created directory "scf/service/deploy/product" [root@localhost test] # tree scf/scf/ |-- bin |-- doc | |-- info | `--product |-- logs |-| |-- Info | `--product`-- service`-- deploy |-- info `--product12 directories 0 files [root@localhost test] #

Rm command

The command to delete files and directories in linux: rm command. Rm is a commonly used command, the function of this command is to delete one or more files or directories in a directory, it can also delete a directory and all files and subdirectories under it. For linked files, only the links are deleted, and the original files remain unchanged.

Rm is a dangerous command, and be especially careful when using it, especially for beginners, otherwise the entire system will be destroyed by this command (such as executing rm *-rf under / (root)). Therefore, before executing rm, we'd better confirm which directory you are in, what you want to delete, and keep a high level of sanity during the operation.

1. Command format:

Rm [options] file...

2. Command function:

Delete one or more files or directories in a directory, and rm does not delete the directory if you do not use the-r option. If you use rm to delete a file, you can usually restore the file to its original state.

3. Command parameters:

-f,-force ignores files that do not exist and never gives prompts.

-I,-interactive for interactive deletion

-r,-R,-recursive instructs rm to recursively delete all directories and subdirectories listed in the parameter.

-v,-verbose shows the steps in detail

-help displays this help and exits

-version outputs version information and exits

4. Command example:

Example 1: if the file file is deleted, the system will first ask whether to delete it.

Command:

Rm file name

Output:

[root@localhost test1] # ll Total 4murwMurrMurray-1 root root 56 10-26 14:31 log.logroot@localhost test1] # rm log.log rm: do you want to delete the general file "log.log"? Yroot@localhost test1] # ll Total 0 [root@localhost test1] #

Description:

After you enter the rm log.log command, you will be asked if you want to delete it. If you enter y, you will delete the file. If you do not want to delete it, the data n.

Example 2: forcibly delete file and the system no longer prompts you.

Command:

Rm-f log1.log

Output:

[root@localhost test1] # ll Total 4 [root@localhost test1] # root root 23 10-26 14:40 log1.log [root@localhost test1] # rm-f log1.log [root@localhost test1] # ll Total 0 [root@localhost test1] #

Example 3: delete any .log file; ask for confirmation one by one before deletion

Command:

Rm-I * .log

Output:

[root@localhost test1] # ll Total 8 root 11 10-26 14:45 log1.log-rw-r--r-- 1 root root 24 10-26 14:45 log2.log [root@localhost test1] # rm-I * .logrm: do you want to delete the general file "log1.log"? Yrm: do you want to delete the generic file "log2.log"? Y [root@localhost test1] # ll Total 0 [root@localhost test1] #

Example 4: delete all files in the test1 subdirectory and subdirectories

Command:

Rm-r test1

Output:

[root@localhost test] # ll Total 24drwxr-xr-x 7 root root 4096 10-25 18:07 scfdrwxr-xr-x 2 root root 4096 10-26 14:51 test1drwxr-xr-x 3 root root 4096 10-25 17:44 test2drwxrwxrwx 2 root root 4096 10-25 17:46 test3drwxr-xr-x 2 root root 4096 10-25 17:56 test4drwxr-xr-x 3 root root 4096 10-25 17:56 test5 [root@localhost test] # rm-r test1rm: enter the directory "test1"? Yrm: do you want to delete the generic file "test1/log3.log"? Yrm: do you want to delete directory "test1"? Y [root@localhost test] # ll Total 20drwxr-xr-x 7 root root 4096 10-25 18:07 scfdrwxr-xr-x 3 root root 4096 10-25 17:44 test2drwxrwxrwx 2 root root 4096 10-25 17:46 test3drwxr-xr-x 2 root root 4096 10-25 17:56 test4drwxr-xr-x 3 root root 4096 10-25 17:56 test5 [root@localhost test] #

The instance 5:rm-rf test2 command deletes all files in the test2 subdirectory and subdirectories without confirming them one by one

Command:

Rm-rf test2

Output:

[root@localhost test] # rm-rf test2 [root@localhost test] # ll Total 16drwxr-xr-x 7 root root 4096 10-25 18:07 scfdrwxrwxrwx 2 root root 4096 10-25 17:46 test3drwxr-xr-x 2 root root 4096 10-25 17:56 test4drwxr-xr-x 3 root root 4096 10-25 17:56 test5 [root@localhost test] #

Example 6: delete files that start with-f

Command:

Rm-- f

Output:

[root@localhost test] # touch-f [root@localhost test] # ls-Fmurf [root@localhost test] # rm-- frm: do you want to delete the general empty file "- f"? Y [root@localhost test] # ls-fls:-f: there is no file or directory [root@localhost test] #

You can also use the following steps:

[root@localhost test] # touch. /-f [root@localhost test] # ls. /-f. Melody [root@localhost test] # rm. /-frm: do you want to delete the general empty file ". /-f"? Y [root@localhost test] #

Example 7: customize the Recycle Bin function

Command:

Myrm () {moved to ok; mv "D & & echo" TMP ok ";}

Output:

[root@localhost test] # myrm () {Demphand TMP $(date +% Y%m%d%H%M%S); mkdir-p $D; mv "$@" $D & & echo "moved to $D ok" } [root@localhost test] # alias rm='myrm' [root@localhost test] # touch 1.log 2.log 3.log [root@localhost test] # ll Total 16 root root RW root root-1 root root 0 10-26 15:08 1.LogSumi RWML RW Mustang-1 root root 0 10-26 15:08 2.Log Mustang RW Mustang-1 3.logdrwxr-xr-x 0 10-26 15:08 Lorenzo 7 root root 4096 10 -25 18:07 scfdrwxrwxrwx 2 root root 4096 10-25 17:46 test3drwxr-xr-x 2 root root 4096 10-25 17:56 test4drwxr-xr-x 3 root root 4096 10-25 17:56 test5 [root@localhost test] # rm [123] .logmoved to / tmp/20121026150901 ok [root@localhost test] # ll Total 16drwxr-xr-x 7 root root 4096 10-25 18:07 scfdrwxrwxrwx 2 root root 4096 10-25 17:46 test3drwxr-xr-x 2 root root 4096 10-25 17:56 test4drwxr-xr -x 3 root root 4096 10-25 17:56 test5 [root@localhost test] # ls / tmp/20121026150901/1.log 2.log 3.log [root@localhost test] #

Description:

The above procedure simulates the effect of the Recycle Bin, that is, when you delete a file, you just put the file in a temporary directory so that it can be recovered when needed.

What is Linux system Linux is a free-to-use and free-spread UNIX-like operating system, is a POSIX-based multi-user, multi-task, multi-threaded and multi-CPU operating system, using Linux can run major Unix tools, applications and network protocols.

Thank you for your reading, the above is "5 file directory operation related Linux command how to understand" all the content, learn friends hurry up to operate it. I believe that the editor will certainly bring you better quality articles. Thank you for your support to the website!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report