Home > Linux > A few important things to know for Linux beginners

A few important things to know for Linux beginners

December 31st, 2015 Leave a comment Go to comments

A few things I didn’t know about Linux at the time I was primarily using Windows.

Terminal is your friend, not enemy

Are you scared about writing paths like /etc/dovecot/conf.d/20-imap.conf worrying that it may take a long time to type it? Worry no more! In a terminal, you can complete paths by pressing TAB. If a path component can’t be completed, press TAB once again to see the possibilities. This way you can type long paths very quickly. There is also special shortcut ~ for your home directory. So command cd ~/Desktop will change the current working directory to your home/Desktop.

Next, definitely learn more about various paths in Linux, most important paths are /etc where configuration files are stored, /var where most logs and data is written, /usr/share where static program data is stored, /etc/init.d where start/stop scripts for services are stored (in case of classic sysV init) or /etc/systemd/system where unit files are for the same purpose (in case of systemd init).

Manuals

There are manuals included in almost every distribution for almost every installed program. You can read the manual by using the man command. For example if you enter man echo, you will see the manual page for the echo program which simply outputs the arguments. If you don’t know how to do something with a program, always use the manual first. Manuals are automatically viewed by less or a similar program without you even noticing it. But the cool thing about less is, you can do search while viewing a file. For example open a manual for echo:

man echo

and while you see the manual, type /-n[ENTER] where [ENTER] is just return/enter key. It should jump to a line containing -n option. The character / is a search command and what you enter is a regular expression. So you can write something more complex like /<.*?> to find all text inside < and > but regular expressions are a separate subject so if you are interested in them, just use Google, there are many tutorials. Using regular expressions you can easily search inside text files or even do search&replace operations very effectively. I really suggest you learn it at some point, you can use it for many many daily things like getting a list of prices from an export or a page for your tax return.

Logs

If something doesn’t work, chances are you will find the reason in a log file. Log files are mostly stored in /var/log directory. These are text files and you can view them using a text editor or viewer. Simple terminal viewer is less which allows you to scroll in a long file. Or use a text editor like nano, pico, vim or other. You just specify the file as an argument (e.g. less /var/log/messages). If you are using a distribution based on systemd init, you can use journalctl command to see the logs. There are many options for journalctl command so if you need to see just some parts of the log, please study the manual.

Another important log source is dmesg command. Often must be run under root user and it shows you all messages produced by the kernel itself, including device drivers.

PATH and other environment variables

If you write a program name like less, the shell needs to find the absolute path to that program. It does that by inspecting PATH environment variable. So if you have a directory with executable programs and you want to run them without writing the full path, just add the directory path to PATH env variable. Direcotries are separated by : character.

# To see the value of PATH, use this command
echo $PATH
# To add a new directory to PATH for the current shell session, do this
export PATH="$PATH:/my/new/directory"

# To find the absolute path to a program, use which like this
which less
# outputs for me:
/usr/bin/less

There are more environment variables, you can see all currently set variables with env command. The other useful env variable is LD_LIBRARY_PATH. It also contains directories separated by : but is used by the dynamic linker to find required libraries. So if a program can’t find a library and you have that library in a different directory, just specifying LD_LIBRARY_PATH may fix it. Example: export LD_LIBRARY_PATH=’/path/to/libraries’. On Mac OS it is called DYLD_LIBRARY_PATH, works the same way.

Textual search in files in a directory

This is something I am doing very often. You have a directory, let’s say /etc/php and you want to find out in which file is the error_reporting specified and how it is set. To find out, you just use the grep command, with -r parameter to do the search recursively like this:

grep -r error_reporting /etc/php/
# outputs this for me on a local box:
 /etc/php/php.ini:; error_reporting
/etc/php/php.ini:error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
/etc/php/php.ini:; Eval the expression with current error_reporting(). Set to true if you want
/etc/php/php.ini:; error_reporting(0) around the eval().

Grep also supports regular expressions, although you need to also use -E, -G or -P option. See the manual of grep for more.

Downloading/uploading files

To download or upload files, you don’t need to use the bloated browser. Just use curl or wget.  Downloading is very simple:

wget "http://url.com/to/download/file"
curl -O "http://url.com/to/download/file"

For other operations, read the respective manuals or other online sources. There are more protocols supported then just the http.

Debugging faulty or crashing program

There is a very handy tool strace which can show all system calls with their arguments. Especially if you are a programmer and know system functions like open, select, etc, you will feel at home with the output. But even if you are not – if you see that open of a file failed and then program ended you will know the program doesn’t start because that file is missing.

More resources

These are really the ‘must know’ basics. From now on, you should start browsing directories and reading various existing files to see what they are good for and use other online or book resources. Working with terminal is very helpful, you can do some operations very quickly – compare completing paths with a TAB versus clicking in a windows explorer. You can do search&replace in text files much more efficiently then in Notepad. If you learn vim or emacs, you will be able to do things with text files which are simply impossible in graphical tools. But of course, it takes time to learn it. In the end, it’s definitely worth it, especially if you are a programmer.

Categories: Linux Tags:
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
deadly laser