Files, directories, and navigation
- For our first real bash command, let's get existential. Type
whoami
at the prompt and press enter. (I'm going to stop telling you to press enter now.) What is happening here in terms that we covered earlier today? How does bash
know what whoami
means?
- four basic types of CLI commands:
- built-ins: part of the shell
- aliases: user- or system-defined macros
- scripts: executable text files written in scripting languages like perl, python, shell, etc
- binaries: compiled executable files
$ which whoami
- In this case,
which
is the command and whoami
is the argument. (You may also hear it called the parameter. Argument and parameter have very slightly different meanings in the world of programming, but it's mostly a semantic difference.)
which
returns the absolute path to the program name passed as argument
- path is exactly what it sounds like.
- a hierarchical structure
- absolute paths start at the root directory, represented as a a forward-slash
- relative paths are relative to your current working directory
- folders followed by forward-slashes (
/
)
- case-sensitive
- How can we find out where we are?
- Whichever directory we are currently in is our current working directory.
pwd
, print working directory
- If
whoami
tells me who the user is, and which
tells me where a program is located, why is it pwd
instead of whereami
. Legacy. Colorful, painful legacy.
- What is in our current working directory?
- What kinds of things are they? Files, directories, links, executable files?
$ ls -F
-F
is a flag (also called a switch) that lets us specify an option or setting
- Important Detour: to find out which flags are available, you can use a few options:
- the
--help
flag on many commands prints basic usage information
- the command
man
, passing the program name as the argument. For instance, man ls
. This opens the manual page for this program, with detailed usage information.
man
is not available in Git Bash, but you can also type "man " into Google and view the man pages there.
- Detour:
man
pages open in something called a pager. Pagers are used to display output that won't fit in one screen. , , and will move around within the document. You can search by typing a forward slash followed by the search term and step through the search results using n
. You can exit a pager by hitting q
.
- Detour: File extensions are short suffixes to filenames that can give us a hint about what is inside of the file. For instance,
.txt
files contain text, .jpg
files are JPEG images, and .py
files are python scripts.
- Can we get directory listings for other directories?
- Pass the absolute or relative path to the directory as an argument to
ls
.
$ ls /
$ ls jrc_intro_programming
- How do we change directories?
$ cd jrc_intro_programming
- to change to the last working directory,
cd -
- to go "up" one directory,
cd ..
- to go "up" two directories with one command,
cd ../..
- Detour: What does
..
mean?
ls -a
gives you a list of "all" files, including invisible files.
- Any file beginning with a
.
is invisible.
- Every directory has two special "folders",
.
and ..
. .
points at the directory itself, and ..
points at the parent directory.
- Detour: tab completion
- Typing things is hard.
- If you press the Tab key while typing,
bash
will attempt to automatically complete your thought, as if reading your mind. Ok, really it just matches up to the first point of ambiguity. For example, if your directory is empty except for data files named:
data1.txt
data2.txt
data3.txt
data4.txt
then if you type d
(without pressing enter) and press the Tab key, it would be automatically extended to data
, waiting for you to disambiguate before moving on.
- Use tab-completion to your advantage. It reduces errors due to typos and allows you to move much more quickly.
- While we're on the subject of saving time, you can use the and arrows to cycle through previous commands in your history, and if you want a complete history, try the
history
command.
<< Challenge 1
>> Challenge 2