Docsity

Linux File System: Understanding Files and Directories, Study notes of Biotechnology

Toggle sidebar
bg1
Page 1 of 16
Reference :-- http://lowfatlinux.com/#INTRO
LINUX FILES
How Does The Linux File System Work?
In this section, you'll learn how to work with files and directories in a Linux system. You'll
understand how the file system is structured to separate system data from personal data and how
to navigate through it. We'll cover two ways to manipulate your files: using the command line
and the Midnight Commander.
When you're done, you'll be creating, deleting, copying, renaming, listing, linking, and protecting
your files like a pro. You'll also learn the Nine Deadly Keystrokes--or how to wipe out the entire
file system without really trying. (But don't worry; this command can affect you only if you're
logged in as root.)
Log in as hermie and try the examples in this section as you read through it. You'll find that the
Linux file system is a lot like DOS, only more robust and flexible.
What Is a Linux File?
We've created a structured file system and filled it with a whole bunch of files--without knowing
what Linux files are. Here's what files are in the Linux world and how they are named.
Types of Files
In most operating systems, a file is simply a blob of data stored on disk with a unique name. A file
could be a list of names and numbers, a cheesecake recipe, or an executable program. But under
Linux, everything is a file. In addition to data and executable files, Linux treats directories and
even the various components of your computer as files.
This means there are files that represent your keyboard, console, printer, CD-ROM, and even your
system's RAM. These special files are called devices, and they are found in the /dev directory (see
Figure 4-1). (If you look in this directory later, you'll see files such as /dev/fd0 (a floppy drive),
/dev/tty1 (a console), and others you may be able to identify by their names.) When Linux (or
any Unix program) needs to talk to a physical hardware device, it does so by simply reading
from or writing to one of these special files.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Linux File System: Understanding Files and Directories and more Study notes Biotechnology in PDF only on Docsity!

Reference :-- http://lowfatlinux.com/#INTRO

LINUX FILES

How Does The Linux File System Work?

In this section, you'll learn how to work with files and directories in a Linux system. You'll understand how the file system is structured to separate system data from personal data and how to navigate through it. We'll cover two ways to manipulate your files: using the command line and the Midnight Commander.

When you're done, you'll be creating, deleting, copying, renaming, listing, linking, and protecting your files like a pro. You'll also learn the Nine Deadly Keystrokes--or how to wipe out the entire file system without really trying. (But don't worry; this command can affect you only if you're logged in as root .)

Log in as hermie and try the examples in this section as you read through it. You'll find that the Linux file system is a lot like DOS, only more robust and flexible.

What Is a Linux File?

We've created a structured file system and filled it with a whole bunch of files--without knowing what Linux files are. Here's what files are in the Linux world and how they are named.

Types of Files

In most operating systems, a file is simply a blob of data stored on disk with a unique name. A file could be a list of names and numbers, a cheesecake recipe, or an executable program. But under Linux, everything is a file. In addition to data and executable files, Linux treats directories and even the various components of your computer as files.

This means there are files that represent your keyboard, console, printer, CD-ROM, and even your system's RAM. These special files are called devices, and they are found in the /dev directory (see Figure 4-1). (If you look in this directory later, you'll see files such as /dev/fd0 (a floppy drive), /dev/tty1 (a console), and others you may be able to identify by their names.) When Linux (or any Unix program) needs to talk to a physical hardware device, it does so by simply reading from or writing to one of these special files.

What Are The Rules for Linux File Names?

When naming files, you can use uppercase and lowercase letters, numbers, and certain special characters. It's a really good idea to stick with letters, numbers, and the dash, dot, and underscore characters to avoid trouble and confusion.

Note: Don't use asterisks, backslashes, or question marks in Linux file names--these characters have special meaning to the shell and could cause your commands to do something quite different from what you intended. Also avoid using a dash as the first character of a file name, since most Linux commands will treat it as a switch.

Files starting with a dot are hidden files. They behave just like any other file, except that the ls (list files) command will not display them unless you explicitly request it to do so. Your .profile file is an example of a hidden file.

