Experimental report on Linux command Basics

Posted by Buglish on Fri, 14 Jan 2022 21:09:35 +0100

  • ls
Abbreviation of common parametersFull name of parameterdescribe
-a--allList all files in the directory, including "." Implied file at the beginning
-lIn addition to the file name, the file permission, owner, file size and other information are listed in detail
-h--human-readableList file sizes in an easy to understand format
-tSort by file modification time
# Lists the details of all files and directories in the / usr folder
$ ls -al /usr

# Lists the details of all file directories starting with "y" in the current directory
$ ls -al y*

# List the sizes of all files and directories in the / usr directory in an easy to understand format
$ ls -alh /usr
  • cd
# Enter the system root directory from the current directory
$ cd /

# Enter parent directory from current directory
$ cd ..

# Enter the current user's home directory from the current directory
$ cd ~

# Enter the last directory from the current directory
$ cd -
  • pwd
Abbreviation of common parametersFull name of parameterdescribe
-PDisplays the actual physical path instead of using the link path
-LWhen the directory is a connection path, the connection path is displayed
# Displays the path where the current directory is located
$ pwd

# Displays the physical path of the current directory
$ pwd -P

# Displays the connection path of the current directory
$ pwd -L
  • mkdir
Abbreviation of common parametersFull name of parameterdescribe
-m--modeSet permissions < mode >
-p--parentsCan be a path name. If some directories in the path do not exist, after adding this option, the system will automatically establish those directories that do not exist, that is, multiple directories can be established at a time
-v--verboseInformation is displayed each time a new directory is created
# Create multiple directories recursively
$ mkdir -p  yohann/test

# Create a directory with permission 777
$ mkdir -m 777 yohann

# Create directory display information
$ mkdir -vp yohann/test
  • rm
Abbreviation of common parametersFull name of parameterdescribe
-f--forceIgnore nonexistent files and never prompt
-i--interactiveInteractive deletion
-r--recursiveInstructs rm to recursively delete all directories and subdirectories listed in the parameter
-v--verboseShow the steps in detail
# Delete file
$ rm test.txt

# Forcibly delete files
$ rm -f test.txt

# Delete the suffix log all
$ rm *.log
  • mv
Abbreviation of common parametersFull name of parameterdescribe
-b--backIf the file needs to be overwritten, backup it before overwriting
-f--forceIf the target file already exists, it will not be queried but directly overwritten
-i--interactiveIf the target file already exists, you will be asked whether to overwrite it
-u--updateIf the target file already exists and the source file is relatively new, it will be updated
-t--targetThis option is applicable to moving multiple source files to a directory. At this time, the target directory is first and the source file is second
# The file test Rename log to error log
$ mv test.log error.log

# The file error Log is moved to the log directory
$ mv error.log log

# The file info Log is moved to the log directory. If the file exists, you will be asked whether to overwrite it before overwriting
$ mv -i info.log log
  • cp
Abbreviation of common parametersFull name of parameterdescribe
-t--target-directorySpecify target directory
-i--interactiveAsk before overwrite (invalidate the previous - n option)
-n--no-clobberDo not overwrite existing files (invalidate the previous - i option)
-s--symbolic-linkEstablish symbolic links to source files instead of copying files
-f--forceForcibly copy files or directories, regardless of whether the destination file or directory already exists
-u--updateAfter using this parameter, the source file will be copied only when the modification time of the source file is newer than that of the destination file, or the corresponding destination file does not exist
# For file index HTML to create a symbolic link home html
$ cp -s index.html home.html

