Regardless of the Linux distribution, you have chosen for your server, Debian / Ubuntu or CentOS, UNIX underneath the clothes will be very similar, just like the OS x of your Mac.
And that’s a good thing since it’s possible to compile a series of tips we can use to make our day-to-day administration of these systems more accessible.
The help command
Whenever you have questions about options for such command, type -help in front of the order (ex.:tail –help), and all options will be shown. If you need more help, go to the manual by typing Man(E.g., man tail).
Installing packages
Be apt-get or yum. When we want to install a yum package or apt-get install, we will be asked if “we are sure” that we want to install. You can skip this confirmation just by passing the option -y with the install:
Apt-get install -y Or yum install -y
It also works with removal.
Creating custom shortcuts
You must have some experience with UNIX and know that the command ls is responsible for listing files and directories. Still, it does not show hidden files or further information, such as permissions or which the file owner is.
We list the information more with the command ls -l, but if we want to see the hidden files, we also need the -a flag, getting ls -la.
Entering four letters and a hyphen may not seem like a big deal. But what if we reduce this effort to just two simple letters to be reached on the keyboard, as ll and it?
The alias command exists exactly for this. We can do it directly in the terminal:
Alias la = “ls -laG”
The only thing is that we will lose this customization when the terminal is restarted. We must add that line to the file if we want to keep it permanently. bash_profile (it’s loaded every time we open the terminal and apply the scripts that put it).
Edit the .bash_profile your user with vim ~ / .bash_profile and add the alias as it deems necessary.
A quick tip: If you type 1s, ll, or some other custom shortcut you created, it will list the contents of the current directory. But if you want to see the contents of any directory, without necessarily needing to enter it, type the command and then the directory of interest, for example:
Ll / var / www /
So I can see what’s in /var/www without leaving where I was.
The tail command
Administering a server is linked directly to understanding how it is behaving and how the applications running on it respond or return errors. Knowing how to read or debug an error log can significantly help you.
The command tail alone functions as the cat – shows only the right file content (except that the bottom is limited to only show the last lines of this file, while the cat plays for the entire output file).
The tail has an exciting option, which allows you to “listen” to the changes to a file, showing real-time any added content. Every time someone accesses your site, an entry is recorded in the access.log web server.
Tail -f /var/log/nginx/access.log
Take a test: leave a terminal tab with the above command and access any URL of your application. Instantly, the record of your visit will appear in the terminal.
Mingling
Remember I talked about interpreting a log well?
For this, we need to be able to locate information in our log.
We know that the cat shows all the correct file content on the screen, but what if we want to see the default just right? For example, in an Apache error file, we want to see only the errors that this IP saw?
We can do the following:
Cat /var/log/apache2/error.log | Grep “client 187.12.34.567”
Logically, changing the IP there.
So how can we do this with any part of the log line, as if I want to look at the error lines that have “mysql_num_rows ()”:
Cat /var/log/apache2/error.log | Grep “mysql_num_rows ()”
And an exciting tip here is to add to your .bash_profile one highlight for grep:
Alias grep = ‘grep –color = auto’
So when the grep finds the pattern you ordered, it will be shown “color” on the screen for easier viewing of what was sought.
Deleting file contents completely
Deleting a file is easy. Just use rm or rm -r to directories. But what if we want to delete only the contents of that file without deleting the file?
Imagine a log or a script of hundreds of lines. Entering the file, selecting everything with vim, and deleting it is not practical.
For this, it is much simpler to make one:
Echo -n>/var/file
With the echo -n is writing an “empty” without “new line” within the file. If we had the interest to write something, just put the string after the echo:
Echo “Any text whatsoever”>/var/file
So we write inside the file without even opening it with vim.
The> (more significant than sign) will replace everything in the file with the new string. If we want to add content instead of switching, we use two >>
Echo “Adding text” >> /var/file
The new text will be added after the last line of the file.
Creating directories
Okay, creating directories is simple, just one mkdir, But what if we need to develop multiple embedded guides?
Public/css/fonts
We would have to type
mkdir public
Cd public
mkdir css
Cd css
mkdir fonts
It isn’t enjoyable since every new child directory has to be inside his father before using the media again. Fortunately, there is the option -p, allowing us to create a complete tree with one command:
mkdir -p public/css/fonts
Deleting with recursive confirmation
I’m pretty sure you’ve already had to delete a directory. git, and be a hidden directory full of files that cannot be deleted directly, a simple
Rm -r .git
It causes you to confirm the deletion of dozens of files with a y (yes). This isn’t very pleasant! Thankfully there is a command “yes.”
Type yes to your terminal and print a y below the other endlessly until you stop running with Ctrl + C. This command fits perfectly in situations like the one I just described:
Yes | Rm -r .git
The .git directory will be deleted without any manual confirmation from us.
I am running multiple commands on a single line.
Often, we need to run multiple commands or move to a co-worker, and sending multiple lines is a bit time-consuming. We can “concatenate” commands to be executed with a single line using the && operator. If the previous command responds with something other than false, the next command will be executed. Example:
Cd/var/www/&& ll
It will enter the /var/www folder and then list its contents using the shortcut we created above.
Concluding
There are many other things to help you manage your cloud server, but we will cover them in the next post.