Also remember that Linux file names are case sensitive, which can be difficult to get used to if you have a DOS background. Linux allows you to have unique files named goodstuff , GOODSTUFF , and GoodStuff in the same directory.

It's best to always use lowercase in Linux unless you can think of a good reason to use uppercase or mixed case. Most Unix people use lowercase almost exclusively, but aside from this "cultural" point, there's another good reason to use lowercase. If you're sharing or accessing a DOS file system

with Linux, DOS will not be able to see the files that have uppercase or mixed-case file names.

Unlike under DOS, the dot character (.) has no special meaning. You're not limited to the eight dot three ( xxxxxxxx. yyy ) style of naming because Linux treats the dot just like any other character; you can name a file Some.Yummy.CHEESECAKE.Recipes if you're so inclined.

Along these lines, Linux executables do not need or use a special extension such as .exe or .bat. Linux will happily run a program file named zippity just as readily as it will run DOODAH.EXE.

And here's another slight difference between Linux and DOS filesystems. Linux uses the forward slash (/) in path names, and DOS uses the backslash (). Don't blame this little quirk on Linux though... the DOS filesystem was originally modeled after Unix! In fact, the popular rumor is that Bill Gates and Company implemented the DOS filesystem just differently enough from the Unix filesystem to avoid being criticized for stealing the idea. The same charge is made about the DOS batch file (.BAT) utility, since it bears striking similarities to the Unix shell scripting languages, but I digress....

for Linux and other installed software

. Pronounced "slash et-see."

/home Contains the home directories (personal storage) for each user on the system. Pronounced "slash home."

/sbin Contains more Linux binaries (special utilities not for general users). Pronounced "slash ess-bin."

/root The home directory for the root user; not to be confused with /. Some Linux systems use /home/root instead of /root. Pronounced "slash root."

/usr Contains system programs and other files for general users such as games, online help

, and documentation. By convention, a user should not put personal files in this directory. Pronounced "slash user."

Note: Unless you are an All-Knowing Linux Guru, you should never combine or delete any of the directories (or files therein) that appear just below the root directory. This could prevent your system from booting or operating properly.

Can You Explain Linux Directory Terminology?

Subdirectory A directory within the current directory.

Parent directory The directory above the current one. Every directory except the top level has a parent. If you are in the /usr/spool directory, then /usr is the parent.

Home directory A user's personal directory. For example, if your user name is hermie, your home directory is /home/hermie. The user normally has complete control over all files stored in directories

beneath the home directory. Exception: If the root user copies a file to your home directory, root still owns and controls that file.

Root directory The top of the file system, denoted by a slash. Only subdirectories appear below this directory. Don't confuse this with /root, the home directory for the root user. When speaking, always refer to / as "the root directory" and to /root as "slash root."

Absolute file name A file name that is valid no matter where you are in the file hierarchy. In practice, this means it must start with a slash and specify the full path to the file. For example, /home/hermie/recipes/sludge_fudge is an absolute file name, but sludge_fudge is not.

Relative file name A file name that specifies a file relative to the current directory. For example, if you were in hermie's home directory, /home/hermie/, you would reference that healthy fudge recipe as recipes/sludge_fudge.

The single-dot, double-dot, and tilde notations are useful in conjunction with the cd command, but they can also be used in any Linux command where you need to enter a file or directory name.

TIP: One common mistake people make in navigating Linux directories

is using a slash in front of file names when it is not needed. Suppose you have a bin directory under your home directory. If you were at /home/hermie and you entered cd /bin , you'd end up at the bin underneath the root directory--oops. (Linux understands the slash before bin as telling it to go to the root directory first and then to the bin directory just below the root.) The correct way to reach the bin directory under /home/hermie is to type from within your home directory (/home/hermie) cd bin. Don't use a leading slash unless you're sure you want to start at the top (root) of the file tree.

How Do I List My Linux Files?

ls lists the files in the current directory.

ls recipes lists the files in another directory using relative addressing.

