Thursday, March 8, 2007

Terminal velocity: a quick primer to the Linux command line

I think one of the most imposing aspects of Linux for the newbie is its command line. Windows began phasing out its command-line in Windows 95, and now, with Vista, it's little more than a bad dream. So, the ex-Windows user may ask why can't Linux do the same? The answer is that there's a lot of power to the Linux command line, and really everything graphical in Linux is just window dressing for the command line anyways. It may seem scary or awkward, but once you get the hang of it, it'll soon become your best friend. So, to this end, I decided to provide a short primer on some of the most important commands.

A Word About the Linux Filesystem


Linux's filesystem can take a little getting-used-to coming from Windows. The filesystem in Linux always starts from root or '/', and all drives and partitions are mounted in the filesystem from there. Directories are hierarchically written out with slashes (/) between each directory. For instance, your desktop is located at '/home/johndoe/Desktop'. This says that it's a directory named 'Desktop', within a directory named 'johndoe', within the 'home' directory of root. This could have also been written as '~/Desktop', as the tilde (~) indicates the currently logged in user's home directory. Notice also that 'Desktop' is title-cased. The Linux filesystem is case-sensitive, so 'desktop', 'Desktop', 'DESKTOP', and 'DeSkToP' are all different directories.

Basic File and Directory Commands













Changing Directoriescd [directory]
Creating a Directorymkdir [directory]
Copying a Directorycp -r [target] [destination]
Moving a Directorymv [target] [destination]
Removing a Directory (empty)rmdir [directory]
Removing a Directory (non-empty)rm -r [directory]
Listing the Contents of a Directoryls
Getting a Detailed List of the Contents of a Directoryls -l
Copying a Filecp [target] [destination]
Moving a Filemv [target] [destination]
Removing a Filerm [file]

A Note About Permissions


Every file and directory in the Linux filesystem is subject to priviledges, owners and groups. Priviledges are things that are allowed to be done to a particular file or directory by those who have permission to do so. They are broken down widely into read, write, and execute priviledges. Groups are, obviously enough, groups of users who 'own' certain files and directories: they have permission to edit and otherwise alter or use files that have been given to their group. Owners are members of groups but have their own files and directories that they 'own' that the rest of the group does not. Root is a special user who in effect owns all files in the filesystem and belongs to all groups. This gives the root user complete and total authority of every part of the system, which also makes root a potential dangerous user. Most Linux distros, including Ubuntu, do not assign root to the computer's owner, rather creating a separate account for them. Ubuntu goes a step further and assigns a random password to root that the computer owner doesn't even know. This is good in that it protects the system against attacks, but it also creates a problem when things must be done on the system which actually require root priviledges. Thankfully, there's solutions for this.

Administrative Tasks









Do Something in a Terminal as Rootsudo [command]
Use a GUI Program as Rootgksu [command]
Use Terminal as Root (Potentially Dangerous!)sudo su -
Change Priviledges of a File or Directorysudo chmod [u/g][+/-][r/w/x] [target]
Change Priviledges of a Directory and All Files and Directories Withinsudo chmod -R [u/g][+/-][r/w/x] [target]
Change Owner of a File or Directorysudo chown [owner]:[group] [target]
Change Owner of a Directory and All Files and Directories Withinsudo chown -R [owner]:[group] [target]

This will do for now. I might come back and add more later or follow-up in another post. I intend on discussing some of the basic ways of editing files, but this really requires an post in itself.

No comments: