The file system
You may be used to the file system in Windows or Mac OS X, where directories (or folders if you prefer) can contain files and more directories. The Unix filesystem is structured in the same way - as a tree - that begins at the ‘root’ directory ‘/’. Directories are separated by slash characters /.
Click on the image below to see what your file system could look like. In this example the root directory is dir1.
When you work on the command line, you are located in a directory somewhere in this tree. There are two ways to refer to a location: its absolute path, starting at the root directory, or its relative path, starting in the current directory.
# Absolute path
/nfs/course/home/<user_name>
# Relative path
../../home/<user_name>
The .. refers to the directory above a location, so the relative path here goes up twice, then back down to your home directory. If a path starts with ~/ then it refers to your home directory. If a path starts with ./ then it refers to the current directory.
# References the level above
../
# References the home directory
~/
# References the current directory
./
Navigation
pwd will tell you exactly where you are in the file system. If you imagine the tree structure, pwd tells you on which branch of the tree you are sitting. You will start off in your home folder.
ls will list all of the files and directories where you are currently located. Put another way, ls tells you all the branches that go out of the branch you are sitting on. If you give a path as an argument (the route to another branch), it will list the files at that location (the branches that go out from that branch). There is also a convenient option -l that will give you a full table of file and directory information.
# What is here?
ls
# Ok but tell me more
ls -l
cd will change your location (the branch you are sitting on), your ‘working directory’, to the path given, absolute or relative. If no address is given, you return to your home directory.
# Going back one step and check where you are
cd ..
pwd
# Going back to previous directory
cd -
pwd
# Going to your home directory
cd
pwd
# Going to the root
cd /
pwd
Click on the image below to see what every command will do within your file system.
Exercise 0.2
# Use pwd to find you current location
pwd
# Use ls to see what in the directory is.
ls
# Use cd to change directory and .. to go up one level
cd ..
# Use ls to see what is in the directory
ls
# Use cd to change directory and .. to go up two levels
cd ../..
# To get to the home directory just typing cd
cd
# Use cd to change directory and give the absolute path to go to genomes
cd /nfs/teaching/551-0132-00L/1_Unix/genomes
# Use ls to see what is in there
ls
# Use cd to change directory and remember that an absolute path starts at the root
cd /nfs/nas22/fs2202/biol_micro_teaching/course_home/<your eth name>
# Since the pathname is quite long, we created an alias. So, you can shorten the part "nas22/fs2202/biol_micro_teaching/" to "teaching/" and still refer to the correct location. Therefore, the command below leads you also home.
cd /nfs/teaching/course_home/<your eth name>
Note: The alias can be used in any command and we use the shortened version in the OLM.
# Use cd to change directory and to go to the root use /
cd /
# Let's start at the genomes directory:
cd /nfs/teaching/551-0132-00L/1_Unix/genomes
ls
# What's in the bacteria directory?
cd bacteria
ls
# Finally let's go home
cd
Wildcards
When providing a file path as an argument to a command, it is often possible to provide multiple file paths using wildcards. These are special characters or strings that can be substituted for a matching pattern. For many commands using wildcards allows you to execute the associated action on each file that matches the pattern, though this obviously does not work in all cases.
? matches any single character
* matches any number of any characters
[…] matches any character within the brackets
{word1,word2,…} matches any string inside the brackets
For instance:
# Pattern matching
ls /nfs/teaching/551-0132-00L/1_Unix/genomes/bacteria/escherichia/GCF_000005845.2_ASM584v2/* # lists all files in the ecoli directory
ls /nfs/teaching/551-0132-00L/1_Unix/genomes/bacteria/escherichia/GCF_000005845.2_ASM584v2/*.fna # lists all nucleotide fasta files there
ls /nfs/teaching/551-0132-00L/1_Unix/genomes/bacteria/escherichia/GCF_000005845.2_ASM584v2/*.f?a # lists all nucleotide and protein fasta files there