I’ve Been Using These 8 Core Linux Commands Wrong for Years
Linux & macOS Terminal
As a veteran Linux user, I have certain habits baked into my everyday terminal use. Over the years, I’ve learned various shortcuts and handy commands that I use over and over again. But even first-rate tools can be put to better use.
1 Remove Hidden Shortcuts From File Listings
Hidden files on Linux are powerful and convenient, mainly because they are so simple. A file beginning with a period (.) is a hidden file. It won’t show up, by default, in terminal listings and file managers.
When you need to see hidden files, just use the -a option and all is good, right?
ls -a
Eventually, though, the “.” and “..” may start to bug you. These special files are simply shortcuts to the current directory and its parent directory. While they can be very useful, you don’t need to know about them every time you’re listing files.
Fortunately, there’s an easy solution: the -a option has a corresponding -A which shows hidden files but hides these shortcuts:
ls -A
You’ll probably want to
set up an alias for this command
, or any of the others covered here. You can then continue using ls as before, safe in the knowledge that you’ll never need to see “.” or “..” again.
2 Grep Recursively Without a Pipe
Very often, I will use find’s exec option to grep for something. This is a basic way of emulating a “find in files” task:
find . -exec grep 'hello' {} ;
But this command is lengthy, and it sure wasn’t fun learning find’s obscure syntax. I felt foolish when I discovered that grep has built-in support for recursive searches, but I’m still glad I did! This command is much more convenient and it’s easy to learn:
grep -r 'hello'
3 Make Deep Directories With One Command
Now and then, you’ll find yourself wanting to create a deep directory hierarchy like this:
mkdir blog/2024/09/16
But, frustratingly, this command will fail if the blog, blog/2024, or blog/2024/09 directories don’t already exist:
The error message doesn’t even clearly explain what the problem is! Helpfully, a simple option will create any missing directories, although you may be left wondering why it isn’t the default. To create a directory, including any intermediate directories that do not already exist, just use -p:
mkdir -p blog/2024/09/16
You can remember this by thinking of “p” as an abbreviation for either “path” or “parents.”
4 Switch Between Two Directories Instantly
If you use the command line regularly, you’ll be used to navigating directories using cd. You’re probably also used to toggling between two directories, going back and forth to run a command in one, inspect files in another, and so on. Maybe you even use the history command to repeat yourself.
By now, you won’t be surprised to learn there’s a better way. The cd has several secrets, but one of the most useful shortcuts is the “cd -” form:
Each time you run “cd -” it will flip between the previous two directories you navigated to, making it very fast and easy to move back and forth.
“cd -” will print the path of the directory it changes to, even if you have the current directory in your prompt.
5 Copy Directories With Maximum Fidelity
You may already know about copying directories with cp -R:
cp -R docs backup-docs
This command lets you copy entire directory structures, producing a full copy of a directory and all its contents, including files and other directories. It’s a great way of backing up files or creating a copy of a larger project to work on.
However, you might notice that the copies aren’t exactly the same as the originals. Their modification times, owners, and permissions may all be different. For example, in the following case, the copy has a different modification time than the original:
These times only differ by a minute, but the problem can be much greater. The fix is simple: use -a (for “archive”) instead of -R:
6 Extract tar.gz Files With a Single Command
Tarfiles are still one of the most popular ways to distribute collections of files on Linux, especially source code. They are usually gzipped to reduce size during download or storage. If you’ve used these two programs your entire Linux life, you may be used to doing this kind of thing with your eyes closed:
gunzip archive.tar.gz
tar xvf archive.tar
Which will first uncompress the gzip file, and then unpack the tar file inside it. But, thanks to modern sensibilities, tar can do both in a single step, using the z flag instead of x:
tar zvf archive.tar.gz
This approach is more convenient and less prone to error. What’s more, recent versions of tar will auto-detect gzip files and uncompress them automatically! So you can skip straight to:
tar xvf archive.tar.gz
7 Use cat to Inspect Files Quickly
The cat command is so-called because it concatenates—joins—files together. But you may be used to using it simply to view the contents of files. If so, you’re probably unaware of a couple of useful options that make file viewing with cat just a little bit more pleasant.
First, cat -s will squeeze several blank lines together, making certain file formats easier to view in a terminal.
Second, cat -b adds line numbers to the output, but only for non-blank lines. This can be useful when you’re writing references to line numbers.
Use them both together, as cat -sb, and you can use cat as a simple file viewer without reaching for a pager like less.
8 Get Concise Disk Usage
Finding out how much space a directory occupies is useful when you’re spring-cleaning. But du’s default behavior produces a lot of output as it reports the size of each and every directory down the complete hierarchy. As a result, you may find yourself running the command several times, piping its output to grep, redirecting it to temporary files, and so on.
To ease your struggles, try reducing the amount of output. The --max-depth
option lets you specify how many levels of directories du will report on. You’ll still get accurate total sizes, just with less detailed information about every directory in the tree. For example, check the total sizes of just immediate sub-directories with this command:
du --max-depth 1
You’ll see results for the current directory, and its immediate sub-directories, without seeing the size of each and every directory below them in the hierarchy:
If you want to learn more about mastering the command line, check out our guide to terminal typos you should avoid at all costs.