howto.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. .. index:: ! terminal, ! shell, ! command Line
  2. .. _howto:
  3. ****************
  4. The command line
  5. ****************
  6. This chapter aims at providing novices with general basics about the shell, common Unix
  7. commands and their Windows equivalent, and some general file system facts.
  8. This chapter is also a place to return to and (re-)read if you come across a
  9. non-DataLad command or principle you want to remind yourself of.
  10. If you are already familiar with the shell and know the difference between an absolute
  11. and a relative path, you can safely skip this chapter and continue to the :ref:`DataLad Basics <basics-intro>`.
  12. While there is a graphical user interface for DataLad (the :term:`DataLad Gooey`), this handbook will first and foremost focus on teaching DataLad concepts without the overhead of a user-interface, using DataLad's most powerful interface on the *command line*.
  13. This means that the code examples in the handbook show no buttons to click on, but a set of commands and options users type into their *terminal*.
  14. If you are not used to working with command-line tools, DataLad can appear intimidating.
  15. Luckily, the set of possible commands is limited, and even without prior experience with a shell, one can get used to it fairly quickly.
  16. .. figure:: ../artwork/src/img/shell.png
  17. :width: 50%
  18. :alt: A z-shell on a Debian system
  19. A terminal window in a standard desktop environment.
  20. The shell (also called a terminal, console, or CLI) is an interactive,
  21. text based interface. If you have used Matlab or IPython, then you are already familiar
  22. with the basics of a command line interface.
  23. On Unix-like systems (e.g., running Linux or macOS), the shell application is usually called "terminal".
  24. On Windows systems, several different interfaces exist: The "CMD" Command Prompt and the Powershell are natively installed, and the Git Bash (provided by Git for Windows) or Anaconda prompt CLI (provided by Anaconda or Miniconda) can come with the installation of the respective software tool.
  25. We recommend using CMD, or, if you have them installed already and prefer them over the CMD, the Git Bash or Anaconda prompt.
  26. As later parts in this section will show, shells under Windows may use different commands than shells under Linux and macOS systems.
  27. Command syntax
  28. ==============
  29. Interactions with the shell take the form of commands, text-based instructions to your computer.
  30. Commands are case sensitive and follow the syntax of: ``command [options...] <arguments...>``.
  31. Whenever you see some example code in the code snippets of this book, make sure
  32. that you capitalize exactly as shown if you try it out yourself.
  33. The options modify the behavior of the program, and are usually preceded by ``-`` or ``--`` on Unix-like systems.
  34. In this example
  35. .. runrecord:: _examples/how-to-1
  36. :language: console
  37. :workdir: dl-101
  38. :lines: 1, 5
  39. :realcommand: dd if=/dev/zero of=output.txt bs=1M count=24 && ls -l output.txt
  40. $ ls -l output.txt
  41. ``ls`` is the *command*. The *option* ``-l`` tells ``ls`` to use a long listing format and
  42. thus display more information.
  43. ``output.txt`` is the *argument* — the file that ``ls`` is listing.
  44. The difference between options preceded by ``-`` and ``--`` is their length:
  45. Usually, all options starting with a single dash are single letters. Often,
  46. a long, double-dashed option exists for these short options as well. For example,
  47. to list the size of a file in a *human-readable* format, supply the short option
  48. ``-h``, or, alternatively, its longer form, ``--human-readable``.
  49. .. runrecord:: _examples/how-to-2
  50. :language: console
  51. :workdir: dl-101
  52. :realcommand: ls -lh output.txt && rm output.txt
  53. $ ls -lh output.txt # note that short options can be combined!
  54. # or alternatively
  55. $ ls -l --human-readable output.txt
  56. Every command has many of those options (often called "flags") that modify their behavior.
  57. On Windows, options of native Windows commands can be preceded by a ``/`` instead of dashes, for example ``dir /p output.txt``.
  58. There are too many to even consider memorizing. Remember the ones you use often,
  59. and the rest you will lookup in their documentation or via your favorite search engine.
  60. DataLad commands naturally also come with many options, and in the next chapters
  61. and later examples you will get to see many of them.
  62. Basic commands
  63. ==============
  64. The following commands, split between Unix-like (e.g., Linux, macOS) and Windows environments, can appear in our examples or are generally useful to know:
  65. They can help you to *explore and navigate* in your file system, copy, move, or remove files, or create new directories.
  66. Note that the Git Bash on Windows emulates a Unix environment in which you could use Unix commands despite being on Windows.
  67. .. index::
  68. pair: terminal commands; on Unix-like systems
  69. Unix-like systems or environments
  70. """""""""""""""""""""""""""""""""
  71. ``ls -lah <folder>``
  72. list the contents of a folder, including hidden files (-a), and all their information (-l);
  73. print file sizes in human readable units (-h)
  74. ``cd <folder>``
  75. change to another folder
  76. ``cp <from> <to>``
  77. copy a file
  78. ``cp -R <from> <to>``
  79. copy a folder and its contents (-R)
  80. ``mv <from> <to>``
  81. move/rename a file or folder
  82. ``rm <file>``
  83. delete a file
  84. ``rm -Rv <folder>``
  85. delete a folder and its contents (-R) and list each file as it's being deleted (-v)
  86. ``mkdir <folder>``
  87. create a folder
  88. ``rmdir <folder>``
  89. delete an empty folder
  90. ``export NAME=Value``
  91. Set an :term:`environment variable` ``NAME`` to ``Value`` for your current terminal session
  92. .. index::
  93. pair: terminal commands; on Windows
  94. Windows systems
  95. """""""""""""""
  96. ``dir <folder>``
  97. list the contents of a folder including hidden files
  98. ``cd <folder>``
  99. change to another folder
  100. ``copy <from> <to>``
  101. copy a file
  102. ``ren <oldname> <newname>``
  103. rename a file or folder
  104. ``mv <from> <to>``
  105. move a file or folder
  106. ``del <file>``
  107. delete a file
  108. ``rmdir /s <folder>``
  109. delete a folder and its contents (``/s``)
  110. ``md <folder>``
  111. create a folder
  112. ``rmdir <folder>``
  113. delete an empty folder
  114. ``set NAME=Value``
  115. Set an :term:`environment variable` ``NAME`` to ``Value`` for your current terminal session
  116. The prompt
  117. ==========
  118. When you first login on the command line, you are greeted with "the prompt",
  119. and it will likely look similar to this:
  120. ``me@muninn: ~$``
  121. This says I am the user ``me`` on the machine muninn and I am in the folder ``~``,
  122. which is shorthand for the current user's home folder (in this case ``/home/me``).
  123. The ``$`` sign indicates that the prompt is interactive and awaiting user input.
  124. In this handbook, we will use ``$`` as a shorthand for the prompt, to allow
  125. the reader to quickly differentiate between lines containing commands vs the
  126. output of those commands.
  127. .. index:: ! paths
  128. Paths
  129. =====
  130. Paths look different on Unix-like and Windows systems.
  131. Most prominently, the *path separators*, i.e., the symbol distinguishing directories in path, are back slashes (``\``) on Windows and front slashes (``/``) on Unix-like systems.
  132. On Windows systems, paths are also usually prefixed with a "disk designator" such as ``C:`` or ``d:``.
  133. Let's say I want to create a new folder in my home folder on a Unix system,
  134. I can run the following command:
  135. .. code-block:: bash
  136. $ mkdir /home/me/awesome_datalad_project
  137. If I want to do the same in Windows CMD, I'd do
  138. .. code-block::
  139. $ mkdir C:\Users\me\awesome_datalad_project
  140. And that both works on the respective system. ``/home/me/awesome_datalad_project`` and ``\Users\me\awesome_datalad_project`` are what is called an *absolute*
  141. path.
  142. Absolute paths *always* start with a ``/`` (on Unix-like systems) or a ``\`` (on Windows systems), and define the folder's location with no ambiguity.
  143. However, much like in spoken language, using someone's full proper name every
  144. time would be exhausting, and thus pronouns are used.
  145. This shorthand is called *relative* paths, because they are defined (wait for it...)
  146. *relative* to your current location on the file system. Relative paths *never* start
  147. with a ``/`` or ``\``.
  148. For example, ``myfile.txt`` is a relative path to the file ``myfile.txt`` in the current directory on Unix-like systems.
  149. The Windows equivalent is to ``myfile.txt`` in the current directory on the ``C`` disk is ``C:myfile.txt``.
  150. Unix knows a few shortcuts to refer to file system related directories, and you will
  151. come across them often. Whenever you see a ``.``, ``..``, or ``~`` in a DataLad command,
  152. here is the translation to this cryptic punctuation:
  153. ``.``
  154. the current directory
  155. ``..``
  156. the parent directory
  157. ``~``
  158. the current user's home directory
  159. So, taking the above example again: given that I am in my home (``~``) folder,
  160. the following commands all would create the new folder in the exact same place.
  161. .. code-block:: bash
  162. mkdir /home/me/awesome_datalad_project
  163. mkdir ~/awesome_datalad_project
  164. mkdir awesome_datalad_project
  165. mkdir ./awesome_datalad_project
  166. To demonstrate this further, consider the following: In my home directory
  167. ``/home/me`` I have added a folder for my current project,
  168. ``awesome_datalad_project/``. Let's take a look at how this folder is organized:
  169. .. code-block:: bash
  170. $ tree
  171. └── home
  172. └── me
  173. └── awesome_datalad_project
  174. ├── aligned
  175. ├── code
  176. └── sub-01
  177. └── bold3T
  178. ├── ...
  179. └── sub-xx
  180. └── bold3T
  181. Now let's say I want to change from my home directory ``/home/me`` into the ``code/``
  182. folder of the project. I could use absolute paths:
  183. ``cd /home/me/awesome_datalad_project/aligned/code``
  184. But that is a bit wordy. It is much easier with a relative path:
  185. .. code-block:: bash
  186. $ cd awesome_datalad_project/aligned/code
  187. Relative to my starting location (``/home/me``), I navigated into the subfolders.
  188. I can change back to my home directory also with a relative path:
  189. .. code-block:: bash
  190. $ cd ../../../
  191. The first ``../`` takes me from ``code/`` to its parent ``aligned/``, the
  192. second ``../`` to ``awesome_datalad_project/``, and the last ``../``
  193. back to my home directory ``me/``.
  194. However, since I want to go back to my home folder, it's much faster to run:
  195. .. code-block:: bash
  196. $ cd ~
  197. Windows similarly knows the ``.`` and ``..`` shortcuts, but can not handle the ``~`` shortcut.
  198. In order to quickly get home, you could use
  199. .. code-block::
  200. $ cd %userprofile%
  201. More information on Windows paths can be found `here <https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file>`_.
  202. Text editors
  203. ============
  204. Text editors are a crucial tool for any Linux user, but regardless of your operating system,
  205. if you use DataLad, you will occasionally find yourself in your default text editor to write
  206. a :term:`commit message` to describe a change you performed in your DataLad dataset.
  207. Religious wars have been fought over which is "the best" editor. From the smoldering ashes,
  208. this is the breakdown:
  209. ``nano``
  210. Easy to use; medium features. If you do not know which to use, start with this.
  211. ``vim``
  212. Powerful and light; lots of features and many plugins; steep learning curve.
  213. Two resources to help get the most out of vim are the vimtutor program
  214. and vimcasts.org. If you accidentally enter ``vim`` unprepared, typing ``:q``
  215. will get you out of there.
  216. ``emacs``
  217. Powerful; tons of features; written in Lisp; huge ecosystem; advanced learning curve.
  218. The text editors above are all command-line editors.
  219. They will open up directly in your terminal.
  220. .. index::
  221. pair: configure default editor; with Git
  222. While those text editors can also be installed on Windows, command-line editors are rarely used on Windows.
  223. Git for Windows might set :term:`vim` as the default editor upon installation, which can require some acclimatization.
  224. A good graphical alternative is Notepad++, a powerful Windows-native text editor.
  225. You may either be able to configure this during the installation, of afterwards by running ``git config core.editor notepad``.
  226. Shells
  227. ======
  228. Whenever you use the command line on a Unix-based system, you do that in a command-line
  229. interpreter that is referred to as a ``shell``.
  230. The shell is used to start commands and display the output of those commands.
  231. It also comes with its own primitive (yet surprisingly powerful) scripting language.
  232. Many shells exist, though most belong to a family of shells called "Bourne Shells"
  233. that descend from the original ``sh``. This is relevant, because they share (mostly)
  234. a common syntax.
  235. Two common shells are:
  236. ``Bash``
  237. The bourne-again shell (``bash``) is the default shell on many \*nix systems (most Linux distros, macOS).
  238. ``zsh``
  239. The Z shell (``zsh``) comes with many additional features, the highlights being:
  240. shared history across running shells, smarter tab-completion, spelling correction, and better theming.
  241. To determine what shell you're in, run the following:
  242. .. code-block:: bash
  243. $ echo $SHELL
  244. usr/bin/bash
  245. .. index:: ! tab completion
  246. Tab completion
  247. ==============
  248. One of the best features ever invented is tab completion. Imagine your favorite animal sitting
  249. on your shoulder. Now imagine that animal shouting "TAB!" every time you've typed the first
  250. 3 letters of a word. Listen to that animal.
  251. Tab completion autocompletes commands and paths when you press the Tab key.
  252. If there are multiple matching options, pressing Tab twice will list them.
  253. The greatest advantage of tab completion is not increased speed (though that is a nice benefit)
  254. but rather the near elimination of typos — and the resulting reduction of cognitive load.
  255. You can actually focus on the task you're working on, rather than your typing. Tab-completion
  256. will autocomplete a DataLad command, options you give to it, or paths.
  257. For an example of tab-completion with paths, consider the following directory structure:
  258. .. code-block:: bash
  259. ├── Desktop
  260. ├── Documents
  261. │ ├── my_awesome_project
  262. │ └── my_comics
  263. │ └── xkcd
  264. │ │ └── is_it_worth_the_time.png
  265. ├── Downloads
  266. You're in your home directory, and you want to navigate to your `xkcd <https://xkcd.com/1205>`_
  267. comic selection in ``Documents/my_comics/xkcd``.
  268. Instead of typing the full path error-free, you can press Tab after the first few letters.
  269. If it is unambiguous, such as ``cd Doc <Tab>``, it will expand to ``cd Documents``.
  270. If there are multiple matching options, such as ``cd Do``, you will be prompted for more letters.
  271. Pressing Tab again will list the matching options (``Documents`` and ``Downloads`` in this case).
  272. .. only:: html
  273. .. figure:: https://upload.wikimedia.org/wikipedia/commons/a/ad/Command-line-completion-example.gif
  274. :alt: Tab completion
  275. A visual example of tab-completion in action:
  276. **That's it -- equipped with the basics of the command line, you are good to go on your DataLad adventure!**