The Linux Command Line: Short Guide for Beginners
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. Small projects and test environments often start with a cheap VPS hosting option to keep infrastructure costs predictable.
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/passwdLook 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/falseIn 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/shellsOutput:
# /etc/shells: valid login shells
/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
/bin/dash
/usr/bin/dashFor example, to change the shell for the hostman user from bash to sh, run the command:
hostman@server:~$ chsh -s /bin/sh hostmanTo 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 hostmanThe 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:
-
catoutputs the contents of the file to the console; -
pwdoutputs the directory you are in; -
cdis used navigate through directories; -
lsdisplays the contents of the directory; -
echooutputs text to the console; -
touchcreates a new empty file; -
mkdircreates a directory; -
rmdirdeletes an empty directory; -
mvis used to move or rename files and directories; -
historydisplays the list of commands run previously in this terminal (the list is stored in the.bash_historyfile).
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.txtTo display the contents of the current directory, enter the command:
hostman@server:~$ lsWe 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 -aShow more detailed information about the contents of the current directory:
hostman@server:~$ ls -lYou 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 -lahIf 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 12Some 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 --allFor 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 -tCommands 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 timehostman@server:~$ ls --sort=timeThe uname command displays system information. If run without any options, it outputs Linux.
hostman@server:~$ unameThe 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 -aTo move to a different directory, enter the cd (change directory) command and pwd to check:
hostman@server:~$ cd Documents/hostman@server:~/Documents$ pwdOutput:
/home/hostman/DocumentsTo return to the home directory, use the cd command without any arguments:
hostman@server:~$ cdEnvironment 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:~$ envTo 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 $HOMETo create a variable, use the simple syntax variable_name=variable_value. To reference a variable, use the $ character, for example:
hostman@server:~$ var=valuehostman@server:~$ echo $varIn this guide, we reviewed basic Linux commands to help you get acquainted with the command line and manage your Linux distribution more easily. You can try our reliable Linux VPS hosting for your projects.