Linux file and directory processing commands

Source: Internet
Author: User

  • Ls-list Directory Contents

    -A: Lists all files and directories in the current directory, including hidden files (infrequently used files)

    -A: Lists all files and directories under the current directory, including hidden files, but not including. and.

    -D: Lists only the current directory

    -F: List directory contents directly, do not sort by default, sort by file name

    -H: Displays the contents of the directory in MB,GB and needs to be used in conjunction with-l

    -I: Lists the inode for each file

    -L: List contents in long format

  • Cd-change Directory

    ~: Go to the user's home directory

    -: Return to the directory where you entered this directory

  • Mkdir-make directories Create a directory that does not exist.

    Common option-p recursively create a directory

    [Email protected] ~]# ll/data1/

    -rw-r--r--1 root root 15 May 5 02:40 1.html

    [Email protected] ~]# mkdir-p/data1/cine/var/logs

    [Email protected] ~]# ll/data1/cine/var/

    Drwxr-xr-x 2 root root 4096 May 05:02 logs

    [Email protected] ~]# mkdir/data1/

    mkdir: Unable to create directory "/data1/": File already exists

  • Rmdir-remove Empty Directories Delete a blank directory

    [Email protected] ~]# rmdir/data1

    RmDir: Delete "/data1" failed: directory not empty

  • Pwd-print Name of current/working directory view current directory or working directory

  • Touch-change file Timestamps Change the timestamp (file exists) and create the file if it does not exist

  • Mv-move (rename) files move or change file names

    [Email protected] ~]# MV user.log/data1/#移动

    [Email protected] ~]# ll/data1/

    -rw-r--r--1 root root 0 May 03:19 user.log

    [Email protected] data1]# mv User.log 1.log #更名

    [email protected] data1]# LL

    Drwxr-xr-x 3 root root 4096 May 05:02 cine

    [[email protected] data1]# mv-b/root/users.log./1.log # Common Options-B before overwriting for backup

    MV: Do you want to overwrite "./1.log"? Y

    [email protected] data1]# LL

    -rw-r--r--. 1 root root 427 November 06:24 1.log

    -rw-r--r--1 root root 0 May 03:19 1.log~

    [Email protected] data1]# mv-f/root/a.log./1.log #-F Direct override

    You have mail in/var/spool/mail/root

    [email protected] data1]# LL

    -rw-r--r--. 1 root root 591618 November 1.log

    [[email protected] data1]# mv-i 1.html 1.log #-i Interactive query whether to overwrite

    MV: Do you want to overwrite "1.log"?

  • Rename-rename files

    Rename source string destination string required to change the file

    [Email protected] data1]# Rename 1 user 1.*

    [email protected] data1]# LL

    -rw-r--r--1 root root 15 May 5 02:40 user.html

    -rw-r--r--. 1 root root 591618 November User.log

    -rw-r--r--1 root root 0 May 03:19 user.log~

    The rename supports wildcard wildcards regular expressions.

  • Basename-strip directory and suffix from filenames display file name and directory name

  • Dirname-strip non-directory suffix from file name to display the path to files or directories

    [[Email PROTECTED]WW1 ~]# dirname NFS

    .

    [Email protected] ~]# Dirname/etc/init.d/nfs

    /etc/init.d

  • Tree-list contents of directories in a tree-like format. Lists files and subdirectories in the specified directory in a tree-like structure

    Common Options-L number Specifies the depth of the list tree

    [Email protected] ~]# tree-l 1/

    /

    ├──bin

    ├──root

    ├──sbin

    ├──tmp

    ├──usr

    ├──var

  • Rm-remove files or directories deleting a file or directory

    Common options

    -F ignores non-existent files, commonly used.

    -R is often used to delete directories (including files and subdirectories in the directory), which is very dangerous.

    [Email protected] var]# rm-f logs #删除一个目录会报错, not added-R

    RM: Cannot delete "logs": is a directory

    [[email protected] var]# RM-RF logs #加入-r delete directory and directory output.log.

    [email protected] var]# LL

    Total dosage 0

    [Email protected] var]#

  • cp-copy files and directories copy file and directory

    Common options

    -A equals-pdr

    -P is copied along with the file attributes

    -D If the file is a linked file, copy the properties of the connection file

    -R recursive replication, commonly used in directories

    -I interactive query whether the target file is overwritten when it is present

    [email protected] ~]# CP Output.log/data1/cine/var #只复制文件

    [Email protected] ~]# ll/data1/cine/var/

    Total Dosage 4

    -rw-r--r--1 root root 48 May 16:12 Output.log

    [Email protected] ~]# mkdir Yang #为复制目录做准备

    You have mail in/var/spool/mail/root

    [email protected] ~]# Touch yang/test.txt

    [[email protected] ~]# CP Yang/data1/cine/var #复制目录, without-R

    CP: Skip Directory "Yang"

    [Email protected] ~]# ll/data1/cine/var/

    Total Dosage 4

    -rw-r--r--1 root root 48 May 16:12 Output.log

    [Email protected] ~]# cp-r yang/data1/cine/var/#辅助目录, after adding-R

    You have mail in/var/spool/mail/root

    [Email protected] ~]# ll/data1/cine/var/

    Total Dosage 8

    -rw-r--r--1 root root 48 May 16:12 Output.log

    Drwxr-xr-x 2 root root 4096 May 16:14 Yang

    [Email protected] ~]#

  • File-determine file type View files

    [[Email protected] ~]# file Output.log

    Output.log:utf-8 Unicode Text

    [Email protected] ~]# File/etc/init.d/nfs

    /etc/init.d/nfs:posix Shell Script Text executable

    [Email protected] ~]# file/bin/

    [Email protected] ~]# File/bin/cat

    /bin/cat:elf 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for Gnu/linux 2.6.18 , stripped

  • Linux file and directory processing commands

    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.