Skip to content

Instantly share code, notes, and snippets.

@kezzico
Last active November 9, 2023 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kezzico/65c515c34773c467496c36e8205974e7 to your computer and use it in GitHub Desktop.
Save kezzico/65c515c34773c467496c36e8205974e7 to your computer and use it in GitHub Desktop.

Current Working Directory

In a shell environment, the current working directory is the directory 'you' are currently in. Unless specified by an absolute path all file references are made relevant to the current working directory. In short, the current working directory is the first place programs will look.

Directory map with you are here pin

Examples Using Current Working Directory

Here are some examples.

The command touch creates an empty file or updates the last modifed date of an existing one. A quick touch. Unless given an absolute path, touch will create the file file relative to the current working directory (CWD).

touch myfile

If the CWD is /home/lee, then my file's absolute path will be /home/lee/myfile.

All other commands that operate on files or directories follow this same convention. mkdir, cat, tail, rm, cp all reference the CWD.

Make a directory called foo

mkdir foo

Print the text in a ./readme.md file

cat ./README.md

Permanently delete the current directory.

rm -r ./

Changing the working directory

The working directory can be changed using the command cd. short for change directory. The cd command uses the same relative path rules.

Which directory am I working in?

To find out which directory you are currently 'working' in. Use the command pwd which is short for 'print working directory.'

Shortcuts

To reference your current working directory, use the dot convention (.). For example, ./myfile.

To reference the directory above the CWD use the double dot (..). The double dots can be chained together to go all the way up the tree. ../../../

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment