Linux file and directory operation commands

Keywords: Linux Attribute PHP socket less

1,cd

(change directory) switch to the specified directory

-Return to last directory
 .. Return to the upper directory
 Enter to return to home directory
 /Root directory

2,cp

(copy) copy a file or directory

-r -R Recursively copy the contents of this directory and its subdirectories
-p  Copy past with file properties
-f  Force copy without asking
-s  Generate shortcuts
-a  Copy all features of the file together

cp -r /home/hj/a    /tmp

3,find

Find directory and files under directory

Options and parameters:
1. Time related options: there are - atime, -ctime and - mtime and - amin,-cmin and - mmin in total, which are described by - mtime
   -mtime n: n is a number, meaning the file whose content has been changed in "one day" before n days;
   -mtime +n: list the file names whose contents have been changed before n days (excluding n days themselves);
   -mtime -n: list the file name of the changed content within n days (including n days itself).
   -newer file: file is an existing file, and lists the file name that is newer than file

2. Parameters related to user or group name:
   -uid n: n is a number, which is the user's account ID, that is, UID
   -gid n: n is the number, which is the ID of the group name, that is, gid
   -user name: name is the user account name! For example, dmtsai
   -Group name: name is the group name, such as users;
   -nouser: people looking for the owner of the file who does not have / etc/passwd!
   -nogroup: the owning group for finding files does not exist in the files of / etc/group!

3. Parameters related to file authority and name:
   -name filename: search for a file named filename (wildcard can be used)
   -size [+-]SIZE: search for files larger (+) or smaller (-) than SIZE. The specifications of this SIZE are:
       c: For byte
       k: For 1024bytes. So, to find a file larger than 50KB, it's 「 - size +50k 」
   -type TYPE: the TYPE of the search file is TYPE, mainly including:
       General formal files (f)
       Device file (b, c)
       Directory (d)
       Link file
       socket (s)
       FIFO (p)
   -perm mode: search for files with file permission "exactly equal to" mode. This mode is chmod like attribute value. For example, the attribute of - rwsr-xr-x is 4755!
   -Perm mode: to search for files with file permissions, you must include all the permissions of mode. For example,
       We want to search the file of - rwxr--r -- 0744, use - perm -0744, when the permission of a file is - rwsr-xr-x, that is, 4755, it will also be listed, because the attribute of - rwsr-xr-x already includes the attribute of - rwxr--r.
   -perm +mode: search for files with file permission "including the permission of any mode", for example
       Say, when we search for - rwxrxrxrx, that is - perm +755, but a file attribute of - rw ------ will also be listed, because it has the attribute of - rw!
4. Additional actions:
   -exec command: command is another instruction, - exec can be followed by another instruction to process the found result.
   -Print: print the result to the screen, this action is the default action!
   eg:
       find / -perm +7000 -exec ls -l {};, extra instructions start with - exec, and end with {} instead of what was found earlier
   | xargs 
       -i the default front output is replaced by {} 
       eg:
           find . -name "*.log" | xargs -i mv {} test4

Example:

# Find by filename
# Find all passwd in / etc directory
find /etc/ -name passwd

# By find depth
root@dly-virtual-machine:/home/dly# find /etc -maxdepth 1 -name passwd
/etc/passwd

root@dly-virtual-machine:/home/dly# find /etc -maxdepth 2 -name passwd
/etc/cron.daily/passwd
/etc/pam.d/passwd
/etc/passwd

# By file size
root@dly-virtual-machine:/home/dly# find /mnt -size 20k
root@dly-virtual-machine:/home/dly# find /mnt -size -20k    # Less than 20k
/mnt
root@dly-virtual-machine:/home/dly# find /mnt -size +20k    # Greater than 20k

# By file type
find /dev -type f   # Ordinary documents
find /dev -type b   # Block device
find /dev -type s   # socket
find /dev -type p   # The Conduit
find /dev -type d   # Catalog

# Find by permission

##Find files with file permission 404
[root@localhost mnt]# find /mnt -perm 404
/mnt/file2

##View files with r permission in u position and r permission in o position
[root@localhost mnt]# find /mnt -perm -404
/mnt
/mnt/file1
/mnt/file2

##View files with r permission in u position or r permission in o position
[root@localhost mnt]# find /mnt -perm /404

