참조 원문 : 20 interesting and extremely helpful Linux command line tricks
1. 두 디렉토리 사이를 효율적으로 반복해서 이동하는 방법
A 디렉토리에서 B 디렉토리로 이동한 후 다시 A 디렉토리로 이동할 때 A 디렉토리의 경로를 쓰는 대신 ‘cd -‘를 사용하여 같은 효과를 볼 수 있습니다.
/home$ cd /var/log
/var/log$ cd –
/home
/home$
2. 콤마(,) 연산자
첫 번째 글자나 문자열 전체를 소문자로 변환 방법
$ string=”Example of comma OPERATOR”
$ echo ${string,}
example of comma OPERATOR
$ echo ${string,,}
example of comma operator
파일명 확장(extension)에 사용
$ touch new_file{1,2,3}
$ ls new_file*
new_file1 new_file2 new_file3
파일명을 my_filename.old에서 my_filename.new로 변경
$ mv my_filename.{old,new}
3. 파일명에 공백이 있을 때 제어하는 방법
파일명 전체를 큰 따옴표로 감싸거나 공백 앞에 역슬래쉬(\)를 넣습니다.
$ rm “temp File”
$ rm temp\ File
4. 하이픈(-)으로 시작하는 파일명 삭제 또는 생성 방법
$ rm -1mpFile.out
rm: invalid option — ‘1’
Try `rm ./-1mpFile.out’ to remove the file `-1mpFile.out’.
Try `rm –help’ for more information.
$ rm — -1mpFile.out
$ touch — -1mpFile.out
5. 디렉토리 안에 있는 파일 중 특정 확장자가 아닌 모든 파일 삭제 방법
$ rm !(*.c|*.py)
6. touch와 find를 이용해 특정 시간 사이에 생성되거나 수정된 파일 복사
어떤 디렉토리 안에 있고 09~17시 사이에 생성되거나 수정된 파일만 골라 복사하고 싶다고 가정했을 때 먼저 원하는 두 시간에 맞춰 타임스탬프 값을 가진 두 파일을 생성합니다.
$ touch -d “9am” temp1
$ touch -d “5pm” temp2
이제 대상 파일이 있는 디렉토리로 이동해서 아래 명령어를 사용합니다.(또는 find에서 대상 디렉토리를 지정)
$ find . -newer ../temp1 ! -newer ../temp2 -exec cp ‘{}’ ./target/ ‘;’
실행하면 대상 파일이 target이라는 하위 디렉토리 안에 복사됩니다. 물론 target 디렉토리를 먼저 만들어야 합니다.
7. 대상 파일이 너무 많아서 삭제가 안 될 때
디렉토리 안에 파일이 너무 많을 때 삭제를 시도하면 아래의 에러 메시지와 함께 삭제가 안 될 수 있습니다.
-bash: /bin/rm: Argument list too long
이럴 땐 아래의 방법을 사용합니다. 사용하기 전에 현재 디렉토리를 다시 한 번 확인하는 것이 좋습니다.
find * -xdev -exec rm -f ‘{}’ ‘;’
8. 특정 문자열을 포함한 파일 찾는 방법
$ grep -l “printf” *.c
하위 디렉토리에 있는 파일들까지 포함하여 문자열이 포함된 파일과 그 문자열이 있는 줄을 보고 싶다면 find, xargs, grep 명령어를 활용합니다.
$ find . -name “*.c” | xargs grep -H “buff”
./bfrovrflw.c: char buff[15];
./bfrovrflw.c: gets(buff);
./bfrovrflw.c: if(strcmp(buff, “MyLinuxBook”))
9. 파일 안의 내용을 모두 지우는 방법
$ > ./logfile
10. man 페이지에서 특정 문자열 검색 방법
$ man -k login
access.conf (5) – the login access control table file
add-shell (8) – add shells to the list of valid login shells
chsh (1) – change login shell
faillog (5) – login failure logging file
faillog (8) – display faillog records or set login failure limits
getlogin (3) – get username
getlogin_r (3) – get username
gnome-session-properties (1) – Configure applications to start on login
hotot (7) – lightweight & opensource microbloging client
issue (5) – prelogin message and identification file
lastlog (8) – reports the most recent login of all users or of a given user
login (1) – begin session on the system
login (3) – write utmp and wtmp entries
login.defs (5) – shadow password suite configuration
login_tty (3) – tty utility functions
logname (1) – print user’s login name
…
…
…
11. 표준 에러 출력 메시지를 파일로 리다이렉트시키는 방법
일반 리다이렉트로는 표준 에러로 출력되는 메시지를 담을 수 없습니다.
$ touch new > /home/himanshu/practice/logfile
touch: cannot touch `new’: Permission denied
$ cat /home/himanshu/practice/logfile
$
$ touch new > /home/himanshu/practice/logfile 2>&1
$ cat /home/himanshu/practice/logfile
touch: cannot touch `new’: Permission denied
2>&1은 표준 에러도 표준 출력으로 보내라는 뜻입니다.
12. history에 남기지 않고 명령어 사용
앞에 공백을 넣고 명령어를 사용합니다.
$ a
a: command not found
$ vi
$ b
b: command not found
$ history | tail -3
1055 a
1056 b
1057 history | tail -3
13. 영화처럼 같은 속도로 텍스트 출력
$ echo “You can simulate on-screen typing just like in the movies” | pv -qL 10
14. alias와 같은 이름을 가진 명령어가 있을 때 alias가 아닌 그 명령어 실행
앞에 역슬래쉬(\)를 넣고 명령어를 사용합니다.
mirashi@mirashi-virtual-machine:~/test$ alias ls=’ls -al’
mirashi@mirashi-virtual-machine:~/test$ ls
합계 8
drwxrwxr-x 2 mirashi mirashi 4096 7월 17 14:33 .
drwxr-xr-x 34 mirashi mirashi 4096 7월 17 14:33 ..
-rw-rw-r– 1 mirashi mirashi 0 7월 17 14:33 test
mirashi@mirashi-virtual-machine:~/test$ \ls
test
15. 특정 파일을 사용하고 있는 프로세스 찾아내기
파일을 지우려고 하는데 어떤 프로세스가 그 파일을 사용 중이라 지울 수 없을 때가 있습니다. 이럴 땐 fuser 명령어로 그 파일을 사용하고 있는 프로세스들의 PID를 알아낼 수 있습니다.
# fuser /var/log/httpd/access_log
/var/log/httpd/access_log: 24273 24277 24278 24279 24280 24281 24282 24283 24284 24305 24306 24307 34994 35370 35371 35372 35373 35374 35375 35376
또한 -k 옵션을 사용하면 해당 프로세스들을 죽일 수 있습니다.
16. 복사와 붙여넣기 단축키
먼저 복사하고 싶은 부분을 마우스로 드래그해서 선택한 후 Ctrl+Shift+C를 누르면 복사가 되고 Ctrl+Shift+V를 누르면 붙여넣기가 됩니다.