It’s just so satisfying to type out a series of commands and watch them execute flawlessly. But let’s face it sometimes we need a little help navigating this magical world. That’s why I’m here to share some Linux command line tips and tricks that will make your life easier (or at least more entertaining).
Before anything else, aliases. Aliases are like shortcuts for long commands they allow you to create a new name for an existing command or series of commands. For example:
# This script creates an alias for the "ls" command, allowing the user to use a shorter and more convenient command "ll" instead.
# First, we need to declare that this is a bash script by using the "bash" keyword.
#!/bin/bash
# Next, we use the "alias" command to create the alias "ll" for the "ls" command.
# The "=" sign is used to assign the alias name to the command we want to use.
# The single quotes are used to enclose the command and its options.
# The "-lh" option displays the long format of the file list with human-readable file sizes.
# The "--color=auto" option enables colorized output for better readability.
alias ll='ls -lh --color=auto'
This creates an alias called “ll” that is equivalent to the “ls -lh –color=auto” command. Now, whenever you type “ll”, it will execute this command instead of having to remember all those options and arguments every time. Pretty handy, right?
Another useful tip is using tab completion. This feature allows you to autocomplete commands by pressing the Tab key after typing part of a command or file name. For example:
bash
#!/bin/bash # This line specifies the interpreter to be used for executing the script
# This script changes the current directory to the project folder in the user's Documents directory
cd ~/Documents/project/ # The ~ symbol represents the user's home directory, making the script more flexible and portable
# Tip: Instead of hardcoding the path, it's better to use variables or the ~ symbol to make the script more adaptable to different systems
# Another tip: It's good practice to add a shebang (#!/bin/bash) at the beginning of the script to specify the interpreter to be used
# Tip: It's also a good idea to add comments to explain the purpose and functionality of each line of code
# For example, this line changes the current directory to the project folder in the user's Documents directory
# Tip: It's important to use descriptive variable names to make the code more readable and understandable
# For example, instead of using "user" as a variable, we can use "current_user" or "user_name"
# Another useful tip is using tab completion. This feature allows you to autocomplete commands by pressing the Tab key after typing part of a command or file name.
# For example, if you type "cd /home/user/Doc" and then press Tab, it will autocomplete to "cd /home/user/Documents/"
# This saves time and reduces the chances of making typos or errors in long commands
# Tip: It's always a good idea to include helpful tips and tricks in your scripts to make them more user-friendly and efficient
# Overall, this script makes it easier for the user to navigate to the project folder without having to remember the full path every time they want to access it. Pretty handy, right?
If you type “cd /ho” and press Tab, it will automatically complete the path for you:
# This script changes the current directory to the project folder within the user's Documents folder.
# It is used to quickly navigate to the project folder without having to type the full path.
# Change directory to the Documents folder within the user's home directory.
cd ~/Documents/
# Check if the project folder exists within the Documents folder.
if [ -d "project" ]; then
# If it exists, change directory to the project folder.
cd project/
else
# If it does not exist, create the project folder and change directory to it.
mkdir project/
cd project/
fi
This can save a lot of time when working with long file paths or complex commands.
Now piping. Piping allows you to chain multiple commands together and execute them in sequence. For example:
ls -l | grep -a "*.txt"
This command lists all files that end with “.txt”, which is pretty handy for finding specific types of files quickly. You can also use piping to filter output based on certain criteria, like this:
# This command lists all running processes and filters the output to only show processes that contain "python" in their name.
# The "ps aux" command lists all processes, while the "grep -i python" command filters the output.
# The "-i" flag makes the search case-insensitive, so it will also match "Python" or "PYTHON".
# The "|" symbol is used for piping, which takes the output of the first command and uses it as input for the second command.
# This allows for more specific and efficient filtering of output.
ps aux | grep -i python
This command shows a list of all processes running in the background that are using Python (case-insensitive). Pretty cool, right?
Finally, redirection. Redirection allows you to redirect output from one place to another or input from one place to another. For example:
ps aux | grep -i python > list.txt
This command creates a new file called “list.txt” and writes the contents of the current directory to it (overwriting any existing content). You can also redirect output to standard error instead of standard output, like this:
# This command creates a new file called "list.txt" and writes the contents of the current directory to it (overwriting any existing content).
# The "&>" operator redirects both standard output and standard error to the specified file.
# In this case, the command output and any error messages will be written to "logfile.log".
# The ">" operator would only redirect standard output, leaving any error messages to be displayed in the terminal.
bash
ls &> list.txt
# The "ls" command lists the contents of the current directory.
# The "&>" operator redirects both standard output and standard error to the specified file.
# In this case, the output of "ls" will be written to "list.txt", overwriting any existing content.
# Any error messages, such as "No such file or directory", will also be written to "list.txt".
This command sends both standard output and standard error to a file called “logfile.log”. Pretty handy for debugging or logging purposes!
Remember, the CLI is all about being efficient and productive, so don’t be afraid to experiment with different commands and options.