Introducing the command line - Part 2

  • December 3, 2006
  • Avatar for peter
    Peter
    Upfold

Welcome back, everybody. Once again it's time to dip our toes in the ocean of geeky productivity that is the command line. If you haven't already taken a quick look at Part 1, now's your chance!

Moving back up the tree

So, we've got as far as explaining how to see what our current working directory is, and how to change it. However, we haven't covered something quite important, and that's going up one directory (to the current folder's parent folder).

That's achieved by using the special folder name .. (yep, two full stops). One full stop (.) means 'the current folder'.

So, to go up by one folder, cd to ..:

$ cd ..

Listing folder contents

One of the first things I usually do once I've cd'ed into a folder on the command line, is I list the contents of the folder so I can see what I'm doing. To list the contents of the current working directory, use the ls command.

$ ls

This will show you a list of the files and folders in this folder (excluding hidden ones - that is to say, files and folders beginning with a dot). The output might look something like this:

$ cd Documents
$ ls
Another Document.pdf A Third Document.odt Document.odt mandriva review

While this is good in that it tells me what's in the folder, it only shows me the name of the items, not who owns them, the permissions, their size etc.

Switch!

However, that's not all that ls can do. You can change the way command line programs behave using things called 'switches'. Like our real-life friends, they turn on and off specific behaviour. In Unix, switches usually begin with one or two dashes, followed by a letter.

The first switch I'm going to show you how to use is the l (lowercase L) switch for ls. The l switch gives me a 'long listing' format, which displays each file on its own line, along with information about the file - like its size, owner and permissions.

To invoke ls with this switch, we type ls, then a space, then a dash (to indicate it's a switch not a folder name), then l. I'll try it on my Documents folder.

$ pwd
/home/peter/Documents
$ ls -l
total 4548
-rw-r--r-- 1 peter peter 3455935 Dec 3 09:45 Another Document.pdf
-rw-r--r-- 1 peter peter 8626 Dec 3 09:46 A Third Document.odt
-rw-r--r-- 1 peter peter 1170674 Dec 3 09:45 Document.odt
drwxr-xr-x 2 peter peter 4096 Dec 3 09:48 Folder
-rw-r--r-- 1 peter peter 1075 Dec 3 09:45 mandriva review

More switches! More! That's not possible!

There are more switches! Try the a switch. This lists all files, including hidden ones that we mentioned earlier. I don't have any hidden files in my Documents folder, so I'll head back up to my home folder and use ls -a there.

$ cd ..
$ ls -a

You'll notice a lot more files than you ever thought existed before. The reason for this is that dotfiles (as these elusive hidden files are known) are often used for configuration and settings purposes. Each one of these is a hidden file which probably contains settings and preferences for your applications. Don't delete any of these dotfiles, they'll reappear anyway next time you start your program (and you'll have to reset all your preferences!)

Combining switches together

"But hang on," you're thinking. "What if I want to list all files AND I want a long listing?"

Well, that's easy. You can have multiple switches.

$ ls -l -a

In fact, providing your switches only usually start with one dash, you can use the shorthand version too:

$ ls -la

OK, stop. My brain is fried.

OK, I will. In fact, you're probably thinking I (and everyone else who uses the command line) is a stupid person who likes making life hard for themselves by remembering these damn switches and command names! Maybe, but you won't understand why and how all of this is important or makes life easier just yet. Don't expect to.

For now, you need to slowly get a grasp of the basic concepts behind using commands (switches, arguments - and later redirects and pipes). That's why we've just been cd'ing and ls'ing around for now.

I'm trying to do this tutorial in more bite-sized chunks (as they are tastier, and with something as nasty as the command line, it has to be done)!

In Part 3, we'll return back to the wonderful world of command line goodness and we'll be looking at arguments (no, not emacs vs. vi) and a few more basic file management commands. If your brain is fried, stop, take a break and subscribe to FOSSwire so you can read Part 3 later.

When Part 3 is out, a link will be here!

Avatar for peter Peter Upfold

Home » Articles »