Maker.io main logo

A Beginner's Guide to Common and Useful Unix (Linux and MacOS) Commands

2023-10-13 | By Andrew Chen

Terminal commands, integral to the Linux operating system, are text-based directives that facilitate direct communication with the system's core functionalities. Operating within the command-line interface, users can efficiently navigate the file system, manipulate files and directories, install, and manage software packages, administer system configurations, and initiate various tasks all within their fingertips. This streamlined approach grants the user precise control over their Linux environment, enabling intricate operations, automation, and scripting that might not be as accessible through graphical interfaces. Essentially, Linux terminal commands form the backbone of system interaction, offering a potent mechanism for both beginners and experienced users to interact with and optimize their Linux-based machines. In this blog post, we will go over the most common and fundamental commands that any STEM student should know to best optimize their productivity and work style. Keep in mind that a lot of these commands are also applicable in other operating systems, not just exclusive to Linux:

  • ls

ls is one of the first commands that every computer user should know about. ls, short for list, lists all the subdirectories and files in the directory that you're in. The ls command is a fundamental tool for navigating the file system and gaining insights into the contents of directories. It helps users quickly see what files and subdirectories are present, understand their attributes, and navigate to the locations they need. For example:

A Beginner's Guide to Common and Useful Unix (Linux and MacOS) Commands

From my main directory, I'm able to see all the folders and directories I've created by simply typing ls into my command prompt. That's really cool, but how do I access the individual subdirectories inside my main directory? We can use the next command to "move ourselves" into a subdirectory to take a more specific look at the files we have.

  • cd

The command "cd", short for change directory, is a very useful tool to help the user navigate from one directory to another. By default, when you open the terminal on Linux (or any operating system), it directs you to the main directory, also known as the root directory. This is the first or the top-most directory since it contains all the files and folders in your computer system. However, it's no help to us if we can only view the root directory. The command "cd" requires passing an argument of the directory that you want to move into. The format of this would be something such as cd yourDirectory:

A Beginner's Guide to Common and Useful Unix (Linux and MacOS) Commands 

Here, I called the command cd Documents to take a closer look at what files I have specifically in that directory (think of it as moving into that directory for a closer inspection). This helps us navigate into the directories of our interest instead of manually trying to find these folders in our computer system, which can be cumbersome and tedious.

  • sudo

The next command sudo, short for “superuser do,” grants permission for the user to perform specific commands. It often prompts the user to type in their password to gain administrative or "superuser" privileges. By doing this, you'll be able to perform higher permission tasks, such as installing software, modifying system files, or running administrative commands. "sudo" also helps maintain the security of your system by controlling who can perform these types of operations. For example, a common command you might see a lot is sudo apt install <package-name>, where the sudo prefix ensures that the installation runs with the necessary permissions to modify the system! sudo will always prefix a high permission task at the front.

  • mv

Let's say you have a file or a directory in which you want to move into a specific directory. For example, mv file.txt /path/to/new/location/ moves your file of interest, file.txt, to the specified directory. Besides moving stuff around in your computer, mv also has a nifty trick where you can rename files and directories by typing mv oldname.txt newname.txt.

A Beginner's Guide to Common and Useful Unix (Linux and MacOS) Commands 

Let's say I want to move this file, textfile.txt, into my directory textfiles. We can run something like:

A Beginner's Guide to Common and Useful Unix (Linux and MacOS) Commands

After running mv textfile.txt textfiles, we know that this command will move the textfile.txt into the new directory textfiles. In the red boxes, we can see before and after; where the textfile.txt is no longer present in the root directory after the mv command. If we cd into the new directory and check for its contents using the ls command, we now see that the text file is inside that directory!

To demonstrate renaming files using mv:

A Beginner's Guide to Common and Useful Unix (Linux and MacOS) Commands

Here I boxed the old name in red and the new name in blue. We can see that the file name has indeed changed thanks to the mv command! This can be really useful for renaming files to more descriptive names, so you know what your files are for.

  • chmod

chmod is very similar to sudo in the sense that it is used to change the permissions of files and directories. It is short for "change mode", and it allows you to modify permissions using a combination of letters and symbols.

Here's a brief overview of how chmod works:

  • Read (r): Allows a file to be opened and read
  • Write (w): Permits modifications to a file's content
  • Execute (x): Grants the ability to run or execute a file (for executable programs or scripts) or access a directory

The permissions are set for three categories of users:

  • Owner (u): The user who owns the file or directory
  • Group (g): Users who belong to the group associated with the file or directory
  • Others (o): All other users on the system

For example:

A Beginner's Guide to Common and Useful Unix (Linux and MacOS) Commands

Here, I am trying to run a bash script that prints "Hello World" onto my terminal using the “./ “ command but I get an output telling me that my permission was denied, and I can't run the rogram script. We can get around this using the chmod command; since I wanted to execute the file, I used u+x (x for execute) to run the program. If I only need to read what's on the script, I can substitute the r in place of the x in the previous command (chmod u+r helloworld.sh).

  • mkdir

mkdir is one really important command that every Linux user should know; short for "make directory", it allows you to create new directories on top of existing directories.

A Beginner's Guide to Common and Useful Unix (Linux and MacOS) Commands

In this example, I first created a new directory called ReallyCoolStuff, which didn't exist in my root directory before I ran the mkdir command. I can interact with this directory the same way I can interact with any other directory: I can move files into this directory, or I can create even more subdirectories inside this new directory ReallyCoolStuff! Just make sure to cd into the new directory if you want to create files that are exclusively in a directory of your choice.

  • rm

