Navigating to files and directories =================================== Your challenges of the day: --------------------------- 1. Does type case matter? Is there a difference between ``ls -s`` and ``ls -S``? 2. Do spaces matter? Is there a difference between ``ls-F`` and ``ls -F``? Questions to think about: ------------------------- - What is a command shell and why should I use one? - How can I move around on my computer? - How can I see what files and directories I have? - How can I specify the location of a file or directory on my computer? What is Unix Shell? ------------------- We usually interact with our devices on a **graphical user interface** (GUI). Shell is a **Command-Line Interface** (CLI), where we type commands in the **prompt** ``$`` instead of pointing and clicking. We use it to invoke complicated programs. Shell is a scripting language, and we will use the Unix Shell: Bash (Bourne Again SHell by Stephen Bourne). Why use Bash? ------------- Bash combines existing tools into powerful pipelines and handle large volumes of data automatically. We can write sequences of commands into a script, improving the reproducibility of workflows. It is essential to interface with hardware, HPCC, and remote machines. Navigating files and directories -------------------------------- The **file system** is the part of the operating system responsible for managing files and directories. - **Files** hold information. - **Directories** (or **folders**) hold files or other directories. Think of them like *places*. - The **current working directory** is the place where you are in the file system when you are using the shell. - The **root directory** is the top directory that holds everything else. It is referred to by a slash ``/`` on its own. This is the leading slash in other directory paths, for example ``/home/fordfishman/`` - **Hidden files and directories** start with ``.`` like ``.bash_profile``. They are usually configuration settings and are hidden to prevent cluttering the terminal with a standard ``ls`` command. Add the ``-a`` option see hidden files. Example of directory structure: .. code:: none Documents ├── Code │ ├── analysis.py │ └── functions.py ├── Data │ └── info.csv ├── description.txt └── .gitignore Paths ----- The path to a file displays the directories that file is a member of. For instance, if you have a file called ``data.txt`` on your Desktop, its path could be ``/home/fordfishman/Desktop/data.txt``. This is an **absolute path** because it begins at the root directory. We also use **relative paths** that change based on our current working directory. If the working directory is ``/home/fordfishman``, the relative path to the file is ``Desktop/data.txt``. General syntax of a shell command --------------------------------- .. tab:: Bash .. code:: bash $ ls -F / ``ls`` is the **command**, with an **option** (or **switch** or **flag**) ``-F`` and an **argument** ``/``. **Options** start with a single dash (``-``) or two dashes (``--``) and change the behavior of the command. Arguments tell the command what to operate on (e.g. files and directories). Options and argements are refered to as **parameters**. .. note:: Type case is important. Spaces are important between command and options. (But options can be combined with a single ``-`` and no spaces -> ``-aF``). Listing: ``ls`` --------------- We use the ``ls`` command to list the contents of the current directory. It has many options we can provide: - ``-F`` option (switch or flag) tells ls to classify the output by adding a marker to file and directory names to indicate what they are. - ``-a`` option displays all files, including hidden files (files starting with ``.``) - ``-s`` option displays the size of files and directories - ``-S`` option will sort the files and directories by size - ``--help`` option will tell us how to use the command and what options it accepts .. tab:: Bash .. code:: bash $ ls -F .. tab:: Output .. code:: none Applications/ data_shell/ Music/ Desktop/ data_shell.zip Pictures/ Documents/ Library/ Downloads/ Movies/ Explore more ``ls`` flags. ~~~~~~~~~~~~~~~~~~~~~~~~~~ 1. What does ``-l`` option do? What if you use ``-l`` and ``-h``? 2. The default ``ls`` lists contents in alphabetical order. What option do I use to see them by time of last change? Print working directory: ``pwd`` -------------------------------- To check where you are, use ``pwd`` to print the path of the **working directory**. .. tab:: Bash .. code:: bash $ pwd .. tab:: Output .. code:: none /home/fordfishman/ Change directory: ``cd`` ------------------------ ``cd`` will change your working directory. ``cd`` can only see sub-directories inside your current working directory. - ``cd ..`` is a shortcut to move up one directory to the *parent directory* of the one we are in - ``cd ~/`` is a shortcut to move to the current user’s home directory. For example, if my home directory is ``/home/fordfishman``, then ``~/data`` is equivalent to ``/home/fordfishman/data`` .. tab:: Bash .. code:: bash $ cd .. $ pwd .. tab:: Output .. code:: none /home/ Other commands -------------- - ``clear``: clears the terminal if it gets to cluttered - up and down arrows can be used to access previous commands (or scroll) - ``man`` will give you the manual for a command, for example ``man ls`` will tell us all about listing - The help option can be used with a command, for example ``ls --help`` References ~~~~~~~~~~ - `Intermediate Linux Commands `__ - `Software Carpentry Unix Shell `__