ls, cd, pwd, mkdir, cp, mv, rm, touch, and man, cat, find, grep, wget, unzip, echo, and input/output modificators: |, >, ».Exercise 1
Simple program to output text:
#include <stdio.h>
int main()
{
//this line is a comment, it will be ignored
printf("Hello World!");
return 0;
/*this is
multiline comment
*/
}
Practice compiling it:
clang myprogram.c
Clang will take your code and produce a binary executable from it. See if you can figure out:
How to customise your executable filename from clang? Make it format correctly in the terminal.
$\qquad\bullet\,$ Hint (click to view)
Exercise 2
We can use printf(“here is an integer: %i”, myint); to format an integer.
Similarly %f to format a float.
We can use scanf(“%f”, &fnum); to get a variable from the command line user.
Implement a function that calculates the Euclidean distance of a point $(a,b)$ from the origin $(0,0)$. That is, it takes two numbers ($a$ and $b$), squares each of them, adds them, and returns the square root.
sqrt() function from math.h library
-lm compilation flag
Exercise 3
To obtain access to command-line arguments, we must define main function with two parameters, usually named argc (argument count) and argv (argument vector):
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("The program got %d command-line arguments", argc);
return 0;
}
Compile the code and run it with three command-line arguments:
./myprogram hello cpl 2025
How many command-line arguments did the program get? Does it correspond with the number of arguments you gave to it?
Try to access the command-line arguments and print them to standard output (use %s to format a string).
Open terminal: Ctrl + Alt + T
New tab: Ctrl + Shift + T
End programs by running: Ctrl+C
Close by: Ctrl + D
Manual to (almost) every terminal program can be found using man:
$ man <program>
You shoud start working in linux terminal by asking, how to use manual
$ man man
To find out, where are you in the directory tree, the present working directory, use pwd:
$ pwd
To list files in your directory, use ls:
$ ls
There is also possibility to see a content of different directory
$ ls <relative/absolute address>
The useful parameters you should know are:
-a do not ignore entries starting with ., usualy hidden files,
-A the same as -a but it does not list implied . and ..,
-l uses a long listing format,
-p to append / at the end of the name of directories to differentiate them from files,
-S size, -t time, etc.,
$ ls -Al
To change the directory use cd:
$ cd <relative/absolute address>
Go to the parent directory
$ cd ..
Go to the parent's parent directory
$ cd ../../
Go to the parent's parent directory and then to its child directory
$ cd ../../some_directory/
Go to the child's child directory
$ some_directory/some_subdirectory/
You can use absolute address:
$ /home/my_account/my_files/my_video/
There is also trivial way to your home directory:
$ cd
The manual is not presented, therefore use parameter –help to find out more
$ cd --help
For fast navigation, use keypad Tab. Use it frequently.
If you want to create a directory, use mkdir:
$ mkdir <directory name>
and touch for file creation:
$ touch <file name>
Please, do not use spaces in your names:
$ mkdir "I have come here from some advertising environment called Windows" $ mkdir and\ I\ really\ like\ to\ troll
and if necessary, use _ underscore instead:
$ touch I_love_linux
Use mv for moving files or directories from one position in a directory tree to another:
$ mv <source> <destination> $ mv random_file.txt ~/random_directory/ $ mv random_directory/ ~/some_directory/some_subdirectory/
Note that character ~ stands for your home directory.
Use mv also for renaming the files:
$ mv obsolete_name much_better_name
Copying files can be done using cp:
$ cp <source> <destination> $ cp random_file.txt ~/random_directory/ $ cp random_file.txt ~/random_directory/better_name.txt
To copy whole directory, use -r recursive option:
$ cp -r random_directory/ ~/other_directory/some_subdirectory/
You can delete file using rm
$ rm <file name>
Beware! It is very hard to undo!
Deleting whole directory and its content needs -r recursive option:
$ rm -r obsolete_directory/
It is also possible to delete file (or directory) at different position in directory tree:
$ rm ~/random_directory/random_file.txt $ rm ../../some_system_directory/some_essential_file