For many, the command line interface is something viewed with much dread and aversion. It seems like something only those with computer science degrees would consider using. However, the command line interface (or CLI), is an extremely powerful tool. CLI skills can speed up your workflow. Many tools not available on a computer’s graphical interface are available on the CLI.
While there’s some truth that one can do serious damage to a computer by recklessly inputting commands, there is much value in knowing your way around the command line. For one thing, if you are using a Unix-like operating system (such as a Mac or Linux computer), most of these commands will be universal. Additionally, pretty much every web server in existence uses some flavor of Linux, so these commands will apply there too. Knowing how to work with a server on the command line is often a basic prerequisite for doing any sort of back-end development.
The full list of Unix commands, with all its options is quite long and potentially intimidating to the novice. There are however, only a handful of commands you would need to know to make use of the CLI in an effective way. This post will outline the most common and useful commands that one would likely use on a regular basis.
ls – List Files
You can use
ls to list the files in the current directory (folder) you are in.
There are a few options available to change what information
ls displays.This is possible by adding the command’s optional “flags”.
If you type
ls -a, this will display ALL files in a current directory, even hidden files such as dot files.
ls -l will display the list in long-form. Additional information includes date created, date modified, read/write permissions, file size, etc.
ls -t will sort the files in the directory by date last modified, and
ls -S will sort the files by file size.
cd .. – Change Directories
To change directories, you would simply type
cd followed by the pathway you are trying to navigate to. For example, to go up one directory from where you are currently are, type
.. is the universal symbol for going up one directory. For example, if your file path is “Desktop/directory1/directory2”, you would type
cd .. . To get back to the
directory2, you would type
The examples above use relative pathways. This is the relationship of the target directory with the current directory. However, you can also navigate using the full path. To navigate to
Desktop from any directory, simply type
To make a new directory, type
mkdir <directoryname> , where
<directoryname> is the name of the folder you want to create. This will create a new empty directory wherever you currently are.
Similarly, to remove a directory, you would just need to type
touch is a command you can use to create a brand new empty file, with no content or file type. For example,
touch newfile will just create a new empty file entitled
You can remove files by typing
You can also remove all files in a current directory by adding an asterisk to the command, for example,
If you would like to remove a folder, including all subdirectories and files inside, you would need to use the recursive option
-r , for example
rm -r mydirectory.
One needs to be very careful with the
rm command, as it is not reversible. It is different from deleting a file and then having it in your trash bin before permanently deleting. Once you remove a file is removed with the
rm command, it is totally gone.
To copy a file, you can do so by typing
cp <filename> <filename2>. This will copy
<filename> to a new file,
You can also use this command to copy a directory. You will need to use the recursive
-r flag to also copy all files and subdirectories. For example,
cp -r <directory1> <directory2>.
To move a file or directory, you would use the
mv command. Typing the command
mv <filename> .. would move
<filename> up one directory.
You can also use this command to rename a file. Similar to
cp, you would type
mv <filename> <filename2>. The only difference here is that it just renames the single file, instead of creating a copy.
Nano is a text editor that comes pre-installed with most Unix-like systems. To open up a text file that directly on the command line, simply type
Nano is a fairly simple text editor, recommended for beginners to the command line interface.Once one is comfortable with Nano,
vim (“vi improved”) are far more powerful text editors available for the CLI. Either
vim typically come pre-installed on most *nix operating systems. Otherwise be manually installed if they are not.
If you would like to display the contents of a file without editing, you can use either
less. All of these commands have their advantages and disadvantages.
cat typically works well for smaller files.
less are appropriate for longer files, with the option to display the file one screen at a time. Hitting spacebar will display another screen’s worth of the file.
sudo is one of the most important commands to know. We’ve waited to explain it, as it is helpful to have some context to understand its purpose.
Every *nix operating system is setup for multiple users, each with different levels of permissions.Permissions specify what a user can and cannot view, edit or remove. If any user of a given system has complete permissions to do anything, this can potentially wreak havoc — modifying or deleting important files could cause irreversible damage, only rectified by wiping the hard drive (or server) clean, and starting from scratch.
The purpose of the
sudo command is to temporarily invoke root user (also known as the super user) permissions, allowing full permissions for any file on the system.
Attempting to view a protected file or directory with just a regular user account will pull up the common “Permission denied” message. The only way to access a file like this is to temporarily switch to the root user.
As an example, say you are trying to open a file named “protected-file.txt” that is set to only have view permissions for the root user (for example opening the file in the vim text editor), typing
vim protected-file.txt will simply pull up the
Permission denied message.
The only way to get around this (aside from changing the user permissions of the file via the
chown command), is by prepending the
sudo command before the rest of the command you initially tried:
sudo vim protected-file.txt
After entering that command, you will be prompted to enter the password for the root user. Hit enter, and you’ll finally be able to view the file!
There is a small window of time (typically about 1 minute) where you can continue to enter commands requiring root user permissions, without having to re-enter the
sudo command. After that times out, you will have to use the
sudo command again.
Aside from accessing files you may otherwise not have access to, this can be seen as an extremely useful security measure. Remaining in a user account other than the root user should be a common practice (even for advanced users), as it helps prevent the accidental execution of a command you may have not really intended for. Although it is possible to set your user type as the root user by default, this is rarely recommended to help prevent any serious accidents from having.
Sometimes it’s easy to forget to add
sudo before a command that requires it. To avoid having to re-enter the entire command again with
sudo prepended to it, you can simply type
sudo !! after receiving the
Permission denied. This will execute the previous command you entered, but in root user mode.