Regardless of the Linux distribution that you have chosen for your server to be, 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 easier.
The help command
Whenever you have questions about options for such command, type -help in front of the command (ex.:tail –help), and all options will be shown and, if you need more help, go to the manual by typing Man(E.g., man tail).
Be apt-get or yum, when we want to install a yum package, install or apt-get install, we will be asked if “we are sure” that we want to install. You can skip this confirmation just passing the option -y with the install:
Apt-get install -y Or yum install -y
It also works with remove.
Creating custom shortcuts
You must have some experience with UNIX and know that the command ls is responsible for listing files and directories, but it does not show hidden files or further information, such as permissions or who the owner of that file is.
With the command ls -l, we list the information more, but if we want to see the hidden files, 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 directly in the terminal:
Alias la = “ls -laG”
The only thing though is that we will lose this customization when the terminal is restarted. If we want to keep it permanently, we need to add that line to the file. 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.
Quick tip: If you just type 1s, ll or some other custom shortcut you created, 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 leave where I was.
The tail command
Administering a server is linked directly to understanding how it is behaving and how the applications that are running on it are responding or returning errors. Knowing how to read or debug an error log can be of great help to you.
The command tail alone functions as the cat – shows only the right file content (except that the tail is limited to only show the last lines of this file, while the cat plays for the entire output file).
The tail has a very interesting 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 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.
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 on the screen all the right file content, but what if we want to see 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 22.214.171.1247”
Logically, changing the IP there.
So how can we do this with any part of the log line, as if I just want to look at the error lines that have “mysql_num_rows ()”:
Cat /var/log/apache2/error.log | Grep “mysql_num_rows ()”
And an interesting tip here is to add to your .bash_profile one highlight for grep:
Alias grep = ‘grep –color = auto’
So when the grep find 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 is not practical at all.
For this, it is much simpler to make one:
With the echo -n are writing an “empty” without “new line” within the file. If we had the interest to actually 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> (greater 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.
Okay, create directories is simple, just one mkdir, But what if we need to create multiple embedded directories?
We would have to type
It is quite annoying since for every new child directory has to be inside his father before using the mkdir 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
Causes you to confirm the deletion of dozens of files with a y (yes). This is pretty annoying! Thankfully there is a command “yes”.
Just type yes to your terminal and will 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.
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. As long as the previous command responds something other than false, the next command will be executed. Example:
It will enter the /var/www folder, and then it will list the contents of it using the shortcut we created above.
There are still many other things to help you manage your cloud server, but we will cover them in the next post.
Originally posted on January 12, 2017 @ 6:35 pm