当前位置:网站首页>Liunx operation and maintenance (2) - file and directory operation

Liunx operation and maintenance (2) - file and directory operation

2020-12-07 01:16:02 Mrwhite86

Document directory :

One 、pwd: Show current position

Two 、cd: Toggle directory

3、 ... and 、tree: The tree structure shows the directory

Four 、mkdir Create directory

5、 ... and 、touch: Create an empty file or change the file timestamp

  6、 ... and 、ls: Display the attribute information related to the contents in the directory

7、 ... and 、cp: Copy files or directories

8、 ... and 、mv: To move or charge

Nine 、rm: Delete files or directories

Ten 、rmdir: Delete empty directory

11、 ... and 、ln: Build hard 、 Soft link

Twelve 、readlink: View the content of the symbolic link file

13、 ... and 、find: Find the file in the directory

fourteen 、xargs: Convert standard input to command line arguments

15、 ... and 、rename: rename

sixteen 、basename: Displays the file name or directory name

seventeen 、dirname: Show file or directory path

eighteen 、chattr: Change to file extension properties

nineteen 、lsattr: View file properties

twenty 、file: Show file type

The 21st 、md5sum: Calculation and verification file MD5 value

Twenty-two 、chown: Users and user groups that change to files or directories

23 、chmod: Change file or directory permissions

Twenty-four 、chgrp: Change file user group

twenty-five 、umask: Show or set permission mask

--------------------------------------- Split line : Text --------------------------------------------------------

One 、pwd: Show current position

1、pwd:print working directory

2、 # Show logical path

pwd -L Same as echo $pwd 

 

3、 Show link path

 

Two 、cd: Toggle directory

1、cd:change directory

2、cd -P: Switch to the path of the link

3、cd -L: Switch logical Directory

4、cd - : Switch to the user's previous Directory

5、cd ~: Switch to user HOME For directory , Same as CD

6、cd.. : Switch to the previous Directory , Same as cd . And cd ../

 

3、 ... and 、tree: The tree structure shows the directory

