Working with Filenames and Path and Globing

From docwiki
Jump to: navigation, search


Motivation

Here you will get an rough overview of where to find things in the file system and how to navigate the file system.

File System Hierarchy

In Unix/Linux the Top of the file system is the root directory named /. There are no drive letters as you know them from a certain other operating system. What you find below is a structure that exists since the early days of Unix. Here are the most important directories and what they are used for in Linux.

Filesystem Hierarchy
/ everything starts here.
/etc System-wide configuration files.
/usr Program Files (executable, libraries and files the programs need)
/bin System programs
/var If programs need to store data they should put it here. (/usr is for data that is only read and not written)
/home Every regular user of the system has their home directory below /home. E.g /home/anna
/tmp Temporary files - usually deleted on startup.
/lib Important system libraries
/sbin Important system programs that usually only the root user needs
/usr/lib libraries for user programs
/usr/local Stuff that is not part of your linux distribution, things you installed locally
/opt Optional - used if you do not want to follow the filesystem standard
/mnt A place where you would want to mount extra filesystems
/media Below /media the system automatically mounts your CD drive or your USB thumb drive, ...
/root Only the root user has his/her home directory here.
/dev Here you find device files which are placeholders to directly access almost any hardware on our system. E.g. you could directly read the naked hard-drive.
/proc It looks like there are many files, but those are actually just a simulation that allows you a direct view into some kernel data. E.g. try cat /proc/cpu to see your CPUs.
/sys Similar to proc but better organized

Shortcuts

Shortcuts
. A single dot means the current working directory
.. 2 dots: One level up in the hierarchy
~ The tilde sign is often a shorthand for your home directory. This is not a feature of kernel but expanded by the bash shell.


Programs handling Files and moving around in the Filesystems

$ pwd               # print working directory. This way you know where you are
$ ls                # list files and directories
$ cd                # jump to your homedirectory
$ cd /tmp           # go to another directory. Here /tmp
$ mkdir myproject   # create a new directory from where you are: name myproject
$ cd ..             # move up one directory in the hierarchy
$ cp file1 file2    # copy file1 to another name: file2
$ mv file1 file2    # move file1 to another name: file2
$ mv file1 /tmp     # moves file1 to /tmp directory
$ rm file1          # delete file1

Mounting

As there are no drive letters, if we want to add additional drives they must be located within the file-system hierarchy. The directory where they are mounted on is the mount point. Usually a Linux System has already many different file systems mounted. (E.g. /proc and /sys and most likely /dev are virtual file systems generated on the fly by the kernel. Most people prefer /tmp and /home and /var in their own file system so that if one runs full the other is still available).

Here are the most important commands that help you with file systems

$ mount            # show all filesystems 
$ df               # disk free. show filesystem and how much there is space free
$ df -h            # like df but human readable (in GB or MB instead of long numbers)
$ mount /mnt/test  # mounts /mnt/test but only if it is defined in the file /etc/fstab
$ umount /media/dvd # unmount (note the missing n) but only if you have permission to do so

The file /etc/fstab has a table that describes the file systems that need to be mounted at boot time.

Absolute and Relative Path Names

When you say /home/anna/Documents/somefile.txt the this is the absolute path (Starting with /). When you are anna and you are in your home directory you could also write: Documents/somefile.txt or ./Documents/somefile.txt or ../anna/Documents/somefile.txt - those would be example of relative path (Starting from where you are).

Globbing

Sometimes you want to specify more then one file, where all have a common pattern. The bash shell helps you here with so called globbing. E.g. you can say ls *.txt to list all files ending with .txt

Unlike dos/windows, globbing in unix is done in the shell and the programs that are called will already receive the list of expanded file names. This means you can test the globing by using:

$ echo *.jpg     # lists the names of all jpg files in your directory

As the echos echos all its input, it can be used to see what the shell has expanded for you with its globbing rules.


Examples of Globbing in bash
*.txt all files ending with .txt
img????.jpg all files starting with img and ending with .jpg and with 4 numbers/letters after img.
[abxy]* all files starting with a,b,x or y.
*[0-9] all files ending with a digit
{Pi,Do}* all files or directories starting with either Pi or Do. E.g. Pictures, Documents, Downloads

Exercises

  • Try to move around in the file system hierarchy.
  • See how much free space you have on your system
  • Investigate which parts are in separately mounted file systems
  • go home
  • try out some globbing patterns to list some files
  • list all programs from /usr/bin with a name of 3 characters and starting with a c