Apache
- Start Apache:$ service apache2 start
- Restart Apache:$ service apache2 restart
Archive and compress
- Compress to gzip: $ tar -zcvf archive.tar.gz dir_to_compress
- Uncompress tar: $ tar -tf archive.tar
- Uncompress gzip: $ gzip -d archive.gz
- Uncompress tar gzip: $ tar -zxvf archive.tar.gz -C /destination/dir/
- Uncompress bzip2: $ bzip2 -jtf archive.tar.bz2
- Compress to zip: $ zip -r archive.zip dir_to_compress/
- Uncompress 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 a file: $ rm file_name
- Create a directory: $ mkdir 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
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
- Upgrade a specific package: $ apt-get upgrade package_name
- Clean your system: $ apt-get autoremove
- Uninstall package: $ apt remove --assume-yes package_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 file-name
- Read while showing the line numbers: $ nl file-name
- Read in reverse order: $ tac file-name
- Count lines, words, characters: $ wc file-name
- Show the first lines: $ head file-name
- Show the last lines: $ tail file-name
- Put in alphabetical order: $ sort file-name
- Filter out non unique lines: $ sort file-name | uniq
- Read a file with pagination: $ less file-name
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/
- Limit the number of results with grep: grep -m3 'str' file_name
- Find pattern starting with (-): grep -e '-str' *.php
- 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
- 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
vi editor
- Move to INSERT mode: i
- Move to COMMAND mode: ESC
- Go to the first line of the file: gg
- Go to the third line of the file: 3gg
- Go to the last line of the file: G
- To delete a line go to that line then press: D
- To delete all the lines: :%d
- Save file: w
- Quit if no changes were made: q
- Quit file without saving: q!
- Save file and quit: wq