thecodingidiot.com

The TerminalMoving Around

Moving Around

The filesystem[1] is a tree[2]. Your home directory is somewhere in it. At any moment, the shell has a current working directory — the node[3] in that tree where you currently stand. Every relative path you type is resolved from that node.

Where are you

pwd

pwd stands for print working directory. It prints the absolute path of your current location. Run it now. You will see something like /home/user. That is where you are.

What is here

ls

ls lists the contents of the current directory. By default it shows filenames in columns. It is the command you will type more than any other.

Two flags[4] you will use constantly:

ls -l    # long format: permissions, owner, size, date, name
ls -la   # long format, including hidden files (names starting with .)

Hidden files start with a dot. Many configuration files live in your home directory as dot files — .bashrc, .gitconfig, .ssh/. Others follow conventions like ~/.config/ for application settings. You will not see dot files with plain ls; you need -a to reveal them.

ls -la prints one entry per line. The first column is the permission string: -rw-r--r-- means a regular file, owner can read and write, group and others can only read. The next number is the hard-link count. Then owner, group, size in bytes, modification timestamp, and filename. Two entries are always present: . (the current directory itself) and .. (its parent). These are not quirks — they are how the filesystem represents directory relationships, and you will use them constantly (cd .., ./program).

Moving

cd Documents

cd changes the current directory. The argument is where you want to go. It can be a relative path (relative to where you are now) or an absolute path (starting from /).

cd ..          # go up one level
cd ~           # go to your home directory, from anywhere
cd -           # go back to where you just were
cd /etc        # absolute path — go directly to /etc

cd with no arguments does the same as cd ~ — it takes you home, while cd - takes you back to where you just were.

Getting lost

The first time I used a terminal I ran cd a dozen times in a row, lost track of where I was, and typed ls expecting to recognise something. I did not. The fix:

pwd

Always. When you are confused, pwd tells you where you are. Then ls tells you what is there. Those two commands together are your compass.

Absolute vs relative paths

An absolute path starts with /. It describes the full location from the root of the filesystem.

cd /home/user/Development

A relative path starts with anything else. It is interpreted relative to your current directory.

cd Development         # only works if Development is in your cwd
cd ./Development       # the ./ is explicit: "starting here"
cd ../other-project    # up one level, then into other-project

Get comfortable reading paths. You will type them hundreds of times per day.

Footnotes

  1. File system - Wikipedia

  2. Tree structure - Wikipedia

  3. Node (computer science) - Wikipedia

  4. Command flags - IBM Documentation