Basic 3 of Linux commands.
Function to change the standard input / output destination
[wataru@localhost testgo]$ ls -l > lstext.txt
# >Use symbols to redirect
#In this example, the result of ls is lstext.Save to txt
[wataru@localhost testgo]$ ls -l
total 4
drwxrwxr-x. 2 wataru wataru  25 Jul  7 16:25 2020dir
-rw-rw-r--. 1 wataru wataru 225 Jul 15 04:47 lstext.txt
-rw-rw----. 1 wataru wataru   0 Jul  5 04:26 testtest.txt
-rw-rw-r--. 1 wataru wataru   0 Jul 11 01:55 work.txt
[wataru@localhost testgo]$ cat lstext.txt 
total 0
drwxrwxr-x. 2 wataru wataru 25 Jul  7 16:25 2020dir
-rw-rw-r--. 1 wataru wataru  0 Jul 15 04:47 lstext.txt
-rw-rw----. 1 wataru wataru  0 Jul  5 04:26 testtest.txt
-rw-rw-r--. 1 wataru wataru  0 Jul 11 01:55 work.txt
[wataru@localhost testgo]$ ls xxxxx 2> error.txt
#Standard error output redirect is 2>Use symbols
[wataru@localhost testgo]$ cat error.txt 
ls: cannot access 'xxxxx': No such file or directory
[wataru@localhost testgo]$ ls xxx >error2.txt  2>&1 
#To combine standard output and standard error output, 2>&Use 1
#Used when combining the result and error message into one log file
[wataru@localhost testgo]$ ls > co.txt
[wataru@localhost testgo]$ ls -F >> co.txt 
#>>You can add to the file by using the symbol
[wataru@localhost testgo]$ cat co.txt 
2020dir
co.txt
error2.txt
error.txt
lstext.txt
testtest.txt
work.txt
2020dir/
co.txt
error2.txt
error.txt
lstext.txt
testtest.txt
work.txt
Connect the standard output of a command to the standard input of another command
[wataru@localhost testgo]$ history | wc -l
#The result of the history command that outputs the command history
#Output the number of lines with the wc command that counts the number of lines
729
[wataru@localhost testgo]$ history | head -n 10 
#Output the 10th line from the top with the head command
    1  date
    2  exit
    3  date
    4  bash
    5  exit
    6  pwd
    7  ls -l
    8  cd Music
    9  ls -l
   10  date
[wataru@localhost testgo]$ history | tail -n 5 
#Output 5 lines from the end with the tail command
  727  hostory
  728  hostory | wc -l
  729  history | wc -l
  730  history | head -n 10 
  731  history | tail -n 5 
[wataru@localhost testgo]$ history | tac
#Output in reverse order with the tac command
  732  history | tac
  731  history | tail -n 5 
  730  history | head -n 10 
  729  history | wc -l
  728  hostory | wc -l
  727  hostory
Count the number of bytes, words, and lines
[wataru@localhost testgo]$ ls | wc
      7       7      69
#From the left, it represents "number of lines", "number of words", and "number of bytes".
#-l -w -Can be specified with the c option
[wataru@localhost testgo]$ ls
2020dir  co.txt  error2.txt  error.txt  lstext.txt  testtest.txt  work.txt
Sort lines
[wataru@localhost testgo]$ cat word.txt 
wan
aiueo
fgh
cde
[wataru@localhost testgo]$ sort word.txt 
#Sorted alphabetically
aiueo
cde
fgh
wan
[wataru@localhost testgo]$ cat sort.txt 
99999
4321
564
67
684
681
20
[wataru@localhost testgo]$ sort -n sort.txt 
#-Use the n option to sort numerically
20
67
564
681
684
4321
99999
[wataru@localhost testgo]$ sort -r word.txt 
#-Use the r option to sort in reverse order
wan
fgh
cde
aiueo
Remove duplicate lines (omit consecutive lines with the same content)
[wataru@localhost testgo]$ cat word.txt 
akira
tanaka
tanaka
sasaki
tanaka
akira
akira
yamamoto
[wataru@localhost testgo]$ uniq word.txt 
#Consecutive "tanaka" and "akira" duplicate lines are omitted
#The first line "akira" is not omitted
akira
tanaka
sasaki
tanaka
akira
yamamoto
wataru@localhost testgo]$ sort word.txt | uniq
#If you execute the sort command and then the uniq command, duplicate lines will be omitted from the whole.
akira
sasaki
tanaka
yamamoto
wataru@localhost testgo]$ sort word.txt | uniq -c
#-Use the c option to count duplicate rows
      1 
      3 akira
      1 sasaki
      3 tanaka
      1 yamamoto
Cut out a part of the input
** cut -d 
[wataru@localhost testgo]$ cat word.txt 
akira,124,459,root
tanaka,523,756,akira
tanaka,789,465,tanaka
sasaki,100,200,sasaki
tanaka,200,600,aida
[wataru@localhost testgo]$ cut -d , -f 2 word.txt 
#,(comma)Separated by, field number 2 is cut out
124
523
789
100
200
[wataru@localhost testgo]$ cut -d , -f 2,4 word.txt 
#It is also possible to specify multiple field numbers
124,root
523,akira
789,tanaka
100,sasaki
200,aida
Convert and delete characters Character replacement for each character
wataru@localhost work]$ ls
gogodur  work.02.txt  work.04.txt  work.06.txt  work.08.txt
testgo   work.03.txt  work.05.txt  work.07.txt  work.09.txt
[wataru@localhost work]$ ls | tr a-z A-Z
#Lowercase a-Uppercase z A-Convert to Z
GOGODUR
TESTGO
WORK.02.TXT
WORK.03.TXT
WORK.04.TXT
WORK.05.TXT
WORK.06.TXT
WORK.07.TXT
WORK.08.TXT
WORK.09.TXT
[wataru@localhost work]$ ls | tr -d txt
#-Characters can be deleted using the d option
#This time, txt is deleted
gogodur
esgo
work.02.
work.03.
work.04.
work.05.
work.06.
work.07.
work.08.
work.09.
Display the end part
[wataru@localhost work]$ ls | tail -n 3
#-Use the n option to specify the number of lines
#Same for head command
work.07.txt
work.08.txt
work.09.txt
Show diff
[wataru@localhost work]$ diff work.02.txt work.04.txt 
#diff <Comparison source file> <Comparison file>
3,4c3
#「3,4 ”The 3rd and 4th lines of the first file
#"3" On the third line of the second file
#Change "c"
< 123445
< 
# <Is the deleted line
---
> 2345
# >Is the added line
        Recommended Posts