1、 install :yum -y install tree

 2、tree The structure of the current directory

 3、tree -a: contain . The hidden file at the beginning

 4、tree -L 1: Traverse 1 Hierarchical structure

 5、tree -d . Show only directories

 6、tree -f(i): Show full path name ( Don't show branches )

 7、tree - L 1 F : Distinguish between directory and file

  Filter directory

  Filter the end of the slash ( It's equivalent to filtering the directory )

 

Four 、mkdir Create directory

1、mkdir:make directory

2、mkdir dir1 dir2 # Create multiple directories

 3、mkdir -p # Create directory recursively , There is no error when there is a directory

 4、mkdir -pv # Show creation process

 5、 Create multiple directories 1:mkdir -pv dir0/{dir1-1,dir1-2}/{dir2-1,dir2-2}

 

  Create multiple directories 2:mkdir -pv test/dir{1..5} ok/{a..e}

 

 6、mkdir -m 333 dir2 # Create a directory and set permissions

 

7、 Brief introduction {} usage

echo {b,c}

echo a{b,c}

echo a{,c}

 

 echo {1..8} 1{a..h} 

8、 Clone directory structure

mkdir -pv test/dir{1..5} ok/{a..e}

tree -fid --noreport test >> ~/.test.txt

cd  /tmp/

mkdir -p `cat _/.test.txt`( The quotation marks )

 

5、 ... and 、touch: Create an empty file or change the file timestamp

1、touch a.txt b.txt # Create multiple empty files

2、 Check the timestamps :stat a.txt

 3、 Query the corresponding timestamp separately

ls -lu:access time

ls -lt:modify time

ls -lc:change time

 4、touch -m a.txt   # Change the last modification time

 

  5、touch -a a.txt   # Change the last access time

  6、touch -d 20201001 a.txt   # Specify create / Time of revision ( Modification time )

 

7、 modify b.txt Time property and a.txt Agreement ( Modification time )

touch -r a.txt b.txt

 8、 Set the file to 201512312234.50 Time format of

touch -t 201512312234.50 a.txt

 

  6、 ... and 、ls: Display the attribute information related to the contents in the directory

1、ls=list directory contents Same as dos Next dir command

2、ls -a  # With hidden files , among . Is the current directory .. For the superior Directory

3、ls -l  # The details include the last modification time

 4、ll --time-style=long-iso # Show the full time format

Equate to ll --full time

5、ll --time-style=long-iso --time=atime   # Display the access time of the file

  You can use cat To verify

 6、ls -F   # Filter files and directories

ls -F|grep /   # Filter directory

ls -F|grep -v /   # Filter ordinary files

  7、ls -l mytest20201204/  # Show the contents of the directory

ls -ld mytest20201204/   # Show the directory itself

  8、ls -R: Recursively view the directory

9、ls -lt   # In chronological order ( At the end, it shows the top )

ls -lrt  # Reverse in chronological order ( Finally, show the last )

 10、 ls -F   # The link is displayed as @

 

 ls -lF /etc/init.d/  # * Stands for executable ordinary files

 11、ls -lhi  #-h The parameter is the file size, which is human readable , -i Show the inode value , Link related

 

 

7、 ... and 、cp: Copy files or directories

1、cp=copy centos Added an alias cp -i Coverage needs to be confirmed

2、cp -a contain

cp -p: Keep the owner of the file when copying , Permissions and time attributes

cp -d: Copy the link itself , And keep the file or directory that the symbolic link points to

cp -r: Recursive malfeasance Directory

 3、cp Overlay direct file without prompting scheme

Normal replication requires manual confirmation as follows :

  Method 1:/usr/bin/cp file1.txt file2.txt   # Use the absolute path command - Directly covered

  Method 2: \cp file1.txt file2.txt

  Method 3:unalias cp file1.txt file2.txt   # Temporarily remove alias

  Method 4: Modify system environment variables ( Not recommended )

 4、 Quick backup command

cp file1.txt{,_backup}

cp -a mytest7{,_backup}

 

 

8、 ... and 、mv: To move or charge

1、mv=move+rename, Default alias mv -i, Tip whether to overwrite

2、 shielding mv Alias :\mv file1.txt file2.txt

 

 3、 Moving multiple files * matching :mv dir* testdir/

  4、mv -t testdir1/ dir*   # Reverse movement

 

Nine 、rm: Delete files or directories

1、rm=remove files or directories, Default band rm -i

2、rm -rf testdir1/  # Force directory deletion , There is no need to confirm

 3、rm You need to back up when deleting , And avoid using wildcards

 

Ten 、rmdir: Delete empty directory

1、rmdir=remove empty directories

2、rmdir -p -v dir1/a/b  # Recursively delete the directory and show the deletion process , The order is from subdirectory to parent directory

 

11、 ... and 、ln: Build hard 、 Soft link

1、ln=link branch hard link And symbolic link

2、 System limitations , Hard link cannot be created at this time

 3、ln -s dir1/dir1.txt dir_softlink  # Soft links cannot exist in advance

 4、 File link test

Delete source file , Soft links are shown in red

  Delete soft links , Source files are not affected

 

Twelve 、readlink: View the content of the symbolic link file

1、readlink dir1_softlink

 2、readlink -f dir1_softlink   # Display the last non symbolic link file

 

13、 ... and 、find: Find the file in the directory

1、 Find files that have been modified within a specified time

find . -atime -2  # lookup 2 Files accessed within days

 find /tmp/ -mtime -5  # Under the absolute path ,5 Documents that were modified within days

 

find /tmp/ -mtime 2  # Under the absolute path ,2 File modified days ago

 

2、 use -name Specify keywords to find

find . -mtime +2 -name '*.txt'  # lookup 2 Days ago with txt Final document

 3、 utilize ! Reverse lookup

find . -type d  # Find all the directories

find . ! -type d  # Find all non directories

 4、 Search by directory or file permissions

find . -perm 755  # lookup 755 The content of authority

 5、 View by size

find . -size +20c  # Find file size >20 Byte file

 6、 Ignore directory when looking for files

find /root/mytest20201204/mytest1/ -path '/root/mytest20201204/mytest1/dir1' -prune -o -print  # Ignore individual directories

  find  /root/mytest20201204/mytest1/ \( -path /root/mytest20201204/mytest1/dir1 -o -path /root/mytest20201204/mytest1/dir2 \) -prune -o -print # Ignore multiple files

 7、user And nouser Lookup

[root@localhost mytest1]# find . -user nobody  # The user is nobody

[root@localhost mytest1]# find . -nouser # Search for taskless user files

 

 8、group And nogroup Options ( ditto )

 9、 Find new or old files than a file

find . -newer dir3  # Search ratio dir3 New files

find . -newer dir1 ! -newer dir2  # Search ratio dir1 new But more than dir2 Old documents

 10、 Logical operators

[root@localhost mytest1]# find . -maxdepth 1 -type d # Traverse 1 layer , similar tree -L 1

 11、 Regular expressions

[root@localhost mytest1]# find . -regex "dir" # The exact match path is dir, There is no result

[root@localhost mytest1]# find . -regex ".*dir" # Match suffix

[root@localhost mytest1]# find . -regex ".*/dir" # matching /dir suffix

12、 Find and print

[root@localhost mytest8]# find . -type f -exec ls -l {} \;

13、 lookup n Days ago and delete

[root@localhost mytest8]# find . -type f -mtime +2 -exec rm {} \;

 14、-exec Option safe mode -ok

[root@localhost mytest8]# find . -type f -mtime +2 -ok rm {} \;

 15、find+xargs Filter

[root@localhost mytest8]# find . -type f | xargs ls -l # Pass find and display

[root@localhost mytest8]# find . -name '*.txt' | xargs -i mv {} testdir/ # Find pass and move

 

 [root@localhost mytest8]# find . -name '*dir*' |xargs -p rm -f # Need to confirm y、n And delete

 

 16、 Case study , Add all extensions in the directory .txt In the file test001 Replace with test002

Method ( One )

[root@localhost mytest8]# find . -name '*.txt' -exec sed -i 's#test001#test002#g' {} \;

 

Method ( Two )

[root@localhost mytest8]# find . -name '*.txt'|xargs sed -i 's#test001#test002#g'

 

Method ( 3、 ... and ): Efficient methods ( Backquotes take precedence )

[root@localhost mytest8]# sed -i 's#test001#test002#g' `find . -name '*.txt'`

 17、 Delete all files but keep one of the specified files

Method ( One ):

[root@localhost mytest8]# find . -type f ! -name 'dir5.txt' | xargs rm -f

Method ( Two ):

[root@localhost mytest8]# find . -type f ! -name 'dir5.txt' -exec rm -f {} \;

 

fourteen 、xargs: Convert standard input to command line arguments

Preset data :

1、[root@localhost mytest8]# xargs < test001.txt # All the numbers are displayed in one line

2、[root@localhost mytest8]# xargs -n 3 < test001.txt # Output per row 3 individual

  3、echo splitXsplitXsplitXsplitX

[root@localhost mytest8]# echo splitXsplitXsplitXsplitX|xargs -d X # With X As a separator

 

[root@localhost mytest8]# echo splitXsplitXsplitXsplitX|xargs -d X -n2 # Each line shows 2 # With X As a separator , Each line shows 2

 

 4、xargs -i: Specify a symbol to replace the previous result

[root@localhost mytest8]# find . -name 'test*' | xargs -I [] cp [] dir/

 

15、 ... and 、rename: rename

preset :

 1、[root@localhost mytest8]# rename '_finished' '' * # Put all the documents _finished Replace empty

 2、[root@localhost mytest8]# rename .jpg .hello *.jpg # Will all .jpg Replace with .hello

 

sixteen 、basename: Displays the file name or directory name

1、[root@localhost mytest8]# basename dir1/dir2/dir3/test001.txt # Remove the path part

[root@localhost mytest8]# basename dir1/dir2/dir3/test001.txt .txt # Remove the path part , And remove the suffix

 

 

seventeen 、dirname: Show file or directory path

1、[root@localhost mytest8]# dirname dir1/dir2/dir3/test001.txt

  2、[root@localhost mytest8]# dirname test001.txt # Return the relative path according to the path

 

eighteen 、chattr: Change to file extension properties

1、[root@localhost mytest8]# chattr +a test001.txt # You can only add data , Can't delete

 2、[root@localhost mytest8]# chattr +i test001.txt  # Add read only properties

 

nineteen 、lsattr: View file properties

1、[root@localhost mytest8]# lsattr test001.txt # see file

2、[root@localhost mytest8]# lsattr -d testdir/ # View directory

 

twenty 、file: Show file type

 

The 21st 、md5sum: Calculation and verification file MD5 value

1、 To generate a file md5

[root@localhost mytest8]# md5sum test001.txt

 2、 Check if the file has changed

[root@localhost mytest8]# md5sum -c md5.log

 

Twenty-two 、chown: Users and user groups that change to files or directories

1、 Change the user group to which the file belongs ‘

[root@localhost mytest8]# chown baikang test001.txt

  2、 Change the properties of the user group to which the file belongs

[root@localhost mytest8]# chown .baikang test001.txt

[root@localhost mytest8]# chown :baikang test001.txt

 3、 At the same time, change the properties of the user and group to which the file belongs

[root@localhost mytest8]# chown baikang.root test001.txt

 4、 Recursively change the user and user group properties of all directory files in the directory

[root@localhost ~]# chown -R baikang:baikang mytest20201204/

 

23 、chmod: Change file or directory permissions

1、 Set the permission to be empty

[root@localhost mytest1]# chmod a= test.txt test.txt

 2、 Set up usr The file belongs to the main execution authority

[root@localhost mytest1]# chmod u+x test.txt

 3、 Set up group File user group writable permissions

[root@localhost mytest1]# chmod g+u test.txt

  4、 Set up other Other user readable permissions

[root@localhost mytest1]# chmod o+r test.txt

 5、 Set multiple permissions

[root@localhost mytest1]# chmod a= test.txt
[root@localhost mytest1]# chmod ug+r,o+r test.txt

 6、 Common permissions

Catalog :755

file :644

Total quantity :777

 

Twenty-four 、chgrp: Change file user group

1、chgrp testuser test.txt   # Change the file

2、chgrp -R root dir/  # Recursively change the files in the directory

 

twenty-five 、umask: Show or set permission mask

1、 File permissions =666- Mask

2、 The directory permissions =777- Mask

3、root User default mask :0022

 4、 Normal user default mask :0002

 5、umask 044  # Provisional entry into force

 

One 、pwd: Show current position

 

版权声明
本文为[Mrwhite86]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/20201207011507779f.html