Top

Essential Linux Commands

Must have commands to get started on the linux command line

Fedora Gnome Desktop

Essential Linux Commands

GNU-Linux, more commonly known as simply: Linux, is a beautiful operating system. For me, I liken it to a performance car, where everything is laid bare for me to tinker with. Windows and MacOS, on the other hand, are like cars with the hoods welded shut, keeping you out and necessitating their existence for anything you might need, of course at a cost. Not Linux though! On Linux it is expected that you will want to tinker with things, get under the hood, and do whatever you want. Most large defense contractors, who sell multi-million dollar software and equipment use Linux or BSD bases. Not only because of their flexible licensing model, but because their both free and opensource and let them change almost anything they want to suit their needs. With that in mind, here are a few essentials you’ll need to know to get around and perform common tasks on the command line. Not all modern Linux distros require command line interaction for basic functionality, but for those occasions where you want to tinker, it’s wide open.

Optional/variable params are displayed in italics and orange

Informational Commands

These commands can be prepended to most GNU Utils and other applications to get more information about that specific utility/program and usually includes valuable usage information. If you don’t know what a command does, run one of these!

man command
Will take you to the MANual page. You can press “q” to exit the pager
info command
Some linux distros install the info command, some don’t - it’s a shorter alt to man
apropos term
Will search for the provided term through the manual pages
whatis command
Will give you some brief information about what the utility does
which command
Will tell you where a command’s binary is located on your system or an error if it is not installed

System and Hardware Commands

shutdown -h now
Shuts the system down without a reboot
halt
Stop all running process - another variation of the above
shutdown -r X
Shutdown the system in X minutes, where X is a number, and reboot
shutdown -r now
Shutdown the system now and do a reboot
reboot
Same as above, just shorter
lscpu
Will display information about your CPU
lsblk
Show block devices i.e. HDDs and other storage
lsusb
Show USB device information
lspci
Show connected PCI device information
printenv
Will display current shell session environment variables

Filesystem Traversal and other Operations

I included some common variations of commands that you will likely use on a daily basis

pwd
Print working directory - find out where the hell you are
ls
Lists all files in your current directory
ls -l
Lists all files in your current directory in long format
ls -la
Lists all files same as above but shows hidden dot files
cd dir
Change directories into a specific dir - omitting the dir will take you home
cd ~
Another way to get home. ~ can be used to other path inputs as well
locate filename
Will look through the database to give you the location of said filename
updatedb
Will update the above database (Typically this is done at startup anyway)
df -h
Gives you available disk space (Disk Free) in a human-readable format (not bytes!)
du -Hs dir
Gives you a human-readable summary of disk usage for a directory dir or the current directory if no dir is provided
mkdir dir
Will create a directory dir
rmdir dir
Will delete a directory dir if it is not empty
rm -r dir
Will delete a directory dir that has stuff in it
rm -rf dir
Will remove a directory and all content in it without prompting (be careful)
rm file
Will remove a file, usually prompting you before doing so on some distros
rm -f file
Will remove the file without the prompt (be careful)
cp filepath newfilepath
Will copy a file filepath to new file path
mv filename newfilename
Move/rename a file filename to newfilename
find startingpath -name filename
Search for a file name in the starting path directory
cat filename
Will concatenate/print the content of a file to the terminal
head filename
Will print the first few lines of a file filename to the terminal (line count is adjustable through options)
tail filename
Will print the last few lines of a file filename to the terminal (line count is adjustable through options)
file filename
Will display meta information about a file: filename
more filename
Opens file up for paging. It’s ok, but less is better.
less filename
Opens file for paging using a more advanced pager where you can use arrow keys to move up and down.

Permissions and the like

sudo command
On most systems this is the way to run a command as root - requires wheel/sudo privileges
doas command
The modern version of sudo - not widely defaulted on most distros, but much more secure and lightweight.
chown user:group filename
Will change the owner of file filename to user and group
chmod g+w filename
Will set the write permission bit to on for file filename for the owning group. There are numerous examples online, but options include u for user, g for group, o for other/everyone else and + and - act as you would expect to add or remove either r for read, w for write, and x for execute permissions. So to add read and write permissions for the user and group you could do chmod ug+rw filename. Adding the -R flag makes the change recursive when running on a directory.
su username
Switch user to username if no username is provided it will attempt to switch to the root account.

Pipes and IO Redirection

The philosophy or *nix like operating systems has always been creating small, portable tools that do one thing well. Big huge tools are not unheard of in the Linux world, but they are the exception vs the rule. More often than not, you will need more than one tool to do what you need to do. What if I want to cat out a large file to grep command to select only the text that matches what I want and then save that matched subset into a new file? Well, with BASH and the GNU core utils, that’s no problem! Here’s an example:

cat somefile.txt | grep "*mytext*" > newfile.txt

This looks crazy if you’ve never seen anything like it before, but after a while, you’ll be stringing all kinds of cool stuff together! Let’s walk it from left to right:

  1. cat will concatenate the content of somefile.txt to the terminal i.e. Standard Out
  2. The | symbol takes that standard output and “pipes” it into the grep utility
  3. grep does pattern matching - in this case I want all lines that have “mytext” in them - the * is a wildcard that represents anything in front or behind my supplied term
  4. The > operator redirects the processed result into a file named newfile.txt

Makes sense when you break it down right? Well, I hope so! Here’s a more complicated example:

lastb | tr -s ' ' | cut -d' ' -f1 | uniq | sort | uniq -c | sort -rn

Whoa…yeah, and that’s not even a long one! However, when we break it down step by step it will start to make sense - and I recommend adding each element one at a time to get a sense of what is happening:

  1. lastb outputs information from the BTMP log which records unsuccessful account authentication attempts.
  2. The tr utility with the -s flag is removing duplicate spaces
  3. cut then breaks up the text using a single space as the delimiter and selects field one using the -f1 flag
  4. uniq gets rid of duplicate entries
  5. sort is sorting them numerically in this case
  6. uniq -c is running uniq again with a count prepended to the number of occurrences for each entry
  7. Finally the sort -rn command with flags sorts that list in reverse numerical order

Phew…that’s a mouthful right. Pipes are a powerful feature of the shell. They make it possible to use small, portable, applications together to do complex operations on our data. These examples were very rudimentary. Real-world examples can get pretty unwieldy and might require moving into a larger BASH shell script vs an inline pipe.

For more information on redirection operators and piping, check out this really awesome guide supplied by Redhat.

Where to go next

Obviously this is just a very small sampling of commands. There are so many more topics to cover and each depends on the distro. Most of these commands work on all distros, since most distros bake in the GNU-Core Utils by default (minus Alpine Linux). In later articles we’ll explore Linux desktop environments, networking, firewall configurations, and more! Hopefully the information in this article has helped you feel a little more comfortable on the command line!

This article will be continuously updated in the future. It was last updated on 4 June 2023.

Think I might be a good fit for your project?

Let's get the conversation started!