4 min read

Unix basics

As previously said, this post will cover basic Unix commands. Bear in mind that others such as pwd or echo, or the use of wildcards, have all been addressed in this post.

NOTE: If you want to dig deeper, you can explore each of the following commands by typing <your_command> man. This will print the manual of the order. To exit the manual, type :q and then hit enter.

In short, the manual will teach you (1) what is the command for, (2) how it should be used, and (3) optional flags to get the most of it.

Here you have a pretty exhaustive Unix cheat sheet.

Without further ado, here it is, usage of basic Unix commands.

[1] ls will “list” the content of a given directory. You will see the content coloured according to whether these are compressed files, executable files, or folders.

Usage: ls <your_folder>, or just ls inside a folder.

You can exclusively list folders if you want by adding the -d flag (this is, an optional feature).

Also, you can print in a text file the output from ls to further use in loops. To do so, type ls -d *struc > list.txt. This will print all your folders ending in “struc” (e.g., “subj01_struc”, “subj02_struc”, and so on).

[2] cd is for “change directory”. Basically, type cd <your_folder> to get there. If you want to move to the parent directory (the one containing the folder you are currently at) type cd .. instead. Finally, typing cd - will return you to the last directory you were positioned.

[3] mv is… complicated. Mainly, it is used to move files, but also has the potential to rename them. If you want to move a file, let’s say the “list.txt” you have created earlier to your Desktop, then type mv list.txt ~/Desktop/. HEADS UP. Be sure to end the second argument (the path to where you are moving the files to) with a slash (/); otherwise you will rename the file.

If it is between your interest to rename this “list.txt” as “list2.txt” (although there is a more powerful command to do so) you can do it as exhibited below.

mv list.txt list2.txt, and voilĂ !

[4] cp stands for “copy-paste” (d’oh). Its usage is basically the same as with mv (e.g., cp list.txt ~/Desktop/).

[5] rm or remove. Use it with care. There is nothing like a trash-bin here. When removing things through the command-line you won’t be able to restore them.

Type rm <your_file> to delete this file for good.

To remove a folder, type rm -R <your_folder> instead.

[6] mkdir is for creating folders. Just type mkdir <your_folder> to create a new one. If you need to create a folder containing others (such an inception thing to do) then add the -p flag.

For instance, mkdir -p <your_folder/folder1> will create a folder (“folder1”) inside a folder (“your_folder”).

[7] Other commands.

  • cat will concatenate files in the Terminal (or as an output, if specified). It is also used as a viewer, although the command vi is more appropiate to do so (remember to exit the view-mode by typing :q and hitting enter).

  • awk is designed for data extraction and manipulation. Its usage is more complicated than the others because it requires the use of pipes (i.e., “|”). Briefly, a pipe is used when you want the output from a first command be the input for a second one.

    So, let’s say you want to print just a column from a text file containing values of your interest (e.g., mean cortical thickness of a significant cluster). Here, typing cat file.txt | awk '{print $1}' will do that for you. Thus, awk will digest the output from cat to show you exclusively the content of the first column.

    Here the dollar sign ($) refers to a variable or an element. In this case, $1 indicates that the first column is the one we want to print in the Terminal. If you want to perform mathematical operations between columns, you can. Thus, cat file.txt | awk '{print $1 / $2}' will display the result of dividing column 1 by column 2.

  • locate will help you find a specific file. Type locate list.txt and it will print the path in which your file is at.

  • tar and unzip are commonly used to uncompress “.tar” and “.zip” files.

  • rename. This command is way more powerful than mv in renaming files. However, it is tricky enough to deserve an entry itself.

  • apt-get, upgrade and update. The first one will install packages or libraries (e.g., apt-get <your_package_name>). The others will upgrade and update libraries and repositories.

  • chmod. It gives permissions to write files or folders. You will need to use this one regularly. For more details, please visit this link.

  • sudo. Super User DO. It will give you the power, as an administrator, to run whatever you want in the computer without restrictions. To do so, use it as a start in every order (e.g., sudo rm <your_file>). It will ask you for your pass. NOTE: commands like apt-get or chmod will potentially require the use of sudo.