Let's say you got a little carried away with creating directories or downloading files from the internet and now you want to delete them. The rm command, short for remove, allows you to delete a file or a directory on your computer. For example:

A Beginner's Guide to Common and Useful Unix (Linux and MacOS) Commands

I'm looking inside the directory ReallyCoolStuff and I find that there is a png file that I no longer need in my system! Instead of spending time manually going through the system and typing out the name of this file (which is far more impractical), use the rm command. For a file, you can use rm fileName.txt to delete the file entirely.

A Beginner's Guide to Common and Useful Unix (Linux and MacOS) Commands

After running the rm command and the ls command, you can see that the screenshot no longer exists inside the ReallyCoolStuff directory, which means it has been deleted! If you want to delete whole directories, just add the -r flag in front, for example: rm -r UnwantedDirectory. Just be careful when using this command, as you may delete hours’ or days’ worth of work with a single command, and be sure that what you're deleting is the intended file/directory that you want to remove.

A Beginner's Guide to Common and Useful Unix (Linux and MacOS) Commands

Always make sure that when you're using the rm command, that you are within the scope of the file/directory that you want to delete. For example, you cannot delete a directory that you're currently in nor can you delete a file that is not present in your current directory. From the root directory, I cannot delete files inside the ReallyCoolStuff directory, and I must cd into the ReallyCoolStuff directory first before I can do anything meaningful.

  • pwd

This command tells the user what their current directory is by displaying the full path of the directory that you are in. For example:

A Beginner's Guide to Common and Useful Unix (Linux and MacOS) Commands

From the home directory, the pwd command will return the path of the root directory. If I were in a different subdirectory inside the root directory, the pwd command will reflect that such as my example above; in the root directory, the output returns /Users/andrewchen1228 as opposed to the output /Users/andrewchen1228/Documents after I cd'ed into the Documents directory. The pwd helps show the total path of all the directories that you are currently present in.

  • man

man (or manual) is arguably one of the most important commands for any Linux user; by typing man anyCommand into the command prompt, it'll explain the arguments that the command can take, how to use it, and what the command explicitly does in your system. For example:

A Beginner's Guide to Common and Useful Unix (Linux and MacOS) Commands

This command will further help clarify any of the commands written in this blog post and commands not mentioned here and will help save a ton of time!

  • ./

In a terminal prompt, this command is used to refer to the current directory or path. It is generally used to run executable files, for example:

A Beginner's Guide to Common and Useful Unix (Linux and MacOS) Commands

Here I wanted to execute a simple Hello World program in C in the terminal, and this command ./hello lets me do just that! In summary, "./" is just a way to explicitly indicate that you want to refer to a file or an executable in the current directory when working in a terminal. This command isn't just exclusive to C but can be used in tandem with bash scripting or other languages like Java. Sometimes, an executable may not run due to permission issues, and this is where you would use the chmod command from before: chmod u+x file.name, where the chmod command is telling the u, the user, or the owner of the file, to have permission to x, execute the file as a program or script. The chmod and ./ command are almost always used together, and they are very common in bash scripting as well.

  • vim

vim is a very powerful text edit command that can be used for editing and creating text, from code files to configuration files and more. To use vim, you'll typically run the command vim filename.txt, which will open a new file (if there is no file with that name) or edit a pre-existing file with that name. You'll be prompted to press "i" to go into insert mode, which allows you to start texting text. After you are done and want to save changes, you can press "Esc" followed by typing ":wq" and "Enter" to leave the file.

A Beginner's Guide to Common and Useful Unix (Linux and MacOS) Commands

A Beginner's Guide to Common and Useful Unix (Linux and MacOS) Commands

  • history

Like its name suggests, the command is used to display a list of previously executed commands. It provides a way for users to review and reuse commands that have been executed in the past. Commands that you have executed in the past are typically stored in a history file, with each command occupying its own unique index line.

A Beginner's Guide to Common and Useful Unix (Linux and MacOS) Commands

The neat thing about this history command is the ability to rerun past commands without having to retype the whole thing. Let's say that I wanted to edit coolstuff.txt again, but instead of typing vim coolstuff.txt all over again on my terminal, I can go:

A Beginner's Guide to Common and Useful Unix (Linux and MacOS) Commands

Since that command is on line 593, I can rerun this command by appending an exclamation mark in front of the line number and as expected, open the text editor for coolstuff.txt.

On top of that, Linux (and not just Linux) has a neat feature where previous commands can be rerun by pressing the "Up" arrow key, allowing you to cycle through your most recent commands. It's also worth noting that the specifics of how the history command works may vary slightly depending on the shell that you're using (e.g., Bash, Zsh, Fish), but the general functionality should be the same across most Unix-like operating systems.

  • whatis

The whatis command is used to display a brief explanation or summary of a command. It provides a quick way to obtain information about any command without the need to consult the full manual pages. The basic syntax of the whatis command is: whatis command_name

For example, if you want to know what the ls command does, you can type in:

A Beginner's Guide to Common and Useful Unix (Linux and MacOS) Commands

This command searches a precompiled database of manual page descriptions to give the user the most concise information about the command of interest. It's useful when you only need a quick summary of how a command works without spending a ton of time delving into detailed documentation. However, whatis may not be available on all Unix-like systems and it is recommended to use the man command to access the full manual page instead.

By no means does this post represent an exhaustive list of all the commands available on Linux, as there are always new commands being developed for the Linux system. This guide serves to encompass the important fundamentals of using these commands to better enhance your user experience, boost your productivity, and provide a gateway for more advanced tutorials online.

TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum