These are just a few of the many command line tricks and shortcuts that will save you a lot of time while working in the Linux terminal.
Jump to the beginning of the line: ctrl+a
Jump to the end of the line: ctrl+e
Clear the entire line: ctrl+u
Stop any command: ctrl+c
Jump left one word: esc+b
Jump right one word: esc+f
Exits the terminal session: ctrl+d
Paste whatever you deleted with ctrl+u: ctrl+y
Browse through the history that matches what you typed: ctrl + r
Use the Up arrow on your keyboard to scroll through your previous commands
Use tab for autocompletion. After you start typing something, hit tab and it will print suggestions that start with the string you typed.
Redo the previous command with sudo
sudo !!
To list all files using a long listing format with the extension ".php," type:
ls -l *.php
Vim editor
Exit without saving: ESC then :q!
Save and exit: ESC then :wq
Delete all lines in a file: in command mode (note that Vim starts in "command" mode by default) type "gg" to move the cursor to the first line of the file then type dG to delete all lines in a file
For any command, you can view its manual page using man [command]. This is a great way to learn what a command does.
This command shows your recent command history:
history
Use the following example to get a command from history:
history | grep command
To display a list of your 10 most recent commands, type:
history 10
Using tail shows the ends of files, for example, analyzing logs that change over time like authentication logs for both successful or failed logins.
Enter the tail command, followed by the file you would like to view:
tail /var/log/secure
This will print the last ten lines of the /var/log/secure file to your terminal output. By default, if no options are specified, the tail command prints the last 10 lines of a file.
To change the number of lines displayed, use the -n option:
tail -n 20 /var/log/secure
This will print the last twenty lines of the /var/log/secure file to your terminal output. You can modify this number by as many or as few lines as you need.
Use the following commands to search the root directory or current directory for a filename or folder. The root directory is the top-level directory that contains all other files and folders. The forward slash (/) represents the root of the filesystem. The dot (.) represents the current directory in the filesystem. To find all the files with the extension ".php" in the current directory and its subdirectories, use the asterisk wildcard operator (*). To find files by searching only part of their names, put the search pattern in quotes.
find '/' -name "filename or folder"
find '.' -name "filename or folder"
find '.' -name "*.php"
find '.' -name "*foo*"
grep -rnw "." -e "string"
-r read all files under each directory, recursively
-n line number
-w select only those lines containing matches that form whole words
-e is the pattern used during the search
Display the amount of disk space in human readable format available on the filesystem and exclude directories that we don’t care to see in our output. Open the file in your text editor and declare the alias then make the alias available in your current session by running the source command.
vim ~/.bashrc
alias df="df -h -x tmpfs -x devtmpfs"
source ~/.bashrc
Go to your home directory
cd ~
You can also just cd to return to your home directory without the tilde
cd
Do you want to go back to the directory you were just in without typing the whole path out? The following command will take you back to the last directory:
cd -
The following command clears the file without deleting it:
> filename
> /path-to-file/huge_file
The following command will create both a new folder and its nested folders. The -p option allows a parent folder to be created along with its nested folders.
mkdir -p new_folder/{folder_1,folder_2,folder_3}
Running multiple commands in one single command only if the previous command was successful with the double-ampersand (&&)
command1 && command2 && command3
To improve your workflow, you can create several "pseudo terminals" from a single terminal with tmux.
For more information on tmux please visit: