Command line operations

This material section considers first the structure (hierarchy) of directories. Knowledge about it enables you to use the command line commands, the most essential ones of which will be introduced at the end part of the material.

Note

Here we are using Linux command line commands working at least on Linux remote desktop and in Git Bash. Windows has a command line of its own with a bit different commands.

Directories and paths

Let us go back to the example on the previous page. While completing it, you copied the file koodi.cpp from the directory Downloads into the directory /worktmp/ohj2_staff/rounds/03/code/:

Copy action in command line (``cp ~/Downloads/koodi.cpp /worktmp/ohj2_staff/rounds/03/code/``)

As we can see from the picture, working on the command line basically means that you use paths to reference files, and the paths reveal the location of the files within the file hierarchy. First, we will take a look at writing a path on the command line.

Please open the command line so that you can try out these steps as you read on. In the remote desktop you can do this by selecting Application > System Tools > Terminal or, faster, Application > Favourites > Terminal.

In the example we called the program cp and gave it two paths as arguments. The paths refer to a location within the file system. The first path refers to the location of the file to be copied, and the second to the target, i.e. the directory where you want to save the copied file. In fact, cp itself is interpreted as a path to the program located in the path /bin/cp. So, you put the ”addresses” of the programs you want to run and the arguments you want to pass to them into the command line.

  • Directory

    Directory means the exact same thing in Linux as folder in the Windows operating system. The difference in names is purely historical.

  • Current directory, working directory

    Current directory means in directory hierarchy, the directory you are located in while writing your command. Let us take a small break and think this over. You might simultaneously have multiple folders open in different windows of the graphical user interface and you might, for example, move a file by dragging the icon from one window to another. Because of this, you might not feel like you are located in any of the folders when using a graphical interface. The command line, on the other hand, sets your location to a point in the directory hierarchy. It is also possible to open several command line windows, and then go to a different directory in each of them, and execute commands in different locations of the directory hierarchy that way.

    Depending on the settings, the current working directory or a partial path to the working directory might be visible at the beginning of the command line. Sometimes you need to find out the path to your working directory, and you can easily do it with the command pwd. It prints the path to your current working directory.

    You can think of the working directory as your default directory. If the user has not specified a certain path for something, it will be presumed that the thing is located in the working directory.

    You can change the working directory with the command cd (change directory). You can think of cd as a command for changing places. The parameter of the command is the new directory path you want to move to.

    Try it out yourself:

    • First write the command pwd to find the name of your working directory.
    • Then use cd to move to the directory Desktop. You can do it by typing cd Desktop.
    • Now find the name of your working directory again.
  • Home directory, ~

    When a user logs into a Linux system, the working directory will automatically be set as the home directory of the user. All the files of the user are located in the home directory or its subdirectories. The path of the home directory is /home/<username>, where your own username is in the place of <username>. You can refer to your home directory with the ~ sign (tilde) in the command line interface.

    Try it out yourself:

    • If you are using the command line with a remote desktop connected to a Windows interface, you can type ~ by holding down the Alt Gr key while pressing tilde key, then releasing them, and finally pressing the spacebar.
    • Go from the subdirectory back to your home directory by typing the command cd ~. Try the command pwd again to see where you are located now.
    • Go back to the directory Desktop so we can try one thing again: The fastest way to go to your home directory is to type just cd. Try it and after that, see where your working directory is located now.
  • Path or search path

    In Linux, as well as in all the other modern operating systems, the user can organize their files the way they want into different directories. The search path means the text-based format of showing the location of a file within the directory structure. If you type in the search path in Linux, you separate the names of directories with a forward slash (/). The corresponding character in Windows is the double backslash \\. In Linux, all the search paths begin at the root (just /). In Windows, the paths start with drives, named with a single letter (i.e. C:\). The path is an analogy to an address.

    Try it out yourself:

    • Go to the directory into which you cloned your repository of this course. In case you had created a subdirectory called prog2, cloned your repository of the course there, and named the directory with your user id, for example xxyyzz, the command you should now type is cd prog2/xxyyzz. Naturally, the directory names here will be the ones you have used to create them.
    • Tip: You can easily fill the paths with the tabulator key (tab). It can speed up your typing significantly, but the most important benefit of the tabulator is that the user does not have to remember a path or its parts by heart. The automatic completion of directory and file names is also less prone to mistakes than typing them in.

Special characters in paths

