At semanti.ca we use Linux a lot and we are big fans of this operating system. Below is the list of Linux commands that we use on the daily basis to build and maintain our AI systems.

cd

The cd (for "change directory") command lets you navigate to any directory on the Linux filesystem.

This command is probably the most important one to know.

Examples:

$ cd ~

The above command will move you to your home directory.

$ cd /etc

The above command will move you to the "/etc" directory.

$ cd /etc/asl

The above command will move you to the "/etc/asl" directory.

$ cd ..

The above command will move you one level up in the directory hierarchy.

$ cd ../..

ls

The ls command will show you the contents of the current directory you are in.

If you call ls with the -l flag it will show the files and directories with additional information: date they were created, who is the owner, size, and the rights (reading/writing/executing).

$ ls -l

If you want to list only subdirectories of the current directory, you can type:

ls -d */

cp

This command will copy your file from its current directory to another directory. The format is cp path_to_file path_to_the_target_directiry:

$ cp ~/my_data/tasks.xls ~/my_archive/

pwd

The pwd command will show you the path to the current directory you are in:

$ pwd

rm

The rm command removes the given file or directory. To remove the file you have to provide the path to the file:

$ rm ~/my_data/tasks.xls

To remove the directory you have to provide the path to the directory and use the flag -r:

$ rm -r ~/my_data

Another way to remove an empty directory is to use the command rmdir:

$ rmdir ~/my_empty_dir

mv

This command will move a file or a directory to another location. You can also use it to rename a file or a directory:

The following command will move the file tasks.xls from ~/my_data to ~/my_archive:

$ mv ~/my_data/tasks.xls ~/my_archive/

The following command will rename the file tasks.xls into old_tasks.xls:

$ mv ~/my_data/tasks.xls ~/my_data/old_tasks.xls

mkdir

The mkdir command creates a directory.

$ mkdir data

The above command will create the directory data in your current location (remember that you can know your current location by typing pwd).

$ mkdir /tmp/data

will create the empty directory data in the directory /tmp

touch

The touch command will create an empty file with a given name:

$ touch /tmp/data/requests.log

The above command will create the file with name requests.log in the directory /tmp/data.

ifconfig

This command will show your current network configuration. It contains useful information like gateway, IP, and packet statistics. Use this command to get your own IP address on the local network:

$ ifconfig

locate

The locate command will find files or directories that match the pattern:

$ locate requests.log

ping

The ping command applied to a hostname or an IP address will show you if the host or the IP is accessible from your machine over the network:

$ ping www.google.com

If you have lost packets during the ping command execution, this means that the access to the remote server from your machine has problems.

chmod

This command modifies the access rights to some directories or files. One important use case for chmod is to make some file executable. By default, Linux doesn't know whether some file can be executed or not. You can tell Linux that your script filename.sh is executable like follows:

$ chmod +x filename.sh

Now you can execute your script like this:

$ ./filename.sh

assuming the script is located in your current directory.

top

The top command will show you the currently running processes, how much CPU and memory they consume in real time.

ps

Another way to see all running processes, and only the top ones, is ps:

$ ps aux

grep

This command will analyze the input text and find lines that match a regular expression. One important use case is to find the line in the list of running processes that corresponding to a specific process:

$ pas aux | grep python

The above command will show you the information about all running python processes.

tar/zip/gzip/xz

These utilities will allow you to archive and unarchive files and directories:

tar allows you to archive and unarchive files and directories.

gzip/xz allows you to compress files.

gunzip/unxz allows you to decompress files.

zip/7z allows you to archive and compress or decompress and unarchive files and directories at once.

nano/pico

These commands will open file editors that novices can easily use:

$ nano /tmp/data/requests.log

If you want to become an advanced user of Linux, you can also try vi and emacs editors which more powerful but have a steep learning curve.

kill

Once you identified the running process using ps aux | grep python you can kill the process by giving its id (PID) as input to the kill command:

$ kill -9 PID

In the above command you have to replace "PID" with the actual id.

