The CSCS Intro To Linux/Unix (Week 1)
Ok, so tonight’s CSCS was focused around the unix userland & tools, and how to administrate a Linux/Unix box. Lack of networking hampered things slightly, but there was definitely some useful stuff demonstrated (and its the kind of thing that’s really useful when it’s exactly what you’re looking for). I’m posting a rough crib sheet from what was discussed, anything anyone wants to add in the comments would be great. So here, in no particular order…
First, a note on tab completion. With the bash shell, you can tab complete file names and commands. Simply press a letter or two, then hit tab (you might need to do it twice). You’ll get a handy list of what matches so far, and you can keep doing it as you type the path. This can get really useful for navigating directory structures without having to step through them with
cd eternally. Just tab complete as you choose elements of the path..
Dokuro:~/directory jon$ ru<tab> ruby ruby1.8 run-parts ruptime Dokuro:~/directory jon$ vi ~/University/CO52<tab> CO522 CO524 CO525 CO526
ls is the equivalent of windows
dir and shows you a list of all the files in the current directory (probably shown in your prompt, or otherwise visible via the
Dokuro:~/directory jon$ pwd /Users/jon/directory Dokuro:~/directory jon$ ls mydir myfile
ls -al shows you a list of all the files in your current directory with some extra details, like who owns the file, what permissions it has, and when it was created.
Dokuro:~/directory jon$ ls -al total 0 drwxr-xr-x 4 jon jon 136 Jan 18 01:39 . drwxr-xr-x 46 jon jon 1564 Jan 18 01:37 .. drwxr-xr-x 2 jon jon 68 Jan 18 01:37 mydir -rw-r--r-- 1 jon jon 0 Jan 18 01:37 myfile
file inputfile allows you to determine the type of any given file on your system, assuming file knows about it. The tool works based on the file’s contents (making file extensions irrelevant. this has very useful implications!)
Dokuro:~/etc jon$ file randomfile randomfile: Macromedia Flash data, version 5
exit closes the current shell, and if it was the shell you got after logging in, will log you out. You can nest instances of shells, so this comes in useful. Also handy for exiting from
su (see next).
adduser username will add a new user to your system, prompting you for appropriate values. You need to have superuser permissions, so you’ll have to use
su to the root account (see below).
man something lets you get documentation on what a specific command is, and what parameters it accepts. This documentation is extremely terse, and designed for people who know they have the right command but are looking for the particular parameter to enable an obscure/random behaviour. Learning unix/linux commands from man pages is probably not best advised, but they can be helpful.
Dokuro:~/etc jon$ man mplayer
cat something lets you write the contents of files, pipes, or anything else you can get a reference to to your terminal (more correctly, standard output). A popular trick is
cat /dev/urandom > file, or just plain
cat /dev/urandom. These commands are best avoided as they mess up your terminal (and/or terminal emulator) or fill large amounts of disk space quickly. Useful things include being able to cat random input to a file.
Dokuro:~/directory jon$ cat > MyInput This is some text that i typed into the terminal ^C Dokuro:~/directory jon$ tail MyInput This is some text that i typed into the terminal
Also introduced there was tail which allows you to view (by default) the last 5 lines of a file (the tail of the file?). You can also use tail to watch additions to a file using tail -f /path/filename, a command useful for monitoring realtime usage of services through their logs.
Superusers/Root, su and sudo
su makes you the superuser (or ‘root’). You’re basically capable of destroying the OS install at this point.
sudo allows you to execute a single command, as the superuser. This removes the dangers associated with having a root shell active, and is authenticated with your own password. Whether you can run sudo or not is determined by a list of users maintained by the superuser (
/etc/sudoers). Note that sudo can effectively offer the functionality of
su, should you happen to
sudo bash or another shell of your choice.
Dokuro:~ jon$ sudo bash Password: Dokuro:~ root#
sudo no safer than full root access in the hands of inexperienced users, but the best way to run your own machine. (Random: The root account on my OS X system is disabled, and I have never needed to enable it.
sudo is my friend)
A Basic Tour of the Unix (Linux?) Filesystem Layout
/boot A very bad place to delete things. In fact, you probably shouldn’t even have this thing mounted read/write all the time anyway. It contains various files that tell your system how to boot the OS, including the kernel. Poking here may render your install unable to boot on your computer.
/dev Where devices go on Linux. Doing ls in this directory reveals interesting numbers about what type of device is mapped to each entry.
/etc This is where you find a lot of configuration files for your machine. Notable is
/etc/MOTD which you can modify for both good and evil. You will require superuser access though.
/usr is where your software lives, doing accidental removals in this directory could cause you some serious issues (as was shown in the demonstration)
A command so useful and potentially destructive that it deserves an entire section of its own. More seriously, rm is one of the commands that should be used with care, and several of the use cases were examined.
rm file will remove file, prompting you to confirm if it happens to be write protected.
rm * will remove all files in the current directory (wildcard matching), but won’t remove directories. It will warn you that it couldn’t remove the directories explicitly.
rm -rf * will remove everything in the current directory without any confirmations or warning (the
-rf parameters stand for recursive and force respectively).
No really, go ahead, don’t ask me if I’m sure.. (MCJ)
It’s well worth giving a second thought before you issue this command to what your current working directory is. It can be especially perilious to be working with files with explicit paths stated then use one of the rm commands without said path, assuming that you were in the directory that contains the files (one of the easiest destructive mistakes to make).
A final tip on making
rm * hurt less is to keep your home directory rigorously structured. Several times this week I’ve accidentally issued that command and it hasn’t removed anything either time. Having the contents of my home directory sorted into folders paid off, it seems.
Again, I’m throwing this open in the comments. I don’t claim to be a Linux/Unix expert and only have my own subset of commands that I use on a frequent basis. If there’s anything you’d like to throw in, feel free.