Hey there! Welcome to Hostman! 🎉

The Linux Command Line: Short Guide for Beginners

23.11.2023
Reading time: 5 min
Hostman Team
Technical writer

Working with GNU/Linux operating systems means mostly working with the command line. The command shell allows the user to interact with the operating system. There are many command shells; in this article, we will look at the most common command shell, bash.

To follow this guide, you should have access to the server console or connect to the server via the SSH protocol.

To determine which shell the system uses, you need to check the user database file /etc/passwd

To view the file, use the cat command:

cat /etc/passwd

Look at the last part in each line that specifies the shell. 

root:x:0:0:root:/root:/bin/bash
hostman:x:1000:1000:Hostman,,,:/home/hostman:/bin/bash
mysql:x:106:112:MySQL Server,,,:/nonexistent:/bin/false

In this example, the root superuser and the regular hostman user use the bash shell and, therefore, have access to the command line. The system user mysql, which runs the MySQL database service, is not allowed to use the shell for security reasons.

You can change the shell for the user with the chsh command. But first, let's look at the list of available shells we can use.

hostman@server:~$ cat /etc/shells

Output:

# /etc/shells: valid login shells
/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
/bin/dash
/usr/bin/dash

For example, to change the shell for the hostman user from bash to sh, run the command:

hostman@server:~$ chsh -s /bin/sh hostman

To change the shell for another user, you need root privileges. For example, you can block the hostman user from being able to access shell usage.

root@server:/home/hostman# chsh -s /bin/false hostman

The global command line settings are in the /etc/profile file, and the per-user settings are in a hidden ~/.bash_profile file in the user's home directory.

When working in the command line, you will need these common commands:

  • cat outputs the contents of the file to the console;

  • pwd outputs the directory you are in;

  • cd is used navigate through directories;

  • ls displays the contents of the directory;

  • echo outputs text to the console;

  • touch creates a new empty file;

  • mkdir creates a directory;

  • rmdir deletes an empty directory;

  • mv is used to move or rename files and directories;

  • history displays the list of commands run previously in this terminal (the list is stored in the .bash_history file).

Before using the commands, let’s take a look at their syntax:

command [options…] [arguments…]

To create an empty newfile.txt file, enter the command:

hostman@server:~$ touch newfile.txt

To display the contents of the current directory, enter the command:

hostman@server:~$ ls

We can add additional parameters to the command. For example, to display the entire contents of a directory, including hidden files and folders, use the ls command with the -a option.

hostman@server:~$ ls -a

Show more detailed information about the contents of the current directory:

hostman@server:~$ ls -l

You can also combine several options for the command, such as displaying a list of files, including hidden ones, in a detailed format and counting their size in human-readable form (KB, MB, GB instead of bytes):

hostman@server:~$ ls -lah

If you use multiple options that require arguments, do not combine them. For example, the -T option, which determines the size of the tab, also requires an argument. In this case enter the options separately:

hostman@server:~$ ls -w 40 -T 12

Some commands support additional options that are longer than one character. They consist of a double hyphen -- and the parameter value, such as --all. The --all option lets you list all files, including hidden files. For example:

hostman@server:~$ ls --all

For commands that support both long and short options, you can run the command using both long and short options at the same time:

hostman@server:~$ ls --all --reverse -t

Commands that support long options often support arguments, which can be specified with or without the = character (the output of both commands is the same):

hostman@server:~$ ls --sort time
hostman@server:~$ ls --sort=time

The uname command displays system information. If run without any options, it outputs Linux.

hostman@server:~$ uname

The uname command is useful when you need to find out the computer name as well as the current version of the OS kernel. To display additional information about the system, you can use one of the many options of the uname command. For example, to display all system information, use the -a option:

hostman@server:~$ uname -a

To move to a different directory, enter the cd (change directory) command and pwd to check:

hostman@server:~$ cd Documents/
hostman@server:~/Documents$ pwd

Output:

/home/hostman/Documents

To return to the home directory, use the cd command without any arguments:

hostman@server:~$ cd

Environment variables are global variables used by the operating system and common variables that run within a running console session. To view your environment, enter the command without arguments:

hostman@server:~$ env

To display the value of an environment variable, run the command echo $HOME. For example, to view the user's home directory stored in the $HOME environment variable, use:

hostman@server:~$ echo $HOME

To create a variable, use the simple syntax variable_name=variable_value. To reference a variable, use the $ character, for example:

hostman@server:~$ var=value
hostman@server:~$ echo $var

In this guide, we reviewed basic Linux commands to help you get acquainted with the command line and manage your Linux distribution more easily.