Skip to Content

Do You Know These Linux Tips and Tricks?

When I first started using Linux, I was overwhelmed by its complexity and the sheer volume of commands and configurations.

I spent countless hours fumbling through documentation and forums, trying to find the shortcuts and tweaks that would make my workflow smoother.

It wasn’t until I stumbled upon a few key tips and tricks that everything changed.

Suddenly, tasks that used to take me ages were completed in minutes, and my overall efficiency skyrocketed.

Let’s dive into some of the most powerful strategies that can transform the way you work with Linux and boost your efficiency to new heights.

Do You Know What the Tilde (~) Symbol Represents?

In Linux, the tilde symbol (~) serves as a convenient shortcut that represents the current user’s home directory.

This is incredibly useful for simplifying the commands you need to type, especially when you’re working with files and configurations in your home directory.

Instead of writing out the full path to your home directory, such as /home/username, you can simply use the tilde symbol.

This is especially handy when you’re working in different directories and want to reference files in your home directory.

Using the tilde symbol ensures that you’re always referencing the correct home directory, even if you’re not sure of the exact path. This can help prevent errors that might occur from mistyping a directory path.

If you want to edit your Bash shell configuration file, you can use the command nano ~/.bashrc instead of nano /home/username/.bashrc.

The tilde makes it clear that you’re referring to the .bashrc file in your own home directory.

Do You Know How to Find Files Without the find Command?

If you ever forget the find command or need a faster alternative, the locate command comes to the rescue.

Unlike find, which searches in real-time, locate uses a pre-built database (updated by updatedb) to quickly find files by name.

Keep in mind that locate may not find files created or modified after the last database update, so it’s best suited for searching older or unchanged files.

Do You Know That Linux Keeps Track of Open Files Even After You Close the Terminal?

Linux tracks open files through inodes, which are data structures that store information about files and directories.

This means that even if you close a terminal or lose a connection, a file remains open as long as it is still being accessed by a process.

For example, if you are editing a file with vim and lose your terminal session, the file remains open and locked by vim until you explicitly close it or terminate the process.

To view a comprehensive list of open files and determine which processes are using them, you can use the lsof (List Open Files) command.

For instance, running lsof without any options will display all open files on your system:

lsof

To filter the results and see which processes are using files in a specific directory, use:

lsof +D /path/to/directory

If you want to check which processes are using a particular file, such as /var/log/syslog, you can use:

lsof /var/log/syslog

This command will list all processes that have the syslog file open, including their process IDs and user information.

If you need to identify which process is accessing a specific file, the fuser command can be very helpful. For example, to find out which process is using the file /var/log/syslog, you would use:

fuser /var/log/syslog

This will return a list of process IDs that are using the syslog file. To get more detailed information about these processes, use the -v option:

fuser -v /var/log/syslog

This provides additional details, including the user owning the process and the type of access (read or write).

Both lsof and fuser are essential commands for debugging and monitoring system resources.

Understanding and using these tools effectively can help you maintain system performance, resolve file access issues, and manage resources more efficiently.

Do You Know Linux Keeps a Record of Commands You’ve Used?

Linux keeps a history of commands you’ve executed, which you can view with the history command.

When you run history, it displays a list of previously executed commands along with their line numbers. For example:

history

This will output a list like:

1 ls -la
2 cd /var/log
3 grep “error” syslog
4 sudo apt update

This feature is particularly useful for recalling frequently used commands or for finding commands you have executed previously, which can save time and reduce the need to retype long or complex commands.

Additionally, you can use CTRL + R to search through your command history interactively.

Pressing CTRL + R opens a reverse search prompt where you can start typing a portion of a previous command.

As you type, the terminal displays matching commands from your history.

For example, if you type grep, the prompt will show the most recent command that included grep:

(reverse-i-search)`grep': grep "error" syslog

You can press CTRL + R repeatedly to cycle through previous matches. Once you find the desired command, press Enter to execute it or use the arrow keys to edit it before running.

Using these features helps streamline your workflow by quickly reusing past commands without retyping them.

It’s also useful for troubleshooting, as you can quickly revisit and re-execute previous commands that may have been part of a debugging process.

Have you made use of history and CTRL + R to improve your efficiency or recall complex commands more easily?

Do You Know How to View and Manage Running Processes?

To monitor and manage running processes, the top command provides a dynamic, real-time view of system activity, showing processes and their resource usage.

For a more user-friendly interface, htop offers an enhanced version with additional features, such as color-coded displays and easier navigation.

Both tools are essential for keeping tabs on system performance and resource consumption.

Do You Know Linux Can Monitor Network Usage?

To monitor network traffic and bandwidth usage, tools like iftop and nethogs are incredibly useful.

iftop provides a real-time, graphical representation of network traffic, while nethogs breaks down bandwidth usage by process.

These tools help you identify which applications or processes are consuming your network resources, allowing for better network management and troubleshooting.

Conclusion

Linux offers a range of features and commands that can significantly enhance your system management and efficiency.

From understanding the tilde symbol and finding files with locate to tracking open files and monitoring network usage, these tips and tools are essential for mastering the Linux environment.

Whether you’re a seasoned user or just getting started, knowing these Linux tricks can help you navigate and manage your system more effectively.

Jeff

Thursday 29th of August 2024

The "command &" trick is useful to run a command in the background, but what if you already started a long-running command in the foreground, and then wish you would have used the "&" ? You could press Ctrl+C to kill the command, and then start it again with the &, but then you would lose any progress the command has made, and possibly even corrupt the data.

A better way is to press Ctrl+Z to temporarily pause the command (you will get a message saying "stopped") then you can type "bg" and press [Enter] to send the command to the background.

Also worth noting that the "command1 && command2" form will only run the second command if the first one SUCCEEDS (exits with a zero error code). In other situations you might want to run the second command only if the first one FAILS, for that you can use: command1 || command2

Or, if you always want the second command to run, regardless of the first one's exit status you can use: command1 ; command2

Chris

Wednesday 28th of August 2024

Your helpsheet #4 no.

The "&&" lets subsequent commands run only if the preceding one ran to completion successfully. Otherwise use semicolon (";") to separate commands and run them sequentially. The rest are good.

For example

- true && echo yes # "yes" - false && echo no # nothing (the "false" prevents the echo running)

But

- true; echo yes # "yes" (the two commands run sequentially) - false; echo no # "no" (the two commands run sequentially)

David Cao

Thursday 29th of August 2024

Thanks!