What Would You Enter at the Command Prompt to Display the Present Working Directory
Navigating Files and Directories
Overview
Instruction: 30 min
Exercises: 10 minQuestions
How can I move effectually on my computer?
How tin I see what files and directories I accept?
How can I specify the location of a file or directory on my calculator?
Objectives
Explicate the similarities and differences between a file and a directory.
Interpret an absolute path into a relative path and vice versa.
Construct absolute and relative paths that identify specific files and directories.
Use options and arguments to change the behaviour of a shell command.
Demonstrate the apply of tab completion and explain its advantages.
The part of the operating arrangement responsible for managing files and directories is called the file organisation. It organizes our data into files, which hold information, and directories (also called 'folders'), which hold files or other directories.
Several commands are oft used to create, inspect, rename, and delete files and directories. To start exploring them, we'll go to our open shell window.
First, let's find out where we are past running a control called pwd
(which stands for 'print working directory'). Directories are like places — at any fourth dimension while we are using the shell, we are in exactly one identify called our current working directory. Commands mostly read and write files in the current working directory, i.east. 'here', and so knowing where you are earlier running a command is important. pwd
shows y'all where you are:
Here, the computer'due south response is /Users/nelle
, which is Nelle's home directory:
Home Directory Variation
The abode directory path will look different on different operating systems. On Linux, it may look like
/dwelling house/nelle
, and on Windows, it will be similar toC:\Documents and Settings\nelle
orC:\Users\nelle
. (Note that it may look slightly dissimilar for different versions of Windows.) In future examples, we've used Mac output as the default - Linux and Windows output may differ slightly but should exist generally similar.We will likewise assume that your
pwd
command returns your user's home directory. Ifpwd
returns something different, you lot may need to navigate there usingcd
or some commands in this lesson will not piece of work as written. See Exploring Other Directories for more than details on thecd
command.
To understand what a 'home directory' is, let's have a await 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'due south computer. Afterward this illustration, you'll be learning commands to explore your ain filesystem, which will be synthetic in a similar way, merely not be exactly identical.
On Nelle's figurer, the filesystem looks like this:
At the top is the root directory that holds everything else. We refer to information technology using a slash graphic symbol, /
, on its ain; this character is the leading slash in /Users/nelle
.
Within that directory are several other directories: bin
(which is where some built-in programs are stored), information
(for miscellaneous information files), Users
(where users' personal directories are located), tmp
(for temporary files that don't need to exist stored long-term), so on.
We know that our electric current working directory /Users/nelle
is stored inside /Users
because /Users
is the first part of its proper name. Similarly, we know that /Users
is stored inside the root directory /
because its proper name begins with /
.
Slashes
Notice that there are ii meanings for the
/
character. When it appears at the front of a file or directory proper name, it refers to the root directory. When information technology appears inside a path, it'southward simply a separator.
Underneath /Users
, we find 1 directory for each user with an account on Nelle'southward machine, her colleagues imhotep and larry.
The user imhotep's files are stored in /Users/imhotep
, user larry's in /Users/larry
, and Nelle'due south in /Users/nelle
. Because Nelle is the user in our examples here, therefore nosotros go /Users/nelle
as our home directory. Typically, when you open a new command prompt, you lot will be in your home directory to start.
At present permit's learn the command that will let us see the contents of our ain filesystem. Nosotros can meet what'due south in our home directory past running ls
:
Applications Documents Library Music Public Desktop Downloads Movies Pictures
(Over again, your results may exist slightly dissimilar 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. Nosotros can make its output more comprehensible by using the -F
choice which tells ls
to allocate the output past adding a marker to file and directory names to signal what they are:
- a trailing
/
indicates that this is a directory -
@
indicates a link -
*
indicates an executable
Depending on your shell'due south default settings, the beat might as well utilize colors to indicate whether each entry is a file or directory.
Applications/ Documents/ Library/ Music/ Public/ Desktop/ Downloads/ Movies/ Pictures/
Here, we can see that our dwelling house directory contains only sub-directories. Whatever names in our output that don't have a classification symbol are plain old files.
Clearing your concluding
If your screen gets besides chaotic, you lot tin clear your terminal using the
articulate
control. You can yet access previous commands using ↑ and ↓ to move line-by-line, or by scrolling in your concluding.
Getting help
ls
has lots of other options. There are two common ways to find out how to use a control and what options information technology accepts — depending on your environment, you might find that only 1 of these ways works:
- Nosotros can pass a
--help
option to the control (not available on macOS), such every bit: - Nosotros tin read its transmission with
man
(non bachelor in Git Fustigate), such as:
We'll describe both ways next.
The --help
option
Nigh fustigate commands and programs that people have written to be run from within bash, support a --assist
selection that displays more information on how to employ the command or programme.
Usage: ls [OPTION]... [FILE]... Listing information most the FILEs (the current directory by default). Sort entries alphabetically if neither -cftuvSUX nor --sort is specified. Mandatory arguments to long options are mandatory for short options, likewise. -a, --all practice not ignore entries starting with . -A, --near-all practise not list implied . and .. --author with -l, print the author of each file -b, --escape impress C-style escapes for nongraphic characters --block-size=SIZE scale sizes by SIZE before printing them; e.g., '--cake-size=M' prints sizes in units of i,048,576 bytes; see SIZE format below -B, --ignore-backups do not list implied entries catastrophe with ~ -c with -lt: sort by, and testify, ctime (fourth dimension of terminal modification of file status information); with -l: testify ctime and sort by name; otherwise: sort by ctime, newest first -C list entries by columns --color[=WHEN] colorize the output; WHEN tin can be 'e'er' (default if omitted), 'auto', or 'never'; more info below -d, --directory listing directories themselves, not their contents -D, --dired generate output designed for Emacs' dired fashion -f exercise not sort, enable -aU, disable -ls --colour -F, --classify append indicator (one of */=>@|) to entries ... ... ...
Unsupported command-line options
If y'all try to utilise an option that is non supported,
ls
and other commands volition usually print an mistake message similar to:ls: invalid selection -- 'j' Endeavour 'ls --aid' for more data.
The man
command
The other mode to learn virtually ls
is to blazon
This control will turn your terminal into a page with a description of the ls
command and its options.
To navigate through the homo
pages, you may use ↑ and ↓ to move line-by-line, or try B and Spacebar to skip up and down past a full folio. To search for a grapheme or word in the homo
pages, use / followed past the character or give-and-take y'all are searching for. Sometimes a search volition result in multiple hits. If so, you can move betwixt hits using Northward (for moving forward) and Shift+N (for moving backward).
To quit the man
pages, printing Q.
Transmission pages on the web
Of course, there is a third way to admission assist for commands: searching the internet via your spider web browser. When using net search, including the phrase
unix human page
in your search query will 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
FlagsYou can besides apply two options at the same fourth dimension. What does the command
ls
practise when used with the-l
selection? What near if you use both the-50
and the-h
selection?Some of its output is about properties that we practice non cover in this lesson (such equally file permissions and ownership), but the residual should exist useful notwithstanding.
Solution
The
-l
option makesls
apply a long listing format, showing not only the file/directory names but also additional information, such as the file size and the fourth dimension of its terminal modification. If you lot utilise both the-h
pick and the-l
choice, this makes the file size 'human readable', i.e. displaying something like5.3K
instead of5369
.
List in Contrary Chronological Order
Past default,
ls
lists the contents of a directory in alphabetical club by name. The controlls -t
lists items by time of final change instead of alphabetically. The commandls -r
lists the contents of a directory in reverse lodge. Which file is displayed terminal when you combine the-t
and-r
options? Hint: You may need to use the-l
choice to come across the terminal changed dates.Solution
The nigh recently changed file is listed last when using
-rt
. This can be very useful for finding your most recent edits or checking to come across if a new output file was written.
Exploring Other Directories
Not only can we use ls
on the current working directory, but we tin can use it to listing the contents of a different directory. Let's take a await 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 desire a listing of something other than our current working directory:
Note that if a directory named Desktop
does not be in your current working directory, this command volition return an error. Typically, a Desktop
directory exists in your home directory, which nosotros assume is the electric current working directory of your bash shell.
Your output should be a listing of all the files and sub-directories in your Desktop directory, including the shell-lesson-information
directory yous downloaded at the setup for this lesson. On many systems, the command line Desktop directory is the same every bit your GUI Desktop. Accept a look at your Desktop to confirm that your output is authentic.
As you may now meet, using a bash shell is strongly dependent on the thought that your files are organized in a hierarchical file organisation. Organizing things hierarchically in this way helps the states keep rail of our work: it'south possible to put hundreds of files in our home directory, just as it'due south possible to pile hundreds of printed papers on our desk-bound, just it's a self-defeating strategy.
Now that nosotros know the trounce-lesson-data
directory is located in our Desktop directory, we can do two things.
Showtime, we can look at its contents, using the same strategy every bit before, passing a directory name to ls
:
$ ls -F Desktop/shell-lesson-data
exercise-data/ northward-pacific-ringlet/
2nd, nosotros tin actually change our location to a different directory, and so we are no longer located in our home directory.
The control to change locations is cd
followed past a directory proper noun to change our working directory. cd
stands for 'change directory', which is a fleck misleading: the control doesn't alter the directory; information technology changes the beat'southward idea 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.
Permit's say nosotros desire to move to the data
directory nosotros saw higher up. We tin can apply the following serial of commands to go there:
$ cd Desktop $ cd shell-lesson-data $ cd practice-data
These commands volition move us from our home directory into our Desktop directory, then into the shell-lesson-data
directory, then into the do-data
directory. You will notice that cd
doesn't print annihilation. This is normal. Many shell commands volition non output anything to the screen when successfully executed. Merely if we run pwd
after it, we can see that nosotros are now in /Users/nelle/Desktop/shell-lesson-data/do-data
.
If we run ls -F
without arguments now, it lists the contents of /Users/nelle/Desktop/beat-lesson-data/exercise-data
, considering that'south where we at present are:
/Users/nelle/Desktop/beat-lesson-information/exercise-data
beast-counts/ creatures/ numbers.txt proteins/ writing/
We now know how to get down the directory tree (i.e. how to go into a subdirectory), but how do we become up (i.e. how practise we leave a directory and get into its parent directory)? Nosotros might attempt the following:
-fustigate: cd: shell-lesson-information: No such file or directory
But we get an error! Why is this?
With our methods then far, cd
can only meet sub-directories inside your current directory. At that place are different ways to meet directories above your current location; we'll starting time with the simplest.
There is a shortcut in the trounce to motility upwardly one directory level that looks like this:
..
is a special directory name significant "the directory containing this ane", or more succinctly, the parent of the current directory. Sure enough, if we run pwd
after running cd ..
, we're back in /Users/nelle/Desktop/trounce-lesson-data
:
/Users/nelle/Desktop/vanquish-lesson-data
The special directory ..
doesn't usually prove up when we run ls
. If we want to brandish it, we can add the -a
option to ls -F
:
./ ../ do-data/ north-pacific-gyre/
-a
stands for 'prove all'; it forces ls
to show us file and directory names that brainstorm with .
, such as ..
(which, if we're in /Users/nelle
, refers to the /Users
directory). Every bit you can see, it likewise displays some other special directory that'southward just called .
, which means 'the current working directory'. It may seem redundant to take a name for it, but we'll run across some uses for it soon.
Note that in almost control line tools, multiple options can be combined with a single -
and no spaces between the options: ls -F -a
is equivalent to ls -Fa
.
In add-on to the hidden directories
..
and.
, y'all may likewise see a file chosen.bash_profile
. This file normally contains shell configuration settings. You may also meet other files and directories beginning with.
. These are usually files and directories that are used to configure unlike programs on your computer. The prefix.
is used to prevent these configuration files from cluttering the concluding when a standardls
command is used.
These iii commands are the bones commands for navigating the filesystem on your reckoner: pwd
, ls
, and cd
. Permit'south explore some variations on those commands. What happens if you blazon cd
on its ain, without giving a directory?
How can you check what happened? pwd
gives us the answer!
It turns out that cd
without an argument will return you to your habitation directory, which is neat if you've got lost in your own filesystem.
Permit'southward endeavor returning to the practise-data
directory from before. Concluding time, we used three commands, but 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 nosotros've moved to the right place by running pwd
and ls -F
.
If we want to move upward i level from the data directory, nosotros could employ cd ..
. But at that place is another manner to motility to any directory, regardless of your current location.
Then far, when specifying directory names, or fifty-fifty a directory path (as higher up), nosotros have been using relative paths. When yous utilise a relative path with a command like ls
or cd
, it tries to find that location from where we are, rather than from the root of the file organisation.
However, it is possible to specify the absolute path to a directory by 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 organisation, so information technology always refers to exactly 1 directory, no matter where we are when we run the command.
This allows usa to motility to our beat out-lesson-data
directory from anywhere on the filesystem (including from within exercise-data
). To detect the accented path nosotros're looking for, we can utilise pwd
and then excerpt the piece we need to move to beat out-lesson-data
.
/Users/nelle/Desktop/beat-lesson-information/exercise-data
$ cd /Users/nelle/Desktop/crush-lesson-data
Run pwd
and ls -F
to ensure that we're in the directory we wait.
Two More Shortcuts
The shell interprets a tilde (
~
) character at the start of a path to hateful "the current user's domicile directory". For instance, if Nelle's home directory is/Users/nelle
, and so~/data
is equivalent to/Users/nelle/data
. This only works if it is the first graphic symbol in the path:hither/there/~/elsewhere
is nothere/there/Users/nelle/elsewhere
.Another shortcut is the
-
(dash) grapheme.cd
will translate-
into the previous directory I was in, which is faster than having to remember, then type, the total path. This is a very efficient way of moving back and forth between two directories – i.e. if you executecd -
twice, you lot end up dorsum in the starting directory.The difference between
cd ..
andcd -
is that the former brings yous up, while the latter brings you lot dorsum.
Effort it! Offset navigate to
~/Desktop/shell-lesson-data
(you lot should already be there).$ cd ~/Desktop/shell-lesson-data
Then
cd
into thepractice-data/creatures
directory$ cd exercise-data/creatures
Now if you run
you'll see you're back in
~/Desktop/shell-lesson-data
. Runcd -
again and you're dorsum in~/Desktop/shell-lesson-data/practice-data/creatures
Absolute vs Relative Paths
Starting from
/Users/amanda/data
, which of the following commands could Amanda use to navigate to her dwelling house directory, which is/Users/amanda
?
cd .
cd /
cd /home/amanda
cd ../..
cd ~
cd dwelling
cd ~/data/..
cd
cd ..
Solution
- No:
.
stands for the electric current directory.- No:
/
stands for the root directory.- No: Amanda'southward domicile directory is
/Users/amanda
.- No: this command goes up 2 levels, i.due east. ends in
/Users
.- Aye:
~
stands for the user's home directory, in this case/Users/amanda
.- No: this control would navigate into a directory
home
in the current directory if it exists.- Yes: unnecessarily complicated, but right.
- Yes: shortcut to go back to the user's home directory.
- Yes: goes up ane level.
Relative Path Resolution
Using the filesystem diagram beneath, if
pwd
displays/Users/affair
, what willls -F ../backup
display?
../backup: No such file or directory
2012-12-01 2013-01-08 2013-01-27
2012-12-01/ 2013-01-08/ 2013-01-27/
original/ pnas_final/ pnas_sub/
![]()
Solution
- No: there is a directory
fill-in
in/Users
.- No: this is the content of
Users/thing/backup
, but with..
, we asked for 1 level further up.- No: encounter previous explanation.
- Yes:
../backup/
refers to/Users/backup/
.
ls
Reading ComprehensionUsing the filesystem diagram beneath, if
pwd
displays/Users/backup
, and-r
tellsls
to brandish things in opposite lodge, what command(due south) volition result in the post-obit output:pnas_sub/ pnas_final/ original/
![]()
ls pwd
ls -r -F
ls -r -F /Users/backup
Solution
- No:
pwd
is not the proper name of a directory.- Yeah:
ls
without directory argument lists files and directories in the current directory.- Yes: uses the absolute path explicitly.
General Syntax of a Shell Command
Nosotros accept now encountered commands, options, and arguments, merely it is maybe useful to formalise some terminology.
Consider the command beneath equally a full general example of a command, which we volition dissect into its component parts:
ls
is the command, with an selection -F
and an argument /
. We've already encountered options which either start with a single dash (-
) or two dashes (--
), and they change the beliefs of a command. Arguments tell the command what to operate on (e.m. files and directories). Sometimes options and arguments are referred to every bit parameters. A command tin be called with more than ane option and more than than one argument, but a command doesn't always require an argument or an selection.
You might sometimes see options existence referred to as switches or flags, especially for options that take no statement. In this lesson nosotros will stick with using the term option.
Each office is separated by spaces: if you omit the space between ls
and -F
the shell volition await for a command called ls-F
, which doesn't be. Also, capitalization can be important. For instance, ls -southward
volition brandish the size of files and directories alongside the names, while ls -South
will sort the files and directories by size, every bit shown beneath:
$ cd ~/Desktop/beat out-lesson-data $ ls -s do-data
total 28 4 beast-counts 4 creatures 12 numbers.txt 4 proteins 4 writing
animate being-counts creatures proteins writing numbers.txt
Putting all that together, our control higher up gives us a list of files and directories in the root directory /
. An case of the output you might go from the above control is given below:
Applications/ System/ Library/ Users/ Network/ Volumes/
Nelle's Pipeline: Organizing Files
Knowing this much almost files and directories, Nelle is set up to organize the files that the protein assay car will create.
She creates a directory called north-pacific-gyre
(to remind herself where the information came from), which volition contain the data files from the assay machine, and her information processing scripts.
Each of her physical samples is labelled according to her lab's convention with a unique ten-character ID, such every bit 'NENE01729A'. This ID is what she used in her collection log to record the location, time, depth, and other characteristics of the sample, so she decides to use it as part of each data file's name. Since the analysis machine's output is plain text, she will call her files NENE01729A.txt
, NENE01812A.txt
, and so on. All 1520 files will become into the aforementioned directory.
At present in her current directory shell-lesson-information
, Nelle can see what files she has using the command:
This control is a lot to type, but she tin let the beat out exercise most of the piece of work through what is called tab completion. If she types:
and then presses Tab (the tab central on her keyboard), the beat out automatically completes the directory name for her:
Pressing Tab again does naught, since at that place are multiple possibilities; pressing Tab twice brings upwards a list of all the files.
If Nelle adds G and presses Tab once more, the shell will append 'goo' since all files that start with 'g' share the first three characters 'goo'.
$ ls n-pacific-gyre/goo
To encounter all of those files, she can press Tab twice more.
ls north-pacific-whorl/goo goodiff.sh goostats.sh
This is called tab completion, and we will meet it in many other tools every bit we keep.
Fundamental Points
The file system is responsible for managing information on the disk.
Information is stored in files, which are stored in directories (folders).
Directories can also store other directories, which then form a directory tree.
cd [path]
changes the 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 current working directory.
/
on its own is the root directory of the whole file organization.Most commands take options that begin with a
-
.A relative path specifies a location starting from the current location.
An absolute path specifies a location from the root of the file arrangement.
Directory names in a path are separated with
/
on Unix, but\
on Windows.
..
means 'the directory in a higher place the current one';.
on its own means 'the current directory'.
Source: https://swcarpentry.github.io/shell-novice/02-filedir/index.html
Post a Comment for "What Would You Enter at the Command Prompt to Display the Present Working Directory"