ls /usr/bin lists the files in another directory using absolute addressing.

By default, ls prints a simple, columnar list of your files; but it will list your files in many different formats if you add one or more flags on the command line. To use any of them, simply type the ls command at the prompt, followed by a space, and then add a switch by typing a hyphen followed by the flag, like this:

$ ls -F

Here are some of the most commonly used ls flags:

a Lists all files, including hidden ones.

l Displays the file list in long format, including file details like size, time stamp, and owner.

F Adds a slash after the name for directories, an asterisk

for executables, and an at sign (@) for linked files.

r Reverses the sort order (alphabetic or time).

t Sorts the list by the time each file was created.

Here's a look at the contents of a directory named animals, using some of these flags:

$ ls -F animals cat_info cow_info dog_info pig_info slugs/ zippity*

In this case, ls returns just the file names in columnar format, but with a few marks, thanks to the -F flag. The slash suffix on slugs indicates that it is a directory, and zippity is identified as an executable.

$ ls -al animals -rw-r--r-- 1 hermie users 1758 Mar 17 23:17 .hoohah -rw-r--r-- 1 hermie users 45090 Mar 23 23:17 cat_info -rw-r--r-- 1 hermie users 64183 Feb 14 22:07 cow_info -rw-r--r-- 1 hermie users 115032 Jan 06 11:14 dog_info -rw-r--r-- 1 hermie users 248 Jan 16 09:18 pig_info

How Do I Display the Contents of a Linux File?

Displaying File Contents

You know how to get a list of files. But what if you want to take a peek at a file's contents? Several commands, including cat , more , and less , can be used to display files. Let's see how they work in this example:

$ cat pig_info Pigs make great pets, but they are not dogs or cats. Maybe somewhere in between. I tend to believe that they have more personality. They do require significant amounts of attention, love, and scratches. But they aren't always snuggly in return.

The cat command (short for concatenate) shown in this example is like the DOS type command. In response to the command cat pig_info , the system simply splatters the file on the screen. If there is more data in the file than will fit on one screen, the contents whiz by before you can see it. The more command solves this problem by displaying the file screen by screen:

$ more dog_info Affectionately known as the "Toller," the Nova Scotian Duck Tolling Retriever was once called the Little River Duck Dog, since it was developed in the Little River district of Yarmouth County, Nova Scotia. This engaging dog is a specialist with waterfowl. Tolling, or luring, is the practice of tricking ducks into gunshot range. Hunters had long observed this behavior --More--(01%)

The more command pauses when the screen fills up and waits for you to press the spacebar before it rolls out another screenful of text. A legend at the bottom of the screen tells you what percentage of the file has been displayed so far. You can press ctrl-C to quit more before reaching the end of the file.

The less command works like more except that it lets you move both forward and backward in the file while the command is running--use the B key on your keyboard to back up one screen and the spacebar to move forward and show the next screen.

Although all of these commands will let you view the contents of a file in different ways, none allow you to change the data in the file. If you want to view and update a file, use one of the text editors discussed in the "Text Editors" section.

For more information on the cat command, see the cat manual.

How Do I Copy and Rename Linux Files?

Copying Files

Copying files under Linux is similar to copying files under DOS. Here's an example using the cp (copy) command:

$ cp goulash recipes/hungarian $ cp stuff stuff.bak

The first example copies the goulash file from the current directory and stores it in your recipes/hungarian directory. The second example creates a file called stuff.bak in the current directory that is identical to the source file stuff.

Note: The cp command will overwrite files with the same name without warning. To be prompted before overwriting, use the -i flag, like so:

$ cp -i goulash recipes/hungarian cp: overwrite recipes/hungarian/goulash (y/n)?

Renaming Files

Use the mv command to rename a file or move it to another directory, like so:

$ mv stuff junk $ mv junk trashcan

The first example renames the file stuff as junk , and the second moves the file junk into a directory called trashcan.

Note: The mv command will wipe out files without warning. To be prompted before overwriting, use the -i flag, like so:

