More about files
The section highlights essential theory on files in the Linux system.
Everything is a file
Under the Linux hood, everything is a file: a text, a directory, your keyboard (read-only), your monitor (write-only), and so on. Keep this in mind to understand the behavior of Linux as you manage files and directories.
Linux is an extensionless system
A file extension is normally a set of 2 to 4 characters after the full stop at the end of a file, denoting what type of file it is. The following are common extensions:
file.exe—an executable file, or programfile.txt—a plain text filefile.png,file.gif,file.jpg—an image
In other systems, such as Windows, the extension is important, and the system uses it to determine what type of file it is. Under Linux, the system actually ignores the extension and looks inside the file to determine what type of file it is.
For instance, you have the myself.png file, which is a picture of you. You can rename the file to myself.txt or myself, and Linux would still happily treat the file as an image file. As such, it can sometimes be hard to know for certain
what type a particular file is. Luckily, there is a command called file you can use to find this out.
file [path]
Note that the command-line argument above is specified as a path, not a file. Whenever you specify a file or directory on the command line, it is actually a path. Also, because directories are a special type of file (as mentioned above), it would be more accurate to say that a path is a means to get to a particular location in the system and that location is a file.
Linux is case sensitive
This is very important and a common source of problems for people new to Linux. Other systems, such as Windows, are case insensitive when it comes to referring to files. Linux is not like this: you can have two or more files and directories with the same name but letters of a different case.
user@bash: ls Documents
FILE1.txt File1.txt file1.TXT
...
user@bash: file Documents/file1.txt
Documents/file1.txt: ERROR: cannot open 'file1.txt' (No such file or directory)
user@bash:
Linux sees these all as distinct and separate files.
Also, be aware of case sensitivity when dealing with command-line options. For instance, for the ls command, there are two options—s and S. Each of them does different things. A common mistake is to see an option that is the upper case but enter it as lower case and wonder why the output doesn't match your expectation.
Spaces in names
Spaces in file and directory names are valid, but you need to be careful with them. As you remember, a space on the command line is how you separate items. They are how you know the program name and can identify each command-line argument. For example, to move to a directory called Holiday Photos, the following would not work:
user@bash: ls Documents
FILE1.txt File1.txt file1.TXT Holiday Photos
...
user@bash: cd Holiday Photos
bash: cd: Holiday: No such file or directory
user@bash:
What happens is that Holiday Photos is seen as two command-line arguments. cd moves to whichever directory is specified by the first
command-line argument only. To get around this, identify to the terminal that you wish Holiday Photos to be seen as a single command-line argument. There are two ways to go about this—quotes and escape characters.
Quotes
The first approach involves using quotes to wrap the entire item. You can use either single or double quotes. Anything inside the quotes is considered a single item.
user@bash: cd 'Holiday Photos'
user@bash: pwd
/home/ryan/Documents/Holiday Photos
Escape characters
Another method is to use what is called an escape character, which is a backslash (\\). What the backslash does is escape (or nullify) the special meaning of the next character.
user@bash: cd Holiday Photos
user@bash: pwd
/home/ryan/Documents/Holiday Photos
In the above example, the space between Holiday and Photos would normally have a special meaning, which is to separate them as distinct command-line arguments. When you add a backslash in front of it, the special meaning is removed.
If you use tab completion before a space in the directory name, the terminal automatically escapes any spaces in the name for you.
Hidden files and directories
Linux has a simple and elegant mechanism for specifying that a file or directory is hidden. If the file or directory's name begins with . (full stop), it is considered to be hidden. You don't even need a special command or action to hide a file.
You may need to hide files and directories for a variety of reasons. For instance, configuration files for a particular user are hidden so that they don't get in the way of the user doing their everyday tasks.
To hide a file or directory, create the file or directory with a name beginning with . or rename it to be as such. Likewise, you can rename a hidden file to remove ., and it will become unhidden.
The ls command does not list hidden files and directories by default. However, you can change the default behavior by including the -a command-line option:
user@bash: ls Documents
FILE1.txt File1.txt file1.TXT
...
user@bash: ls -a Documents
. .. FILE1.txt File1.txt file1.TXT .hidden .file.txt
...
Activities for practicing
Now, let's put this stuff into practice. Have a go at the following:
- Try running the command file, giving it a few different entries. Make sure you use a variety of absolute and relative paths when doing this.
- Issue a command that will list the contents of your home directory, including hidden files and directories.