File Management Essentials
Linux is a file-oriented operating system. That means that many things an administrator has to do on Linux can be traced down to managing files on the Linux operating system. This chapter introduces you to essential file management skills. You learn how the Linux file system is organized and how you can work with files and directories. You also learn how to manage links and compressed or uncompressed archives.
File System Hierarchy
To manage a Linux system, you should be familiar with the default directories that exist on almost all Linux systems. This section describes these directories and explains how mounts are used to compose the file system hierarchy.
Defining the FHS
The file system on most Linux systems is organized in a similar way. The layout of the Linux file system is defined in the Filesystem Hierarchy Standard (FHS). Picture below shows an overview of the most significant directories that you’ll encounter on a BackSlash Linux system, as specified by the FSH.
So using the above Linux file system chart, we need to explore what each folder in the Linux file system is for, which will help us to better understand how BackSlash Linux works in general.
- / - this is known as “root”, the logical beginning of the BackSlash Linux file system structure. Every single file path in Linux begins from root in one way or another. / contains the entirety of your operating system.
- /bin - Pronounced “bin” (as opposed to “bine”), this is where most of your binary files are stored, typically for the Linux terminal commands and core utilities, such as cd (change directory), pwd (print working directory), mv (move), and so on.
- /boot - This is where all the needed files for Linux to boot are kept. Many people, like to keep this folder in it’s own separate partition on the hard drive, especially when dual-booting is involved. A key thing to note is that even when**/boot**is stored on different partition, it is stilllogicallylocated at /boot as far as BackSlash Linux is concerned.
- /dev - This is where your physical devices are mounted, such as your hard drives, USB drives, optical drives, and so on. Typically, your system hard drive is mounted under /dev/sda, whereas your USB thumb drive might be mounted under /dev/sde. You may also have different partitions on your disk, so you’ll see /dev/sda1, /dev/sda2, and so on. In Windows, when you go to “My Computer” or “Computer” and you can see all of the physical devices and drives connected to your computer, this is the equivalent of /dev in BackSlash Linux file structure.
- /etc - Pronounced “et-see”, although some also prefer to spell it out, is where configuration files are stored. Configurations stored in /etc will typically affect all users on the system; whereas users can also store configuration files under their own /home folders, which will only affect that particular user.
- /home - This is where you’ll spend the overwhelming majority of your time, as this is where all of your personal files are kept. The Desktop, Documents, Downloads, Photos, and Videos folders are all stored under the /home/username directory. You can also store files directly in your /home folder without going to a sub-folder, if you wish so. Typically, when you open a command-line terminal in BackSlash Linux, the default location that the terminal points to is your /home/username folder, unless you’ve manually changed the default location to something else.
- /lib - This is where libraries are kept. You’ll notice that many times when installing BackSlash Linux software packages, additional libraries are also automatically downloaded, and they almost always start with lib-something. These are basically the files needed for your programs on BackSlash Linux to work. You can think of this folder as somewhat equivalent to the Program Files folder on Windows, although it’s not exactly the same. Unlike Windows, libraries can be shared between many different programs, which results in BackSlash Linux installations typically being much more lightweight than Windows, because typically in Windows each program needs it’s own library installed, even if it’s redundant and already exists for another program. Surely a benefit of BackSlash Linux file system structure.
- /media - Another place where external devices such as optical drives and USB drives can be mounted.
- /mnt - This is basically a placeholder folder used for mounting other folders or drives. Typically this is used for Network locations, but you could really use it for anything you want.
- /opt - Optional software for your system that is not already managed by BackSlash Linux's package manager.
- /proc - The “processes” folder where a lot of system information is represented as files (remember, everything on BackSlash Linux is a file). It basically provides a way for the Linux kernel (the core of the operating system) to send and receive information from various processes running in the BackSlash Linux environment.
- /root - This is the equivalent to the /home folder specifically for the root user, also called the superuser. You really don’t want to touch anything in here unless you know what you’re doing.
- /sbin - Similar to /bin, except that it’s dedicated to certain commands that can only be run by the root user, or the superuser.
- /tmp - This is where temporary files are stored, and they are usually deleted upon shutdown, which saves you from having to manually delete them like is required in Windows.
- /usr - Contains files and utilities that are shared between users.
- /var - This is where variable data is kept, usually system logs but can also include other types of data as well.
Managing Files
As an administrator, you need to be able to perform common file management tasks. These tasks include the following:
- Working with wildcards
- Listing files and directories
- Copying files and directories
- Moving files and directories
- Deleting files and directories
Working with wildcards
When working with files, using wildcards can make your work a lot easier. A wildcard is a shell feature that helps you referring to multiple files in an easy way.
Wildcard | Use |
---|---|
* | Refers to an unlimited number of all characters. ls, for instance, shows all files in the current directory (except those that have a name starting with a dot). |
? | Used to refer to one specific character that can be any character. ls c?twould match cat as well as cut. |
[auo] | Refers to one character that may be selected from the range that is specified between square brackets. ls c[auo]twould match cat, cut, and cot. |
Listing files and directories
While working with files and directories, it is useful if you can show the contents of the current directory. For this purpose, you can use the ls
command. If used without arguments, ls shows the contents of the current directory. Some common arguments make working with ls easier.
Command | Use |
---|---|
ls -l | Shows a long listing, which includes information about file properties, such as creation date and permissions. |
ls -a | Shows all files, including hidden files. |
ls -lrt | This is a very useful command. It shows commands sorted on modification date. You’ll see the most recently modified files last in the list. |
ls -d | Shows the names of directories, not the contents of all directories that match the wildcards that have been used with the ls command. |
ls -R | Shows the contents of the current directory, in addition to all of its subdirectories; that is, itRecursively descends all subdirectories. |
TIP A hidden file on BackSlash Linux is a file that has a name that starts with a dot. Try the following: touch .hidden. Next, type ls. You will not see it. Then type ls -a. You’ll see it.
Copying files and directories
To organize files on your system, you’ll often be copying files. The cp
command helps you do so. Copying a single file is not difficult: Just use cp /path/to/file / path/to/destination
. To copy the file /etc/hosts to the directory /tmp for instance, use cp /etc/hosts /tmp
. This results in the file hosts being written to /tmp.
With the cp command, you can also copy an entire subdirectory, with its contents and everything beneath it. To do so, use the option -R, which stands for recursive. (You’ll see the option -R with many other commands also.) To copy the directory /etc and everything in it to the directory /tmp, you would, for instance, use the command cp -R /etc /tmp
.
While using the cp command, permissions and other properties of the files are to be considered. Without extra options, you risk permissions not being copied. If you want to make sure that you keep the current permissions, use the -a option, which has cp work in archive mode. This option ensures that permissions and all other file properties will be kept while copying. So, to copy an exact state of your home directory and everything within it to the /tmp directory, use cp -a ~ /tmp
.
A special case when working with cp are hidden files. By default, hidden files are not copied over. There are three solutions to work hidden files as well:
- cp /somedir/. /tmp* This copies all files that have a name starting with a dot (the hidden files, that is) to /tmp. It gives an error message for directories whose name starts with a dot in /somedir, because the -R option was not used.
- cp -a /somedir/. This copies the entire directory /somedir, including its contents, to the current directory. So, as a result, a subdirectory somedir will be created in the current directory.
- cp -a /somedir/.. This copies all files, regular and hidden, to the current directory.