Linux Command Line Tips

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:

http://www.redhat.com/en/blog/introduction-tmux-linux

Learn

Related articles

Getting Started with React

In this guide, we will cover the basics of setting up a local development environment, starting a simple React project, and the basics of how it works.

How to Create Bash Aliases

This tutorial demonstrates how to use the alias command to create personalized shortcuts, which can help you save time and feel less frustrated.

Using Git for PHP Development

This guide walks through the fundamentals of Git. In this tutorial, we will show you Git commands. For Linux you can use the built-in terminal.

How to Connect to MySQL with Laravel

In this guide, you will learn how to connect your Laravel application to your MySQL database.

How do you change the default SSH Port on CentOS Stream 9?

Changing the default SSH port adds an extra layer of security by reducing the risk of your password being cracked from a brute force attack.

What is Inheritance in PHP?

In this tutorial we will explain inheritance in PHP, a mechanism that allows a child class to inherit properties and behaviors from a parent class.