banner



Which Of The Following Makes Changes To The Directory No Matter Where In The File Path You Are?

Navigating Files and Directories

Overview

Teaching: thirty min
Exercises: 10 min

Questions

  • How can I move around on my computer?

  • How can I see what files and directories I have?

  • How tin can I specify the location of a file or directory on my computer?

Objectives

  • Explain the similarities and differences betwixt a file and a directory.

  • Translate an absolute path into a relative path and vice versa.

  • Construct absolute and relative paths that identify specific files and directories.

  • Utilize options and arguments to modify the behaviour of a shell command.

  • Demonstrate the use of tab completion and explain its advantages.

The role of the operating system responsible for managing files and directories is called the file system. Information technology organizes our data into files, which hold data, and directories (also called 'folders'), which agree files or other directories.

Several commands are frequently used to create, inspect, rename, and delete files and directories. To get-go exploring them, we'll go to our open shell window.

First, allow's find out where nosotros are by running a command called pwd (which stands for 'print working directory'). Directories are similar places — at any fourth dimension while we are using the shell, we are in exactly i place called our current working directory. Commands generally read and write files in the current working directory, i.due east. 'here', and then knowing where you are before running a control is of import. pwd shows you where y'all are:

Here, the computer'due south response is /Users/nelle, which is Nelle's domicile directory:

Abode Directory Variation

The domicile directory path will look different on dissimilar operating systems. On Linux, it may look like /domicile/nelle, and on Windows, it will exist similar to C:\Documents and Settings\nelle or C:\Users\nelle. (Note that it may look slightly unlike for different versions of Windows.) In future examples, we've used Mac output every bit the default - Linux and Windows output may differ slightly just should be generally similar.

We volition too assume that your pwd command returns your user's home directory. If pwd returns something different, you may demand to navigate in that location using cd or some commands in this lesson volition not work every bit written. Run into Exploring Other Directories for more details on the cd command.

To empathize what a 'domicile directory' is, permit's have a look at how the file system as a whole is organized. For the sake of this example, we'll be illustrating the filesystem on our scientist Nelle'south computer. Subsequently this analogy, you'll exist learning commands to explore your own filesystem, which will be synthetic in a like way, but non exist exactly identical.

On Nelle's computer, the filesystem looks like this:

The file system is made up of a root directory that contains sub-directories  titled bin, data, users, and tmp

At the top is the root directory that holds everything else. We refer to information technology using a slash character, /, on its ain; this grapheme is the leading slash in /Users/nelle.

Inside that directory are several other directories: bin (which is where some congenital-in programs are stored), information (for miscellaneous data files), Users (where users' personal directories are located), tmp (for temporary files that don't need to exist stored long-term), and so on.

We know that our current working directory /Users/nelle is stored inside /Users because /Users is the first office of its proper name. Similarly, we know that /Users is stored inside the root directory / considering its proper name begins with /.

Slashes

Observe that there are two meanings for the / graphic symbol. When it appears at the front of a file or directory proper name, it refers to the root directory. When it appears inside a path, it's simply a separator.

Underneath /Users, nosotros discover ane directory for each user with an account on Nelle'south machine, her colleagues imhotep and larry.

Like other directories, home directories are sub-directories underneath  "/Users" like "/Users/imhotep", "/Users/larry" or  "/Users/nelle"

The user imhotep's files are stored in /Users/imhotep, user larry'due south in /Users/larry, and Nelle'south in /Users/nelle. Because Nelle is the user in our examples here, therefore we get /Users/nelle as our domicile directory. Typically, when you open a new control prompt, you will be in your home directory to start.

Now permit's learn the command that will let us see the contents of our own filesystem. We tin can see what's in our home directory by running ls:

            Applications Documents    Library      Music        Public Desktop      Downloads    Movies       Pictures                      

(Again, your results may be slightly unlike depending on your operating system and how you have customized your filesystem.)

ls prints the names of the files and directories in the current directory. We can make its output more than comprehensible by using the -F choice which tells ls to allocate the output by calculation a marker to file and directory names to indicate what they are:

  • a abaft / indicates that this is a directory
  • @ indicates a link
  • * indicates an executable

Depending on your trounce's default settings, the shell might too use colors to indicate whether each entry is a file or directory.

            Applications/ Documents/    Library/      Music/        Public/ Desktop/      Downloads/    Movies/       Pictures/                      

Hither, nosotros can see that our habitation directory contains only sub-directories. Any names in our output that don't have a nomenclature symbol are plain old files.

Clearing your terminal

If your screen gets too cluttered, you tin can clear your terminal using the clear command. You can still access previous commands using and to move line-past-line, or past scrolling in your terminal.

Getting help

ls has lots of other options. There are two common ways to observe out how to use a control and what options information technology accepts — depending on your environment, you might observe that but 1 of these ways works:

  1. We can pass a --help option to the command (not bachelor on macOS), such as:
  2. Nosotros tin can read its transmission with human (not available in Git Bash), such as:

We'll depict both means next.

The --assist choice

Most bash commands and programs that people have written to be run from within bash, support a --assistance option that displays more information on how to utilise the command or program.

            Usage: ls [OPTION]... [FILE]... List information almost the FILEs (the electric current directory past default). Sort entries alphabetically if neither -cftuvSUX nor --sort is specified.  Mandatory arguments to long options are mandatory for short options, too.   -a, --all                  exercise not ignore entries starting with .   -A, --almost-all           do not list implied . and ..       --writer               with -l, print the writer of each file   -b, --escape               print C-style escapes for nongraphic characters       --block-size=SIZE      scale sizes past SIZE earlier printing them; e.1000.,                                '--block-size=M' prints sizes in units of                                1,048,576 bytes; see SIZE format beneath   -B, --ignore-backups       practice not list implied entries catastrophe with ~   -c                         with -lt: sort by, and show, ctime (time of last                                modification of file status information);                                with -l: show ctime and sort by name;                                otherwise: sort by ctime, newest first   -C                         listing entries by columns       --color[=WHEN]         colorize the output; WHEN tin can be 'always' (default                                if omitted), 'car', or 'never'; more info beneath   -d, --directory            listing directories themselves, not their contents   -D, --dired                generate output designed for Emacs' dired mode   -f                         practice not sort, enable -aU, disable -ls --color   -F, --allocate             append indicator (one of */=>@|) to entries ...        ...        ...                      

Unsupported control-line options

If you try to apply an selection that is not supported, ls and other commands will usually print an error message like to:

              ls: invalid pick -- 'j' Try 'ls --assist' for more information.                          

The man control

The other mode to learn almost ls is to type

This command volition plough your terminal into a page with a description of the ls control and its options.

To navigate through the man pages, you may use and to move line-by-line, or endeavor B and Spacebar to skip up and down by a full page. To search for a character or word in the man pages, utilize / followed by the graphic symbol or word y'all are searching for. Sometimes a search will result in multiple hits. If so, yous can move between hits using N (for moving frontwards) and Shift+Due north (for moving backward).

To quit the human pages, printing Q.

Transmission pages on the web

Of course, there is a third fashion to access help for commands: searching the net via your web browser. When using cyberspace search, including the phrase unix human page in your search query volition help to find relevant results.

GNU provides links to its manuals including the core GNU utilities, which covers many commands introduced within this lesson.

Exploring More ls Flags

You can also use two options at the aforementioned fourth dimension. What does the command ls exercise when used with the -fifty option? What well-nigh if you use both the -l and the -h option?

Some of its output is about backdrop that we practise not encompass in this lesson (such every bit file permissions and ownership), only the balance should exist useful nevertheless.

Solution

The -l pick makes ls use a long listing format, showing not merely the file/directory names simply as well additional information, such as the file size and the time of its last modification. If yous apply both the -h option and the -l choice, this makes the file size 'human readable', i.due east. displaying something like 5.3K instead of 5369.

List in Reverse Chronological Order

By default, ls lists the contents of a directory in alphabetical order by name. The command ls -t lists items by time of last change instead of alphabetically. The command ls -r lists the contents of a directory in reverse guild. Which file is displayed terminal when you combine the -t and -r options? Hint: Y'all may need to utilise the -fifty choice to see the final inverse dates.

Solution

The most recently changed file is listed last when using -rt. This can be very useful for finding your well-nigh recent edits or checking to see if a new output file was written.

Exploring Other Directories

Not only tin we use ls on the current working directory, but nosotros can use it to list the contents of a different directory. Let'south take a look at our Desktop directory by running ls -F Desktop, i.e., the command ls with the -F option and the argument Desktop. The argument Desktop tells ls that we want a listing of something other than our electric current working directory:

Note that if a directory named Desktop does non be in your current working directory, this command will return an error. Typically, a Desktop directory exists in your dwelling house directory, which we presume is the current working directory of your bash shell.

Your output should exist a list of all the files and sub-directories in your Desktop directory, including the shell-lesson-information directory you downloaded at the setup for this lesson. On many systems, the command line Desktop directory is the same as your GUI Desktop. Take a look at your Desktop to ostend that your output is accurate.

Every bit you may now see, using a fustigate trounce is strongly dependent on the idea that your files are organized in a hierarchical file organization. Organizing things hierarchically in this way helps u.s. keep runway of our piece of work: it'south possible to put hundreds of files in our domicile directory, but as information technology's possible to pile hundreds of printed papers on our desk, but information technology's a self-defeating strategy.

Now that nosotros know the shell-lesson-information directory is located in our Desktop directory, nosotros can do two things.

First, we can await at its contents, using the aforementioned strategy as before, passing a directory proper noun to ls:

                          $                            ls              -F              Desktop/beat-lesson-data                      
            practise-data/  north-pacific-gyre/                      

Second, nosotros can really change our location to a different directory, and then we are no longer located in our abode directory.

The command to change locations is cd followed past a directory name to change our working directory. cd stands for 'change directory', which is a chip misleading: the command doesn't change the directory; information technology changes the beat out'south thought of what directory we are in. The cd command is akin to double clicking a folder in a graphical interface to get into a folder.

Let'south say we want to motion to the data directory we saw higher up. We tin use the following series of commands to get there:

                          $                            cd              Desktop              $                            cd              shell-lesson-data              $                            cd              exercise-data                      

These commands will move us from our home directory into our Desktop directory, so into the beat out-lesson-data directory, then into the practise-data directory. You volition find that cd doesn't impress anything. This is normal. Many shell commands volition non output anything to the screen when successfully executed. But if we run pwd after information technology, we can run into that we are at present in /Users/nelle/Desktop/vanquish-lesson-data/practice-data.

If we run ls -F without arguments at present, it lists the contents of /Users/nelle/Desktop/shell-lesson-data/exercise-information, considering that'south where we at present are:

            /Users/nelle/Desktop/trounce-lesson-data/practise-data                      
            animal-counts/  creatures/  numbers.txt  proteins/  writing/                      

We at present know how to go down the directory tree (i.due east. how to go into a subdirectory), simply how do we go up (i.due east. how do nosotros leave a directory and go into its parent directory)? We might try the following:

            -bash: cd: shell-lesson-data: No such file or directory                      

Just we get an mistake! Why is this?

With our methods so far, cd can only see sub-directories inside your current directory. There are unlike ways to see directories in a higher place your current location; nosotros'll start with the simplest.

At that place is a shortcut in the shell to move up 1 directory level that looks similar this:

.. is a special directory name meaning "the directory containing this one", or more than succinctly, the parent of the current directory. Certain enough, if we run pwd afterward running cd .., we're back in /Users/nelle/Desktop/vanquish-lesson-information:

            /Users/nelle/Desktop/trounce-lesson-data                      

The special directory .. doesn't usually show upward when nosotros run ls. If we desire to display information technology, nosotros can add the -a option to ls -F:

            ./  ../  exercise-data/  north-pacific-curlicue/                      

