



(4 ratings)
Like in MS Windows, the files on a Linux system are arranged in a hierarchial directory structure. This means that the files are organized the same way as in Windows: in a tree-like pattern of directories (or folders in windowspeak), and those directories may contain files or other directories, which in turn may contain more files or directories, and so on...
However, there are many directory trees in Windows. There's one tree starting from C:, other starting from A:, and other trees starting from other drives. But on a Linux system all the files and directories are under the very same tree. The first directory in this system, the one where the tree starts from, is called the root directory. Everything, every single file and directory on a Linux system, is under this root directory.
< Finding out where you are >
Like
in a graphical file manager, you're always working in a single
directory when you're at the CLI, and the directory can be anywhere in
the filesystem tree. The directory contains files and a way to go to to
its parent directory and its subdirectories. The directory where you're
currently working in, is called (surprise surprise) the working directory. To find out where the working directory is, you use the pwd command that stands for print working directory. Now type it and see where you are.
me@puter: ~$ pwd
/home/me
me@puter: ~$
You're most likely in your home directory
now. That's the directory where you put your personal files, and
usually the working directory is set to your home directory when you
log in to your Linux system. The home directories of users are located
under /home, and usually the name of your home directory is /home/your_user_name.
< Listing the contents of a directory >
Now you know where you are. Next you might want to know what there is. For finding it out you use the ls command. Just type ls, and you'll get a list of all the files and subdirectories that your working directory contains.
You have a way of controlling what the ls command displays and how. Just like almost every Linux command, ls can have additional options that change the behavior of it. For example, try typing this:me@puter: ~$ ls -l
You get again a list of the files in your working directory, but as you can see, the output of ls is now different with the -l option. You get additional info about the files your directory contains. What happened here? The shell, bash, took the -l you typed and passed it along to ls. This option told ls to display more info about the files in the current directory. These additions to commands are often called parameters or arguments or options. So, in this case, ls was the command and -l was the option.
You
can separate the command from its options with a space, and you can
also add more options and separate them from each other with a space as
well. For example, the -r option tells ls to
display the files in reverse order. If you want to display both the
files in reverse order and get more info about the files, you could do
it with:me@puter: ~$ ls -l -r
In most cases you can use a shorter form when using multiple options. The following does the same thing:me@puter: ~$ ls -lr
Many
commands accept a lot of arguments, and some commands also accept file
or directory names as arguments. For example, when you give the ls
command without any arguments, it displays the contents of the current
directory, as you already saw. If you want to display the contents of
some other directory, you can give the pathname of the desired directory as an argument to ls.
Like the name suggests, a pathname is the path you take along the
directories to the destination directory. There are two kinds of
pathnames, absolute pathnames and relative pathnames.
< Absolute pathnames >
An absolute pathname simply tells you what the complete path to a certain file or directory is. Because all the files on Linux are under the root directory, an absolute pathname must start from the root directory and then follow the filesystem tree directory by directory until you get to the desired file or directory.
All absolute file names start with a slash
because the slash indicates the root directory. For example, there's a
directory called /usr/X11R6/bin on your system. The path
here means that you start from the root directory (/), go to its
subdirectory "usr", which contains a directory called "X11R6", which in
turn contains the "bin" directory. As you noticed, you use the slash
not only for indicating the root directory, but also for separating the
directories on the path. This is different from Windows where you use a
backslash for separating the directories.
Now let's experiment a little. If you want to see what the /usr/X11R6/bin directory contains, but it isn't your working directory, you can give the path as an argument to ls so that it knows that it should work on /usr/X11R6/bin instead of the working directory:$ ls /usr/X11R6/bin
You can also use the other options of ls, such as:$ ls -l -r /usr/X11R6/bin
This would do the same:$ ls -lr /usr/X11R6/bin
So this is the deal with absolute pathnames. Not hard. You know a pathname is an absolute one if it starts with the slash.
< Moving around in directories >
So far, you know how to find out your current working directory (pwd), list the contents of directories (ls), and you understand what are absolute pathnames. Soon you'll know what are relative pathnames, but before it I'll teach you how to change the working directory. You use the cd command, like in MS-DOS, and you give the desired directory as an argument to it. Now, change your working directory to /usr/X11R6:me@puter: ~$ cd /usr/X11R6
If you want, you can check if your new working directory really is what you wanted:
me@puter: /usr/X11R6$ pwd
/usr/X11R6
me@puter: /usr/X11R6$
Maybe
you noticed that the command prompt has changed. Usually it's
configured to display the name of the working directory. This makes
your life a little easier and you don't have to type pwd all the time!
< Relative pathnames >
Now it's finally time to discuss relative pathnames. As you've learned, an absolute pathname starts from the root directory. A relative pathname, however, starts from the working directory. This is why you need some special symbols for indicating the relative positions in the filesystem. These symbols are a dot (.) and two dots (..) and they mean the working directory and the parent directory, respectively.
Let's see how these things work. Now your current working directory should be /usr/X11R6. List the contents of it:
me@puter: /usr/X11R6$ ls
bin include lib man
me@puter: /usr/X11R6$
There
should be a directory called "lib", and we want to find out what it
contains but we don't want to change the working directory. There are
two ways of doing this. We can use the absolute pathname:me@puter: /usr/X11R6$ ls /usr/X11R6/lib
Or using the relative pathname:me@puter: /usr/X11R6$ ls ./lib
Here, the dot in the path ./lib refers to the working directory, which is /usr/X11R6.
This saved some typing! But it'll save even more typing if you omit the
leading dot. In most cases you don't need it, so this would've been the
same as the above:me@puter: /usr/X11R6$ ls lib
Now let's change the working directory to /usr/X11R6/bin. There are again two ways of doing this. First, using absolute pathname:me@puter: /usr/X11R6$ cd /usr/X11R6/bin
Or using relative pathname, remember that you don't have to type the dot:me@puter: /usr/X11R6$ cd bin
Now your current working directory is /usr/X11R6/bin. Let's change it to /usr/X11R6 which is the parent directory. Again you could use the absolute pathname:me@puter: /usr/X11R6/bin$ cd /usr/X11R6
But it's faster to use the two dots that mean "parent directory":me@puter: /usr/X11R6/bin$ cd ..
Note the space between cd and the dots.
As you can see, in many cases using relative pathnames instead of absolute ones saves some typing.
20 Random Tutorials from the same category :
Introduction To Unix Signals Programming
Unix And C/C++ Runtime Memory Management For Programmers
How to view text files in Linux
Text scrolling off the screen?
Running multiple X sessions
Basic Graphics Programming With The Xlib Library
Understanding file permissions and modifying them using chmod
How to edit and understand /etc/fstab
How to set Shell Environment Variables (bash shell)
Automatic file name completion
Redirecting standard input and output
Wildcards, Quotes, Back Quotes and Apostrophes in shell commands ( * ? [] " ` ')
Debugging "C" And "C++" Programs Using "gdb"
Automating Program Compilation - Writing Makefiles
Installing software from source in Linux
Multi-Threaded Programming With POSIX Threads
How to find files in Linux using 'find'
Moving around in the Linux file system
How and when to use the dd command?
The great features of Linux - an alternative view













