Apache
- Start Apache:$ service apache2 start
- Restart Apache:$ service apache2 restart
Archive and compress
- Archive to tar: $ tar -cvf archive.tar dir_to_archive/
- Extract from tar: $ tar -xvf archive.tar
- Compress to gzip: $ tar -czvf archive.tar.gz dir_to_compress/
- Extract from gzip: $ tar -xzvf archive.tar.gz
- Archive to zip: $ zip -r archive.zip dir_to_archive/
- Extract from zip: $ unzip archive.zip
Files and directories
- Create a file: $ touch file_name
- Copy file to directory: $ cp file to_dir/
- Rename file: $ mv original_name new_name
- Move file to directory: $ mv file_name some_dir/file_name
- Delete file: $ rm file_name
- Clear file content w/o deleting: $ truncate -s 0 file_name
- Create a directory: $ mkdir dir_name/
- Recursive make dir: $ mkdir -p dir_name/
- Copy directory A to directory B: $ cp -r dir_A/ dir_B/
- Move directory A to directory B: $ mv -r dir_A/ dir_B/
- Move the content of directory A to directory B: $ mv -rv dir_A/* dir_B/
- Delete directory recursively: $ rm -r dir_name
- Force directory deletion: $ rm -rf dir_name
- Delte directories older than x dats: $ find dir_name/* -mtime +x -exec rm -rf {} \;
- Download file to dir: $ wget https://example.com/file-name dir-name
- Open directory in the file manager: $ gnome-open dir-name/
Keyboard shortcuts
- Open terminal: Ctrl+Alt+T
- Tab completion: tab, tab X 2
- Switch windows: Alt+Tab
- Switch languages: Sup+Space
- Previous commands: up arrow
- Recent commands: Ctrl+R
- Kill process: Ctrl+C
- Exit session: Ctrl+D
- Copy: Ctrl+Shift+C
- Clear the terminal: Ctrl+L
- Repeat last command: !!
- Suspend process: Ctrl+Z
- Paste: Ctrl+Shift+V
- Hide the history: CTRL+L
Network
- Ping a domain by ip: $ ping ip
- Get whois information for domain: $ whois domain
- Connect a remote server: $ ssh -l domain-user example.com
List directory contents
- Detailed list: $ ls -l
- Including hidden: $ ls -a
- List by time: $ ls -t
Package management
- Install packge: $ yum install package_name
- Show package data: $ yum info package_name
- Uninstall package: $ yum remove package_name
- Install a specific package: $ apt-get install package_name
- Install multiple packages: $ apt-get install package1 package2
- Update the package list: $ apt update
- Upgrade the package list: $ apt-get upgrade
- Update and upgrade without asking: $ sudo apt-get update && sudo apt-get upgrade --yes
- Update; upgrade (even if update fails): $ sudo apt-get update; sudo apt-get upgrade
- Upgrade specific package: $ apt-get upgrade package_name
- Clean your system: $ apt-get autoremove
- Uninstall package: $ apt remove --assume-yes package_name
- Remove PPA: $ add-apt-repository --remove ppa:[ppa_name]
Permissions
- Set privileges recursively: $ chmod -R 755 dir
- Set privileges on a file: $ chmod 644 file
- Set ownership on a file: $ chown username:group file
- Set user on a file: $ chown username file
- Set group on a file: $ chown :group file
- Set ownership recursively: $ chown -R username:group directory
Process management
- Show active processes: $ ps
- Show the hierarchy of the commands: $ ps f
- Show active processes by pattern: $ ps ax | grep pattern
- Show jobs status: $ jobs
- Bring a job into the foreground: $ fg %n
- Send a job into the background: $ bg %n
- Show the top processes: $ top
- Monitor all traffic on HTTP (port 80): $ tcpdump -i eth0 'port 80'
- Kill process safely by pid: $ kill pid
- Dirty killing (if nothing else works): $ kill -9 pid
- Suspended a process (Ctrl+Z): $ kill -20 pid
- Restart a suspended process: $ kill -18 pid
- Kill all processes by pattern: $ pkill -f pattern
Read files
- Read a file: $ cat filename
- Read n first lines of file: $ cat -n filename
- Read while showing the line numbers: $ nl filename
- Read in reverse order: $ tac filename
- Count lines, words, characters: $ wc filename
- Show the first lines: $ head filename
- Show the last lines: $ tail filename
- Show lines matching pattern: $ cat filename | grep {pattern}
- Watch the last lines as they update: $ tail -f filename
- Put in alphabetical order: $ sort filename
- Filter out non unique lines: $ sort filename | uniq
- Read a file with pagination: $ less filename
Search
- Find files and directories by name: $ locate name
- Find files in /home that start with "ax": $ find /home/ -name 'ax*'
- Find file in /home by type: $ find /home -name '*.txt'
- Find files in /home larger than 30MB: $ find /home/ -size +30M
- Linux find command tutorial
- Search for pattern in file: $ grep pattern file
- Search recursively for pattern in dir: $ grep -r pattern directory
- Find string within a file: grep 'str' file_name
- Find string within multiple files: grep 'str' file1 file2 file3
- Find multiple strings within a file: grep 'str1 str2' file1 file2 file3
- Recursive search: grep -r 'str' /path/to/dir/
- Recursive search: grep -d recurse 'str' /path/to/dir/
- Ignore case: grep -i 'str' file_name
- Find only words: grep -w 'word' file_name
- Find more than 1 word: grep -w 'word1|word2' file_name
- List only the names of the matching files: grep -l 'str' /path/to/dir/
- List only files with the extension: grep -ril --include=\*.js 'pattern' /path/to/dir/
- Show 3 lines before the pattern: grep -ri -B3 'pattern' /path/to/dir/
- Show 3 lines after the pattern: grep -ri -A3 'pattern' /path/to/dir/
- Show 3 lines before and after: grep -ri -C3 'pattern' /path/to/dir/
- Show 3 lines consider file extension: grep --include=\*.js -ri -C3 'pattern' /path/to/dir/
- Limit the number of results with grep: grep -m3 'str' file_name
- Find pattern starting with (-): grep -e '-str' *.js
- Linux grep command tutorial
- Replace in directory if file ends with 'js':
find dir/ -name '*.js' | xargs sed -i 's/old/new/g' - Search and replace recursively:
grep -rl 'oldtext' dir/ | xargs sed -i 's/old/new/g' System
- Check os version: $ cat etc/os-release
- Show current date and time: $ date
- Show current uptime: $ uptime
- Show memory usage: $ free -h
- Show command info: $ man command-name
- Show the latest commands: $ history
- Show Linux system data: $ uname -a
- Show who is online: $ w
- Who am I logged in as: $ whoami
Terminal
- Run command in exactly the same way you last ran it: $ ![command_name]
- Rerun the last command: $ !!
- Rerun last command with privileges: $ sudo !!
- Show a list of the latest commands used: $ history
- Run command number xxx from the history: $ !xxx
- Search in history: CTRL+R
- Wipe the history: $ clear
- Hide the history: CTRL+L
- Decrease font size: CTRL+Minus(-)
- Increase font size: CTRL+Plus(+)
- Move cursor to the beginning of the line: CTRL+A
- Delete line: CTRL+K
- Move cursor to the end of the line: CTRL+E
- Full screen (IN & OUT): F11
- Run command A then B: $ [command_A] && [command_B]
- Run command A then B (independently): $ [command_A]; [command_B]
Vim editor
- Show line numbers :set number
- Move to INSERT mode; put cursor before i
- Move to INSERT mode; put cursor after a
- Move to INSERT mode; put cursor in a new line below o
- Move to INSERT mode; put cursor at the start of line Shift + i
- Move to INSERT mode; put cursor at the end of line Shift + a
- Move to INSERT mode; put cursor in a new line above Shift + o
- Move to COMMAND mode ESC
- Undo last operation u
- Go to the first line of the file gg
- Go to line 3 of the file 3gg
- Go to the last line of the file G
- Move 3 lines up 3 + ↑
- Move 5 lines down 5 + ↓
- Move 6 places right 6 + →
- Move 2 places back 2 + ←
- Go to the start of the line 0
- Go to the end of line $
- Search for pattern /{pattern}
- Search backward for pattern ?{pattern}
- Repeat search in same direction n
- Repeat search in opposite direction N
- Copy current line yy
- Copy everything within & including apostrophe yi"
- Copy everything within & including parentheses yi(
- Copy everything within & including brackets yi[
- Paste after cursor p
- Replace :%s/old/new
- Replace all with confirmation :%s/old/new/gc
- Delete the character under the cursor x
- Delete current line dd
- Delete lines 8 to 10 :8,10d
- Swap the current line with the line below it ddp
- Delete all lines containing pattern :g/{pattern}/d
- Delete all lines not containing pattern :g!/{pattern}/d
- Delete everything between apostrophe da"
- Delete everything between parentheses da(
- Delete everything between brackets da[
- Save file :w
- Quit if no changes were made :q
- Quit file without saving :q!
- Save file and quit :wq
Fun