** Reference
☞ http://milli.tistory.com/14 ( sed )
☞ http://slog2.egloos.com/3689816 ( sed, awk )
☞ http://wiki.kldp.org/wiki.php/Awk ( awk )
** Shell
☞ https://jhnyang.tistory.com/57 : [리눅스] 셸(Shell)이란? 셸의 변경, 쉘 개념, 기능, 종류와 특징(sh, bash, csh, tcsh, ksh)
- A kind of utility between user and kernel
- Check shells
> echo $SHELL // current shell
> cat /etc/shells // available shell
> cat /etc/passwd | grep root // default shell during log-in
- 1st line of shell script file
#!bin/bash -> Execute the script with bash shell
( '#!' : use the following program )
cf. 'setenv' command is not available in bash(#!/bin/bash). csh(#!/bin/csh -f) is recommended for script.
- Variable
* Declaration
Syntax 1 : set variable_name = variable_value ## General
Syntax 2 : set variable_name = (value1 value2 value3 … valuen) ## Set
" " : "String, Space, etc."
( ) : (multiple contents with space)
cf. unset
* Reference
Syntax : $variable_name, $variable_name[index]
* from standard input
Syntax : set variable_name $<
* argument
1st : $argv[1] ## There is no $argv[0] in C-shell
cf. setenv : used in other application / set : used in Shell
** Special characters in sed, shell
. * [ ] ^ $ \ `
=> if you want use these characters as the character itself, '\' is added before them
e.g. Replace {`include "} for {`include "/user/bin/bin}
=> sed 's/\`include "/\`include "\/user/\bin/\bin/g
** Regular Expression
- ^ : 1st column of the line
- $ : Last column (or line) of the line ( or file )
** mv, cp, rm
unix> mv dir1/ dir2/ // change directory name from 'dir1' to 'dir2'
unix> mv dir1 par_dir/. // move dir1 directory into par_dir
unix> cp -r dir1/* dir2/. // copy files together with directories
unix> rm -rf * // remove * with 'recursive' and 'force' option -> including directories
unix> find ./ ! -name 'file_name' -exec rm -rf {} \; // Delete all the files except for the specified files
**
unix> wc -l TARGET_FILE
=> Count the line number in the TARGET_FILE
** Check disk spaces
// df : disc(file system) capacity -> -h : in easily readable unit ( human )
// du : directory or file size -> -s : specified file or directory
unix> du -sk FILE_OR_DIRECTORY_NAME // check disk usage ( s:sum, k:unit=KB ) cf. -m : MB
unix> df -h FILE_NAME // check disk free
** tar(Tape ARchive), zip
unix> tar cvf TAR_FILE_NAME FILE1 FILE2 ... // Create tar file
unix> tar xvf TAR_FILE_NAME // Extract tar file
unix> gzip FILE_NAME
unix> gunzip FILE_NAME
** sed : Stream EDitor
foreach HH (*.v)
set -e 's/PHRASE_TO_FIND/PHRASE_TO_SUBSTITUTE/g'
{$HH} > ../{$HH}
end
cf. -e : There is another target;
sed -e 's/Index/Index_/g' -e 's/Contents/Contents_/g' FILE_NAME
// Substitute
sed 's/Target_String/Replaced_String/' Target_File_Name
=> Replace the Target_String with Replaced_String in the file with Target_File_Name
// Delete
sed '/Target_String/d' Target_File_Name
=> Delete the lines which includes Target_String in the files with Target_File_Name
sed '/Target_String/!d' Target_File_Name
=> Remain the lines which includes Target_String and remove the other lines
sed '1,5d' Target_File_Name : Delete the lines from 1st to 5th
sed '/^$/d Target_File_Name : Remove the empty lines
** grep : g/re/p = global / regular expression / print
☞ https://ra2kstar.tistory.com/100
unix> grep pattern_to_be_found File_Name1 File_Name2 ... // To display all the rows including the pattern from the files
cf. -i option : without case sensitivity
unix> grep -n pattern_to_be_found File_Name // To check # of pattern in File
unix> grep -l pattern_to_be_found * // To find a list of files including the pattern in the current directory
unix> grep -r pattern_to_be_found * // To display strings with the pattern from all the files in the current and the sub directories
cf. if grep does not support '-r' option, then 'find' command can be used to deliver the files in sub-directories
> find -name "Target_Files" Target_Directory | xargs grep -ni "Target_Pattern" // find all the files and deliver them as arguments used for 'grep' command
$ grep '^a' File_Name // To search the rows which starts with 'a'
$ grep '^[ab]' File_Name // To search the rows which starts with 'a' or [b]
$ grep 'e$' File_Name // To find the rows which ends with 'e'
$ grep [aA]pple File_Name // To search the words with 'apple' and 'Apple'
cf. Meta Characters for grep
. :
* :
[] : matched with one of the characters in square brackets
[-] : matched within the ranges of characters in sqaure brackes
^ : first of the string line
$ : last of the string line
-> meta character is recognized as normal character if '\'(backslash) is placed before it
e.g.
> grep "\*" file.txt
> grep "\." file.txt
** Job search and kill
> jjobs
> jkill <Process ID>
** User group
> newgrp <Account_Name> // add account to User group
> groups // Check the user groups
** Make files Executable
> chmod +x
** File search
> find -name "File_Name" -print // Wild character can be used for File_Name
> find -name "Target_Files" Target_Directory // find Target_Files from the target directory and its sub-directories
> find -name "Target_Files" Target_Directory -metadepth 2 // find Target_Files from the target directory and its sub-directories whose depth is within 2
-------