-a stands for 'show all'; it forces ls to bear witness u.s. file and directory names that brainstorm with ., such as .. (which, if we're in /Users/nelle, refers to the /Users directory). Equally you can come across, it besides displays another special directory that's just called ., which ways 'the current working directory'. It may seem redundant to have a proper name for information technology, but nosotros'll see some uses for it presently.

Notation that in nearly command line tools, multiple options can be combined with a unmarried - and no spaces between the options: ls -F -a is equivalent to ls -Fa.

In addition to the hidden directories .. and ., you may too see a file called .bash_profile. This file unremarkably contains shell configuration settings. Y'all may likewise see other files and directories commencement with .. These are usually files and directories that are used to configure dissimilar programs on your computer. The prefix . is used to foreclose these configuration files from cluttering the terminal when a standard ls command is used.

These iii commands are the bones commands for navigating the filesystem on your computer: pwd, ls, and cd. Let'due south explore some variations on those commands. What happens if you lot type cd on its ain, without giving a directory?

How tin you bank check what happened? pwd gives us the answer!

It turns out that cd without an argument will return you to your dwelling house directory, which is bang-up if you've got lost in your own filesystem.

Let's effort returning to the exercise-data directory from before. Final fourth dimension, we used iii commands, merely we can actually string together the list of directories to move to exercise-data in i step:

                          $                            cd              Desktop/shell-lesson-data/exercise-data                      

Check that we've moved to the right place past running pwd and ls -F.

If we desire to move upward one level from the information directory, nosotros could utilise cd ... But there is another manner to move to whatsoever directory, regardless of your current location.

Then far, when specifying directory names, or even a directory path (as higher up), we have been using relative paths. When y'all utilize a relative path with a command similar ls or cd, it tries to notice that location from where we are, rather than from the root of the file system.

Still, information technology is possible to specify the absolute path to a directory past including its unabridged path from the root directory, which is indicated by a leading slash. The leading / tells the computer to follow the path from the root of the file system, so it always refers to exactly i directory, no matter where we are when we run the command.

This allows us to motility to our vanquish-lesson-data directory from anywhere on the filesystem (including from inside exercise-data). To find the absolute path we're looking for, nosotros can utilise pwd and then excerpt the piece we demand to motion to shell-lesson-data.

            /Users/nelle/Desktop/shell-lesson-information/practise-data                      
                          $                            cd              /Users/nelle/Desktop/shell-lesson-data                      

Run pwd and ls -F to ensure that we're in the directory we await.

Two More Shortcuts

The shell interprets a tilde (~) character at the start of a path to mean "the current user'due south home directory". For example, if Nelle's habitation directory is /Users/nelle, so ~/data is equivalent to /Users/nelle/data. This merely works if it is the first graphic symbol in the path: here/there/~/elsewhere is non here/there/Users/nelle/elsewhere.

Another shortcut is the - (dash) character. cd will translate - into the previous directory I was in, which is faster than having to recall, and then type, the full path. This is a very efficient way of moving back and forth betwixt ii directories – i.eastward. if you lot execute cd - twice, you terminate upward back in the starting directory.

The divergence between cd .. and cd - is that the former brings you upwardly, while the latter brings y'all back.


Endeavour it! First navigate to ~/Desktop/crush-lesson-data (you should already be in that location).

                              $                                cd                ~/Desktop/shell-lesson-data                          

Then cd into the exercise-information/creatures directory

                              $                                cd                exercise-data/creatures                          

Now if you run

you lot'll run into you're back in ~/Desktop/shell-lesson-data. Run cd - again and yous're dorsum in ~/Desktop/trounce-lesson-data/do-information/creatures

Absolute vs Relative Paths

Starting from /Users/amanda/data, which of the following commands could Amanda utilise to navigate to her home directory, which is /Users/amanda?

  1. cd .
  2. cd /
  3. cd /domicile/amanda
  4. cd ../..
  5. cd ~
  6. cd home
  7. cd ~/information/..
  8. cd
  9. cd ..

Solution

  1. No: . stands for the current directory.
  2. No: / stands for the root directory.
  3. No: Amanda'south dwelling house directory is /Users/amanda.
  4. No: this control goes up 2 levels, i.e. ends in /Users.
  5. Yes: ~ stands for the user'southward habitation directory, in this instance /Users/amanda.
  6. No: this command would navigate into a directory home in the current directory if information technology exists.
  7. Yes: unnecessarily complicated, but correct.
  8. Yes: shortcut to go dorsum to the user's dwelling directory.
  9. Aye: goes up one level.

Relative Path Resolution

Using the filesystem diagram below, if pwd displays /Users/affair, what will ls -F ../fill-in display?

  1. ../backup: No such file or directory
  2. 2012-12-01 2013-01-08 2013-01-27
  3. 2012-12-01/ 2013-01-08/ 2013-01-27/
  4. original/ pnas_final/ pnas_sub/

A directory tree below the Users directory where "/Users" contains the  directories "backup" and "thing"; "/Users/backup" contains "original",  "pnas_final" and "pnas_sub"; "/Users/thing" contains "backup"; and  "/Users/thing/backup" contains "2012-12-01", "2013-01-08" and  "2013-01-27"

Solution

  1. No: there is a directory fill-in in /Users.
  2. No: this is the content of Users/thing/backup, simply with .., we asked for one level further up.
  3. No: meet previous explanation.
  4. Yes: ../backup/ refers to /Users/backup/.

ls Reading Comprehension

Using the filesystem diagram below, if pwd displays /Users/backup, and -r tells ls to display things in reverse guild, what control(southward) will result in the following output:

              pnas_sub/ pnas_final/ original/                          

A directory tree below the Users directory where "/Users" contains the  directories "backup" and "thing"; "/Users/backup" contains "original",  "pnas_final" and "pnas_sub"; "/Users/thing" contains "backup"; and  "/Users/thing/backup" contains "2012-12-01", "2013-01-08" and  "2013-01-27"

  1. ls pwd
  2. ls -r -F
  3. ls -r -F /Users/fill-in

Solution

  1. No: pwd is not the name of a directory.
  2. Yeah: ls without directory argument lists files and directories in the current directory.
  3. Yes: uses the absolute path explicitly.

Full general Syntax of a Shell Command

We have now encountered commands, options, and arguments, but it is possibly useful to formalise some terminology.

Consider the control beneath as a general instance of a command, which we will dissect into its component parts:

General syntax of a shell command

ls is the command, with an option -F and an statement /. We've already encountered options which either start with a single dash (-) or ii dashes (--), and they change the beliefs of a command. Arguments tell the command what to operate on (e.k. files and directories). Sometimes options and arguments are referred to as parameters. A control can be called with more than one choice and more than one argument, but a control doesn't always crave an argument or an option.

You might sometimes run into options being referred to as switches or flags, peculiarly for options that take no statement. In this lesson we will stick with using the term option.

Each part is separated past spaces: if you omit the space between ls and -F the shell volition look for a command called ls-F, which doesn't exist. Too, capitalization can be important. For example, ls -s volition brandish the size of files and directories alongside the names, while ls -S volition sort the files and directories past size, as shown below:

                          $                            cd              ~/Desktop/beat-lesson-data              $                            ls              -s              practise-data                      
            total 28  4 animate being-counts   iv creatures  12 numbers.txt   iv proteins   4 writing                      
            animal-counts  creatures  proteins  writing  numbers.txt                      

Putting all that together, our command higher up gives the states a list of files and directories in the root directory /. An example of the output y'all might go from the above command is given beneath:

            Applications/         System/ Library/              Users/ Network/              Volumes/                      

Nelle's Pipeline: Organizing Files

Knowing this much about files and directories, Nelle is ready to organize the files that the protein assay machine will create.

She creates a directory called north-pacific-curlicue (to remind herself where the data came from), which volition contain the data files from the analysis car, and her data processing scripts.

Each of her physical samples is labelled according to her lab's convention with a unique ten-graphic symbol ID, such as 'NENE01729A'. This ID is what she used in her collection log to tape the location, time, depth, and other characteristics of the sample, so she decides to use information technology as role of each information file'southward name. Since the analysis car'due south output is plain text, she will call her files NENE01729A.txt, NENE01812A.txt, and and so on. All 1520 files will become into the aforementioned directory.

Now in her current directory shell-lesson-data, Nelle can see what files she has using the command:

This command is a lot to type, but she tin let the shell do most of the work through what is called tab completion. If she types:

and then presses Tab (the tab cardinal on her keyboard), the shell automatically completes the directory name for her:

Pressing Tab again does nothing, since there are multiple possibilities; pressing Tab twice brings upward a listing of all the files.

If Nelle adds Chiliad and presses Tab again, the beat will append 'goo' since all files that commencement with 'g' share the start three characters 'goo'.

                          $                            ls              northward-pacific-gyre/goo                      

To see all of those files, she can press Tab twice more.

                          ls              due north-pacific-coil/goo goodiff.sh   goostats.sh                      

This is called tab completion, and we will encounter it in many other tools as nosotros go on.

Key Points

  • The file system is responsible for managing information on the deejay.

  • Information is stored in files, which are stored in directories (folders).

  • Directories tin as well shop other directories, which then grade a directory tree.

  • cd [path] changes the electric current working directory.

  • ls [path] prints a listing of a specific file or directory; ls on its own lists the current working directory.

  • pwd prints the user's electric current working directory.

  • / on its ain is the root directory of the whole file arrangement.

  • About commands take options that brainstorm with a -.

  • A relative path specifies a location starting from the current location.

  • An accented path specifies a location from the root of the file organization.

  • Directory names in a path are separated with / on Unix, but \ on Windows.

  • .. means 'the directory above the electric current ane'; . on its ain means 'the current directory'.

Which Of The Following Makes Changes To The Directory No Matter Where In The File Path You Are?,

Source: https://swcarpentry.github.io/shell-novice/02-filedir/index.html

Posted by: hernandezantionne.blogspot.com

0 Response to "Which Of The Following Makes Changes To The Directory No Matter Where In The File Path You Are?"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel