The Ultimate Guide to Master Essential Linux Commands: 37 Must-Know Commands for Every User

The Ultimate Guide to Master Essential Linux Commands: 37 Must-Know Commands for Every User

The Ultimate Guide to Master Essential Linux Commands: 37 Must-Know Commands for Every User

Key Takeaways

Use aliases to create shortcuts for commonly used commands. This can help save keystrokes and make navigating between different systems easier.

The Best Linux Commands for the Terminal

: Essential Linux Terminal Commands for Beginners

Whether you are a Linux newbie or simply need a quick refresher, this comprehensive list of commands will be your go-to resource. Consider this your ultimate reference guide for mastering the Linux terminal. This knowledge also applies to the command line of macOS.

1. alias

The alias command allows you to assign a custom name to a command or a series of commands. This enables you to execute the command or series of commands simply by typing the assigned short name in the shell.

The following alias, named pf (short for process find), is a slightly more elaborate example. Take note of the quotation marks surrounding the command sequence, which are necessary when the command sequence contains spaces. This alias utilizes the ps command to display the currently running processes, followed by piping them through the grep command. The grep command searches for entries in the ps output that match the command line parameter $1.

The Ultimate Guide to Master Essential Linux Commands: 37 Must-Know Commands for Every User

Aliases created on the command line will cease to exist when the terminal window is closed. They will not persist. To ensure that your aliases are always accessible, you should add them to the .bash_aliases file located in your home directory.

cat .bash_logout

The Ultimate Guide to Master Essential Linux Commands: 37 Must-Know Commands for Every User

To manage files that are longer than the number of lines displayed in your terminal window and ensure readability, consider piping the output from the "cat" command to "less". This allows you to navigate through the file using the Up and Down Arrow keys, PgUp and PgDn keys, as well as the Home and End keys. To exit the "less" interface, simply type "q".

The cd command allows you to change your current directory, essentially relocating you within the filesystem.

To switch to a directory located within your current directory, simply type cd followed by the name of the target directory.

cd /usr/local/bin

To quickly return to your home directory, use the ~ (tilde) character as the directory name.

Here's another trick: You can use the double dot symbol .. to represent the parent of the current directory. You can type the following command to go up a directory:

cd ..

The Ultimate Guide to Master Essential Linux Commands: 37 Must-Know Commands for Every User

4. chmod

If the first character is "-" it indicates a file, while if it is "d" it represents a directory. The remaining part of the string is divided into three sets of three characters each. Beginning from the left, the first three characters denote the file permissions of the owner, the middle three characters indicate the file permissions of the group, and the last three characters represent the permissions for others. Within each set, the presence of "r" implies read permission, "w" implies write permission, and "x" implies execute permission.

The file permission is granted if the respective "r", "w", or "x" character is present. Conversely, if the letter is absent and is replaced by "-", that specific file permission is not granted.

1: Execute permission

2: Write permission

3: Write and execute permissions

4: Read permission

7: Read, write and execute permissions

Looking at our example.txt file, we can see that all three sets of characters are rwx. That means everyone has read, write and execute rights with the file.

The Ultimate Guide to Master Essential Linux Commands: 37 Must-Know Commands for Every User

To grant read, write, and execute permissions (7 from our list) for the owner, and read and write permissions (6 from our list) for the group, as well as for others, the chmod command should be executed with the digits 766.

The chown command allows you to alter the owner and group owner of a file. By using ls -l, we can observe the file description of our example.txt file, which currently displays dave dave. The initial entry denotes the file owner, in this case, the user dave. The second entry reveals that the group owner is also dave. When a user is created, a default group is assigned to them, in which they are the sole member. This confirms that the file is not shared with any other user groups.

To modify the file's owner or group, or both, you can employ chown. You will need to provide the owner's name and the group's name, separated by a : character. Please ensure that you use sudo. If you wish to retain dave as the owner while assigning mary as the group owner, execute the following command:

