Home Linux A Complete Essential Guide to Master The Linux Terminal

A Complete Essential Guide to Master The Linux Terminal

These days, in theory, there is no necessity to use the command line to run and maintain a Linux system: there are so many graphical tools available for most tasks. But when did we start doing only those things we absolutely had to?

The command line provides an alternative environment to the typical desktop GUI, with its own set of pros and cons. The main advantage is that if you know what you are doing, many tasks can be accomplished more efficiently in the command line.

That’s just for single tasks, if you want to do something to ten files in a GUI it will often take ten times as long, compared with an extra couple of seconds and menus until you find something that does what you want; the command line requires some prior knowledge. None of us was born with that, and we all continue to learn. So put on your best Nineties outfit, give yourself a bad nickname and get ready to look like a movie hacker.

Essential Guide to Master The Linux Terminal

Strat from Basics

ls
You can use ls to list the files and folders that are inside the directory you are in.

cd
The cd command enables you to move between directories on your system. Like the
following, for example:

$ cd /home/user/

cp

The copy command, or cp, can be used to copy files from one location to another. To do
this use the command below:

$ cp file /home/user/Desktop/file

mv
Similar to copy, mv instead moves the file to the new location, deleting the original file:

$ mv file /home/user/Desktop/file

rm
The rm command is for removing or deleting fi les and directories. You can use it like:
$ rm file

mkdir
You can create directories with the mkdir command using something like:
$ mkdir folder

nano
Nano is one of the programs that enables you to edit text in files – it’s vital for working and editing in the command line. You use it like so:
$ nano file

Tab
The Tab key lets you auto-complete commands and file names. Double-tapping
will list all objects with a similar name. Autocompletion works for unambiguous file and command names. For example, ‘fir’ gives you ‘firefox’ as no other commands begin with
‘fir’. If you get multiple hits, keep typing to narrow the selection then hit Tab again.

Up
Up on the keyboard has the very simple task of enabling you to pull up the last command that was entered, to run it again or edit it.

Copy text
If you’re in the terminal, you may want to copy text output to use somewhere. To do this, you can use: Ctrl+Alt+C.

Paste text
If you’ve found a command online or need to paste some text into nano, you can enter any text on the clipboard with Ctrl+Alt+V.

Open terminal
This trick works in a lot of desktop environments: a shortcut for opening the terminal can be done with: Ctrl+Alt+T.

sudo
This lets you do commands as the super user. The super user in this case is root and you just need to add sudo to the start of any command.

Access root
The super user, or root, can also be accessed by logging in as it from the terminal by typing su. You can enter the root user password and then run every command as root without having to use sudo at all.

Stop processes
If you have run a command and either it’s paused, taking too long to load, or it’s done its
job and you don’t need to see the rest of it, you can stop it forcefully by using either Ctrl+C or Ctrl+Z. Remember, only do this if the process is not writing any data, as it is not safe in this instance and you could find yourself in trouble.

Access root without password

If your regular user is in the sudoers list (ie so that they can use sudo, in general, to run things as root), you can access the su account by using sudo su. This uses your normal user password to access root terminal functions.

To add a regular user to the sudoers list, you would need to first log in as the superuser by using su. Then, you would simply run adduser username sudo (replacing ‘username’). Be careful who you add to the sudoers list, though!

Search for hidden files and directories

The ls command can be used in more than just the basic way of listing all the fi les in a
folder. To begin with, you can use it to list all the hidden items along with the normal items by using:
$ ls -a

Home directory shortcut

The home directory is located at /home/user/ in the absolute filesystem, but you can use
the tilde (~) to signify the home directory when moving between directories or copying, or doing mostly anything else – like with the following, for example:
$ cd ~

Link commands together

If you want to do a series of commands one after the other, like updating and then upgrading software in Debian, for example, you can use && to have a command run right after the one before.

For example:
$ sudo apt-get update && sudo apt-get install libreoffice

Long terminal input

Sometimes when using a list or anything else with a long terminal output, you might not be able to read it well. To make it easier to understand, you can send the output to another command, less, through the | pipe. It works like so:
$ ls | less

Readable storage size

One of the details that ls -l displays is the size of the files that are located on the hard drive. This is done in bytes though, which is not always that useful. You can have it parse this file to become more legible simply by changing the -l to -lh, where the long-listing format option (-l) is tweaked with the human-readable option (-h).

Move to the previous directory

If you want to move back to the directory that you were working on before, there is a cd command to do that easily. This one does not move up the filesystem, but rather back to the last folder that you were in:
$ cd –

Move up directory
The cd command can also be used to move up in the filesystem. Do this with two full stops, like so:
$ cd ..

General wildcards

The asterisk (*) can be used as a wildcard in the terminal to stand for anything. A typical use case is copying or removing specific types of files. For example, if you want to remove all PNG files from a directory, you cd to it and type:
$ rm *.png

More with the pipe

The pipe (|) can be used to feed all outputs into the next command, enabling you to call a piece of data or string from one command and put it straight into the next command. This works with grep and other core Linux tools.

Delete directories

Using rm on a directory with objects within it won’t work, as it needs to also delete the files inside. You can modify the rm command to delete everything within a directory recursively using:
$ rm -r directory

Shutdown command
You can shut down the system from the terminal using the shutdown command, the halt option (-h), which stops all running programs at the same time, and specifying a time of now so it turns off immediately rather than in 60 seconds:
$ sudo shutdown -h now

