UC3M

Telematic/Audiovisual Syst./Communication Syst. Engineering

Systems Architecture

September 2017 - January 2018

13.4.3.  Commands to manage files

The interpreter offers the commands to perform basic operations over files and the current directory. A detailed description of each of them can be found using the man command. The most common are:

  • ls (list directory). Shows the content of files and directories that are containned in the current directory, or the one in the path written after the command name. The general syntax is ls [options] [path] being the "options" and the "path" optional parameters. Some of the more relevant options are "-l" for giving a list with detailed information about files and directories (mode, size, number of links, owner, etc.), "-a" for including hidden files and directories in the list, "-t" for giving the list in a specific order taking into account the last modification time of the file, or "-r" for reverse the list order "-r". In order to practice with this command, from the initial path /home/teleco, first list all the files and directories that are containned there without any additional information, using ls . Next, list all the files and directories that are containned in another different path from the present path, writting the absolute path, specifically all the content that is under the directory /bin , executing ls /bin . To achieve not only a list of such files and directories but also detailed information about them execute ls -l and ls -l /bin respectively. Finally, execute ls -lat , ls -lart , ls -lat /bin and ls -lart /bin and observe the differences in the files list order.

  • cd (change directory). Changes the current directory to that written next to the command. If none is given, it changes to the user directory (the one abbreviated with ~). For example, execute cd /bin for changing to a new directory given by an absolute path, you can see the new directory after the change executing pwd. Next, return to the initial user directory executing cd without any paths, which will return to the initial user directory. You can check it, executing pwd

  • mkdir (make a directory). Creates a folder in the path given as argument. Only the last folder of the path is created unless an appropriate option is given to allow the creation of the folder in higher levels. From /home/teleco , create a new folder called proyecto with mkdir proyecto , in this way a new folder proyecto is created with a relative path under the present directory. Next, you will create a new folder but with an absolute path in the following way: mkdir /home/teleco/proyecto/grupoA and a new folder passing a relative path in the following way: mkdir /home/teleco/proyecto/grupoB. Note that under the working directory, a proyecto folder has been created and there are two subfolders grupoA and grupoB under the proyecto folder

  • rmdir (remove directory). Removes the folder given as argument. If the folder is not empty, the operation is rejected. From the directory /home/teleco remove the folder grupoB using an absolute path in the following way: rmdir /home/teleco/proyecto/grupoB How would you do the same removal using a relative path?

  • touch (touch file). Modify the access and modification time of a file if the parameter passed as argument (the file) exists, or an empty file is created if it does not exist. Execute touch /home/teleco/proyecto/grupoA/test.txt to create an emty file. Next, execute ls -lart /home/teleco/proyecto/grupoB/test.txt, next execute again touch /home/teleco/proyecto/grupoA/test.txt and finally execute again ls -lart /home/teleco/proyecto/grupoB/test.txt and note that the time information of the file has changed. In any case, the content of the file is emty at the end.

  • mv (move file). It moves or renames a file from the name given as first argument to the second. If only file names are specified, the command only renames the file. But if the second argument is a path, the file is moved to the new location in the file tree. For example, if in the directory /home/teleco we execute mv proyecto/grupoA/test.txt proyecto/grupoB/test.txt the file will be moved from directory grupoA to grupoB

  • cp (copy files). Copies a source file to a destination, or a set of files to a destination folder. For example, if the command cp /home/teleco/proyecto/grupoA/test.txt /home/teleco/proyecto/grupoB/test.txt is executed, then the file from directory grupoA will be copied to grupoB.

  • rm (remove file>. Removes the file in the path given as argument. The behavior of this command has multiple variations that can be invoked including different options (see Section 13.4.1). Remove the file that was created previously executing rm /home/teleco/proyecto/grupoA/test.txt , next list this directory with ls and check that this file has disappeared. Specially useful is to remove not only a file but all the directories and files that are under a specified directory. This can be done using some specific options of the command. In this way, for example, executing rm -rf /home/teleco/proyecto all files and directories under proyecto will be removed in a recursive mode.

The following figure shows a session with the command interpreter. The exit command terminates the execution of the interpreter and therefore closes the window. We recommend that you replicate this session in your computer to verify that the commands work as expected.

$ pwd
/home/teleco
$ ls
Descargas Documentos Escritorio
$ mkdir name
$ ls
Descargas Documentos Escritorio name
$ cd name
$ pwd
/home/teleco/name
$ ls
$ cd ..
$ pwd
/home/teleco
$ rmdir name
$ ls
Descargas Documentos Escritorio
$ exit

The interpreter starts with /home/teleco as the current directory. A new subfolder is created with name name using the mkdir command. The current directory is then changed to this new folder and its content is shown with the ls command (it is empty because we just created it). The interpreter sets the current directory to the upper folder with the cd .. command, uses rmdir to erase the folder (only empty folders are deleted), and finally it shows the actual directory, which is the one we were at the beginning.