fbpx
Linux 101

11 Linux commands and 2 Bash loop structures you must know starting out as a Linux support engineer

So you want to become a Linux support engineer? Check out these very important commandline tools that you will need to know for working with files, processes and network streams.

ls

List files. Either in the current directory or a specified directory.

lsof

List open files. Everything in Linux is a file, even open network connections. lsof lists open file handles and you can specify which type of files you want to list.

cd

Change the current directory that you are in, or print the current directory.

find

Find files and directories in the filesystem, that matches a pattern or has attributes such as size/modification date.

mkdir

Create a directory.

rmdir

Remove a directory.

ps

Show the running processes, one process per line. One construction is ps aux.

top

Monitor the running processes with this interactive tool. It prints one process per line and updates in realtime with process statistics.

netstat

Print network connections, routing tables or statistics about your network interfaces.

traceroute

Trace a route over Internet to specific hosts.

ping

Check if a specific Internet node is up, by using the ICMP protocol.

while-do-done and for-do-done loops

When you are working with larger sets of data to input or output, you will find that you need to iterate over the data elements. It could be file names, processes or rows in text files. Regardless, you will need to write loops in Bash to run as scripts to iterate over all elements. Check out these two really useful loops, while-do-done that loops until a specific condition is met, and for-do-done which loops through a known set of elements/values.

while-do-done

The while-loop will check for a condition at the beginnin of the loop, so the condition will be evaluated once per loop, and in the beginning of the while-block.

while [condition]; do
command(s)..
done

for-do-done

The basic construct is for element in [list] where [list] is the set of elements to iterate through, and element is the temporary variable assigned to each list element as the loop steps through the list. Check out the following example, where we loop through listed files (one file per line, and only the filename):

for i in $(ls -1); do
echo $i
done

Naturally there are many more commands that are basic and that you need to know as a Linux support engineer. The selection above is absolutely crucial and a good starting point to build on. The same goes for the loop constructs, for instance the loop construct until-do-done is also important. But already with for-do-done and while-do-done you will be able to create very handy Bash scripts that automate daily tasks you encounter as a Linux engineer.