killall is another command allowing killing processes. It kills all processes that match a given keyword:

$ killall python

Be careful when using killall, as you can unintentionally kill some processes you would not want to kill.

clear

This command will clear your terminal window.

cat

The cat command will output the content of any file:

cat /tmp/data/requests.log

head

Similarly to cat, the head command will output the content of any file, however, you can specify how many lines you want to see:

head -15 /tmp/data/requests.log

The above command will output the first 15 lines of the file /tmp/data/requests.log.

tail

Similarly to head, the tail command will output the content of any file, however, it will output the given number of the lines on the bottom of the file:

tail -15 /tmp/data/requests.log

The above command will output the last 15 lines of the file /tmp/data/requests.log.

One important use case of the tail command is with the option -f. It will show the last lines of the given file and update them automatically (live) when the file changes:

tail -f /tmp/data/requests.log

less

The less command will allow you to inspect a file page by page or line by line:

less /tmp/data/requests.log

awk/sed

Both awk and sed command gives you the opportunity to write complex text processing pipelines in the command line. Please refer to the following user guides to learn about them: awk, sed.

history

The history command will show you the history of your previously executed commands.

ssh

The ssh command allows you connecting to another UNIX machine using the command line:

In the above command, the username john has to exist on the machine 192.168.0.1.

scp

The scp command combines cp and ssh: it allows copying files from one Linux machine to another. For example, you can copy files from your local machine to a remote machine like this:

The above command will copy the file data.txt that is located on your local machine to the directory my/remote/path located in john's home directory on the remote machine.

reboot

The above command will reboot your Linux machine.

crontab -e

cron is a utility that allows tasks to be automatically run in the background at regular intervals by the cron daemon. crontab (for "cron table") is a file that contains the schedule of cron entries to be run and at specified times. crontab -e allows editing the crontab file.

For example, if you want to run some task (a command or a bash script) twice an hour on minute 5 and 25, you can add the following line to crontab:

5,25 * * * * rm /home/someuser/tmp/*

The above will clear the directory /home/someuser/tmp twice an hour on minute 5 and 25.

sudo

In some cases, certain locations are restricted to the users with superpowers. Such users are usually root or belong to the list of sudoers. The system administrator has to give your user the sudoers privileges. Otherwise, you cannot use the sudo command.

For example, if you cannot create a directory using mkdir, you can prepend it with sudo and it will work if you are a sudoer or root:

sudo mkdir archive

sudo can prepend any restricted command.

wget and curl

The wget command will download any file from the internet:

wget http://fly.cc.fer.hr/jpg/flyweb.jpg

The above command will download the file and save it in the current directory.

curl will download a URL and print its source HTML code to the console:

curl https://www.google.com

apt-get, yum, dnf or pacman

Those commands, depending on your Linux distribution, allows installing Linux packages (applications). On Ubuntu, apt-get is used:

$ sudo apt-get update $ sudo apt-get install python3.6

The above commands will install Python 3.6 on your system.

Please, let us know if you don't see some important Linux command on this list and we will update it.

!!

The !! command will execute the previously executed command. For example, if you typed a long command and it failed because you have to be sudo to execute it, you can repeat the previous commands as sudoer like this:

$ sudo !!

man

Type man followed by any command and you will find the description of this command and its options:

man ls

Hotkeys

tab will complete commands and locations for you so you don't need to type them completely.

ctrl-c or ctrl-x will stop the execution of any command.

ctrl-r will allow searching in the history of previously executed commands.

ctrl-z will stop a currently executing command. It is useful if you want to send the command execution to the background and continue using the command line.

After pressing ctrl-z, type bg on the command line to run it in the background.

Then type:

disown -h [job-spec]

where [job-spec] is the job number (like %1 for the first running job) so that the job isn't killed when the terminal closes. you can find the exact job number by typing jobs in the command line.

Read our previous post "How to Prepare for a Machine Learning Interview" or subscribe to our RSS feed.

Found a mistyping or an inconsistency in the text? Let us know and we will improve it.