Quantcast
submit to reddit       
       

Files and directories in Linux

The root of the Linux file system is the '/' directory. All other drives, directories and files are contained in this directory. The Linux directory structure follows conventions laid down by the Unix directory structure, where different types of files put in specific directories:

  • /bin - binaries/exectuables
  • /boot - boot parameters and kernel
  • /dev - devices
  • /etc - configuration files
  • /home - users' home directories
  • /lib - libraries and system modules
  • /media - removable drives are mounted here
  • /mnt - permanently attached devices are mounted here
  • /proc - virtual directory tree containing information about the operating system
  • /root - root user's home directorydirectory
  • /tmp - temporary files
  • /usr - level programs, libraries
  • /var - dynamic data like logs, web site content, print jobs

Every user has their own home directory. The default user on a Raspberry Pi is named pi, so the home directory for this user is /home/pi.

When packages are installed, their configuration files are place in /etc. If you have installed Apache and Samba, you will find their configuration files in /etc/apache2 and /etc/samba.

The /proc directory contains virtual files that represents operating system information in files. For example, this command will display information about the CPU:

$ cat /proc/cpuinfo Processor : ARMv6-compatible processor rev 7 (v6l) BogoMIPS : 697.95 Features : swp half thumb fastmult vfp edsp java tls CPU implementer : 0x41 CPU architecture: 7 CPU variant : 0x0 CPU part : 0xb76 CPU revision : 7 Hardware : BCM2708 Revision : 000d Serial : 00000000da09f419

The cat command simply prints the contents of a file in the terminal. The file cpuinfo is Linux's representation of the CPU that it's running on.

Disk drives

Disk drives aren't represented in Linux in the same way as they are in Windows. In Linux, a computer's peripheral devices are generally represented as files in /dev. You can see your disk drives in the /dev directory by opening a terminal (double click on the LXTerminal icon on the desktop) and typing this command:

$ sudo fdisk -l

You should see output that looks like this:

Disk /dev/mmcblk0: 3965 MB, 3965190144 bytes 4 heads, 16 sectors/track, 121008 cylinders, total 7744512 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00014d34 Device Boot Start End Blocks Id System /dev/mmcblk0p1 8192 122879 57344 c W95 FAT32 (LBA) /dev/mmcblk0p2 122880 7744511 3810816 83 Linux

This is information about the SD card that your Pi uses as a disk drive. You can see the Raspberry Pi's SD card listed as /dev/mmcblk0, and the two partitions on it are listed as /dev/mmcblk0p1 and /dev/mmcblk0p2.

Note that you can't access the files on your SD card via /dev/mmcblk0p1 or /dev/mmcblk0p2. These files are used by Linux device drivers to interact with the card.

Mounting drives

To access the files on the card, Linux must 'mount' it. This means Linux must open the disk and link the disk's directories with the rest of the file system. The directories are attached to an existing directory. For example, I have created a directory on my Pi called usbhdd in /media. When I connect a USB hard disk to my Pi and mount it, all the files and folders can be accessed in /media/usbhdd as if the disk was just a subdirectory in /media.

Mounting drives in this way makes it possible for Linux to have a single, continuous file system seamlessly spanning multiple drives.

File System Formats

Linux uses different disk drive formats for storing files than Windows. On Windows, disks and SD cards are formatted with the FAT32 file system or NTFS. On Linux, the most common file system is ext4. Linux can read drives formatted with Windows file systems, but Windows can't access drives formatted with Linux file systems.

Absolute and relative paths

When you specify the location of a file, you can use its complete path. If I create a file in my home directory called myfile.txt, its complete path is /home/pi/myfile.txt. This is known as an absolute path.

It is also possible to use a relative path to access this file. When I log into Linux as pi, the default working directory is /home/pi. I can reference my file as ./myfile.txt. Unix systems use a single dot to refer to the current working directory, so a path starting with './' is relative to the current directory. Typing this command lists the contents of the current directory:

$ ls ./

Two dots are used to refer to the next directory up in the directory tree. If the current working directory is /home/pi, then typing this command lists the contents of /home:

$ ls ../

The '~' character refers to the current user's home directory. If I use the 'cd' command to change the current working directory to /etc (for example), this command will list the contents of my home directory:

$ ls ~

Hidden files and folders

File names and directory names that start with a dot are hidden. On a fresh install of Raspbian, if you type ls ~, there are only a couple of directories. If you type ls -a ~, the ls command will list everything in the home directory, including hidden files and folders.

See also: Connect your Raspberry Pi to a USB hard disk.


Comments

Comments

comments powered by Disqus



Follow me