Below you will see a few special characters used, for example, in search paths:

  • / - separator of directories.
    • NOTE: The character / alone refers to the root. Please be careful! For example, when we practice deleting directories, you should not go wild with /, because there is a possibility that you could erase everything under the root directory.
  • . - working directory
  • .. - a directory one level higher in the directory hierarchy than the working directory
  • ~ - home directory
  • * - an undefined amount of any characters
  • ? - one character of any kind
  • [] - you can define alternative characters within square brackets.

Examples:

  • ~/* - refers to everything in the home directory

  • ~/Desktop/../* - same as above

  • ~/*/*.txt - all the files with the suffix .txt, located in all of the subdirectories of the home directory

  • ./?????.txt - the files with any five characters and the suffix .txt in their name, located within the working directory.

    Try it out yourself:

    • After the experiment above, you should be located in the directory that has the clone of you repository. Go to the directory Desktop by typing only one command.
    • Use one command to go back to the directory where the clone of your repository is, and use a different special character than above (if you used ~ above, now use .., and if you used .. above, now use ~).

Running a program from command line

As we confirmed earlier, you can run a program from the command line by giving the path to the program you want to run. For example, if you compiled a program in your working directory, and the resulting file is called a.out, you can run that program by typing ./a.out into the command line. If you just write a.out, the shell will most likely answer with "command not found: a.out". The reason is that a.out does not refer to an existing path.

Try it out yourself:

  • You probably have a compiled solution to an assignment of this course. In your repository, if you have the directory hello_world, where you have implemented the program, after compilation you should also have the directory build-hello_world-Desktop-Debug at the same level, and that is where you find the compiled program. Go to one of these directories with build- at the beginning of their name.
  • Execute the program file you find there. If you have not changed the settings on Qt Creator, the name of the program file will be the name of the directory containing your program code.

Some programs, like cp, are located in the path (/bin/). You can run such programs from anywhere using just the name of the program.

The greatest benefit of running programs from the command line is the way you can pass arguments to the program in question. To give arguments in the graphical user interface, you always need to open the part of the user interface where you can define the arguments. In the command line, you can just type in the arguments after the call of the program.

The standard way to adjust programs is to use flags (arguments starting with -). One dash usually means a one letter flag, and two dashes mean a word.

You can find more information on running programs and the flags used by the programs by using the argument --help. For example, cat --help prints the instructions on using the program cat.

Try it out yourself:

  • Find out more about the command cp.

Commands for listing the contents of a directory

Next we will consider and try some useful commands. Previously, you moved to the directory containing an executable program. It is a good working directory also for practicing the commands that will be introduced from now on.

You can look at the contents of a directory with the command ls (abbreviation from the word ‘list‘). The ls command will print all the names of the files and subdirectories in that directory. Because the output has several names on one line, it might be difficult to read, and you rarely need to know just the filenames. This is why ls is usually used with some options:

  • -l - shows the printed content in rows
  • -a or --all - shows also the hidden files and folders which, in Linux, start their name with the character ‘period’.

It is common to use the command ls -la that lists all the files and subdirectories in the working directory.

With the command tree, you can show the contents of the directories recursively as a tree.

As a default, ls and tree list the files of the working directory, but it is possible to define a specific path for them.

Try it out yourself:

  • List the files in your working directory in a few different ways.
  • List the files in the directory ~/Desktop. Note that you can do it without going to the directory ~/Desktop.
  • List the files that are located one step higher in the hierarchy than your working directory.
  • See if you have hidden files in your home directory (= their name starts with a period).

Commands for manipulating files and directories

Next, we will go through some commands you can use to manipulate files and directories.

Caution

Before executing these commands, please be certain that you know what you are doing. Surely you do not want to spend time finding out whether or not you can retrieve the files that you accidentally destroyed when practicing from somewhere (in the version control or the backup files).

