Now that we’ve learned all about the terminal and what it can do, we’re going to start using it and begin showing off the power of it. We’ll use the pwd, cd and ls tools to move around and and list directory contents, and we’ll also take a look at the file and less tools.
First let’s take a look our first tool we’ll be using today, pwd. The pwd tool means print working directory. This tool tells you where you currently are in the directory tree. The cd tool is used to change from one directory to another. cd is the command, while the directory path you wish to change to is the argument. The ls command is used to show the contents of a given directory. It uses the same syntax as the cd command, though ls also has various flags you can use to tell it how exactly to list your files.
Assuming you’ve already got the terminal open let’s see what we can do with these tools. Let’s start with pwd. If you ever want to know what directory you’re currently in, this is where pwd comes in.
NIfty, huh? Now we’re not gonna learn anything just sitting here. Let’s get going! To move from directory you can use the cd tool. Lets go to the root directory.
andrew@debian:~$ cd /
Notice how the tilde behind the “$” changed to a “/”? This part is to let you know what directory you’re in. similar to pwd, right? It is, actually. The tilde is a sort of shorthand way of saying /home/username. The root directory or / is the directory of directories. It’s the base of where everything is and everything is connected to it in some fashion. Let’s see just what’s in here.
bin dev initrd lost+found opt sbin sys var
boot etc initrd.img media proc selinux tmp vmlinuz
cdrom home lib mnt root srv usr
Before we begin the tour, lets look a bit more at the cd tool.
andrew@debian:/$ cd /usr/bin
Now, here’s a couple interesting tricks you can do with the cd tool. Now lets say we wanted to go up a directory. we could do
andrew@debian:/$ cd /usr
or we could do something like
andrew@debian:/$ cd ..
Use the pwd tool to show what each one did. Now lets say we wanted to go back to /usr/bin. We could simply do
andrew@debian:/$ cd /usr/bin
like we did before, or we can simply do
andrew@debian:/$ cd bin
Use pwd to compare your results. Now lets take a look at a few more tools, ls, less, and file. The ls tool is used to list the contents of directories. Note how we already used it to look inside the / directory earlier. Inside a directory could be text files, scripts, compressed files, or even more directories. The ls tool is actually quite a flexible tool and can tell us a lot of info. Let’s back out of the /usr/bin directory to /. Now, we know if we use ls here, it’ll list the contents of the / directory. Without moving oureselves anywhere, lets look in the /bin directory.
andrew@debian:/$ ls /bin
arch chmod fuser mktemp readlink umount
bash chown grep more rm uname
bunzip2 cp gunzip mount rmdir uncompress
busybox cpio gzexe mountpoint rnano vdir
bzcat csh gzip mt run-parts which
bzcmp date hostname mt-gnu sed zcat
bzdiff dd ip mv setpci zcmp
bzegrep df kill nano sh zdiff
bzexe dir ln nc sleep zegrep
bzfgrep dmesg loadkeys netcat stty zfgrep
bzgrep dnsdomainname login netstat su zforce
bzip2 echo ls pidof sync zgrep
bzip2recover ed lsmod ping tar zless
bzless egrep lsmod.modutils ping6 tcsh zmore
bzmore false lspci ps tempfile znew
cat fgconsole mkdir pwd touch
chgrp fgrep mknod rbash true
The ls tool can look into any directory, whether you’re currently in it or not. We can also list the contents of two different directories.
andrew@debian:/$ ls /home /boot
config-2.6.18-6-686 initrd.img-2.6.18-6-686 System.map-2.6.18-6-686
grub initrd.img-2.6.18-6-686.bak vmlinuz-2.6.18-6-686
Another thing about the ls tool are flags. Flags on a command tell the command to do it’s job a slightly different way. For instance, ls will list the contents of a given directory. if we ad the -l flag to it, it’ll give us more information. The -l flag is short for “long”.
andrew@debian:/$ ls -l /boot
-rw-r–r– 1 root root 70682 2008-10-13 11:29 config-2.6.18-6-686
drwxr-xr-x 2 root root 4096 2008-11-06 02:43 grub
-rw-r–r– 1 root root 4901618 2008-11-05 02:34 initrd.img-2.6.18-6-686
-rw-r–r– 1 root root 4499289 2008-11-05 02:16 initrd.img-2.6.18-6-686.bak
-rw-r–r– 1 root root 723233 2008-10-13 17:34 System.map-2.6.18-6-686
-rw-r–r– 1 root root 1260081 2008-10-13 17:34 vmlinuz-2.6.18-6-686
I’m sure this looks quite confusing, but sit tight and I’ll go over all of it. The ls has several flags that can be used. The -a flag will show all files in a directory (including the hidden ones), -t will sort output of ls by modification time, -o won’t list group information, -g won’t list owner information, etc. There are many flags for the ls command.
As for what all the above info means, I’ll get to that right now. The first column indicates permissions. There are 10 available spots for permissions letters in that column. The first spot either a “-” or a “d”. The latter indicates it’s a directory. The next 3 spots indicate permissions for the owner of the file, the next 3 spots indicate the owner’s group permissions, while the last 3 indicate global permission. Global meaning simply everyone else. Those sets of 3 spots will either have a “r”, a “w”, or an “x”. The r is for read, w for write, x for execute.
The next two columns tell you the owner’s username, and what group the owner belongs to, respectively. The two columns after that tell of the file size in bytes, and the last modification time. If you’d rather see the file size in a more human readable format, then use the -h flag. Lastly, the last column will tell you the name of the file.
Now one reason Linux and UNIX differ from other operating systems is that most of it’s files are human readable. They’re mostly just text files. Instead of pointing and clicking your way into carpal tunnel, you can type it. 🙂 Seriously though, most modifications made in Linux are simply making a few changes in a text file or two, rather than going through endless amounts of menus, tabs, utilities, etc.
Of course today, we won’t be editing any text files. Just window shopping, if you will. For simple output of a text file, we use the less tool. Let’s use less to look at the /boot/grub/menu.lst file
andrew@debian:/$ less /boot/grub/menu.lst
Fear not, there are ways to navigate the less tool. Space bar to go forward a page, “b” to move back a page, “G” to go to the end of the text file, “1G” to go to the beginning of it, and “q” to quit and go back to the command prompt. You can also use the less tool to search a document for a word. For instance, instance, once viewing a text file with less, you can type “/characters” to look for the word characters in that file.
The last tool we’ll look at today is the file tool. We use the file tool to see what type of a file a file is. I toss out a few examples.
andrew@debian:/$ file /bin
andrew@debian:/$ file /etc/network/interfaces
/etc/network/interfaces: ASCII English text
andrew@debian:/$ file /boot/vmlinuz-2.6.18-6-686
/boot/vmlinuz-2.6.18-6-686: Linux kernel x86 boot executable RO-rootFS, root_dev 0x6803, swap_dev 0x1, Normal VGA
The Linux filesystem has many different file types in it. Most of the files themselves are human readable, while most of the different filetypes are not. For instance, ASCII text files, SH and BASH scripts, PostScript docs, and HTML pages are human readable. However, LSB core files, LSB executables, LSB shared objects, zip/gzip/bzip2/tar archives, JPEG files, and RPM or DEB packages aren’t human readable. Thankfully, most of a Linux system is comprised of text files and shell scripts.
Now, you can move about the filesystem, check out directories and files… have a look around. Thanks for taking the time to read this, and I hope it helped you somehow. Keep checking back as I’m working on a couple other posts and should have them up soon. Up next, a comprehensive tour of the Linux File System.
-That Linux guy