# Copy all files in the image directory to the upload directory. Ask before overwriting
$ cp -i image/* upload

# Copy the recently updated files in the image directory to the upload directory. Ask before overwriting
$ cp -iu image/* upload
  • cat
Abbreviation of common parametersFull name of parameterdescribe
-A--show-allEquivalent to - vET
-b--number-nonblankNumber non empty output lines
-eEquivalent to - vE
-E--show-endsShow at the end of each line$
-n--numberNumber all output lines, starting from 1
-s--squeeze-blankIf there are more than two consecutive blank lines, replace them with one blank line
-tEquivalent to - vT
-T--show-tabsShow skip characters as ^ I
-u(ignored)
-v--show-nonprintingUse ^ and M-references, except LFD and TAB
# Put error-20220101 After adding the line number to the file content of log, enter error Log in this file
$ cat -n error-20220101.log > error.log

# Put error-20220101 After adding the line number to the file content of log, enter error In the log file, multiple blank lines are replaced with one line for output
$ cat -ns error-20220101.log > error.log

# Set error The contents of the log file are displayed in reverse
$ tac error.log
  • nl
Abbreviation of common parametersFull name of parameterdescribe
-ba--body-numbering=aIndicates that the line number is also listed whether it is empty or not (similar to cat -n)
-bt--body-numbering=tIf there is an empty line, do not list the line number of the empty line (default value)
-n ln--number-format=lnThe line number is displayed on the far left of the screen
-n rn--number-format=rnThe line number is displayed on the rightmost side of its own field without adding 0
-n rz--number-format=rzThe line number is displayed on the rightmost side of its field, plus 0
-w--number-widthNumber of digits occupied by line number field
# Put error The contents of the log file are displayed after adding the line number, and blank lines are not added with the line number
$ nl -b t error.log

# Put error Log file content is displayed after adding the line number. The line number is displayed on the leftmost, rightmost without 0 and rightmost with 0 respectively
$ nl -n ln error.log
$ nl -n rn error.log
$ nl -n rz error.log

# Put error The file content of log is displayed after adding the line number. The line number is displayed by adding 0 to the right of the screen, and the occupation number of line number column is 3
$ nl -n rz -w 3 error.log
  • more
Abbreviation of common parametersFull name of parameterdescribe
+nDisplay from line n
-nDefines the screen size as n lines
+Search for the string before each file display, and then display it after the first two lines of the string
-cClear the screen from the top and display
-dDisable ringing function
-pPage feed the file by clearing the window instead of scrolling, similar to the - c option
-sDisplays consecutive blank lines as one line
-uRemove the underline from the contents of the document
Common operation symbolsdescribe
=Output the line number of the current line
qExit more
Space barScroll down one screen
bReturn to previous screen
# Error is displayed from the fifth line Contents in log file
$ more +5 error.log

# From error Log file to find the first line where the "y" string appears, and display the output from the first two lines there
$ more +/y error.log

# Set the number of lines per screen to 10
$ more -10 error.log

# Use the ll and more commands to display the / etc/nginx directory information
$ ll /etc/nginx | more -10
  • less
Abbreviation of common parametersFull name of parameterdescribe
-eWhen the file display ends, it will leave automatically
-fForce the opening of special files, such as peripheral codes, directories, and binaries
-iIgnore case when searching
-mDisplays the percentage similar to the more command
-NDisplays the line number of each line
-sDisplays a row of consecutive empty rows
Common operation symbolsdescribe
/StringAbility to drill down on "string"
? character stringThe ability to search up for "string"
nRepeat the previous search (related to / or?)
NReversely repeats the previous search (related to / or?)
bTurn one page forward
dTurn back half a page
qExit the less command
Space barTurn back one page
Up keyFlip up a line
Down keyFlip down one line
# Display error Log file and display the line number
$ less -N error.log

# Display error Log file, search string "500"
$ less error.log
/500

# ps view the process information and display it in less pages
$ ps -f | less
  • head
Abbreviation of common parametersFull name of parameterdescribe
-q--quietHide file name
-v--verboseDisplay file name
-C < byte >--bytesDisplay bytes
-N < number of lines >--linesNumber of rows displayed
# Display error The first 10 lines in the log file
$ head -n 5 error.log

# Error-2020-01-01 is displayed Log and error-2021-01-01 The first 5 lines in the log file
$ head -n 5 error-2020-01-01.log error-2021-01-01.log
  • tail
Abbreviation of common parametersFull name of parameterdescribe
-fCyclic reading
-qDo not display processing information
-vDisplays detailed processing information
-C < byte >Number of bytes displayed
-N < number of lines >Display rows
# Display error The last 10 lines in the log file
$ tail -n 10 error.log

# Display error The last 10 lines in the log file, when error The log file automatically updates the display when new content is added
$ tail -n 10 -f error.log
  • which
# Confirm that php is installed
$ which php

# View the location and path of python commands
$ which python
  • whereis
Abbreviation of common parametersFull name of parameterdescribe
-bLocate executable
-mLocate help files
-sLocate source code files
-uSearch for files other than executable files, source code files and help files in the default path
-BSpecifies the path to search for executable files
-MSpecifies the path to search for help files
-SSpecifies the path to search for source code files
# Search the path of the php executable 
$ whereis -b php

# Search the path to the php help file
$ whereis -m php

# Search the path of php source code
$ whereis -s php
  • locate
Abbreviation of common parametersFull name of parameterdescribe
-q--quietQuiet mode, no error messages will be displayed
-nDisplay up to n outputs
-r--regexpUse regular expressions to find conditions
-V--versiondisplay version information
# Search all files starting with p in / usr/bin directory
$ locate /usr/bin/p*

# Search for files whose file name contains log in the / var/log directory
$ locate /var/log/*log*
  • find
Abbreviation of common parametersFull name of parameterdescribe
-printThe find command outputs the matching file to standard output
-execThe find command executes the shell command given by this parameter on the matching file
-nameFind files by file name
-typeFind a type of file
-pruneUse this option to make the find command not search in the currently specified directory. If you use the - depth option at the same time, then - prune will be ignored by the find command
-userFind files by file owner
-groupFind files by the group to which they belong
-mtime -n +nFind the file according to the file change time, - N indicates that the file change time is less than n days now, + n indicates that the file change time is more than n days now. The find command also has - atime and - ctime options
# Print the list of files in the current directory
$ find . -print

# Print all in the current directory File name ending in c
$ find . -name "*.c" -print

# Print all in the current directory jpg or png ending file name
$ find . \( -name "*.jpg" -or -name "*.png" \)

# Print all files in the current directory File name at the end of pdf
$ find . ! -name "*.pdf"

# Print all php files with 777 permissions in the current directory
$ find . -type f -name "*.php" -perm 777

# Print all files owned by root in the current directory
$ find . -type f -user root

# Print all files with permissions other than 777 and 664 in the current directory
$ find . -type f \( ! -perm 777 -and ! -perm 644 \)

# Find all c files in the current directory and display their details
$ find . -name "*.c" -exec ls -l {} \;

# Write all c language code files into one file
$ find . -name "*.c" -exec cat {} \; > all.c

# Check all c language code files and execute the script
$ find . -name "*.c" -exec ./command.sh {} \;
  • xargs
Abbreviation of common parametersFull name of parameterdescribe
-n--max-argsSpecifies the maximum number of parameters per row
-d--delimiterSpecify separator
# Convert multi line input to single line output
$ cat info.txt | xargs

# Convert single line input to multi line output
$ echo "1 2 3 4 5 6 7" | xargs -n 3

# Convert single line input to multi line output, specifying the separator as a space
$ cat info.txt | xargs -d " " -n 3

# Find all vue code files in the current directory and count the total number of rows
$ find . -type f -name "*.vue" | xargs wc -l
  • wc
Abbreviation of common parametersFull name of parameterdescribe
-c--bytesCount bytes
-l--linesCount rows
-m--charsCount the number of characters. This flag cannot be used with the - c flag
-w--wordsCount the number of words. A word is defined as a string separated by white space, skip, or newline characters
-L--max-line-lengthPrints the length of the longest line
# Count the number of bytes, lines and characters of the file
$ wc -c info.txt
$ wc -l info.txt
$ wc -m info.txt

# Count the number of bytes, lines and characters of the file. Only numbers are printed, not the file name
$ cat c.txt | wc -c
$ cat c.txt | wc -l
$ cat c.txt | wc -m

# Count the number of commands in the / bin directory
$ ls /bin | wc -l
  • grep
Abbreviation of common parametersFull name of parameterdescribe
-c--countCount the number of times a 'search string' (i.e. pattern) is found
-i--ignore-caseThe case difference is ignored, so the case is considered the same
-n--line-numberOutput line number
-v--invert-matchReverse selection to print unmatched lines
-r--recursiveRecursively searching
--color=autoColor the found keywords
# Take out the line where root appears in the / etc/passwd file, and the keyword part is displayed in color
$ grep "root" /etc/passwd \-\-color=auto
$ cat /etc/passwd | grep "root" \-\-color=auto

# Take out the lines in the / etc/passwd file where root and nologin do not appear
$ grep -v "root" /etc/passwd | grep -v "nologin"

# Recursively search for files containing "login()" in the current directory
$ grep -r "login()".
  • cut
Abbreviation of common parametersFull name of parameterdescribe
-b--bytesSplit in bytes
-c--charactersSplit in characters
-d--delimiterCustom separator, tab by default
-f--fieldsCustom field
--complementExtract the entire line of text, except those specified by the - c or - f option
# Remove info Txt file
$ cut -f 1,3 -d ' ' info.txt

# Remove info Txt file
$ cut -f 1 -d ' ' info.txt

# Remove info Txt file
$ cut -f 1-3 -d ' ' info.txt

# Remove info Txt file except the first column
$ cut -f 1 -d ' ' info.txt \-\-complement
  • paste
Abbreviation of common parametersFull name of parameterdescribe
-s--serialMerge each file into a line instead of pasting by line
-d--delimitersCustom separator, tab by default
# Set member Txt and phone The contents of TXT file are spliced by column
$ paste member.txt phone.txt

# Set member Txt and phone The contents of TXT file are spliced by column, and the specified separator is ":
$ paste member.txt phone.txt -d ':'

# Set member Txt and phone The contents of the txt file are spliced into a line
$ paste -s member.txt phone.txt
  • tr
Abbreviation of common parametersFull name of parameterdescribe
-d--deleteDelete the matching content without replacing it
# Converts the entered characters from uppercase to lowercase
$ echo 'THIS IS YOHANN!' | tr 'A-Z' 'a-z'

# Delete the number from the input character
$ echo 'THIS IS YOHANN001!' | tr -d '0-9'

# ROT13 encryption
$ echo 'yohann' | tr 'a-zA-Z' 'n-za-mN-ZA-M'
  • sort
Abbreviation of common parametersFull name of parameterdescribe
-n--numericSort based on the length of the string, using this option allows sorting based on numeric values rather than alphabetic values
-k--keySpecify sort key
-b--ignore-leading-blanksBy default, the entire row is sorted, starting with the first character of each row. This option causes the sort program to ignore the space at the beginning of each line and sort from the first non white space character
-m--mergeMerge only multiple input files
-r--reverseSort in reverse order, and the results are in descending order, not ascending order
-t--field-separatorCustom separator, tab by default
# List the top 10 directory files that use the most space in the / usr/share / directory
$ du -s /usr/share/* | sort -nr | head -10

# Sort the space size field in the output information of ls command
$ ls -l /usr/share/ | sort -nr -k 5 | head -1
  • uniq
Abbreviation of common parametersFull name of parameterdescribe
-c--countPrefix each line with a number indicating the number of occurrences of the corresponding line item
-d--repeatedOnly duplicate rows are output
-u--uniqueShow only unique rows
-DShow all duplicate rows
-f--skip-fieldsSkip the first n columns during comparison
-i--ignore-caseThe comparison is case insensitive
-s--skip-charsSkip the first n characters during comparison
-w--check-charsThe contents after the nth character of each line are not compared
# Find all the same commands in the / bin directory and / usr/bin directory
$ ls /bin /usr/bin | sort | uniq -d
  • join
Abbreviation of common parametersFull name of parameterdescribe
-j FIELDEquivalent to - 1 FIELD -2 FIELD, - j specifies a field as the matching field
-1 FIELDMatch with the FIELD field in file1
-2 FIELDMatch with the FIELD field in file2
-tCustom separator, tab by default
# Connect the two files by using the first field in the two files as the matching field
$ join member.txt phone.txt
  • comm
Abbreviation of common parametersFull name of parameterdescribe
-1Do not output lines specific to file 1
-2Do not output lines specific to file 2
-3Do not output lines common to two files
# Compare old Txt and new Txt the contents of the two files
$ comm old.txt new.txt

# Compare old Txt and new Txt the contents of two files. Only the contents common to the two files are displayed
$ old.txt and new.txt
  • diff
Abbreviation of common parametersFull name of parameterdescribe
-c--contextContext mode, display all the text and mark the differences
-u--unifiedUnified mode to display the first mock exam file in a combined way.
-a--textOnly text files are compared line by line
-N--new-fileWhen comparing directories, if file A only appears in A directory, the default will display: Only in directory. If you use the - N parameter, diff compares file A with A blank file
-r--recursiveRecursive comparison of files in the directory
# Display old Txt and new Txt difference between two files
$ diff old.txt new.txt

# Context mode displays old Txt and new Txt difference between two files
$ diff -c old.txt new.txt

# The first mock exam shows old.. Txt and new Txt difference between two files
$ diff -u old.txt new.txt
  • patch
Abbreviation of common parametersFull name of parameterdescribe
-p num--stripIgnore several layers of folders
-E--remove-empty-filesIf an empty file is found, delete it
-R--reverseCancel the patch
# Generate old Txt and new Txt difference file
$ diff  -Naur old.txt  new.txt > patchdiff.txt
# Update old with the patch command Txt file
$ patch < patchdiff.txt
# Cancel the patch above
$ patch < patchdiff.txt
  • df
Abbreviation of common parametersFull name of parameterdescribe
-a--allList of all file systems
-h--human-readableEasy to read display
-i--inodesDisplay inode information
-T--print-typefile system type
-T < file system type >--typeDisplays only disk information for the selected file system
-X < file system type >--exclude-typeDo not display disk information for the selected file system
# Display disk usage
$ df

# Display disk usage in inode mode
$ df -i

# Lists the types of file systems
$ df -T

# Displays disks of the specified type
$ df -t ext4
  • du
Abbreviation of common parametersFull name of parameterdescribe
-a--allDisplays the size of all files in the directory.
-b--bytesDisplays the size of the directory or file in byte s.
-c--totalIn addition to displaying the size of individual directories or files, it also displays the sum of all directories or files.
-kOutput in kilobytes (1024 bytes).
-mOutput in MB.
-s--summarizeShow only totals and list only the last summed value.
-h--human-readableTake K, M and G as units to improve the readability of information.
# Displays the space occupied by the specified file in a format that is easy to read
$ du -h error.log

# Displays the space occupied by the specified directory in a format that is easy to read
$ du -h /usr/bin

# Displays the disk space occupied by several files or directories, and counts the total
$ du -ch error.log info.log

# Display in reverse order of space size
$ du -h | sort -nr | head -10
  • time
# Measure the running time of ps command
$ time ps
# Save the execution result of the time command to a file
$ (time date) 2>2.txt

Topics: Linux Operation & Maintenance server