To change both the owner and the group owner to mary, you would use the following command;

sudo chown mary:mary example.txt

6. curl

The curl command is a tool to retrieve information and files from Uniform Resource Locators (URLs) or internet addresses.

To retrieve a specific file from a GitHub repository, you usually need to clone the entire repository. However, using curl, you can directly fetch the desired file. Simply execute the following command, and remember to use the -o (output) option to specify the filename for saving. Failing to do so will result in the file contents being displayed in the terminal without getting saved on your computer.

If you don't want to see the download progress information use the -s (silent) option.

curl -s https://raw.githubusercontent.com/torvalds/linux/master/kernel/events/core.c -o core.c

The Ultimate Guide to Master Essential Linux Commands: 37 Must-Know Commands for Every User

7. df

df -h -x squashfs

The Ultimate Guide to Master Essential Linux Commands: 37 Must-Know Commands for Every User

The line differences are displayed side by side using the -y option. To avoid wraparound lines, you can specify the maximum line width with the -w option. In this example, the two files are alpha1.txt and alpha2.txt. To focus only on the lines with differences, use the --suppress-common-lines option. The command would be:

diff -y -w 70 alpha1.txt alpha2.txt --suppress-common-lines

The echo command prints (echoes) a string of text to the terminal window.

The command below will print the words "A string of text" on the terminal window.

echo $USER

echo $HOME

The following command will cause a bleep to be issued. The -e (escape code) option interprets the escaped a character as a 'bell' character.

echo -e "\a"

The exit command will close a terminal window, end the execution of a shell script, or log you out of an SSH remote access session.

exit

To locate files whose location you cannot recall, utilize the find command. Specify the starting point and the desired criteria for the search. In this instance, the "." denotes the current folder, while the -name option instructs find to identify files with names matching the specified search pattern.

Employ wildcards, wherein "*" signifies any character sequence and "?" symbolizes any single character. By utilizing *ones*, you can find file names containing the sequence "ones." Examples include words such as bones, stones, and lonesome.

find . -name *ones*

The Ultimate Guide to Master Essential Linux Commands: 37 Must-Know Commands for Every User

If you want the search to be case insensitive use the -iname (insensitive name) option.

find . -iname *wild*

The Ultimate Guide to Master Essential Linux Commands: 37 Must-Know Commands for Every User

13. free

The Ultimate Guide to Master Essential Linux Commands: 37 Must-Know Commands for Every User

14. grep

The output lists the name of the file and shows the lines that match. The matching text is highlighted.

The Ultimate Guide to Master Essential Linux Commands: 37 Must-Know Commands for Every User

The groups command tells you which groups a user is a member of.

groups dave

16. gzip

Compress files using the gzip command. By default, the command removes the original file and retains only the compressed version. However, if you want to keep both the original and the compressed version, utilize the -k (keep) option.

gzip -k core.c

The Ultimate Guide to Master Essential Linux Commands: 37 Must-Know Commands for Every User

head -core.c

head -n 5 core.c

Use the history command to view a list of previously executed commands on the command line. To repeat a specific command, enter an exclamation point followed by the corresponding command number from the history list, such as !188.

!!

19. kill

ps -e | grep shutter.

Once we have determined the PID — 1692 in this case — we can kill it as follows:

20. less

The less command provides a convenient way to view files without the need to open an editor. It offers a faster alternative, eliminating the risk of accidentally modifying the file. With less, you can effortlessly navigate through the file using the Up and Down Arrow keys, the PgUp and PgDn keys, as well as the Home and End keys. To exit from less, simply press the Q key.

The Ultimate Guide to Master Essential Linux Commands: 37 Must-Know Commands for Every User

You can also pipe the output from other commands into less. To see the output from ls for a listing of your entire hard drive, use the following command:

Use / to search forward in the file and use ? to search backward.

21. ls

