Skip to content

Instantly share code, notes, and snippets.

@jorgedfbranco
Last active February 21, 2017 16:53
Show Gist options
  • Save jorgedfbranco/4558a3969f1659cfb293dd66afd8bbc9 to your computer and use it in GitHub Desktop.
Save jorgedfbranco/4558a3969f1659cfb293dd66afd8bbc9 to your computer and use it in GitHub Desktop.
Jorge's Linux Cheat Sheet
Terminal's Shortcuts (full list @ http://www.skorks.com/2009/09/bash-shortcuts-for-maximum-productivity/):
CTRL+A - Move the caret to the beginning of line.
CTRL+E - Move the caret to the end of line.
CTRL+U - Remove all the chars between the current caret position and the beginning of the line.
ALT+B - Move to the beginning of the previous word.
ALT+F - Move to the beginning of the next word.
CTRL+W - Delete word before cursor.
CTRL+D - Close shell if the current line is empty or delete current char.
CTRL+L - Clear the screen.
CTRL+R - Reverse search history
!! - Rerun last executed command.
printf "\ec" - Clears the terminal for real (not merely adding new lines).
reset - Same as above.
$BASH_REMATCH{k} - Gets the kth group from the last =~ regex match.
fc 2>/dev/null - Opens the vi editor so we can write down a script to be executed on save. fc will by default
show both the commands we provided and the output of those commands. The first are put into
stderr while the later go to stdout.
------------------------------------------------------------------------------------------------------------------
| apropos - Search available commands. |
------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------
| bc - Calculator utility. Supports floating point arithmetic and arbitrary precision numbers. |
------------------------------------------------------------------------------------------------------------------
| Example: echo "1 + 2 * 3 ^ 4" | bc |
| Recommended Flags: -q, when in interactive mode. |
------------------------------------------------------------------------------------------------------------------
Flags:
-q - Avoid showing welcoming message when in interactive mode
Commands:
obase=X - Sets the output base used by bc to X. Keep in mind that when defining both obase and ibase, obase
must always be defined first, otherwise strange behavior will ensue !!
ibase=Y - Sets the input base when interpreting numbers in bc to Y.
Other:
BC_LINE_LENGTH - bc will by default only show up to 70 sequential digits on a single line. If a given number is
will require more than that on any given line, the number will be truncated with \ and the rest
of the digits will be shown on the next line. To avoid that, you can set BC_LINE_LENGTH = 0.
---------------------------------------------------------------------------------------------------------------------------
| chmod - Set files permissions.
---------------------------------------------------------------------------------------------------------------------------
| Example: sudo chmod +x f.sh
--------------------------------------------------------------------------------------------
| Summary:
| - The general outline of a file or directory's permissions as seen when doing a `ls` is as follows:
| drwxr-xr-x <irrelevant> <file-owner> <group-owner>
| - The first bit signals whether the file/directory is a directory;
| - Then we have 3 blocks of "rwx" (read / write / execute (or search for directories)) permissions -- first for owner,
| followed by owner group and then for the rest of users.
| - When setting
--------------------------------------------------------------------------------------------
Flags:
-R - applies the permissions recursively to all the files and subfodlers.
---------------------------------------------------------------------------------------------------------------------------
| curl - Download from an URL.
---------------------------------------------------------------------------------------------------------------------------
| Recommended execution: curl -sL www.google.com
--------------------------------------------------------------------------------------------
Flags:
-L - Follows redirects.
-s - Does not show a progress bar while downloading off the URL.
---------------------------------------------------------------------------------------------------------------------------
| declare
---------------------------------------------------------------------------------------------------------------------------
Flags:
-f - Shows all the defined functions.
-F - Shows all the defined functions' names.
---------------------------------------------------------------------------------------------------------------------------
| df - Devices' sizes.
---------------------------------------------------------------------------------------------------------------------------
| Recommended execution: df -h
---------------------------------------------------------------------------------------------------------------------------
Flags:
-h - Sizes in human readable form.
---------------------------------------------------------------------------------------------------------------------------
| file - Determines a file's type.
---------------------------------------------------------------------------------------------------------------------------
| Recommended execution: file -b --mime-type <file>
---------------------------------------------------------------------------------------------------------------------------
Flags:
-b - Brief: outputs only the the mime-type.
---------------------------------------------------------------------------------------------------------------------------
| grep - Filter text with regex patterns.
---------------------------------------------------------------------------------------------------------------------------
Flags:
-E - Extended grep.
-P - Perl grep.
-n - Prepend to each line its line number.
-i - Ignore case.
-v - Reverse search.
-x (--line-regex) - Apply regexp to the whole line (the same as standard grep regex starting with ^ and ending with $).
-o - Print only the result of application of the selected grep expression. Anything else outside the grep expression
will not be printed to the screen.
---------------------------------------------------------------------------------------------------------------------------
| tail - Shows the ending of a file.
---------------------------------------------------------------------------------------------------------------------------
-n +K - Skips the first (K-1) lines of the file.
---------------------------------------------------------------------------------------------------------------------------
| less - Text file viewer.
---------------------------------------------------------------------------------------------------------------------------
Arguments:
+123 -> Opens 'less' in line 123.
Commands:
123g - Go to line 123.
g/G - Go to the beginning / end of the file.
-I - Make searches case insensitive.
&<pattern> - Find all results for <pattern>.
/<pattern> - Find next match of <pattern>. Then `n` for subsequent forward matches or `N` for backward matches.
Xj - Go X lines forward.
Xj - go X lines backward.
---------------------------------------------------------------------------------------------------------------------------
| locate - Locates files and directories previously indexed (pretty much ~O(1) searches for a given expression).
---------------------------------------------------------------------------------------------------------------------------
| Example: locate -bi myfile.pdf
| Recommended flags:
| -ri (case-insensitive + regex expressions)
| -bi (doesn't require leading and ending .*'s and is case insensitive)
--------------------------------------------------------------------------------------------
Flags:
-r - Use regex instead of standard globbing.
-i - Case insensitive.
-b (--basename) - Avoid requiring that the pattern matches the whole line.
---------------------------------------------------------------------------------------------------------------------------
| ls - List directories' contents.
---------------------------------------------------------------------------------------------------------------------------
| Example: ls -lha
| Recommended flags: -lha ("Detailed view", human-readable file sizes & showing hidden files.)
--------------------------------------------------------------------------------------------
Flags:
-l - Long listing (detailed view).
-h - Human readable sizes (4k instead of 4096 bytes).
-a - Show hidden files and directories.
-t - Order results by decreasing last modification.
-S - Order results by decreasing file size.
---------------------------------------------------------------------------------------------------------------------------
| pcregrep - grep tool allowing capturing groups.
---------------------------------------------------------------------------------------------------------------------------
| Recommended flags: -o1 -o2 --om-seperator ' ' (captures group 1 and group 2 and separates the results by a single space)
--------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
| pidof - gets the pid(s) associated with a given process name.
---------------------------------------------------------------------------------------------------------------------------
Flags:
-s - Because by default pidof returns multiple results, -s will guarantee that you'll get a single result at most.
---------------------------------------------------------------------------------------------------------------------------
| rm - Remove files or directories.
---------------------------------------------------------------------------------------------------------------------------
| Example: rm -rf <dir>
| Recommended flags: -rf (recursively deletes files and doesn't ask stupid questions)
--------------------------------------------------------------------------------------------
Flags:
-r - Recursively deletes files and directories.
-f - Forces the deletion of files / directories without prompting anything.
---------------------------------------------------------------------------------------------------------------------------
| sort - Sort text.
---------------------------------------------------------------------------------------------------------------------------
Flags:
-r - Reverse sort.
-kX - Sort by the contents found on column `X`.
-n - Sort numerically (instead of using string-comparison).
-tX - Use `X` as column delimiter instead of space.
--version-sort
---------------------------------------------------------------------------------------------------------------------------
| tree - Prints out the whole directory hierarchy of a given directory and its subdirectories.
---------------------------------------------------------------------------------------------------------------------------
| Recommended flags: tree -P <glob pattern> --ignorecase
---------------------------------------------------------------------------------------------------------------------------
Flags:
-a - Show all files.
-L - Max level of directory trees.
-P - A glob pattern to search for. It is case-sensitive by default.
-f - Show full paths (although full they are not *absolute* pathnames!).
-d - Directories only.
--ignore-case
--prune - Omits empty directories.
---------------------------------------------------------------------------------------------------------------------------
| watch - Allows to repeatedly run a given command.
---------------------------------------------------------------------------------------------------------------------------
| Example: watch -n1 -td date
| Recommended flags: -n1 -td
---------------------------------------------------------------------------------------------------------------------------
Flags:
-t - Avoid showing the default top line with the refresh rate and current time;
-d - Highlight differences between successive frames;
-nX - Set the refresh rate to X seconds.
---------------------------------------------------------------------------------------------------------------------------
| wmctrl - Access the windows manager info.
---------------------------------------------------------------------------------------------------------------------------
| Recommended flags: wmctrl -lp
---------------------------------------------------------------------------------------------------------------------------
Flags:
-l - List all windows;
-p - Show the windows' pid.
---------------------------------------------------------------------------------------------------------------------------
| unzip - Uncompresses zip files. |
---------------------------------------------------------------------------------------------------------------------------
| Flags:
| -j - Flattens the extracted files' directory structure;
| -d - Destination dir.
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
| xclip - Clipboard manager. |
---------------------------------------------------------------------------------------------------------------------------
| Example:
| echo abc | xclip # Sets the clipboard to "abc".
| xclip -o # Prints the current clipboard to stdout.
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
| xdg-open - Runs the given file on the default application.
---------------------------------------------------------------------------------------------------------------------------
| Example: xdg-open <file>
---------------------------------------------------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment