Files and Directories




inodes and links

Each file on your system is represented by an inode: an inode contains all the information about the file. Each inode is linked into the filesystem by one or more hard links. Hard links contain the name of the file, and the inode number. The inode contains the file itself, i.e., the location of the information being stored on disk, its access permissions, the type of the file, and so on. The system can find any inode once it has the inode number.

A single file can have more than one hard link. What this means is that multiple filenames refer to the same file (that is, they are associated with the same inode number). However, you can't make hard links across filesystems: all hard references to a particular file (inode) must be on the same filesystem. This is because each filesystem has its own set of inodes, and there can be duplicate inode numbers between filesystems.

A symbolic links (also called symlinks) is a special file that “points to” a hard link on any mounted filesystem. When you try to read the contents of a symlink, it gives the contents of the file it's pointing to rather than the contents of the symlink itself. Since directories, devices, and other symlinks are types of files, you can point a symlink at any of those things.

  • inode: A location in disk containing, meta-file informations and the file content.
  • hard link: is a filename and an inode number.
  • symbolic link: is an inode that contains the name of a hard link.

A symlink pairs one filename with a second filename, while a hard link pairs a filename with an inode number.

Some important differences are:

  • Symlinks can cross filesystems. This is because they contain complete filenames, starting with the root directory, and all complete filenames are unique. Since hard links point to inode numbers, and inode numbers are unique only within a single filesystem, they would be ambiguous if the filesystem wasn't known.
  • You can make symlinks to directories, but you can't make hard links to them. Each directory has hard links — its listing in its parent directory, its . entry, and the .. entry in each of its subdirectories — but to impose order on the filesystem, no other hard links to directories are allowed. Consequently, the number of files in a directory is equal to the number of hard links to that directory minus two (you subtract the directory's name and the . link).
  • You can only make a hard link to a file that exists, because there must be an inode number to refer to. However, you can make a symlink to any filename, whether or not there actually is such a filename.
  • Removing a symlink removes only the link. It has no effect on the linked-to file. Removing the only hard link to a file removes the file.

For this reasons, it is usually preferred to use symlinks instead of hard links:

ln -s target_path symbol_path

References:

Permissions

Each file has 3 security groups:

  • Owner: Each file or directory has a specific owner or creator
  • Group Access: Each file or directory is assigned to a specific group
  • All Others: If a user is not the owner or is not assigned to the group, they are considered in the other category

The command chown changes the owner of a file:

chown user:group file

  • user: the new owner
  • group: the new group
number letters meaning
0 no permission
1 –x execution
2 -w- writing
3 -wx writing and execution
4 r– reading
5 r-x reading and execution
6 rw- reading and writing
7 rwx reading, writing and execution

The command chown changes the permission for a file:

chmod xyz file

  • x: the owner permissions [0-7]
  • y: the group permissions [0-7]
  • z: the all others permissions [0-7]

Another common needs is just to {add|remove} a {read|write|execute} permission to a file for all the security groups.

chmod {+|-}{r|w|x} file

Sticky bit

Historically, when set, it instructed the operating system to retain the text segment of the program in swap space after the process exited. This sped up subsequent executions by allowing the kernel to make a single operation of moving the program from swap to real memory. Thus, frequently-used programs like editors would load noticeably faster. Currently, this behavior is only operative in HP-UX, NetBSD, UnixWare, and Mac OS X.

The most common use of the sticky bit today is on directories. When the sticky bit is set, only the item's owner, the directory's owner, or the superuser can rename or delete files. Without the sticky bit set, any user with write and execute permissions for the directory can rename or delete contained files, regardless of owner. Typically this is set on the /tmp directory to prevent ordinary users from deleting or moving other users' files.

The sticky bit can be set using the chmod command and can be set using its octal mode 1000 or by its symbol t. For example, to add the bit on the directory /usr/local/tmp, one would type chmod +t /usr/local/tmp. Or, to make sure that directory has standard tmp permissions, one could also type chmod 1777 /usr/local/tmp.

Archive Files

Extraction
type command
tar.gz or tgz tar xzf archive.tar.gz
tar.bz2 tar xjf archive.tar.bz2
bz2 bunzip2 archive.bz2
zip unzip archive.zip
  • x extract files from an archive
  • z filter archive through gzip
  • j filter archive through bzip2
  • v verbosely list files processed
  • f use archive file
  • -C dir change to directory “dir”

Size of a directory

du -h | tail -n 1

Navigation

List information about the FILEs in the current directory:

ls

  • -l: use a long listing format
  • -a: do not ignore entries starting with ”.”.
  • -F: append indicator (one of */=@|) to entries:
    • * executable
    • / directory
    • = socket
    • @ symbolic link
    • | FIFO
  • -h: print sizes in human readable format
  • –color: control whether color is used to distinguish file types.
  • -S: Sort by file size
  • -t: Sort by modification time


Change the current directory to DIR. The variable $HOME is the default DIR.

cd [DIR]

Print the current working directory:
pwd

Search

TODO: which, whereis, find, slocate

Find files that contain a text string:

grep -nr "text to find" *

Reference

Editing & Viewing

TODO: vi, more, less, and most

Simple Output

TODO: cat and echo

Creation

TODO: touch and mkdir

Copy and Move

TODO: cp and mv

Deletion

TODO: rm and rmdir

File System Layout

Directory Functionalities
/ This is the root directory. This is where the whole tree starts.
/bin This directory contains executable programs which are needed in single user mode and to bring the system up or repair it.
/boot Contains static files for the boot loader. This directory only holds the files which are needed during the boot process.
/dev Special or device files, which refer to physical devices.
/etc Contains configuration files which are local to the machine. Some larger software packages, like X11, can have their own subdirectories below /etc. Site-wide configuration files may be placed here or in /usr/etc. Nevertheless, programs should always look for these files in /etc and you may have links for these files to /usr/etc.
/home On machines with home directories for users, these are usually beneath this directory, directly or not. The structure of this directory depends on local administration decisions.
/lib This directory should hold those shared libraries that are necessary to boot the system and to run the commands in the root filesystem.
/mnt Is a mount point for temporarily mounted filesystems
/proc This is a mount point for the proc filesystem, which provides information about running processes and the kernel.
/sbin Like /bin, this directory holds commands needed to boot the system, but which are usually not executed by normal users.
/tmp This directory contains temporary files which may be deleted with no notice, such as by a regular job or at system boot up.
/usr This directory is usually mounted from a separate partition. It should hold only sharable, read-only data, so that it can be mounted by various machines running Linux.
/usr/bin This is the primary directory for executable programs. Most programs executed by normal users which are not needed for booting or for repairing the system and which are not installed locally should be placed in this directory.
/usr/include Include files for the C compiler.
/usr/lib Object libraries
/usr/local/include Include files for the C compiler not installed through a standard Debian dev package
/usr/local/lib Object libraries not installed through a standard Debian dev package
/usr/sbin This directory contains program binaries for system administration which are not essential for the boot process, for mounting /usr, or for system repair.
/usr/share This directory contains subdirectories with specific application data, that can be shared among different architectures of the same OS. Often one finds stuff here that used to live in /usr/doc or /usr/lib or /usr/man.
/usr/src Source files for different parts of the system, included with some packages for reference purposes. Don't work here with your own projects, as files below /usr should be read-only except when installing software
/usr/src/linux This has always been the traditional place where kernel sources were unpacked. This was important on systems that /usr/include/linux was a symlink here. You should probably use another directory for building the kernel now.
/var This directory contains files which may change in size, such as spool and log files.
/var/backups This directory is used to save backup copies of important system files.
/var/lock Lock files are placed in this directory. The naming convention for device lock files is LCK..<device> where <device> is the device's name in the filesystem. The format used is that of HDU UUCP lock files, i.e. lock files contain a PID as a 10-byte ASCII decimal number, followed by a newline character.
/var/log Miscellaneous log files.
/var/run Run-time variable files, like files holding process identifiers (PIDs) and logged user information (utmp). Files in this directory are usually cleared when the system boots.

Links

linux/files_and_directories.txt · Last modified: 2010/08/10 (external edit)
CC Attribution-Noncommercial-Share Alike 3.0 Unported
Valid CSS Driven by DokuWiki Recent changes RSS feed Valid XHTML 1.0