To display the contents of the current directory, the "ls" command is typically encountered as the initial command for most Linux users. By default, it presents a listing of files and directories within the specified location. It is highly recommended to consult the man page of "ls" for a comprehensive understanding of the numerous available options. Here are a few common examples:

- To list the files and folders in the current directory:

ls -l

To use human-friendly file sizes include the -h (human) option:

ls -lha

The Ultimate Guide to Master Essential Linux Commands: 37 Must-Know Commands for Every User

For example, to see the man pages for chown, use the following command:

man chown

23. mkdir

To create new directories in the filesystem, you can make use of the mkdir command. Simply specify the name of the new directory as an argument to mkdir. If the new directory is located outside of the current directory, you need to provide the path to the new directory.

mkdir quotes

The Ultimate Guide to Master Essential Linux Commands: 37 Must-Know Commands for Every User

To create a directory along with its parent directories if they don't exist, use the -p (parents) option with the mkdir command. In the given example, we are creating the "2019" directory inside the "yearly" directory, which is inside the "quotes" directory. Since the "yearly" directory does not exist, we can execute the following command to create all the required directories simultaneously:

mkdir -p quotes/yearly/2019

The mv command enables the transfer of files and directories between different directories. Furthermore, it provides the option to rename files.

In order to move a file, you need to specify the current location of the file and the desired destination. As illustrated in this example, we are relocating a file named apache.pdf from the "~/Document/Ukulele" directory to the current directory, symbolized by the single . character.

mv ~/Documents/Ukulele/Apache.pdf .

The file move and rename action could have been achieved in one step:

mv ~/Documents/Ukulele/Apache.pdf ./The_Shadows_Apache.pdf

You can also change the password of another user account, but you must use sudo. You will be asked to enter the new password twice.

sudo passwd mary

The ping command is utilized to confirm network connectivity with another network device. It is frequently employed for troubleshooting networking problems. To use ping, simply input the IP address or machine name of the specific device.

Here's what's going on here:

The device at IP address 192.168.4.18 is responding to our ping requests and is sending back packets of 64 bytes.

The time value represents the length of the round trip from your computer to the device and back. In simpler terms, the lower this time, the better it is.

To specify the number of ping attempts for ping to execute, utilize the -c (count) option.

ping -a 192.168.4.18

27. ps

The Ultimate Guide to Master Essential Linux Commands: 37 Must-Know Commands for Every User

To see all the processes related to a particular user, use the -u (user) option. This is likely to be a long list, so for convenience pipe it through less.

ps -u dave | less

The Ultimate Guide to Master Essential Linux Commands: 37 Must-Know Commands for Every User

28. pwd

Nice and simple, the pwd command prints the working directory (the current directory) from the root / directory.

29. shutdown

The shutdown command lets you shut down or reboot your Linux system.

The Ultimate Guide to Master Essential Linux Commands: 37 Must-Know Commands for Every User

To shut down immediately, use the now parameter.

Schedule a shutdown and notify logged in users about the impending shutdown. To specify the shutdown time, you can either indicate a designated number of minutes from now (e.g., +90) or a specific time (e.g., 23:00). Any accompanying text message will be broadcasted to all logged in users.

Example:

shutdown 23:00 - Please save your work and log out before the scheduled shutdown at 23:00 tonight!

shutdown +15 Shutting down in 15 minutes!

shutdown -c

To establish a connection with a remote Linux computer and log into your account, use the ssh command. Provide your username and the IP address or domain name of the remote computer. For instance, to log into the computer at 192.168.4.23, the user "mary" would execute the command: ssh mary@192.168.4.23. Once the connection is established, you will be prompted to enter your password.

The Ultimate Guide to Master Essential Linux Commands: 37 Must-Know Commands for Every User

Her user name and password are verified and accepted, and she is logged in. Notice that her prompt has changed from "Nostromo" to "howtogeek."

w

exit

The sudo command is required when performing actions that require root or superuser permissions, such as changing the password for another user.