When you conduct your experiments in this section, just to be sure, you should be located in one of the directories starting with build-, because destroying their files would not give you any trouble. You can always create all the files in the build- directory again by compiling the program in Qt Creator (as long as the project directory is intact).

  • mkdir or make directory creates a new directory with the name you have set. For example, mkdir projects creates a new directory called projects in the working directory.

    Try it out yourself:

    • Create a new subdirectory in the working directory and name it.
  • cp or copy copies the defined source materials into your target. For example, cp ~/miscellaneous/*.txt ~/texts/ copies all the files with the suffix .txt from the directory miscellaneous within the home directory into the directory texts, also within the home directory.

    Try it out yourself:

    • Copy all the files in your working directory into the subdirectory you created in the paragraph above.
  • mv or move works just like cp but moves the files instead of copying them.

    Try it out yourself:

    • Edit the names of all the files in the subdirectory to start with a period.
    • Along with that, test again if you can list the hidden files in your file listing. Try the command ls both with the option -a and without it.
  • rm or remove deletes the path you define. For example, rm *.txt deletes all the text files in your working directory.

  • rmdir or remove directory deletes the directory in the path in question if the directory is empty. For example, rmdir ~/projects deletes the directory defined above.

    Try it out yourself:

    • Delete one of the files in the subdirectory you created.
    • Delete all the files left in your subdirectory.
    • Delete the subdirectory.

Commands for listing the contents of a text file

  • cat or concatenate prints the contents of a text file into a standard output stream, which is by default the command line (it concatenates the file). This will make working in the command line easier, because there is no need to open a graphical text editor every time you want to see the contents of a file. It is the simplest way to see the contents of a text file.

  • more prints the contents of a file one page at a time. This way, you are able to read even the first lines of long files. You can move forward by pressing the space key.

  • less prints the contents in a version you can browse in the command line. You can scroll the text with the arrow keys. You can exit the program by pressing q.

    Try it out yourself:

    • Move one step higher in the hierarchy from a build- directory and look at the contents of a long program file of a program you have written, first with cat, then with more, and once more with less. (Of course, this code file is located in one of the subdirectories.)
  • diff or difference compares two files line by line and prints the differences. You can use this to test your work in small quantities. If you already know what your program should print, you can use diff to see how your program currently differs from the expected printout. For example, if you have saved in modelcode.txt what your program should print, and in mycode.txt what the program you created actually printed, the command diff modelcode.txt mycode.txt shows you the differences in the printouts. (The command prints some odd-looking characters at the start of the row.)

    Try it out yourself:

    • Choose a programming assignment that you completed and for which the personnel of the course gave a ready-made code template. Using diff, see the differences between your solution and the template.
    • Are you able to deduce the meaning of the characters < and > at the beginning of the lines? (If you wish, you can also google it.)

Commands for finding a file

  • find finds files on given conditions. For example, find -name '*.txt' shows all the paths to the files with the suffix .txt that are in the working directory or in its subdirectories.

    Try it out yourself:

    • Go to your home directory and find all the files with the suffix .hh that are in your home directory or its subdirectories.

    You can execute commands like -exec <command> ; to your search results. For example, find ~/miscellaneous/ -name '*.txt' -exec cat {} ; gives the command cat to every file in the directory “miscellaneous” with the suffix .txt.

    Try it out yourself:

    • Print all the files with the suffix .hh that are located in the subdirectories of your home directory on the screen.
  • grep or globally search a regular expression and print searches a string you have specified. You can use grep to find the contents of a file or to filter the results of another command with pipelining. For example, grep bachelor * -Rl will list all the files with the word bachelor that are located in the working directory and its subdirectories.

    Try it out yourself:

    • Search the working directory and its subdirectories for all of the .cpp files that define a class, i.e. that include the word class.

Commands for editing the contents of a text file

Minor edits in several files: sed

In the command line, you can execute editors. You can use them to edit the contents of a text file directly with a command such as Stream Editor or sed. It can be used for mass operations to several files.

Stream Editor enables you to add or edit single lines, for example, to replace or delete words. Special characters (such as *) let you execute one editing operation by sed to several files with one command. For more information, see Wikipedia site on sed.

Stream Editor is very useful for making a small edit to several files, such as replacing one word with another in different files.

Try it out yourself: Section b of the next exercise

Major edits: editors

It is good to know that if you want to write text in a file, you can also execute many primitive graphical editors on the command line, for example vim, pico, and jed.

Different Linux distributions differ in which editors are by default included. All of the systems usually include the editor called vim. The very same vim usually launches as the default editor for Git, and that is why you should know at least the basics of its use, or at the very least know how to exit it. It has different modes that react to keyboard input differently. Internet is full of directions, but here you have the most essential information, short and swift.

command mode - the default mode vim has as it starts. Different commands in command mode let you conduct searches or save files, for example. The basic, good to know commands:

  • :q- quits the program if the file does not contain any unsaved changes
  • :wq- writes the file on the disc and quits the program
  • :q!- quits the program without saving the changes to the file.

insert mode - the common text input mode. You can access the insert mode by pressing i in command mode. You can return to command mode by pressing esc.