If you experience any difficulty in accessing content on our website, please contact us at 1-866-333-8917 or email us at support@chicagovps.net and we will make every effort to assist you.

By
September 29, 2024

8 Core Linux Commands You Might Be Using Wrong: Tips to Improve Your Command Line Skills

 

As an experienced Linux user, I have certain practices ingrained in my daily terminal activities. Through the years, I’ve picked up a range of shortcuts and useful commands that I consistently rely on. However, even the best tools can be optimized for better functionality.

Hidden files in Linux are both powerful and practical, primarily due to their simplicity. A file that starts with a period (.) is classified as a hidden file, meaning it won’t appear in terminal listings or file managers by default.

When you want to view hidden files, simply add the -a option, and everything works as expected, right?

However, over time, the presence of the “.” and “..” can become a nuisance. These special files are merely shortcuts to the current directory and its parent directory. While they can be quite helpful, it’s not necessary to be reminded of them every time you display a list of files.

Luckily, there’s a straightforward fix: the -a option has a matching -A which reveals hidden files while concealing these shortcuts:

You’ll likely want to create an alias for this command, or any of the others discussed here. This way, you can keep using ls as you did before, secure in the knowledge that you’ll never have to encounter “.” or “..” again.

Frequently, I utilize the exec option of find to search for specific content. This serves as a simple approach to mimicking a “search in files” operation:

However, this command can be quite extensive, and I found it challenging to grasp find’s complex syntax. I felt a bit silly when I realized that grep offers built-in capabilities for recursive searches, but I’m grateful I did! This command is far more user-friendly and easy to master:

Occasionally, you may feel the need to set up a comprehensive directory structure like this:

However, this command will encounter an error if the blog, blog/2024, or blog/2024/09 directories are not already created:

The error message provided doesn’t clearly specify the issue! Fortunately, a straightforward option will generate any missing directories, although you might be puzzled as to why this isn’t the standard behavior. To establish a directory along with any intermediate directories that aren’t already present, simply use -p:

You can think of “p” as standing for either “path” or “parents” to help you remember.

If you frequently utilize the command line, you’re likely accustomed to moving through directories with the cd command. You might also find yourself switching between two directories, going back and forth to execute a command in one while checking files in another and so forth. Perhaps you’ve even used the history command to simplify your workflow.

At this point, you might not be surprised to discover that there’s an even more efficient method. The cd command holds several hidden gems, but one of the most practical shortcuts is the “cd -” command:

Every time you use “cd -“, it will toggle between the last two directories you visited, allowing for swift and effortless navigation.

Additionally, “cd -” will display the path of the directory it switches to, even if the current directory is already visible in your prompt.

You might be familiar with using the cp -R command for copying directories:

This command allows you to replicate entire directory structures, creating a complete duplicate of a directory along with all its contents, which includes files and subdirectories. It serves as an excellent method for backing up files or making a copy of a larger project for further development.

Nevertheless, it’s possible that the copies do not perfectly mirror the originals. There may be differences in their modification times, ownership, and permissions. For instance, in a particular scenario, the modification time of the copy is different from that of the original:

Although these times may only differ by a minute, the discrepancies can be more significant. The solution to this issue is straightforward: instead of using -R, opt for -a (which stands for “archive”):

Tarfiles remain one of the most common methods for distributing collections of files on Linux, particularly for source code. They are typically gzipped to minimize download or storage size. If you’ve relied on these two tools throughout your Linux experience, you may find yourself performing this task almost effortlessly:

This process initially uncompresses the gzip file and then extracts the tar file contained within it. However, thanks to modern advancements, tar can accomplish both tasks in a single command by using the z flag instead of x:

This method is more straightforward and reduces the likelihood of mistakes. Additionally, recent versions of tar have the ability to automatically detect gzip files and decompress them on their own! This allows you to proceed directly to:

The cat command earns its name because it concatenates—joins—files together. While you might be accustomed to using it primarily to display file contents, there are several handy options that can enhance your experience when viewing files with cat.

Initially, cat -s consolidates multiple blank lines, which makes certain file formats much clearer when viewed in a terminal.

Next, cat -b assigns line numbers to the output, but only for lines that contain text. This feature can be helpful when you need to refer to specific line numbers.

If you combine both options by using cat -sb, you can transform cat into a basic file viewer without having to use a pager like less.

Determining the amount of space a directory occupies is crucial during organization tasks. However, the default behavior of du generates extensive output, as it details the size of each directory throughout the entire structure. Consequently, you may find yourself executing the command multiple times, filtering its output using grep, or redirecting it to temporary files, among other tasks.

To alleviate your challenges, consider minimizing the volume of output. The --max-depth option allows you to define how many levels of directories the du command will display. This way, you can still obtain accurate total sizes, but with reduced detail regarding every directory within the structure. For instance, you can check the total sizes of only the immediate sub-directories using this command:

You will observe results for the current directory along with its immediate sub-directories, without the sizes of all the directories nested beneath them:

If you’re interested in enhancing your command line skills, take a moment to explore our guide on terminal typos you should avoid at all costs.


ChicagoVPS is your gateway to unparalleled hosting solutions. Our state-of-the-art datacenters and powerful network ensures lightning-fast speeds and uninterrupted connectivity for your websites and applications. Whether you’re a startup looking for scalable resources or an enterprise in need of enterprise-grade hosting, our range of plans and customizable solutions guarantee a perfect fit. Trust in ChicagoVPS to deliver excellence, combining unmatched reliability and top-tier support.

For Inquiries or to receive a personalized quote, please reach out to us through our contact form here or email us at sales@chicagovps.net.

Subscribe Email

Top