sudo passwd mary

The tail command displays the last 10 lines of a file. To view a different number of lines, use the -n option followed by the desired number. In this example, we view the last 10 lines of the core.c file. We can also specify to display only five lines by repeating the command.

33. tar

Archive Files with the Tar Command

The tar command is a powerful tool that allows you to create archive files (also known as tarballs) that can contain multiple files. This greatly simplifies the process of distributing a collection of files. Additionally, tar can be used to extract files from an archive. It is common practice to compress the archive when using tar. However, if compression is not specified, the resulting archive file will be uncompressed.

The Ultimate Guide to Master Essential Linux Commands: 37 Must-Know Commands for Every User

The -c (create) option and the -v (verbose) option were employed. The verbose option provides visual feedback by listing the files to the terminal window as they get added to the archive. The -f (filename) option is utilized to specify the desired name of the archive, which in this case is songs.tar.

The files will be displayed to the terminal window as they are included in the archive file.

There are two methods to indicate that you wish to compress the archive file using tar. The first option is to use -z (gzip). This prompts tar to utilize the gzip tool for compressing the archive after its creation.

The Ultimate Guide to Master Essential Linux Commands: 37 Must-Know Commands for Every User

The files are displayed in the terminal window as they are being added to the archive file, just like before. However, the compression process will slightly prolong the creation of the archive due to the additional time it takes.

To create an archive file that is compressed using a superior compression algorithm giving a smaller archive file use the -j (bzip2) option.

tar -cvjf songs.tar.bz2 Ukulele/

When archiving numerous files, you have two options:

select the -z option for a balance between decent compression and reasonable speed, or

opt for the -j option for better compression at a slower pace.

From the screenshot provided, the ".tar" file is the largest, followed by the smaller ".tar.gz" file, and the ".tar.bz2" file is the smallest among the archives.

ls

tar -xvf songs.tar

To extract files from a ".tar.gz" archive, use the -z (gzip) option.

tar -xvzf songs.tar.gz

tar -xvjf songs.tar.bz2

The Ultimate Guide to Master Essential Linux Commands: 37 Must-Know Commands for Every User

The initial line displays the current time and duration of computer operation, the logged-in user count, as well as the load average recorded for the previous one, five, and fifteen minutes.

The subsequent line provides information on the quantity of tasks and their respective states, including running, stopped, sleeping, and zombie.

sy: value is the CPU time spent on running system "kernel space" processes

ni: value is the CPU time spent on executing processes with a manually set nice value

hi: The CPU time spent servicing hardware interrupts

si: The CPU time spent servicing software interrupts

st: The CPU time lost due to running virtual machines ("steal time")

The fourth line shows the total amount of physical memory, and how much is free, used and buffered or cached.

The user has pressed the E key to change the display into more humanly digestible figures instead of long integers representing bytes.

The columns in the main display are made up of:

PR: Process priority

NI: The nice value of the process

SHR: Shared memory used by the process

S: Status of the process. See the list below of the values this field can take

TIME+: total CPU time used by the task in hundredths of a second

COMMAND: command name or command line (name + options)

D: Uninterruptible sleep

R: Running

Z: Zombie

Press the Q key to exit from top.

35. uname

You can obtain some system information regarding the Linux computer you're working on with the uname command.

Use the -r (kernel release) option to see the kernel release.

Use the -v (kernel version) option to see the kernel version.

uname -r

uname -v

The w command lists the currently logged in users.

w

Use whoami to find out who you are logged in as or who is logged into an unmanned Linux terminal.

whoami

Learning Linux is similar to learning any other skill. It requires practice to become familiar with the commands. Once you have a good grasp of these commands, you'll be on your way to becoming proficient.

There's a long-standing joke in the Unix community that states the only command you need to know is the "man" command. While there is some truth to this, many of the man pages can be difficult to understand without an introduction. This tutorial will provide the necessary introduction to help you navigate and understand them.