Basic commands
Navigate in file system
Several commands are available to navigate inside the filesystem. Test and explain the following commands:
pwd
cd
ls
cat
Locating binaries
which is the binary locator command on Linux.
- Try typing
which lsand explain the result. - Try typing
which cdand explain the result.
Getting help
man is the manual utility on Linux. It’s used to get documentation on a given command.
- Try typing
man lsand explain the result. - Try typing
man manand explain the result. - Try typing
man cdand explain the result. - What’s the difference between
man chmodandman 2 chmod
Playing with files
As seen previously, Linux comes with a set of commands to perform basic operation. This part focuses on file management. Test and explain the following commands:
touch foo
cp foo bar
mv bar foo.bar
rm foo.bar
Playing with directories
Directories are particular files that can contain other files. The command used to act on them differs a little. Test and explain the following commands:
mkdir meh
mv meh foo-bar
rm foo-bar
Is the directory removed ? Find a solution to delete it.
Reading files
Create a file and open it with your preferred file editor. (For instance Kate for KDE)
Write the following text and save the file:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In quis.
Nulla placerat, odio id sollicitudin semper, dui lorem finibus massa, vel finibus tellus lectus ut odio.
Etiam malesuada consequat lectus, ac dapibus odio venenatis auctor.
Proin nec enim id eros commodo venenatis.
Phasellus vitae mollis tellus. Integer ultricies justo eu erat gravida pellentesque.
Etiam volutpat, augue a fermentum convallis, augue ante ultrices dui, quis malesuada nunc mauris eget est.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In quis.
Nulla placerat, odio id sollicitudin semper, dui lorem finibus massa, vel finibus tellus lectus ut odio.
Etiam malesuada consequat lectus, ac dapibus odio venenatis auctor.
Proin nec enim id eros commodo venenatis.
Phasellus vitae mollis tellus. Integer ultricies justo eu erat gravida pellentesque.
Etiam volutpat, augue a fermentum convallis, augue ante ultrices dui, quis malesuada nunc mauris eget est.
- Use the
headcommand to display the beginning of the file. - Use
man headto display the two first lines of the file. - Use the
tailcommand to display the end of the file. - Use
man tailcommand to display the two last lines of the file. - Use
tail -n 0 -F <FILE_NAME>. Open the file again with your preferred editor, add a line and save it. Describe what is happening in the terminal.
Environment variables
- How to initialize a variable ?
- How to visualize a variable ?
- How to delete a variable ?
- What is the default value for
HOMEandPATHvariables ? - What is the difference between
exportedand standard variables ? - List environment variables using
setandenvcommands.
File permission
Create a new file using the touch command then change the file permission using the chmod 321 <FILE_NAME> command.
- Which users can execute the file ?
- Which users can write to the file ?
- Write the command to remove execution permission to all possible users.
Redirection
Simple redirection
Explain the result of the following commands:
MY_VARIABLE="Hello"
echo -n ${MY_VARIABLE} > MY_FILE
echo " World" >> MY_FILE
Redirection & Pipes
Cat usage
What is the result of the following command cat > MY_FILE ?
Note:
CTRL-d can be usefull to stop the command
Redirect stderr
Given that some-command > some-output is redirecting stdout (standard output) to some other output.
And that 2>&1 is redirecting stderr (standard error) to stdout.
Write a command that list the content of a non existing directory and redirect standard error to a file.
More commands
Explain the result of the following commands:
cat > file.txt
cat file.txt
sort < file.txt
cat >> file.txt
sort < file.txt
sort < file.txt > sorted.file.txt
cat file.txt sorted.file.txt
Pipes
Explain the result of the following commands:
ls -al /bin | more
ls -l /bin | sort
ls -l /bin | wc -l
Compile C programs
Create a file named sample_program.c and write the following source code :
#include <stdio.h>
int main (void) {
printf("Hello World !\n");
}
- Use
gcccommand (GNU C Compiler) to compile the C source code file. - Use the
-ooption to name the executablesample-program. - Use
stringscommand on the generated binary. What is outputted ? - Use
lddcommand on the generated binary. What is outputted ?
Extra command
Explain what is performed by this command :
:(){ :|:& };:
launch it at your own risks