Delete, new, move, and so on for Linux files and directories

Source: Internet
Author: User

Switch directory CD under Linux

View the contents of the current directory under Linux LS, LL, ls-al

How to display the directory path where the current command is located pwd

Create a directory under Linux mkdir

Create a file under Linux direct vim edit: Wq return is OK

Delete a directory or file under Linux RM-RF

Copy directory or file copy file under Linux CP original path new path and new name Copy Folder cp-r original folder new folder

It's the same thing to move a directory or file mv/home/1.txt/opt/folder under Linux

Get the file name from the path by basename

Description of Use

The basename command is used to remove the directory and suffix of the file name (strip directory and suffix from filenames), and the corresponding dirname command is used to intercept the directory

Common parameters

Format: basename NAME

Remove the directory part from name.

Path            dirname         basename "/usr/lib"       "/usr"              "Lib" "/usr/"          "/"                  "usr" "usr" "            .                  " usr "/"              "/"                  ""             "" ""                  "." "" "" "" "" "" "" "" "" "."

Format: basename NAME SUFFIX

Remove the directory part and suffix suffix in name, and if not, output suffix.

Using the example example one

[Email protected] ~]# Basename/usr/bin/sort
Sort
[Email protected] ~]# Dirname/usr/bin/sort
/usr/bin
[Email protected] ~]#

Example Two

[Email protected] ~]# basename/usr/include/stdio.h. h
Stdio
[Email protected] ~]# basename/usr/include/stdio.h stdio.h
Stdio.h

Get the directory name in the path by dirname

Description of Use

The dirname command can take the directory portion of a given path (strip non-directory suffix from file name). This command is rarely used directly from the shell command line, and I typically use it in a shell script to get the directory where the script file is located and then switch the current directory over. According to the manual page, "Print NAME with its trailing/component removed; If NAME contains no/' s, Output '. ' (meaning the current directory). "It seems that" taking the directory portion of a given path "does not accurately summarize the purpose of the dirname command. Another command under Linux is basename, which, contrary to dirname, is the part that gets the file name.

Common parameters

No.

Example of using a sample from a man page

[Email protected] ~]# Dirname/usr/bin/sort
/usr/bin
[Email protected] ~]# dirname stdio.h
.
[Email protected] ~]#

Example Two

[Email protected] ~]# Dirname/usr/bin
/usr
[Email protected] ~]# dirname/usr/bin/
/usr

Note: The output here seems a bit strange. This should take a look at the source code of the DirName command to explain.

Example three using bash code in a bash script
    1. #!/bin/sh
    2. # Jump to the directory where the script is located
    3. CD $ (dirname "$") | | exit 1
    4. # The rest of the part

I often use this notation because sometimes the script does not know which directory it is in, such as when the script is used in crontab.

Another notation is: CD ' DirName $ ' where the anti-quote · Equivalent to $ ().

Example four comparison with another method of intercepting a directory

You can use ${pathname%/*} to intercept the portion of the file that follows pathname.

[Email protected] ~]# pathname=/usr/bin/sort; echo $ (dirname $pathname) ${pathname%/*}
/usr/bin/usr/bin
[Email protected] ~]# pathname=/usr/bin/; echo $ (dirname $pathname) ${pathname%/*}
/usr/usr/bin
[Email protected] ~]# pathname=/usr/bin; echo $ (dirname $pathname) ${pathname%/*}
/usr/usr
[Email protected] ~]# pathname=/usr/; echo $ (dirname $pathname) ${pathname%/*}
/usr
[Email protected] ~]# pathname=/usr; echo $ (dirname $pathname) ${pathname%/*}
/
[Email protected] ~]# pathname=/; echo $ (dirname $pathname) ${pathname%/*}
/
[Email protected] ~]# pathname=stdio.h; echo $ (dirname $pathname) ${pathname%/*}
. Stdio.h
[Email protected] ~]#

Delete, new, move, and so on for Linux files and directories

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.