Home | Projects | Notes > Unix/Linux > CLI Cheat Sheet
If you are using a mouse,
Highlight some text by holding down the left mouse button and dragging the mouse over it, or double-clicking a word. (It is copied into a buffer maintained by your system.)
Press the middle mouse button to paste the text at the cursor location.
Ctrl + A - Move cursor to the beginning of the line.
Ctrl + E - Move cursor to the end of the line.
Ctrl + F - Move cursor forward one character; same as the right arrow key.
Ctrl + B - Move cursor backword one character; same as the left arrow key.
Alt + F - Move cursor forward one word.
Alt + B - Move cursor backword one word.
Ctrl + F - Clear the screen and move the cursor to the top-left corner. The clear command does the same thing.
Ctrl + xx - Toggle between the start of line and current cursor position.
Ctrl + D - Delete the character at the cursor location.
Ctrl + H - Delete character before the cursor (backspace)
Ctrl + W - Cut the word before the cursor to the clipboard.
Ctrl + K - Cut the line after the cursor to the clipboard.
Ctrl + T - Transpose (exchange) the character at the cursor location with the one preceding it.
Ctrl + Y - Paste the last thing to be cut (yank)
Ctrl + _ - Undo
Alt + T - Transpose the word at the cursor location with the one preceding it.
Esc + T - Swap the last two words before the cursor.
Alt + L - Convert the characters from the cursor location to the end of the word to lowercase.
Alt + U - Convert the characters from the cursor location to the end of the word to uppercase.
Alt + Del - Delete the word before the cursor.
Alt + D - Delete the word afer the cursor
Ctrl + K - Kill text from the cursor location to the end of line.
Ctrl + U - Kill text from the cursor location to the beginning of the line.
Alt + D - Kill text from the cursor location to the end of the current word.
Alt + Backspace - Kill text from the cursor location to the beginning of the current word. If the cursor is at the beginning of a word, kill the previous word.
Ctrl + Y - Yank text from the kill-ring and insert it at the cursor location.
xxxxxxxxxx11441===============================================================================2 LINUX COMMAND LINE INTERFACE (CLI)3===============================================================================4
5SHELL6=====7Shell is a program that takes command from the keyboard and give them to the8operating system to perform.9
10
11TERMINAL12========13Terminal is a tool which you can use to pass your shell commands. This is a14program that opens a window and lets you interact with the shell.15
16* Ctrl+Alt+t - open terminal17* Ctrl+Shift+t - open second terminal in a new tab18
19
20FILESYSTEM21==========22The files on linux system are also arranged in what we called a hierarchical23directory structure which means that they are organized in a tree-like pattern24of directories. (Under 'root' directory all files and folders are residing.)25
26* . : current directory27* .. : parent directory (one directory up)28
29
30
31===============================================================================32 COMMANDS33===============================================================================34
35* pwd (present working directory)36
37 des. whenever you will open the terminal, by default the 'pwd' is your home38 directory ('home/username')39
40* cd (change working directory)41
42 syn. cd [dir]43
44 e.g. 45 cd / : change the working directory to the root directory 46 cd ~ : change the working direcotry to your home directory47 cd : same as 'cd ~'48 cd .. : change the working directory to the parent directory49 cd Music : change the working directory to the music directory50 cd My\ Doc : change the working directory to the 'My Doc' directory51 (to deal with directory whose name includes space, escape52 sequence '\' must be used)53 cd "My Doc" : same as 'cd My\ Doc'54 cd 'My Doc' : same as 'cd My\ Doc'55
56 [!] Note: Both absolute/relative paths will work with 'cd' command57
58* ls (list)59
60 des. list content in the directory61
62 syn. ls [options] [f/dir]63
64 e.g. 65 ls Music/ : list content in the Music directory66 ls -l : list in 'l'ong format67 ls -a : list 'a'll files (including hidden files)68 ls -al : list 'a'll files in 'l'ong format69 ls -S : sort list by 'S'ize70 ls *.html : list html files only71 ls *.* : list files regardless of the file names and extensions72 ls > f1 : redirect the output of ls to the file 'f1' 73 ls -d */ : list 'd'irectories only74 ls -R : list subdirectories 'R'ecursively75
76* clear77
78 des. clear the terminal (you can still see the history by scrolling up)79
80* cat81 82 des. display text files, combine copies of text files, create text files83
84 syn. cat [options] [f1] [f2] ...85
86 e.g. 87 cat : display the input text ('Ctrl+D' to exit)88 cat f1 : display content of f1 89 cat f1 f2 : display content of f1 and f2 combined90 cat -b f1 : display content of f1 with line numbers to the91 non-'b'lank lines92 cat -n f1 : display content of f1 with line 'n'umbers to all lines93 cat -s f1 : display content of f1 with consecutive blank lines94 's'queezed to one blank line (will not change the content95 of the file but just the display it this way for96 convenience)97 cat -E f1 : display conetnes of f1 with 'E'nd of Line character '$'98 appended to the end of each line)99 Ctrl+d : indicate that this is the end of the file and get out of100 the 'cat' command101
102* > (redirction)103
104 des. capture output from a file, command or program and send it as an input105 to another file, command or program106
107 syn. [output] > [f]108
109 e.g.110 cat > f : redirect input text to the file f (overwrite the111 existing conten)112 cat >> f : redirect input text to the file f (append to the113 existing content)114 cat f1 f2 > f3115 : redirect combined content of the files f1, f2 to file f3116 cat f1 >> f2117 : redirect and append the content of the file f1 to f2118 ('cat f1 f2 > f2' will not work)119
120* mkdir (make directory)121
122 syn. mkdiri [options] [dir]123
124 e.g.125 mkdir d : make directory named 'd'126 mkdir -p dp/dc127 : make directory structure (create 'p'arent directory dp128 and then create child directory dc inside dp)129 mkdir --parents dp/dc130 : same as 'mrdir -p dp/dc'131 mkdir -p dp/{dc1,dc2,dc3}132 : make directory structure (create 'p'arent directory dp133 and then create 3 child directories inside dp at once)134
135 [!] Note: 'mkdir -p dp/{dc1, dc2, dc3}' will cause an error due to spaces136 between dc's137
138* rmdir (remove directories)139
140 syn. mkdiri [options] [dir]141 142 e.g.143 rmdir a/b/c : remove the directory 'c' only144 rmdir -p a/b/c145 : remove the whole directory structure a/b/c ('p'arents)146 rmdir -pv a/b/c147 : remove the whole dirctory structure and show extended148 information ('v'erbose)149
150 [!] Note: if a directory contains files in it, rmdir won't work for that151 directory152
153* rm (remove files or directories)154
155 syn. rm [options] [f/dir]156
157 e.g.158 rm -rv a/b/c/d (where directory 'd' contains a file)159 : remove the file in directory 'd' and then remove the160 directory 'd' ('r'ecursive)161 rm -rv a : remove recursively the directories a, b, c, d and any162 files belong to the directory structure (just give the163 parent directory, and 'rm' command will remove everything164 under that directory)165
166 [!] Note: with the option 'r', 'rm' command removes only the given167 directory and its subdirectories (including the files) only and168 it does not remove the whole directory structure given.169 170* cp (copy files and directories)171
172 syn. cp [options] [source f/dir] [destination f/dir]173
174 e.g.175 cp f1 f2 : if f2 didn't exist, create f2 and copy the content of f1176 to f2177 cp f1 dir : copy f1 to dir178 cp f1 f2 f3 dir179 : copy f1, f2, f3 to dir (copy multiple files)180 cp -i f1 dir181 : prompt before overwrite ('i'nteractive)182 cp -R dir1 dir2183 : copy dir1 to dir2 (when dir1 contains files)184 ('R'ecursive)185 : if dir2 didn't exist, dir2 will be created and then the186 contents of dir1 will be copied to dir2187 : if dir2 did exist, dir1 will be copied into dir2 as a188 sub-directory189
190* mv (move (rename) files)191
192 syn. mv [options] [source f/dir] [destination f/dir]193
194 e.g.195 mv f1 f2 : rename f1 to f2196 mv f1 dir : move f1 to dir (overwrite the file named f1 that already197 exists in dir)198 mv -i f1 dir 199 : prompt before overwrite ('i'nteractive)200 mv dir1 dir2201 : if dir2 did exist, dir1 will be copied into dir2 as a202 sub-directory203 : if dir2 didn't exist, rename dir1 to dir2204
205* less (read or search in files)206
207 syn. 208
209 e.g. 210 less f : show the content of f from the begning211 : 'up/down arrow' keys to navigte line by line212 : 'space' key to navigate page by page (forward)213 : 'B' to navigate page by page (backward)214 : 'G' browse to the end of f215 : '1g' or 'g' to go to the begining of f216 : '/pattern' to forward highlight search 'pattern'217 - n : next hit218 - N : prev hit219 : '?pattern' to backword highlight search 'pattern'220 - n : next hit221 - N : prev hit222
223* touch (change file timestampso)224 225 des. easiest way to create new empty files. also used to change timestamps226 on existing files or directories227
228 syn. touch [options] [f/dir]229
230 e.g.231 touch f : create a new empty file f232 : if f is an already exsisting file, 'touch' command233 updates the timestamp of f234
235* nano (Nano's ANOther editor, inspired by Pico)236
237 des. nano is a small and friendly text editor and beside text editing Nano238 offers many extra features like an interactive search or replace, go239 to line or colum number, give indentation in the file or some other240 features.241
242 syn. nano [options] [f]243
244 e.g.245 nano f : open f in nano246 : create f if it didn't exist (f is actually created when247 it is saved from nano - 'Ctrl+o Enter')248
249 [!] Note: get used to nano commands to be fluent (^G means Ctrl+g)250
251* sudo (superuser do)252
253 des. sudo allows user to some extra privileges as an administrator or a254 superuser (when user opens a terminal, by default, he/she does not255 have the administrative or root previleges)256
257 syn. sudo ...258
259 e.g.260 sudo /etc mkdir newdir261 : if 'sudo' command is omitted, permission denied error262 will occur263 sudo -s : switch to the root user (switch to super user mode)264
265* top (display linux processes)266
267 des. top program provides a dynamic real-time view of a running system268
269 syn. top [options]270
271 e.g.272 top : display system summary information as well as a list of273 processes or threads currently being managed by linux274 kernel (by default, refreshes every 3 seconds)275 : press 's' to change refreshing frequency276 : press 'i' to filter out idle processes (press 'i' once277 again to go back to the complete view278 : press 'k' to kill a process by its PID279
280* kill281 282 des. send a signal to a process283
284 syn. kill [options] [pid] [...]285
286 e.g.287 kill 3286 : kill the program with PID 3286288 : if the program is not terminated with simple kill command289 following 3 options can be used force terminating290 - -9291 - -SIGKILL292 - -KILL293
294 [!] Note: when you do not know the PID of a certain process, you can get it295 by using 'pidof' command. if you do not even know the program296 name you want to kill, use either 'top' command or 'ps' command297 to find the PID.298
299* pidof300
301 des. find the process ID of a running program302
303 syn. pidof [options] [program]304
305 e.g.306 pidof unity-control-center307 : find the PID of 'unity-control-center' program308
309* ps310
311 des. report a snapshot of the current processes312
313 syn. ps [options]314
315 e.g.316 ps -ux : show a list of running processes used by current user317 ps -aux : show a list of running processes used by all users318 ps -U username319 : show a list of running processes used by a specific user320 'username'321 ps -C procname322 : show a list of running processes related to the program323 'procname'324
325* echo326
327 des. display a line of text328
329 syn. echo [options] [string]330
331 e.g.332 echo hello : display 'hello'333 echo "hello": wrapping the string with double quotes is a good334 convention335 myvar="kj" : assign "kj" to the variable 'myvar' (make sure there's no336 space around '=')337 echo $myvar : display the content(value) of 'myvar'338 x=10 : assign 10 to variable 'x'339 echo "the value of x is $x"340 : (output) the value of x is 10341 echo -e 'some \text'342 : (output) some ext343 : 'e'nable interpretation of '\' as an escape sequence344
345 [!] Note: variables are valid only in the current session of terminal346 (when terminal is closed and then reopened, myvar is gone)347 [!] Note: echo command becomes more powerful when you are working with348 scripting349
350* permissions351
352 e.g.353
354 -rw-rw-r-- 1 klee klee 36 Sep 9 00:32 README.md355 ---------- --- ---- ---- -- ------------- ---------356 symbolic # of owner group size date/time file name357 permissions symbolic358 links359 360
361 syn. symbolic permission classes (symbolic mode) 362
363 - r w x r w - r - -364 --- --------- --------- ---------365 [file type] [owner/user] [ group ] [ other ]366 | |367 | +--> r : read368 | w : write369 | x : execute370 |371 |372 +---> - : file373 d : derectory374 c : character special file375 b : binary special file376
377 syn. octal permission (numerical representation of the file permission)378
379 - r w x r w - r - -380 --- --------- --------- ---------381 [file type] [owner/user] [ group ] [ other ]382
383 1 1 1 1 1 0 1 0 0384 2^2 2^1 2^0 2^2 2^1 2^0 2^2 2^1 2^0385 4 2 1 4 2 0 4 0 0386 4 + 2 + 1 4 + 2 + 0 4 + 0 + 0387 --------- --------- ---------388 7 6 4389
390 +------------------------------------------------------------+391 | r,w,x permissions | binary | octal |392 |============================================================|393 | --- | 000 | 0 |394 |------------------------------------------------------------|395 | --x | 001 | 1 |396 |------------------------------------------------------------|397 | -w- | 010 | 2 |398 |------------------------------------------------------------|399 | -wx | 011 | 3 |400 |------------------------------------------------------------|401 | r-- | 100 | 4 |402 |------------------------------------------------------------|403 | r-x | 101 | 5 |404 |------------------------------------------------------------|405 | rw- | 110 | 6 |406 |------------------------------------------------------------|407 | rwx | 111 | 7 |408 +------------------------------------------------------------+409
410* chomod (change file mode bits)411
412 des. utility to change permission of files413
414 syn. chmod [options] [mode] [f/dir]415
416 e.g. symbolic permissions417 chmod o+x f : add(+) e'x'ecutable permission to 'o'ther for f418 chmod o+w f : add(+) 'w'rite permission to 'o'ther for f419 chmod g+w f : add(+) 'w'rite permission to 'g'roup for f420 chmod g-wx f: remove(-) read and write permissions from 'g'roup for f421 chmod ug=rwx f422 : change 'u'ser and 'g'roup mode of f to 'rwx'423 chmod ugo-rwx f424 : remove(-) remove all the permissions from all the users425 chmod a-rwx f426 : same as 'chmod ugo-rwx f ('a'll)427 chmod u+rw,g=rw,o+r f428 : work with multiple user's permissions429 chmod u-w dir (u=rx)430 : user does not have write permission to dir431 : user can read the content of dir432 : user can 'cd' to dir433 : user CANNOT create new files inside dir434 chmod u-r dir (u=wx)435 : user does not have read permission to dir436 : user can 'cd' to dir437 : user CANNOT read the content of dir438 chmod u-x dir (u=rw)439 : user does not have execute permission to dir (user440 CANNOT execute anything with dir)441 : user can only read the names of content inside dir (even442 the permissions of the files are '?' marked)443 : user CANNOT 'cd' to dir444 : user CANNOT access to any files in dir445 446 e.g. octal(numerical) permissions447 chmod 000 f : nobody has any permission for f448 chmod 755 f : give user 'rwx' permissions449 : give group 'r-x' permissions450 : give other 'r-x' permissions451
452* which453
454 des. return a path name of a file or command (locate a file)455
456 syn. which [options] [f]457
458 e.g. 459 which bash : locate the command 'bash' (/usr/bin/bash)460 which f : locate f461 which filefox462 : /usr/bin/firefox463 464 [!] Noate: the only options available for 'which' is '-a'465
466* whatis467 468 des. display one-line manual page descriptions469
470 e.g.471 whatis ls : show one-line manual page description of 'ls'472
473* useradd474
475 des. create a new user of update default new user information476
477 syn. useradd [options] LOGIN478 useradd -D479 useradd -D [options]480
481 e.g.482 sudo useradd username -m -s /bin/bash -g users -c "my comment"483 : create the user 'username' with features specified by the484 options485 -m : create default home directory for this user486 -s : allow the user to use the designated shell487 -g : assign default group to this user488 -c : provide comment to this user489 ...490 sudo passwd username491 : reset password for 'username'492 sudo passwd : reset password for root user493
494 [!] Note: new user can be found in '/home/'495
496* userdel497
498 des. delete a user account and related files499
500 syn. userdel [options] LOGIN501
502 e.g.503 sudo userdel username504 : delete the user 'username' 505 : but it does not delete the home directory for 'username'506 (this is useful when a company wants to delete a resigned507 person's account from the HR database but still wants to508 keep the work the person did)509 sudo rm -r /home/username510 : delete the leftover home directory of 'username'511 sudo userdel -r username512 : delete the user 'username' as well as his/her home513 directory514
515* groups516
517 des. print the groups a user is in518
519 syn. groups520
521 e.g.522 groups523 cat /etc/group524 : show all the groups available on your system (groups are525 added based on date-wise order, the newest group goes to526 the bottom of the list)527
528 e.g. sambashare:x:132:klee529 ---------- ----530 group user531 532* groupadd533
534 des. create a new group535
536 syn. groupadd [options] [group]537
538 e.g.539 sudo groupadd groupname540 : add the group 'groupname' to the system ('/etc/group')541
542* groupdel543 544 des. delete a group545
546 syn. groupdel [options] [group]547
548 e.g.549 sudo groupdel groupname550 : delete the group 'groupname' from the system551 ('etc/group')552
553* gpasswd554
555 des. administer '/etc/group' and '/etc/gshadow' (every group can have556 administrators, members and a password)557
558 syn. gpasswd [options] [group]559
560 e.g. 561 sudo gpasswd -a username groupname562 : add a user 'username' to the group 'groupname'563 sudo gpasswd -d username groupname564 : 'd'elete a user 'username' from the group 'groupname'565
566* df567
568 des. report file system disk space usage569
570 syn. df [options] [f/dir]571
572 e.g.573 df : show disk space usage574 df -h : show disk space suage in 'h'uman readable format575
576* du577
578 des. estimate file space usage (estimate and display the disk space used by579 files)580
581 syn. du [options] [f/dir]582
583 e.g. 584 du : estimate file space usage of the current directory 585 du /etc/ : estimate file space usage of the '/etc/' directory586 du -h : ... in 'h'uman readable format 587 du -sh : ... in 's'ummary and in 'h'uman readable format 588 : if this command gives you 'cannot access' warnings, use589 'sudo' command590
591* free592
593 des. display amount of free and used memory in the system as well as the594 buffer used by kernel595
596 syn. free [options]597
598 e.g.599 free -h : display in 'h'uman readable format600 free -b : display in 'b'yte601 free -k : display in 'k'illobyte602 free -m : display in 'm'egabyte603 free -g : display in 'g'igabyte604 free -t : display in 't'errabyte605
606* watch607
608 des. execute a program periodically, showing output fullscreen609
610 syn. watch [options] [command]611
612 e.g.613 watch free -m614 : execute 'free -m' command periodically615 watch -n 0.5 free -m616 : ... adjust frequency to 0.5 sec (default frequency is617 2 sec) 618
619 [!] Note: 'Ctrl+c' to come out of the command620
621* head622
623 des. output the first part of files (10 lines by default)624
625 syn. head [options] [f]626
627 e.g. 628 head -n3 f : display the first 3 lines of f629 head -3 f : display the first 3 lines of f630 head f1 f2 : display the first parts of f1 and f2631
632* tail633
634 des. output the last part of files (10 lines by default)635
636 syn. tail [options] [f]637
638 e.g.639 tail -n3 f : display the last 3 lines of f640 tail -f f : stay in 'tail' command and output appended data as the641 file grows ('f'ollow) 642
643* find644
645 des. search for files in a directory hirerarchy646
647 syn. find [dir] [options] [f]648
649 e.g.650 find /home/kjlee -name file.txt651 : search 'file.txt' in the path '/home/kjlee' and show the652 file location if it is found653 find / -mtime -3654 : search all files created any time between 3 days ago and655 now (this is useful when you don't know the name of the656 file you want to search)657 find / -mtime 1658 : search all files created exactly 1 day ago659
660 [!] Note: in case the 'Permission denied' warning appears, use 'sudo'661 command662
663* wc664
665 des. print newline, word, and byte counts for each file666
667 syn. wc [options] [f]668
669 e.g.670 wc file.txt : 1 6 42 file.txt671 --- --- ---- --------672 # of # of # of name of673 lines words chars file674 wc -c f : display just the byte counts (essentially the number of675 'c'haracters) in f676 wc -l f : display just the number of lines677 wc -w f : display just the number of words678 wc -L f : display just the length(# of chars) of the longest line679
680* cal681
682 des. display a calendar and the date of Easter683
684 syn. cal [options]685
686 e.g.687 cal : display a default calendar (week days on the top)688 ncal : display a calendar with week days on the left689 cal 2020 : display all the calendar months in year 2020690 cal 5 2020 : display the calendar for May, 2020 only691 cal -3 : display prev, current, next months692
693 [!] Note: '-1' is the default option for 'cal' command694
695* date696
697 des. print or set the system date and time698
699 syn. date [options] [format]700 date [format specifiers]701
702 e.g.703 date : display current date and time704 date -s "11/20/2020 12:48:00"705 : set the system date and time to '11/20/2020 12:48:00'706 date +%d%h%y707 : 04Jan17708 date "+%d-%h-%y"709 : 04-Jan-17710 date "+%d/%h/%y"711 : 04/Jan/17712 date "+DATE: %m/%d/%y%nTIME: %H:%M:%S"713 : DATE: 01/04/17714 RIME: 22:25:13715
716 [!] Note: format specifiers of 'date' command717 +%m : specifies the month number718 +%h : specifies the abbreviated month(Jan-Dec)719 +%B : specifies the full month name720 +%d : specifies the day of the month721 +%y : specifies the year722 +%H, +%M and +%S : specifies the hour, minute, seconds723 +%D : specifies the date as mm/dd/yy724 +%T : specifies the time as hh:mm:ss725
726* apt-get727
728 des. APT package handling (install, remove, etc.) utility729
730 syn. apt-get ...731
732 e.g.733 sudo apt-get update734 : resynchronize your local package files with server files735 : these package files are stored in '/etc/apt/'736 ('/etc/apt/sources.list' file contains paths to the737 repositories of the packages)738 sudo apt-get install vim739 : install 'vim' software package740 : this command will display the list of extra packages to741 be installed (due to dependency) and how much space in742 memory this installation will take up.743 sudo apt-get remove vim744 : remove 'vim' software package745 [!] Note: configuration files will NOT be removed! 746 sudo apt-get remove --purge vim747 : remove 'vim' software package + configuration files748 sudo apt-get autoremove749 : remove packages that were automatically installed to750 satisfy the dependency for other packages and that are751 now no longer needed752 [!] Note: please check 'The following packages will be753 REMOVED:' section in case you accidentally remove754 necessary packages!755
756 [!] Note: 'yum' or 'dnf' command for redhat based OS(CentOS, Fedora, etc.)757 758* ifconfig (interface configuration)759
760 des. display or change the configuration of network interface on user's761 system762
763 syn. ifconfig [interface] [options]764
765 e.g. 766 ifconfig : display the configuration of network interface on your767 system768 : 'eth0' suggests that you have WIRED Connection (ethernet769 cable)to the internet (additional ethernet interfaces770 will be named 'eth1', 'eth2', and so on. if this is the771 case 'eth0' will be your main ethernet interface)772 : 'wlan0' suggests that you have WIRELESS Connection to the773 internet774 : 'lo' suggests that you have LOOPBACK Interface775 ifconfig eth0776 : display 'eth0' information only777 sudo ifconfig eth0 up778 : enable internet connection through 'eth0'779 sudo ifconfig eth0 down780 : disable internet connection through 'eth0'781
782* tar (tape archive; an archiving format just like 'zip' format)783 784 des. an archiving utility (create, maintain, modify and extract files which785 are archived in the tar format786
787 syn. tar [options] [compressed_name.tar] [f/dir]788
789 e.g.790 tar -cvf comp.tar dir791 : compress 'dir' to 'comp.tar' file792 - c 'c'reate an archive (create a tar file)793 - v 'v'erbose (display the progress in the terminal794 while creating the archive file 795 - f specify the 'f'ile name ('comp.tar' in this ex)796 tar -xvf comp.tar797 : e'x'tract 'comp.tar' file798 tar -czvf comp.tar.gz dir799 : compress 'dir' to 'comp.tar.gz', a gz(gzip) formatted800 file 801 [!] Note: 'z' flag must come after 'c' flag802 tar -xzvf comp.tar.gz dir803 : e'x'tract 'comp.tar.gz' file804
805* grep (global regular expression print)806
807 des. print line that match specified patterns (case sensitive by default)808
809 syn. grep [options] [pattern] [f/dir]810
811 e.g.812 grep "pattern" file.txt813 : print lines in 'file.txt' that contain 'pattern' 814 (case sensitive by default)815 grep -i "pattern" file.txt816 : print lines in 'file.txt' that contain 'pattern'817 (case 'i'nsensitive)818 grep -n "pattern" file.txt819 : print lines with line 'n'umbers in 'file.txt' that820 contain 'pattern'821 grep "pattern" f1 f2 f3 f4822 : print lines in multiple files (f1, f2, f3, f4) that823 contain 'pattern' (file names are printed as well)824 grep -v "pattern" file.txt825 : print lines in 'file.txt' that does NOT contain 'pattern'826 (in'v'erse match)827 grep --help : display additional information on 'grep' command828 829* netstat (network statistics)830
831 des. a command line tool used to display network connection, routing tables832 and a number of network interfaces. also can be used to view network833 protocol state statistics834
835 syn. netstat [options]836
837 e.g.838 netstat -a : display 'a'll the connections available on your system839 netstat -a | less840 : display the information from the first page on841 netstat -at | less842 : display TCP connections only843 netstat -au | less844 : display UDP connections only845 netstat -l | less846 : display ports that are in 'l'istening state847 netstat -lt | less848 : display 'l'istening TCP connections only849 netstat -lu | less850 : display 'l'istening UDP connections only851 netstat -s | less852 : display 's'tatistics853 netstat -pt | less854 : display 'P'IDs of all TCP connections855 netstat -c | less856 : display status 'c'ontinuously (refreshed)857 : Ctrl+c to quit the mode858 netstat -ie : display 'e'xtend 'i'nterface (same as 'ifconfig'859 command)860 netstat -a | grep ':80'861 : among the output of 'netstat -a' command, filter lines 862 that contain ':80'863
864* dpkg865
866 des. a command line tool to install, build, remove and manage Debian867 packages.868
869 syn. dpkg [options] action870
871 e.g. 872 sudo dpkg -i google-chrome-stable_current_amd64.deb873 : install google-chrome from the Debian package 874
875* diff (GNU diff)876
877 des. compare files line by line.878
879 syn. diff [options] [f1] [f2]880
881 e.g.882 diff f1 ../f1883 : compare f1's in two different locations line by line884 : (in the result) the arrows ('<' and '>') refer to what885 the value of the line is in the left('<') file, with the886 left file being the one user entered first on the command887 line, in this case 'f1'888
889
890
891
892===============================================================================893 COMBINING COMMANDS 894===============================================================================895
896* ; (semicolon)897
898 e.g.899 date ; cal ; pwd900 : execute 'date', 'cal', 'pwd' commands sequentially901
902 [!] Note: even if a command fails, the following commands will still be903 executed904
905* && (double-ampersand)906
907 e.g.908 ls && pwd && date && cal909 : execute 'ls', 'pwd', 'date', 'cal' commands sequentially910
911 [!] Note: whenever a command fails, the following commands will NOT Be912 executed913
914* || (double-pipe)915
916 e.g.917 ls || pwd : execute 'ls' command only 918
919 [!] Note: if execution of the first command is successful, the second920 command will not be executed (basically, logical OR operation)921
922
923
924===============================================================================925 BASH SCRIPTING926===============================================================================927
928* script929
930 a text file that contains a sequence of command for linux based operating931 system932
933* creating a script934
935 nano myscript.sh 936 (including the extension 'sh' is not mandatory but is a good habbit)937
938* writing a script939 940 -----------------------------------TOP------------------------------941
942 #!/bin/bash 943 : location of 'bash' (use 'which bash' command to get this info) 944
945 /bin/ls -l946 : in linux bash script, it's a good practice to use the full path947
948 STRING="Hello World!"949 echo $STRING950
951 (more commands...)952
953 ---------------------------------BOTTOM-----------------------------954
955 [!] Note: when this script is run, a long list will be printed followed by956 the string "Hello World!"957
958* running a script959
960 ./myscript961
962 [!] Note: user must have the execute permission to run the scrip963 964
965
966===============================================================================967 .bashrc 968===============================================================================969
970* des. '.bashrc' file is a script that is executed whenever a new terminal971 session is started in interactive mode (resides in a user's home972 directory)973
974* e.g. .basrc file is used to set evironment variables 975 (setting JAVAHOME variable when installing Java) 976
977 [!] Note: instead of starting a new terminal session, sourcing '.bashrc'978 file will also apply the changes to the current terminal session979
980* opening the script981
982 nano .bashrc983 gedit .bashrc984 vim .bashrc 985
986* editing the script987
988 alias ls='ls --color=auto -l'989
990
991
992===============================================================================993 ELSE 994===============================================================================995
996## Bash Commands997
998* uname -a show system and kernel999* head -n1 /etc/issue show distribution1000* mount shot mounted filesystems1001* date show system date1002* uptime shot uptime1003* whoami show your username1004* man command show manual for 'command'1005
1006
1007## Bash Shortcuts1008
1009* CTRL-c stop current command1010* CTRL-z sleep program1011* CTRL-a go to start of line 1012* CTRL-e go to end of line1013* CTRL-u cut from start of line1014* CTRL-k cut to end of line1015* CTRL-r search history1016* !(history#) Repeat command with history#1017* !! Repeat last command1018* !abc run list command starting with 'abc`1019* !abc:p print last command starting with 'abc'1020* !$ last argument of previous command1021* !* all arguments of previous command1022* ^abc^123 run previous command, replacing 'abc' with '123'1023
1024
1025## Bash Variables1026
1027* env show environment variables1028* echo $NAME output value of $NAME variable1029* export NAME=value set $NAME to 'value'1030* $PATH executable search path1031* $HOME home directory1032* $SHELL current shell1033
1034
1035## Directory Operations1036
1037* pwd show current directory (print working directory)1038* mkdir dir make directory 'dir'1039* cd dir change directory to 'dir'1040* cd .. go up a directory1041* ls list files1042 -a: show all (including hidden)1043 -R: recursive list1044 -r: reverse order1045 -t: sort by last modified1046 -S: sort by file size1047 -l: long listing format1048 -1: on fie per line1049 -m: comma-separated output1050 -Q: quoted output1051
1052
1053## Search Files1054
1055* grep pattern files search for 'pattern' in 'files'1056* grep -i case insensitive search1057* grep -r recursive search1058* grep -v inverted search1059* find /dir/ -name name* find files starting with 'name' in 'dir'1060* find /dir/ -user name find files owned by name in 'dir'1061* find /dir/ -mmin num find files modified less than 'num' minutes ago in 'dir'1062* whereis command find binary / source / manual for 'command'1063* locate file find 'file' (quick search of system index)1064
1065
1066## File Operations1067
1068* touch file1 create 'file1'1069* cat file1 file2 concatenate files and output1070* less file1 view and paginate 'file1'1071* file file1 get type of 'file1'1072* cp file1 file2 copy 'file1' to 'file2'1073* mv file1 file2 move 'file1' to 'file2'1074* rm file1 delete 'file1'1075* head file1 show first 10 lines of 'file1'1076* tail file1 show last 10 lines of 'file1'1077* tail -f file1 output last lines of 'file1' as it changes1078
1079
1080## Process Management1081
1082* ps show snapshot of processes1083* top show real time processes1084* kill pid kill process with id 'pid'1085* pkill name kill process with name 'name'1086* killall name kill all processes with names beginning 'name'1087
1088
1089## Nano Shortcuts1090
1091* Ctrl-R read file1092* Ctrl-O save file1093* Ctrl-X close file1094* Alt-A start marking text1095* Ctrl-K cut marked text or line1096* Ctrl-U paste text1097* Alt-/ end of file1098* Ctrl-A beginning of line1099* Ctrl-E end of line1100* Ctrl-C show line number1101* Ctrl-_ Go to line number1102* Ctrl-W find1103* Alt-W find next1104* Ctrl-\ search and replace1105
1106
1107## Screen Shortcuts1108
1109* screen start a screen session1110* screen -r resume a screen session1111* screen -list show your current screen sessions1112* Ctrl-A activate commands for screen1113* Ctrl-A c create a new instance of terminal1114* Ctrl-A n go to the next instance of terminal1115* Ctrl-A p go to the previous instance of terminal1116* Ctrl-A " show current instances of terminals1117* Ctrl-A A rename the current instance of terminal1118
1119
1120## File Permissions1121
1122* chmod 775 file change mode of 'file' to 7751123* chmod -R 600 folder recursively chmod 'folder' to 6001124* chown user:group file change 'file' owner to user and group to group1125
1126
1127## File Permission Numbers1128
1129* 1st diigt: owner permission1130* 2nd digit: group permission1131* 3rd digit: everyone permission1132* Calculate each of the three permission digits by adding the numeric values of the permissions below.1133 - 4: read(r)1134 - 2: write(w)1135 - 1: execute(x)1136
1137
1138
1139==============================================================================1140 REFERENCE1141==============================================================================1142
1143* Linux Command Line Tutorial For Beginners | Bash Terminal | Linux Terminal1144 - https://www.youtube.com/playlist?list=PLS1QulWo1RIb9WVQGJ_vh-RQusbZgO_As