Linux command # 3

Basic 3 of Linux commands.

redirect

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

pipeline

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

wc command

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 command

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

uniq command

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 command

Cut out a part of the input ** cut -d -f **

[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

tr command

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.

tail command

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

diff command

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

Linux command # 4
Linux command # 3
Linux command # 5
Linux command list
linux at command
[Linux] Search command
Linux command <Basic 2>
Linux [directory command]
Linux server command
Linux # Command Memo 1
Linux command [read]
Linux Command Summary
[Basic] linux command
Linux [shell command]
[Linux] Command / Knowledge
My linux command
Linux command <Basic 1>
Linux command collection
Linux mkdir command
Linux command basics
[Linux] Git command
Linux (command memory)
[Linux] Volume configuration command
Linux command cheat sheet
Linux command (sequential update)
Linux basic command memorandum
Linux command [File operation]
[Linux] Basic command summary
Linux
Linux command for self-collection
linux command error collection 1
Linux command line shortcut
linux sar command CPU usage
[Linux] tar.gz compression / decompression command
What is Linux? [Command list]
Easy df command on Linux
Linux tar xz command memo
Linux Command Dictionary (for myself)
linux: create original Terminal command
[Note] Useful linux command collection
Linux command memorandum [for beginners]
Linux PC spec check command
[Linux] User / group command summary
[Infrastructure] Linux command, shell script collection
linux memorandum
Linux commands
[Linux convenient command] Try inserting exa
Linux commands
Linux overview
Linux basics
Command to create Linux Live USB
Command memorandum
direnv (linux)
nkf command
Organize Linux
command prompt
[Linux] OS recovery with restore command
linux commands
Linux practice
Ubuntu Linux 20.04
Completion of docker command on Linux