Linux is gaining popularity among today’s Operating System (OS) choices. It is estimated that 67% of web servers are powered by Linux and it isn’t slowing down (Finley, 2016). As technology advances, and more importantly as the users of technology advance, Linux starts to offer a great many benefits over proprietary counterparts like Microsoft’s Windows or even Apple’s OS X. While both of those OS brands are great at what they do, they also have limits that open-source software does not. Some of the benefits of Linux are that its free, its extremely customizable, has low-resource consumption, its source code is available, etc… From the Android OS to Toyota’s new Automotive Grade Linux (AGL) being introduced in 2018 vehicle models, Linux is quickly establishing itself as a must know for all citizens of the future (AGL, 2017).
This article strives to provide a clear and useful instroduction to the Linux command-line-interface (CLI), which is where the real power of Linux lies. Many users coming from Windows or OS X can be intimidated by the CLI, but it is actually quite safe and very efficient when you know how to use it. Take a deep breath, grab some coffee, and lets get started!
Note: Many or all of these commands should also work on the OS X terminal, but full OS X support is beyond the scope of this article.
- What is the Command Line Interface (CLI)?
- File System
- Navigating the File System
- List the contents of a directory
- Changing directories
- Navigating the File System
- Managing the File System
- Software Management
- CLI Text Editing
- Backing Up Configuration Files Before Making Changes
- System Functions
- Shutdown commands
- Restart Commands
What is the Command Line Interface (CLI)?
There are different methods that humans use for interacting with computer software, we call these methods interfaces. The most common interfaces are the Graphical User Interface (GUI) and the Command Line Interface (CLI). The CLI was the most common method of interaction humans had with computers until 1983 when Apple released the Lisa platform. The Lisa was the first commercially available computer with a GUI (Abell, 2010). Since the introduction of the GUI there has been a large focus on getting away from the CLI. Windows and Mac OS evolved to hide the command line well out of sight of it’s users.
The Linux GUI is really just an application that interacts with the CLI for you. It runs on top of the CLI and when you push a button on the GUI it issues CLI commands behind the scenes for the user. This application is called a Desktop Environment (DE) when speaking in Unix/Linux terms. While the DE makes it possible for workstations and home computers to access websites and view streaming video, they represent a middle-man between the user and the real Operating System. To clarify, the DE is programmed to act as a translator between the user and the CLI. If the DE has not been programmed with all of the CLI options then the user is limited on how much power they have over the configuration of the system.
One of the strengths of Linux over other proprietary OS brands is that it allows you to interact very closely with the lower level operations of the system and configuration files. It is standard practice for network administrators to manage Linux servers that don’t even have a GUI installed. GUIs take up a lot of resources, require more coding from developers, and are more cumbersome to operate remotely, which are undesirable for many server implementations.
Navigating the File System
The ls command lists the files in the current directory. Using ls with the -l flag lists all files and associated owners, groups, and permissions:
You can also specify an absolute or relative path to a directory:
The above will list the contents of the network folder. The ‘/’ that precedes ‘etc/network’ is what makes the path absolute. The ‘/’ represents the root, or base, directory of the file system. This means that /etc/network and etc/network are not the same; the first is absolute and the second is relative to, or dependent upon, the current directory.
To list all files in the current directory, including hidden files:
Example – This lists all the files and hidden files in the imaginary users Documents/Finances directory:
ls -A Documents/Finances
To navigate to a folder use the cd command with relative or absolute paths:
To move back one directory level you can type:
Note the space between cd and ..
To navigate directly to the root, or base, directory type:
Note the space between cd and /
Managing the File System
The cp command takes two arguments [-SOURCE-] and [-DESTINATION-]:
cp SourceFile DestinationFile
cp myFile.txt myFile-copy.txt
The above creates a copy of myFile.txt and names it myFile-copy.txt
You can also copy a file to another directory without changing the filename:
cp myFile.txt backupFiles/
To copy the file to a new directory and give it a new name:
cp myFile backupFiles/myFile-copy.txt
To delete a file you use the rm command to remove the file:
The above deletes the file example.txt.
mv SourceFile DestinationFile
Warning: Moving files deletes the source file!
To move a file to another directory without changing the filename:
mv myFile.txt backupFiles/
To move a file to another directory while also changing the filename:
mv myFile.txt backupFiles/myFile-moved.txt
To rename a file you simple use the mv command without specifying a different directory:
mv myFile.txt myFile-Renamed.txt
The above moves myFile.txt to myFile-Renamed.txt and deletes the original myFile.txt file. The result is that myFile.txt was renamed to myFile-Renamed.txt.
Managing directories is slightly different than managing individual files, though there are many similarities.
To make a new directory you use the mkdir command:
The above creates a folder called MyDirectory
To copy a directory and all of it’s contents you must add the recursive flag -R to your cp command:
cp -R SourceDir DestinationDir
To copy a direcotry called myStuff and all of its contents to a subdirectory called backupFolders without changing the folder name:
cp -R myStuff backupFolders/
To copy a directory called myStuff and all of its contents to a subdirectory called backupFolders while also changing the folder name:
cp -R myStuff backupFolders/myStuff_backup
To remove a folder/directory and all of it’s contents you must add the recursive flag -R:
rm -R MyDirectory
The -R flag deletes all files and subdirectories before finally deleting the parent MyDirectory folder.
To move a directory called use the mv command:
mv SourceDir DestinationDir
To move a directory called myStuff to a subdirectory called backupFolders without changing the folder name:
mv myStuff backupFolders/
To move a directory called myStuff to a subdirectory called backupFolders while also changing the folder name to myStuffBackup:
mv myStuff backupFolders/myStuffBackup
Renaming a directory is the same as renaming a file; use the mv command without specifying a subdirectory:
mv OldFolderName NewFolderName
What is a package manager?
A package manager is an application that gracefully handles the installation, updates, and removal of applications for the Linux Operating System. Most of these packages managers also access software repositories which are large databases of software available for Linux users to install via the CLI. One of the most valuable aspects of package managers is that they handle security updates for your system and also for the applications you’ve installed using the package manager.
The apt-get feature is one of my favorites of the Debian based Linux distributions (Ubuntu, Linux Mint, and Kali are some Debian based distros). Keep in mind you must be logged in as root or use the sudo command (which temporarily grants the current user root privileges for the current task only). root can be thought of as the Administrator on a Windows machine and has absolute power over the OS.
These commands work on Debian, Ubuntu, Linux Mint, Kali, and any other Debian based Linux distros. The sudo command will be used in these examples. If logged in as root omit the sudo command.
Update Software List
Update the list of packages from the repository. This means you will be installing the latest version of the software you are looking for when you use the install command:
sudo apt-get update
Install a package from the repository:
sudo apt-get install [package_name]
Example – This command installs LibreOffice (an Open Source Office suite):
sudo apt-get install libreoffice
Update Operating System and Applications
If you want to update all the packages on the OS, equivalent to Windows Updates, you can run these two commands:
sudo apt-get update sudo apt-get upgrade
The first command, update, you’ve seen before. This makes sure that the packages you will be downloading are the latest releases. The second command, upgrade, initiates a comparison of your current packages to the new package list. It will alert you to the total download size of the operation at which time you can type yes to confirm the download and installation of the updates. This also updates any applications that were installed using the package manager.
Remove a package from the Linux Operating system, but leave configuration files:
sudo apt-get remove libreoffice
Purge a package entirely; including all configuration files. This is useful when you require a clean install of an incorrectly configured application:
sudo apt-get purge libreoffice
Note: After running the purge command it is recommended to restart your system to ensure that any files that were in use can be erased.
CLI Text Editing
While Graphic User Interfaces (GUIs) make changing settings as easy as pushing buttons, many Linux machines do not have a Desktop Environment (Linux GUI) installed. Remote administration using the Secure Shell Protocol (SSH) is a very popular feature that requires the use of the command line. In many cases, it is actually faster to make configuration changes using the command line than opening windows and wielding your mouse like a fly swatter. That’s great, but how is it done?
The most commonly installed command line editors are the nano editor, vi, and vim (an improved version of vi). Most Linux installations should have one of those installed. It is recommended for most users to use the nano editor as vi/vim is much less intuitive with a steep learning curve, though it is also extremely powerful and worth researching.
To create a new file with the nano editor type:
This will open a console based text editor. You can type your desired text and then use the commands listed at the bottom of the screen to save, exit, etc… Create the MyFileName.txt file and enter the text “Hello World”. Save and exit.
If nano is not installed, you may install it using the apt-get knowledge you gained earlier:
sudo apt-get install nano
To open an existing file we can refer to the file we just created:
The nano editor should now open the same console editor and you should see your text; Hello World.
Backing Up Configuration Files Before Making Changes
Linux is primarily a series of text files that hold information and instructions to be referenced by the Operating System functions. These text files are called configuration (config) files. Whenever you edit a config file you should first make a backup of that file should something go wrong.
Let’s say you are trying to edit /etc/network/interfaces to add an additional Network Interface Card (NIC). If you configure something incorrectly, you may lose your ability to connect to the network. This is especially frustrating when you need to access the Internet to troubleshoot your new found issue. To solve this, simply back up the configuration file beforehand using the cp command. This way if something goes wrong you can simply copy the working configuration file back, which in our example would restore network and Internet access. Note that editing or copying system configuration files will require root or sudo privileges.
The cp command takes two arguments [destination]:
cp /source/file /destination/file
Make a backup copy of the file you wish to edit:
sudo cp /etc/network/interfaces /etc/network/interfaces-backup
The above command makes a copy of the current interfaces config file and saves it as interfaces-backup.
You can now edit the current interfaces config file without fear:
sudo nano /etc/network/interfaces
Once you have saved changes you may find that something went wrong and things are not functioning as expected.
To revert back to the working version of the config file we just reverse the source/destination arguments from the previous cp command:
sudo cp /etc/network/interfaces-backup /etc/network/interfaces
The above command makes a copy of the interfaces-backup file and saves it as interfaces, overwriting the modified interfaces file and restoring original functionality.
To shutdown a Linux machine you can use the following command with root or sudo privileges:
sudo shutdown -h now
To reboot a Linux machine you can use the following command:
sudo shutdown -r now
There are vast amounts of commands and techniques regarding Linux administration, but these few should give new comers a solid foundation for using the command line. If there are other commands you would like to see or feel I should add to this list please comment below.
Thank you for your support!
Abell, John C. (2010, Jan 9th). Apple Gets Graphic With Lisa. Retrieved from https://www.wired.com/2010/01/0119apple-unveils-lisa/
AGL. (2017, Mar 30th). Automotive Grade Linux Platform Debuts on the 2018 Toyota Camry. Retrieved from https://www.automotivelinux.org/announcements/2017/05/30/automotive-grade-linux-platform-debuts-on-the-2018-toyota-camry
Finley, Klint. (2016, Aug 6th). Linux Took Over the Web. Now, It’s Taking Over the World. Retrieved from https://www.wired.com/2016/08/linux-took-web-now-taking-world/
Helmke, Matthew. (2016). Ubuntu UNLEASHED 2016 Edition. 800 East 96th St, Indianapolis, Indiana 46240 USA: Pearson Education, Inc.