4,mkdir

(make directory) create folder

-p recursively create directories, and if the parent directory does not exist, create them in turn
 -m user defined permission to create directory eg:mkdir -m 777 hehe
 -v displays the details of creating a directory

5,mv

(move) move or rename the file (the source file will be deleted when renaming, cp will not)

-b  Backup before overwriting
-f  Force coverage if there is one
-i  Ask if you want to overwrite
-u  More recent coverage
-t  Move multiple source files to a unified directory, with the directory parameter in the front and the file parameter in the back

eg:
   mv a /tmp/ Will file a Move to /tmp Directory
   mv a b take a Named b
   mv /home/zenghao test1.txt test2.txt test3.txt

6,pwd

(print working directory) shows the absolute path of the current working directory.

7,rename

rename file

Usage: rename [-v] [-n] [-f] perlexpr [filenames]

-v(verbose)     Print successfully renamed file
-n(no-act)      Only the files to be renamed will be displayed, not the actual rename operation
-f(force)       Overwrite existing files
perlexprPerl Regular expressions in language format
files Files to be replaced(such as*.c,*.h),If no filename is given, it will be read from standard input

[Common wildcard description]
 ?    Represents an arbitrary character
 *    Represents one or a string of arbitrary characters
-------------------------------------------------
[Description of common regular expression symbols]
^    Match the start of the input
$    Match end of input
.    Match any character except newline
+    Match the previous character one or more times for example,"zo+"Can match"zoo",But it does not match."z"
[a-z]    Represents a range of characters, for example,"[a-z]"matching"a"And"z"Any lowercase alphabetic character between.
[^m-z]    Negative character range. Matches characters that are not in the specified range.

Example:

[root@localhost www]# rename 's/\.sh/\.php/' *        #Change the. sh suffix file in the current directory to. php

[root@localhost www]# rename 's/$/\.bak/' /home/www/*.php     #Add bak suffix to. php in www directory

[root@localhost www]# rename 's/^/bak_/' *.bin     #Prefix the. bin suffix file in the current directory with "bak"

[root@localhost www]# rename 's/\.bin$//'* ා batch delete the. bin suffix of all files in the current directory

[root@localhost www]# rename 's/A-Z/a-z/' *       #Modify all file names of the current directory to lowercase

8,rm

(remove) delete one or more files or directories

-r delete folder
 -f delete without prompt
 -i delete prompt
 -v detailed display steps

9,rmdir

(remove empty directories)

-v display execution process
 -p if the parent directory is empty after deleting from the parent, delete it at the same time

10,touch

Create a new empty file and change the time stamp property of the existing file

-a  Modify access time only
-m  Value change time
-r  eg:touch -r a b ,send b Time and a identical
-t  Specify a specific time eg:touch -t 201211142234.50 log.log 
   -t time [[CC]YY]MMDDhhmm[.SS],C:Top two years ago

eg: 
touch a.txt     # New file a.txt

11,tree

Display the contents of the directory in a tree structure

-a Show all files and directories.
-A Use ASNI Drawing characters display a tree view instead of ASCII Character combination.
-C Add color to the list of files and directories to distinguish different types.
-d Displays the directory name, not the content.
-D Lists when the file or directory was changed.
-f Displays the full relative path name before each file or directory.
-F In the execution file, directory, Socket,Symbolic connection, pipe name name, plus each"*","/","=","@","|"Number.
-g List the group name of the file or directory. If there is no corresponding name, the group ID will be displayed.
-i Do not list file or directory names in a ladder.
-I<Template style> Do not display file or directory names that match the template style.
-l If you encounter a directory that is a symbolic connection, directly list the original directory that the connection points to.
-n Don't color the list of files and directories.
-N Lists file and directory names directly, including control characters.
-p List permission labels.
-P<Template style> Only file or directory names that match the template style are displayed.
-q use"?"The number replaces the control character and lists the file and directory names.
-s Lists the file or directory size.
-t Sort by file and directory change time.
-u List the owner name of the file or directory. If there is no corresponding name, the user ID will be displayed.
-x Limit the scope to the current file system. If some subdirectories under the specified directory are stored on another file system, the subdirectories will be excluded from the search scope.
-L Grade/depth