Display all information

As well as merely listing the files, we can use ls to list all of the information relating to each file, such as the last date modified, permissions and more. Do this by using:
$ ls -l

Reboot from command line

Back in the day, rebooting required a slightly more complex shutdown command: shutdown -r. In recent years it’s been replaced with a very simple:
$ sudo reboot

Timed shutdown

The timing function of the shutdown command can be very useful if you need to wait for
a program or cron job to finish before the shutdown occurs. You can use the time to do
a normal halt/shutdown, or even with -r for a reboot after ten minutes, with something like:
$ sudo shutdown -h +10

Log out

Logging out from the x session is generally advisable from the desktop environment, but if you need to log back out to the login manager, you can do this by restarting the display
manager. In the case of many Linux distros, you use the command below:
$ sudo service lightdm restart.

File management ( Learn the basics of working with files)

1.Listing files

ls -l Photos

Lists the contents of the specified directory, or the current one if none is given. The -l option enables long format with extra information. Files are sorted alphabetically; use -t to sort by date/time and -S to sort by size. -r reverses the sort order.

2.Changing directory

cd /some/path
The cd command changes the current directory to the given path, which may be relative or absolute. When run with no arguments, it changes to your home directory.
To go back to the previous directory, use cd –. To see where you are now, run pwd.

3.A more structured listing

tree ~
Shows the contents of your home directory (~ is a shortcut for home) in a tree layout. This makes it easy to see the relationship of files, but can get very long.
Use -L 3, for example, to restrict tree to only three levels of directories.

4.Copying a file

cp file1 file2
Copies file1 to file2; if file2 is a directory, file1 is copied into it. The -p switch preserves timestamps and permissions, otherwise, file2 will have the current time.
If file1 is a directory, use the -r option, or -a which combines -r and -p.

5.Moving a file

mv file1 file2
Moves, or renames, file1 to file2. Again, if file2 is a directory, it moves file1 into it. Note that ile1 can also be a directory. Add -i or -n to prevent it overwriting files of the same name.

6.Deleting files

rm file1 file2…
Deletes the given files – be very careful when using this with wildcards. If in doubt, add the -i option to ask for confirmation. You cannot remove directories with rm unless you add the -f and -r options – be careful with this!

7.Creating directories

mkdir ~/dir1/subdir1
Creates subdir1 inside dir1 in your home directory. This will fail if ~/dir1 does not exist or if subdir1 does exist. To avoid such errors and create a hierarchy of directories in one go, add the -p option.

8.Changing file ownerships…

chown user:group files
Changes the ownership of the given files to that user and group. If you omit the group, only the user is changed. If you include the : but not the group, the user’s default group is used. You normally need to be root to run this.

9.…and permissions

chmod a+r files
Sets the permissions of the files, a+r means set the read lag (+r) for all users, leaving all other lags unchanged. You can also use numeric options; the man page explains all the choices. You need to own the file, or be root, to change its permissions.

10.Matching directories

rsync -a dir1/ dir2/
Syncs the contents of two directories. All files in dir1 but not in dir2 (or different there) are copied.
To remove files from dir2 not in dir1, add –delete. Add -n to see what will be done without doing it.

Installation (Managing your packages and updating your system is a key part of the command line)

1.Debian: update repositories

Debian-based (and Ubuntu-based) distros use apt-get as the command line package manager. One of the quirks of apt-get as a package manager is that before upgrading or installing software, it does not check to see if there’s a newer version in the repositories. Before doing any installation in Debian, use:

$ sudo apt-get update

2. Debian: install software

Unlike a graphical package manager or software center, you can’t quite search for the kind of packages you want to install, so you need to know the package name before installing. Once you do though, try:

$ sudo apt-get install package

3.Debian: update software

You can upgrade the software in Debian from the terminal by first performing the repository update command in Tip 1, followed by the upgrade command below:

$ sudo apt-get upgrade

4. Debian: uninstall software

As part of package management, apt-get enables you to uninstall software as well. This
is simply done by replacing install with remove in the same command that you would use to install said package (Tip 2). You can also use purge instead of remove if you want to delete any config files along with it.

5.Debian: upgrade distro

Debian systems can often update to a ‘newer version’, especially when it’s rolling or if there’s a new Ubuntu. Sometimes the prompt won’t show up, so you can do it in the terminal with:

$ sudo apt-get dist-upgrade

6.Debian: multiple packages

A very simple thing you can do while installing on all platforms is list multiple packages to install at once with the normal installation command.
So in Debian it would be:

$ sudo apt-get install package1 package2 package3

7.Debian: dependencies

Compiling differs between software and they’ll each have a guide on how to go about it. One problem you might face is that it will stop until you can find and install the right dependency.
You can get around this by installing auto-apt and then using it during configuration with:

$ sudo auto-apt run ./configure

8.Debian: force install

Sometimes when installing software, apt-get will refuse to install if specific requirements
aren’t met (usually in terms of other packages needing to be installed for the software to work properly). You can force the package to install even without the dependencies using:

$ sudo apt-get download package
$ sudo dpkg -i package

9.Debian: install binary

In Tip 8, we used dpkg -i to install the binary installer package that we downloaded from the repositories. This same command can be used to install any downloaded binary, either from the repos or from a website.

You may also like to read these awesome articles

How to rename files in Linux

Why Linux is better than Windows