Skip to content

Documentation

References

Lien format language
introduction à Linux text french, english
Introduction to Linux (LFS101x) online course english

Command line

Linux commands are instructions that allow the user to control and communicate with the Linux operating system. In this section, you will find some of the most commonly used commands.

Directory

  • pwd: Displays the directory we are currently in.
  • mkdir project1: Creates the project1 directory.
  • rmdir project1: Deletes the project1 directory. It must be empty.
  • cd projet1: Allows us to change to the project1 directory.
  • cd ..: Allows us to go to the parent directory of our current directory.
  • ls: This command allows us to see files and directories. This command has many options, including -a to see hidden files, -l to see details including permissions, and -R to list recursively.

Files

  • touch file.txt: Creates en empty file or update modification time if the file already exists.
  • cat fichier1.txt fichier2.txt: display in the terminal the concatenation of the 2 files. We can use it with 1 file to display it in the terminal, often for a quick look at small files.
  • less file.txt: View a file one page at a time.
  • cp fichier1.txt fichier1.txt.backup: Copy a file. Option -r can be use to recursively copy a directory.
  • mv fichier1.txt fichier2.txt: Move or rename a file.
  • rm file.txt: Remove a file
  • head file.txt: Display the first 10 lines of the file.fichier.
  • tail file.txt: Display the last 10 lines of a file.

Disk space

  • df -h ~: To see the space for your account.
  • ncdu ~: To get space used by directory. You can enter directories for more details.

Permissions

We can view file permissions with ls -l file.txt.

Permissions are in 3 parts: - owner: The owner of the file - group: People that are in the same group - others: Users that are not the owner or in the group

There's 3 permissions you can have: - r: can read the file - w: can write in the file - x: can execute the file. In the case of a directory, this allows someone to enter it.

The chmod program allows you to change the permissions.

In this example, everyone can read and execute the file. We will restrict the permissions to only the owner can read.

ls -l file.txt
-rwxr-xr-x. 1 demo demogroup  1132 19 jan 08:40 file.txt
chmod u=r,go= file.txt
ls -l file.txt
-r--------. 1 demo demogroup     0  3 mar 10:59 file.txt

Processes

  • ps-ef: To see all processes.
  • top: To view the processes using the most CPU at the top. Auto-updates the information.
  • htop: Similar to top.
  • kill pid: To stop the process with process id pid.
  • nice program: To reduce the priority of the process. This makes it possible for the other interactive processes to be more responsive.
  • grep word files: Searches the word in multiple files. -i is for case insensitive search, -v to search text not matching and -r for a recursive search.
  • find . -name file.txt: to search a file with the name file.txt exactly. You can use wildcards to be less specific, for example *file*.

Redirections

In bash and most other shells, it is possible to write the output of a program to a file or read the keyboard input from a file without rewriting the program itself.

  • program > output.txt: standard output will be written to the file.
  • program 2> errors.txt: Errors will be written to the file.
  • program &> output.txt: Same as the above 2 combined.
  • program >> output.txt: Appends to the file instead of overwriting it.
  • program < input.txt: Read the file as input.

Since we can redirect the output to a file and can read for a file, it is also possible to send the output of a program to the input of another. We call this a "pipe".

Example: ls -l | wc -l

runs the ls command and it's output becomes the input of wc which will count the number of lines.

Backups

This section is more specific to our systems and might not apply to other Linux computers.

In your account, there's a directory .zfs/snapshots that contains the state of your account at different moments. You can consult this directory if needed. You should considering using a source control program like git.