$ mv -i stuff junk mv: overwrite junk (y/n)?

How do I Delete Files and Directories?

Deleting Files

Ready to do a little damage? The rm command removes a file (assuming you have permission to do so) without even saying hasta la vista. Be sure you really want to delete your files before you use rm , because once the files are gone, they're not coming back. For example,

$ rm wallet

immediately deletes the file named wallet in the current directory without prompting. If you want to be prompted before the deletion, use the -i flag.

Note: There is one little safety feature in rm --it won't delete a directory (unless you use the -r flag).

One other rm flag is -f , which translates roughly to "Don't ask me any questions--just delete the files." While rm normally asks for confirmation before deleting a write-protected file, the -f (force) flag overrides this prompt.

Be careful with the rm command, since the multiuser nature of Linux does not allow for undelete as in DOS. As soon as you let go of the space occupied by a file, the operating system is likely to use it for something else.

Deleting Directories

If you created a directory named spelunking six months ago, and now you're not so keen on crawling through slimy caves, the rmdir command may be able to help:

$ rmdir spelunking

This command removes the specified directory, but only if it's empty. If the directory contains files or subdirectories, you'll have to delete them using rm first.

One alternative to painstakingly removing all the contents of a directory that you just want to make disappear is to use the rm -r command. The -r flag gives rm license to kill directories, their files, and even their subdirectories. Be very sure you understand what's about to happen before using a command like this:

$ rm -r spelunking

Let's just say that careless use of the -r flag might end your spelunking hobby prematurely.

How Do I Use Wildcards to Select Files?

Using Wildcards

Wildcards come in quite handy when you want to operate on more than one file at a time. You can use wildcards with any of the commands in this section to list, copy, rename, or delete groups of files or directories.

The asterisk (*) character will match any number of characters in a file name, so consider these examples in light of the animals directory we used earlier.

$ ls -l c* -rw-r--r-- 1 hermie users 45090 Mar 23 23:17 cat_info -rw-r--r-- 1 hermie users 64183 Feb 14 22:07 cow_info **$ mv inf ../docs $ rm -f ***

The first command lists only those files that begin with the letter "c." The second will move files with the term inf anywhere in the name to another directory. The third command will delete all (nonhidden) files from the current directory.

Note: The expansion of the wildcard is done by the shell, not the command you are running. This means that when you enter the command

**rm -f ***

the shell expands the wildcard by replacing it with all files that match and actually ends up executing this command:

rm -f cat_info cow_info dog_info pig_info slugs zippity

This might seem a bit arcane, but it's actually important. Since the shell handles the wildcards in Linux, you don't have to worry about which commands accept wildcards and which don't. This is quite different from DOS, where only certain commands work with wildcards.

Here's a little pop quiz: What will be left in the animals directory if you execute the rm command in the preceding example? Try it and then use the ls -al command--you may be surprised at the answer.

(pick one or more) (pick one) (pick one or more)

u For the owner + Grant access r For read access

g For the group - Deny access w For write access

o For all others x For execute access

Here are some examples:

chmod o-r pig_info Remove read access from all others.

chmod g+rw pig_info Grant read and write access to group.

chmod ugo+x zippity Grant execute access to everybody.

In effect, you're saying "change the mode for these people by adding/removing their access to read/write/execute the file named whatever ." Just pick the proper combination of flags in each of the three columns, depending on what type of access you want for the file.

Note: If you give chmod a directory name instead of a file name, the permissions have slightly different meanings. For a directory, read access means that you can list the files with the ls command; write access allows you to create or delete files; and execute access gives you the ability to change to that directory with the cd command.

Transferring Ownership of a File Using Chown

If you are logged in as root, you can transfer ownership of a file or directory (if you move it into another user's directory) using the chown command.

To tell chown what to do, just give it the new owner and the file name, like this:

chown sigmund zippity

This will make sigmund the owner of zippity. Once you've transferred ownership, sigmund will be able to set the file's permissions (with chmod ) if he wants to.

For more information on the chmod command, see the chmod manual.