Linux CLI (Command Line Interface) primer for beginners

in #tutorial6 years ago (edited)

This tutorial assumes that your shell is bash. If you’re using a modern linux distro such as Ubuntu 16.04 it will be unless you change it.

Screenshot 2018-02-12 22.29.14.png

15 Important commands briefly covered here:

pwd - Print Working Directory (where am I in the filesystem)
ls - Directory Listing
cd - Change Directory
mkdir - Make Directory
rmdir - Remove Directory
rm - Remove files
man - Manual pages
nano - A simple text editor
cat - Output files to screen (actually for concatenating files)
more - Display files one page at a time
less - Display files one page at a time with navigation
head - Display the start of a file
tail - Display the end of a file
apt - Package Manager (think App Store)
sudo - Superuser

Once logged in and at a linux command prompt you will be greeted with something that looks like your username, an ampersand, your machine name, a colon, your location in the filesystem and a dollar $ sign (# hash if you’re root). This means that you’re ready to start.
myusername@mylinuxbox:~$

The ~ may not seem like a location in the filesystem, that is because it represents your home folder, in this example that would be /home/myusername/. Typing the command pwd and pressing [enter] will confirm where you are in the filesystem.

One more handy tip before I start is the [tab] key will attempt to autocomplete anything that you have started typing, using it can reduce the number of typo’s.

ls - Directory Listing

To get a listing of the files and directories in the current folder the command is ls. Like most commands in linux, you can pass parameters to the command. For instance, if I type ls -latr I get a directory listing in (l)ong form, (a)ll files including invisible, (t)ime/date ordered and in (r)everse so that the newest is last. Your distro may include convenient alias’s to the command with popular parameters already set, such as ll forls -alF, la forls -A, and l for ls -CF.

You can get a manual for any command by typing man [command], in this case man ls will get you a list of what parameters are available and what they do. Press h for help navigating man and q to quit back to the CLI.

cd - Changing Directory

You can change directory by typing cd [directory name]. The directory name can either be relative or absolute, by that I mean that you can either cd Desktop while you are in your home folder and can see the Desktop folder when you type ls or you can type cd /home/myusername/Desktop from anywhere in the filesystem. Starting with a / means that you are starting from the root directory.

When view the result of ls, you will notice a directory name just . (dot) and another named .. (dot dot), . represents the current directory and .. represents the parent of the current directory. To go up one level, type cd ..

mkdir - Create a Directory

To create a directory the command is mkdir [directory name], eg. mkdir MyDirectory. It is important to note that some linux filesystems will be case sensitive so Desktop is not desktop and MyDirectory is not mydirectory.

rmdir - Delete a Directory

Removing an empty directory can be done with rmdir [directory name] if the directory is not empty you will need to use the rm -R command instead , more on that next. rmdir MyDirectory will delete that directory just so long as it is empty, that includes invisible files or folders but does not include the . and .. they cannot be deleted.

rm - Delete Files

Deleting files is done with the rm command. Take great caution with this command in linux. There is no recycle bin, what is deleted is difficult to recover and it is very easy to delete the entire operating system with a single command (never type sudo rm -RF / unless you know exactly what you’re doing). To delete a file named myfile.txt in the current directory you would just type rm myfile.txt at the prompt. If the file is somewhere else in the filesystem you can use the full path like this: rm /user/myusername/myfile.txt to delete it. If you wish to delete a directory you will need to use the -R parameter that means delete recursively so rm -R MyDirectory will delete your directory and everything inside.

nano - Text Editor

My preferred text editor is Nano. There are many in linux, nano is one of the simplest, vi or vim are among the most powerful. To edit a text file is as simple as typing nano myfile.txt or nano /home/myusername/myfile.txt, if the file does not already exist, it will be created. Nano has help at the bottom of the screen. Once you’re done editing press [ctrl]+x to exit and you will be asked if you would like to save and to confirm the filename. Other handy commands in nano are [ctrl]+w to find files and [ctrl]+_ to go directly to a specific line number.

cat - Display files

You can look at a file without opening it in a text editor by using the cat, more, less, head or tail commands. cat myfile.txt will output the content of the entire file to the screen, even if it is thousands of lines long. more myfile.txt will show one page at a time, spacebar jumps to the next page. less myfile.txt is pretty much the same but you can navigate up and down with page up and page down. head myfile.txt displays the first 10 lines and tail myfile.txt the last 10 lines.

sudo - Superuser/root user

Some commands and access to some parts of the filesystem requires you to be root user. It is not recommended to log in as the root user, instead, prefix any command that is required to be run by a superuser with the command sudo. Using the sudo command will require your account to be a sudoer. When you run the a command with sudo, you will need to re-enter your user password. There is a lot to know about privileges and ownership of files in linux. Many issues are a result of insufficient privileges to access a file because something was incorrectly created by the root user and users or applications do not have the access to the file that is required.

apt - Install Software

Finally (for this guide) there is apt. Apt is a ‘package manager’, you can think of it as the App Store. Root privileges are required to install software so you should preface apt with the sudo command. To install software such as nano if it is not already installed is as simple as typing sudo apt install nano, if you want to search for a software package named ‘curl’ you can type apt search curl to see if there are any packages with ‘curl’ in the name. Keeping your software up-to-date is done by typing sudo apt update to update your sources, then sudo apt upgrade to install the new software, you can run two commands on one line by using the ampersand sudo apt update && sudo apt upgrade. The double ampersand will let the first command finish before running the next command. Beware updates to one package can break another package, apt is good at keeping track of this for you.

Coin Marketplace

STEEM 0.20
TRX 0.13
JST 0.030
BTC 65359.95
ETH 3492.90
USDT 1.00
SBD 2.51