eg:
tree /home/dly -L 1     # The depth is 1.
tree -a     # Show all files and directories

12,basename

Get the filename or pathname in the path

# Get the filename or pathname at the end
$ basename /etc/passwd
passwd
$ basename /etc/man.config
man.config
$ basename /etc
etc

# Remove the specified character at the end, and specify the suffix in the second parameter
$ basename /etc/man.config
man.config
$ basename /etc/man.config .config
man

# Unassign character
$ basename namedb.data db.data

13,dirname

Displays the path prefix of the specified path in addition to the filename

dirname /usr/bin/          -> "/usr"
dirname dir1/str dir2/str  -> "dir1" followed by "dir2"
dirname stdio.h            -> "."

14,chattr,lsattr

Change the extended attributes of (view) documents

root@dly-virtual-machine:/home/dly/test# lsattr a.txt 
-------------e-- a.txt

# #This file cannot be modified in any situation
root@dly-virtual-machine:/home/dly/test# chattr +i a.txt 
root@dly-virtual-machine:/home/dly/test# lsattr a.txt 
----i--------e-- a.txt

chattr -i nginx.conf                    #Cancel permission setting for - i

chattr +a /var/log/messages   #Make a file can only add data to it, but it cannot be deleted

15,file

display file types

-b
    Do not output filename (brief mode)
-c
    Parse the result of printout magic number file when checked. It is often used with -m to debug it before installing magic number file.
-f named file
    Read out the file name to be checked (one file per line) from the named file in front of the parameter table. There must be a named file or at least one file name parameter. If you want to check the standard input, use ` ` - as the file parameter
-m list
    Specifies the list of files that contain magic numbers. It can be a single file or multiple files separated by colons
-n
    Refresh the standard output every time a file is checked. Only valid when checking a group of files. This option is generally used when exporting file types to pipes
-v
    Print program version and exit
-z
    An attempt was made to view the internal information of the compressed file
-L
    The (on systems that support symbolic linking) option displays the original file of the symbolic link file, just like the like named option of the ls(1) command
-s
    In general, file is just trying to check the type of file s in the file list that stat(2) reports as normal files. Since reading special files may cause unknown consequences, this can prevent problems. When using the - s option, the file command will also read block special files and character special files in the file list. It is generally used to get the file system from the original disk partition This option also causes the file command to ignore the file size reported by stat(2), because in some systems the size of the original partition is reported as 0

Example:

$ file *        # View the type of current file
$ file a.txt    # View the types of individual files
$ file -z test.bz2  # View file types in compressed files

16,md5sum

Calculate and verify the MD5 value of the file

Usage: md5sum [options]... [file]
Display or check the MD5 (128 bit) checksum.
If there is no file option or "-" at the file, it is read from standard input.

  -b. -- binary read in binary mode
  -c. -- check read the check value of MD5 from the file and check it
  -t. -- text read in plain text mode (default)

The following three options are useful for validation:
      --quiet does not output OK for the verified successful file
      --Status does not output anything, use exit status number to display success
  -w. -- warn warns against malformed checksums

      --strict         with --check, exit non-zero for any invalid input
      --Help display this help message and exit
      --Version displays version information and exits

Checksums are generated in accordance with RFC 1321. When checking, the input format given should match the output of the program
 The template format is the same. In the default output mode, a line of checksum verification results is output, and a character is used to
 Represents the file type ("*" for binary and "" for plain text), and displays the name of each file at the same time

Example:

# Display md5 as binary
root@dly-virtual-machine:/home/dly/test# md5sum -b a.txt 
d41d8cd98f00b204e9800998ecf8427e *a.txt

# Generate md5 encryption verification and    
root@dly-virtual-machine:/home/dly/test# md5sum a.txt > a.md5
root@dly-virtual-machine:/home/dly/test# ls
1.jpg  2.jpg  a  a.md5  a.txt
root@dly-virtual-machine:/home/dly/test# cat a.md5 
d41d8cd98f00b204e9800998ecf8427e  a.txt

# Check whether it is consistent with the document
root@dly-virtual-machine:/home/dly/test# md5sum -c a.md5 
a.txt: Determine

Posted by 0riole on Wed, 29 Jan 2020 03:46:43 -0800