UC3M

Telematic/Audiovisual Syst./Communication Syst. Engineering

Systems Architecture

September 2017 - January 2018

13.4.4.  Other useful commands

The interpreter offers many useful commands. In this section, we are going to review some of them. A detailed description of each of them can be found using the man command. This is a list of other selected commands:

  • date (show the present date). With this command, the present date of the system that is executed can be shown. The date can be obtainned with different formats, depending on the options for invoking it. Execute the date command and visualize the result.

  • echo (print on the standard output). Prints some text that is passed as parameter to the standard output. For example echo "This is a test" will print the correspondent text on the screen

  • Output redirections A command that is executed in a shell, prints its results on the screen by default. But if we want the command result to be stored in a file instead of to be printed on the screen, then we must use output redirections. When an output redirection is executed, then a destination file must be indicated where the output is going to be stored. If the destination file does not exist, then the effect will be that a new file will be created where the output will be stored. If the destination file already exists, then the command output will be stored in such a file. There are two modes of output redirection: command > destination_file in that case, if the file already exists, then the previous file content is overwritten, and command >> destination_file in that case, if the file already exists, then the previous file content is not overwritten but is added at the end of the file, without removing the previous content. Execute date > test.txt and you can observe that the result will not be printed on screen but a new test.txt file will be created where the current date will be stored. Next, execute echo "Text to be written after the date" >> test.txt and observe that the new text as a result of the command execution, will appear in the file after the date instead of on the screen. Finally, if it is executed ls -lart > prueba.txt , then we can observe that the previous file content will be overwritten and will be replaced by the complete list of the current directory.

  • cat (print the content of files). This command prints the content of the different files that are passed as arguments. For example, with cat test.txt you can view the content of the file prueba.txt

  • grep (search a string in one or more files). Search a string in one or several files. Despite this command has many options, only two typical cases are going to be shown right now. First, in order to search a string in a file, we can use grep string fichero. For example, to search the string "la" in the file "test.txt", we would execute grep "la" test.txt . In addition, if we want to search a string in all the files under a specified directory, we would do the following, executing the option -r grep -r string *

  • ps (print the current processes). Allows to list all the processes that are in the operating system in that moment. For example, when a program is executed, in many situations only one process is created associated to that program, but it is also possible to create several processes for a single program. We will try to execute ps that will print the processes that are in the operating system. If we want to obtain more information about these processes, we can add certain parameters, such as for example executing ps auxw, we will obtain more detailed information about the processes. Finally, we are going to create a process that will be indefenetly in the system. In order to achieve it, we will writ a code line that represents an infinite loop, this is e.g. "while(1);" as this condition will be always fulfilled, then the process will be executing indefinetely in the system. This condition will be written in the main of a c file, and we will compile the file and create the executable. Next, the process will be launched in background (so that the shell returns us the control) with ./program &, next if we execute ps auxw we will view the new created process. If we want to remove this process, we can send it a signal to terminate that process, using the command kill. In order to achieve it, we must copy the process identifier PID after the execution of ps auxw and next using kill -9 pid

Self-assessment questions

Answer the following questions

  1. Select the correct option

    • The command arguments ar divided into: the command name and the "-" character

    • The command arguments ar divided into: those objects over which the command operates, and the arguments to modify its behavior

  2. Select the equivalent absolute path to /dirA/dirB/fichero.txt path

    • /dirA/dirB/././.././file.txt

    • /dirA/dirB/../dirB/file.txt

  3. What task does the command $mv Notes.txt notes.old

    • Rename the file Notes.txt to notes.old

    • Move the file Notes.txt to the notes.old folder

  4. Select the correct option regarding the rmdir command

    • Renames a file

    • Moves a file

    • Removes a file

    • Removes a folder

  5. Select the correct command to search the string "wordl" into the file main.c

    • $grep 'wordl' main.c

    • $grep main.c 'wordl'

    • $grep main.c |'wordl'