Linux is like a book store. It’s filled with so many books (commands/tools) that one can’t possibly read them all. It’s up to you to figure out which are important enough to read (spend time learning). I now use Linux almost exclusively and have acquired a list of commands I use quite often. Some are obscure, and others obvious. In no particular order I will be detailing these commands I use and in some instances briefly discussing why and how they are used.
Table of Contents
Linux Distributions
Before diving into commands, it’s worth discussing the all too common question, “what’s the best Linux distribution?” Back to my book store analogy, you may very well get an answer when asking someone that in a book store, it’s likely not going to align with your own preferences. Generally I’d say tailor your choice toward your application. If you’re planning on running Linux on a laptop, maybe choose elementaryos since it’s a lightweight, pretty, and clean OS with much of the overhead taken care of for you. If you’re planning on switching your desktop to Linux for the first time, maybe an LTS (long term support) version of Ubuntu works. It’s pretty stable and most software developers, if planning on supporting Linux, make sure to support Debian based OS’s such as Ubuntu. Do some research, decide what works for you, and remember it’s also fine to try out an OS for a week and switch later on.
Basic Commands
man
“Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime.” You know the quote, and it applies here as well. man, or manual, is a method to learn about every single command yourself. So if you want to learn what “ls” does, type “man ls” and you’ll see:
user@computer$ man ls NAME ls - list directory contents SYNOPSIS ls [OPTION]... [FILE]... DESCRIPTION List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor --sort is specified. Mandatory arguments to long options are mandatory for short options too. -a, --all do not ignore entries starting with . -A, --almost-all do not list implied . and ..
This is just a tool for you to use if you need more information on the commands listed below, or any others you’re interested in.
ls
ls lists the contents of a directory. If executed alone, it lists the contents of the current directory. Most often, I use:
user@computer$ ls -thal
This lists all contents (-a) of the current directory, newest first (-t) in long list form (-l) and in a human readable format (-h). There are many variations on this command people use, but keep some variation of this one in your back pocket.
cp
cp, or copy, will do just that, make a copy in a location you specify leaving the original.
user@computer$ cp myfile.txt /home/mydir/
This copies myfile.txt into /home/mydir. Honestly if I’m lazy, which I typically are, I use:
user@computer$ cp -rf myfile.txt /home/mydir/
-rf doesn’t add any benefit here, but if it were a directory, the -r is recursive; without it, cp wouldn’t work. This is why I often just lazily just add -rf.
mv
mv, or move, very similar to copy, just moves instead of making a duplicate.
user@computer$ mv -f /home/afolder/ /home/mydir/
Again, -f is required if you’re moving a folder, so I often just add it.
watch
Sometimes events occur such as maybe moving a large file from one machine to the next, or scrubbing a zfs raid, where I want to sit and watch that happen for a while to gauge that it’s working and maybe get a general idea of how long it’s taking. In the case of a large file, I can do:
user@computer$ watch ls -thal
This will re-run ls at regular intervals (I think default is 2 seconds) so you don’t have to keep typing it. You can put pretty much anything after watch and it will continually run that command.
du
du, or disk usage, provides a way to figure out how much space is being used in a particular directory.
user@computer$ du -ch
In this usage, we get a total size of all contents in the current directory (-c) and in a human readable form (-h)
df
df reports the filesystem disk usage, which I use even more often than du.
user@computer$ df -h Filesystem Size Used Avail Use% Mounted on udev 3.8G 0 3.8G 0% /dev tmpfs 766M 2.0M 765M 1% /run /dev/nvme0n1p5 109G 18G 86G 17% / tmpfs 3.8G 156M 3.6G 5% /dev/shm tmpfs 5.0M 4.0K 5.0M 1% /run/lock tmpfs 3.8G 0 3.8G 0% /sys/fs/cgroup /dev/loop0 56M 56M 0 100% /snap/core18/2074 /dev/loop1 56M 56M 0 100% /snap/core18/2128 /dev/loop2 219M 219M 0 100% /snap/gnome-3-34-1804/72 /dev/loop3 219M 219M 0 100% /snap/gnome-3-34-1804/66 /dev/loop4 52M 52M 0 100% /snap/snap-store/518 /dev/loop5 66M 66M 0 100% /snap/gtk-common-themes/1515 /dev/loop6 66M 66M 0 100% /snap/gtk-common-themes/1519 /dev/loop7 128K 128K 0 100% /snap/bare/5 /dev/loop8 51M 51M 0 100% /snap/snap-store/547 /dev/nvme0n1p1 96M 31M 66M 32% /boot/efi /dev/loop10 33M 33M 0 100% /snap/snapd/13170 tmpfs 766M 40K 766M 1% /run/user/1000 /dev/loop11 33M 33M 0 100% /snap/snapd/13270
If you ever wondered, “how much space do I have left on my drive?” this is the command to use.
Complex Commands
There are cases where I have very specific “corner cases” in which I need to solve a problem but it requires a rather obscure command. Will you ever need these? No idea, likely not, but if you do, you’ll be so glad to find them. Here are some I have written down so I never forget them.
Drop Caches
Imagine you ran a very complex process that used all of your memory and your cache is full, something similar to the image below:
Although there’s nothing inherently wrong with having your memory cached fully, sometimes you just want to clear it. To do this:
user@computer$ sudo sh -c "sync; echo 3 > /proc/sys/vm/drop_caches"
The result of running this command can be seen below.
As for what precisely this command does, I’m not going to detail, but suffice it to say I find myself using this in various cases prior to kicking off a memory intensive job or after one finished.
Where’s my ZFS raid??
I love ZFS. It’s a way to raid multiple drives via software and I find it to be very useful. If I’m not raiding with a controller (hardware), it’s a ZFS raid. On occasion I will move a ZFS raid from one server to another, or I’ll have some very bad unplanned shutdown of one of my servers and my ZFS raid doesn’t mount and isn’t visible when booting up. To discover any ZFS raids connected to a linux machine, run the following.
user@computer$ zpool import -d /dev/disk/by-id -aN -f
I can’t tell you the number of times (well, not that many fortunately, but still) this has helped me recover what I thought was a lost ZFS raid.
I’m running out of space!
Sometimes you just want to know where that big file is you can delete to give yourself a bit more room. This is one helpful command.
user@computer$ sudo find -type f -exec du -Sh {} + | sort -rh | head -n 20
This will find the 20 biggest (feel free to change 20 to whatever you’d like) files and list them in order. It can take a while to run, but can pay dividends when you find that one wasteful file you don’t need.
Drive Information
Sometimes you want to know things like, “how much longer will my drive last?” or “How hot is my drive right now?” These can be answered by using smartmontools (you’ll have to install).
user@computer$ sudo smartctl -a /dev/sda
-a gives you all information on that particular drive, which is almost certainly too much. If you want to know only the temperature, grep for it.
user@computer$ sudo smartctl -a /dev/sda | grep Temperature: Temperature: 33 Celsius
the | just tells the command that any output should be redirected to grep, and grep is just a filter which will only give you lines matching “Temperature”. As for figuring out how “old” your drive is, if it’s an SSD, you can check the wear leveling count.
user@computer$ sudo smartctl -a /dev/sdb | grep 177 177 Wear_Leveling_Count 0x0013 092 092 000 Pre-fail Always - 130
This number is really only pertinent to SSDs, and you can check your manufacturer to figure out how many “PE” cycles a drive can do before it is predicted to fail.
Conclusion
I could go on forever detailing commands that have helped me in the past and ones I use regularly. As you progress on your journey through linux, you’ll find your own set of tools that continually pave the way toward productivity. I recommend you take note of these when you find them and keep them somewhere safe, as you’ll undoubtedly use them again.