fbpx

Useful Linux Commands to Know

Administering a Linux system is a little different than Windows. For starters, Linux is very CLI heavy but don’t let this scare you off! The CLI is a very efficient and easy way to interact with your system. It also means that your system is light on resources. Instead of using RAM for the window manager, that is RAM that can be used by your application. So let’s jump right in and look at the useful Linux commands to know!

Keep in mind that this guide was written using AlmaLinux 8.5 as the OS.

Useful Linux Commands to Know

1. ls command

This command stands for list and as you can probably guess, it helps you list things. Specifically files and directories in another directory.

Even if you’re not in the current directory, you can still list files in another directory. Just provide ls the file path. For example, here is how you can list out /var/www from the user’s home directory.

2. cd command

Used in conjunction with ls, cd stands for change directory. This lets you move through the file system with ease. If you’re a Windows user and you’ve used the Windows command prompt or PowerShell, you might be familiar with this command already. Just type in cd and the file path you want to go to, for example: cd /var/www/html

cd also has some handy attributes to let you navigate quickly.

cd .. (notice the two dots) will move you a directory up.

cd will take you right to your home directory.

cd – (with a hyphen) will take you back to where you last were, like the back button on your web browser.

3. pwd command

Now that you’re navigating your way around the file system like a pro, how do you know where you’re at without changing directories or listing contents? Easy! Use the pwd command! This stands for Path Working Directory and it will show you the absolute file path you’re currently working in. Think of it like a GPS.

4. mkdir command

If you need to make a directory, just type in mkdir dirname and press enter. You’ve just made a directory! Directories are exactly like folders on Windows. It’s just a different name for the same thing. Tomato, tomatoe.

5. rmdir command

This command is the opposite of mkdir as it stands for remove directory. Just type in rmdir mydirectory and presto! It’s gone. Keep in mind this command will only remove empty directories

6. rm command

When you want to remove something, including directories with files in them, rm will do it! There are several attributes available with rm such as:

rm -r which will remove only directories (like rmdir).

rm -rf which will remove directories and files.

You do need to be careful with rm though! You can easily remove system files with this command and break your system. If you just need to remove a single file at the current path, just type in rm filename

7. touch command

This command lets you create a bank file. Just type in touch /var/www/html/index.html and you’ll have a new index.html file in the web server’s default web location. Or type in touch notes.txt in your home directory to create a blank text file called notes.txt.

8. cat command

Since we’re talking about files, cat which is short for concatenate, allows you to view files or join files. For example, cat notes.txt will let me view my notes file. I can add notes-January.txt and notes-February.txt into my notes.txt file by running the command cat notes-January.txt notes-February.txt>notes.txt

9. find command

This command is very powerful in helping you find files, directories, and more. Think of it like a powerful search engine on your system. There’s a lot you can do with find, but to keep it simple, I’m just going to show you how you can find some files.

Let’s say I’m looking for all the notes I wrote as plain text files. I can use the command find ./notes -name “*.txt” to find any text file located within my notes directory.

You can change around that command to search for a specific file. For example, I know I have a file called “notes.txt” but I’m not sure where it’s at. So let’s find it!

find . -name notes.txt

Found it! Turns out I have two files called “notes.txt”.

10. grep command

This is a command Linux administrators use almost all the time. This is a handy tool that lets you search within files. We use it a lot when troubleshooting log files. Almost always a log file will contain “error” or “warning”.

grep Apache /var/log/httpd/error_log will search the Apache web server error log for “Apache” (remember, Linux is case-sensitive! If we type in grep apache /var/log/httpd/error_log, we won’t see anything returned).

Now let’s say we’re actually not sure if it’s “Apache” or “apache” or even what file.

grep -ir apache /var/log/httpd

The -i means to search case-insensitive which will return both apache and Apache. -r means to search recursively or rather, look in all files and directories below this point. We can shorten the command by combining -i and -r to just -ir.

Now we can see all instances of apache or Apache and what files they’re in.

11. sudo command

This stands for Super User Do. It allows you to run a command with root privileges when you’re not logged in as root. It’s advisable to always run as a standard administrative user and then run a command with sudo when you need to. For example, updating the system requires super user privileges and you’ll be reminded if you try as a standard user.

Run the command again with sudo in front, and you’ll be prompted to enter your password. If this is the first time you’ve ran sudo, you’ll be greeted with this friendly word of caution.

Remember, when you run sudo, you’re running that command with super user privileges! That means you can damage your system if you’re not careful.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.