Programming
Frequently used bash commands
by leanu
2014. 3. 31.
자주 사용하는 Bash 명령어들을 정리해보았다.
Array
Associative Array
| declare -A fullAddr # Create an associative array fullAddr[inc]=Incheon # Insert key,value to array fullAddr[sul]=Seoul # Insert key,value to array location=inc echo "Address List : ${fullAddr[@]}" echo "Current Address : ${fullAddr[${location}]}" # export env by using the value of variable # (ex) export TEST_SRC=/home/myaccount prefix=TEST export ${prefix}_SRC=/home/myaccount |
Loop
Dir
| for dir in ./*; do cp $dir/backup $dir/active; done |
grep
grep with string replacement (xargs)
| grep -nrl "using namespace std" --include=*.h * | xargs sed -i 's/using namespace std;//g # 주석 : 모든 h 파일에서 using namespace std; 라고 쓰여진 부분을 삭제함. |
find
| #test-dir로 시작하는 모든 디렉토리의 모든 파일에 대해 특정 문자열(<DT_)로 시작되는 줄 삭제 $ find /test-dir* -type f -exec sed -i '/^<DT_/d' {} \; |
| # recursively change file extension from .txt to .bak find . -name "*.txt" | sed "s/\.txt$//" | xargs -I% mv -iv %.txt %.bak # recursively remove file extension of .txt find . -name "*.txt" | sed "s/\.txt$//" | xargs -I% mv -iv %.txt % |
sed
| # Look at the specific line(13579'th line) in large file (TEST.TXT) sed '13579q;d' TEST.TXT |
댓글