Hello, welcome to BASH (and Linux). =================================== This document will teach you about BASH and Linux. You should read it without skipping sections especially if you're new. You can open a new shell by pressing the New Session button on the lower left, so that you can try out stuff without having to quit this doc. You can scroll this text with the directional arrows. Use Ctrl-F to search for a phrase, F3 to continue to next occurence. BASH stands for Bourne Again SHell. It is a free and improved shell that is based/inspired on/by Stephen Bourne's original "SH" shell. In your Linux, there are programs, files and directories. Using BASH is a way of working with them proficiently. A thing to remember: in Linux, case matters. "ABC", "abc" and "Abc" are all different files/directories/commands. Wildcards (? and *) can be used for when you don't know a character or a variable sequence of characters, respectively. Example: ls /usr/bin/??a* will list every filename from /usr/bin/ that has 3rd letter 'a'. You can use wildcards in many places other than the shell so it's good to know exactly what they are, and what to use them for. A neat feature of BASH is autocompletion. Just press the Tab key. It can autocomplete files, folders, commands, aliases. Aliases are shortcuts to commands, and are defined in the Shell. Type 'alias' for a list. Aliases have priority over executables, and that's how 'ls' is overriden to print stuff in nice colours! Finally, there are two user accounts on this LiveCD, demo and root. demo is a limited user - as demo you can't make system changes, and the programs you start can't, either. root is the administrator user. The prompt will be a dollar sign $ when you're limited, and a hash # when you're root. Being root all the time is bad practice, because your power over the system will be transferred to the programs you start. Sometimes you need to be root - in those cases login with 'su', password is "root". Use the 'lout' alias to clear history and exit. CONTENTS: Getting help Linux directory structure File types Processes Startup and runlevels Permissions Files and Folders Pipes and Redirects Extra command chaining techniques Commands Encyclopedia System Work Finally Getting help ------------ It's quite easy, type: man [what] where [what] is the command you want to read about. "man" stands for "manual" by the way. Use Up and Down keys to scroll, Q to exit, Ctrl-F to search. Most programs installed have their own manpage. Even "man". A more elegant way is to start Konqueror and type: man:[what] in the address bar. Typing just "man:" will give you the contents. You will notice that there are more sections. Sections exist to eliminate confusion, for instance: man read and man 3 read will give you two different manpages - because "read" is a system program but a C function as well (routines in section 3). So "man" will by default display the first occurrence, which is in section 1. PROTIP: Important files have manpages too, e.g. "man shadow". Linux directory structure ------------------------- First thing you notice after installing Linux is that it just spilled itself all over the place. Maybe you were expecting it to hide away in a single "Linux" folder? I know how you feel, I've been there. The second thing, where on Earth are the drives? Well, there aren't. How Linux is organized may seem chaotic but it makes it a lot easier to control than other operating systems. Every directory has its purpose. "/" is the root directory, and remember that Linux uses slashes "/" and not backslahes "\". So let's explain the directories. . (yes, a dot) - always the current directory you're in .. (double dot) - always the above directory of where you're in / - root directory ~ - your home directory ("/home/Username") bin - for "binary", system base executables boot - boot files, kernel, bootloader config dev - devices (more about these later) etc - text file configurations for everything home - personal files of the users' lib - runtime libraries (just like DLL's in Win) lost+found - data recovery purposes media - (usually) mount points for removable devices mnt - mount points for non-removable devices opt - optional, may not be used proc - a "map" of the actual memory on the hard drive root - just like ~ but for root user sbin - superuser's (root user's) utilities kit tmp - points to /var/tmp, temporary files usr - just like / but at a lower level, most apps will install here, not in / var - temporary files, garbage, etcetera PROTIP: "man hier" will give you even more detailed information. File types ---------- Let's take a look at the files you can find in Linux/UNIX. 1) regular files (hard links) a) data files b) executable files c) dot-files (start with "." and are hidden) 2) symlinks (symbolic/soft links) 3) directories 4) block devices 5) character devices 6) named pipes (FIFOs), sockets 1. a) Regular files (aka hard links) are simple files; data files are trivial. In Linux, an executable file is not identified from its extension. In fact, in Linux the extension doesn't count much at all. b) Executable files are marked by the "Is executable" permission. Permissions are important and will be discussed later on. c) Dot files are files whose names start with "." and are usually considered hidden and are ignored by most programs, unless you specifically ask otherwise. Anyway - you may wonder - why are these called "hard links"? In Linux, there is the data on the disk, and the entry in the filesystem to it. That is the link. But unlike (early) Windows(R) filesystems - the same piece of data can be linked to from multiple locations. Say there's an ISO somewhere and you create another hard link to it, in another directory. Then you delete the original "file" - meaning the original hard link. Will your ISO be lost? No. Because for as long as there is at least one hard link to it, the data will never be lost. And you can have as many as you want. By the way, "ln" is used to create links, both hard and symbolic. 2. Symlinks are like shortcuts in Windows(R). They point to a file or directory or even another symlink. Once the original file is deleted/moved, they become invalid. 3. You know what folders are, I'm sure. If you happen not to: They are things that can contain other folders and files. This allows for a decent organizing of data on the computer. 4. Block devices (usually found in /dev) are files that are actually pointers to a PC device that can access data in blocks (like the hard drive). It is very easy to backup your hard drive's Master Boot Record in Linux. You simply copy the contents of a block device "file" to a normal file, using the "dd" command: dd if=/dev/sda of=/home/MBRBackup bs=512 count=1 That should save the MBR (and bootloader) from sda to MBRBackup. It can be translated as: copy 512 bytes from /dev/sda (input) to /home/MBRBackup (output), one cycle. dd if=/home/MBRBackup of=/dev/sda bs=512 count=1 And that should restore it. PROTIP: sda (or hda) is the designation for the hard disk as a whole, whilst sda1, sda2 etc. are the partitions. The MBR doesn't usually stay on partitions. 5. Character devices are for PC devices that work with data streams. Such as the soundcard. No data is ever saved, only sent or received. 6. Pipes, Sockets, etc. These are only of interest to you if you become a system programmer, security expert or just plain curious. Fully documented elsewhere... (Hint: Linux usage and programming docs section.) Processes --------- There is a PATH variable, which echo $PATH will display. If you forget the "$", "echo" will just display "PATH". I wanted you to know this because programs can be run simply by typing their name only if they're in any of the directories in PATH. If not, you must write their full name. Such as ./a_program_in_the_current_directory.a or /program32/32.elf or ../home/demo/program/program/program/THEprogram This is important to know because you can't start a program in the current directory without adding ./ before its filename (which will autocomplete with Tab). Of course, you are free to add the current directory "./" to $PATH if you're brave and don't mind taking security risks. The command is: PATH=$PATH:./ You can also start programs in the background: ../../dir/program & (A job number in brackets, and a PID - process ID will be displayed.) You can see what you have started with: jobs Bring one to the foreground with: fg [job_number] Send it back with Ctrl-Z. Stop it with: kill [pid] or kill %[job_number] or (this can be unreliable) kill [app_name] You can see what programs are running and consuming most resources with the "top" command. Press Q to quit. "ps" ("ps -A") is used to see the PID and all programs running. Alternatively you could use the "htop" command. In the manpages you can see what arguments a program can accept. By tradition (and unlike say... MS-DOS(R)), Linux programs can take their arguments "welded", such as: rm -fr / which would be the same as: rm -f -r / By the way, DON'T RUN THAT! It'd try to ReMove everything (f)orcibly and (r)ecursively from the root ("/") directory down. Startup and runlevels --------------------- Linux works with runlevels, most commonly S, and from 0 to 6. S is for Single user, and adds itself to runlevel 5. Runlevel 1 is the true, text-only single user mode. You can use it for fixing problems that prevent you from going higher, into graphical mode, perhaps. Runlevel 0 is system shutdown, runlevel 6 is system restart. All usable startup config is found in /etc/init.d/ in the form of scripts. The /etc/rcX.d/ ("X" being S, or a value from 0 to 6) folders contain the actual startup config, with symlinks to the stuff in /etc/init.d/. They are all precedented by Sxx or Kxx, where "xx" is a number. The numbers control the order, S and K control whether the program should be started or stopped ("killed"). The apps that are started and run in the background are called daemons. And it's helpful to know that every daemon script in /etc/init.d/ is (must be) able to respond to 3 possible parameters: start, stop and restart. So if you were to stop the Tor daemon, you'd type: /etc/init.d/tor stop Remember that this "tor" is a script (with the same name as the true program's) and that we must write the whole path to it. Example 2: restart the Avahi m/DNS daemon (and hopefully clear the DNS cache): /etc/init.d/avahi-daemon restart You are invited to change the startup sequence on your own, removing stuff that you do not need. Remember to make backups though. PROTIP: Of course, not all scripts in /etc/init.d/ start up daemons. Some just refresh your system's configuration. Permissions ----------- Each file/directory has permissions - and on Linux it's more advanced than what Windows(R) can provide. A file has permissions for 3 kinds of users, and there are 3 permissions for each. (Admin user doesn't count, as he/she always has full permissions.) The permissions are: R - Read (open, view) W - Write (modify, delete) X - eXecute (start the program, script) So if the X perm isn't set, the program can never be executed. Files have an owner, and the owner's group. These will be retained when they're moved, unless special precautions are taken to prevent this. Files can have Set User ID (SUID) and Set Group ID (SGID), and Sticky bits set. The first two make it so that the file/program uses the owner user's ID and GID when another user runs/uses them. The Sticky restricts deletion to an extent. In a Sticky folder, the users who can add content can't delete it afterwards. You can use the "ls -l" command to see a nice list of files with their perms. As said, there are three groups of users - in the following order - and not counting the root user. * Owner * Group * Anybody else This is why there are there are 3 fields of "rwx", perms missing when necessary: "-". The first letter denotes the type of the file, as follows: - - regular file d - directory l - symlink b - block device c - character device p - named pipe s - socket For to change permissions you will use "chmod", to change owner/group you will use "chown". When changing permissions, you might be better off learning absolute modes. Let's break down all numbers from 0 to 7 in binary. used rwx (for later:) UGS ---- --- --- 0 000 000 1 001 001 2 010 010 3 011 011 4 100 100 5 101 101 6 110 110 7 111 111 So I have a file and want to change permissions to rw-r---wx, that meaning I being the owner can read and write, users from my group can only read, and complete strangers can write and execute my file. I will therefore write: chmod 643 myfile.txt Should be clear why, otherwise check the binary conversion again. They tried to produce something similar in NTFS for Windows(R), but it's different and still somewhat behind Linux/UNIX permissions. But the "chmod" command can in fact take 4 digits, the first digit sets the User ID, Group ID and Sticky bits. Thing with "chmod" is that it considers missing digits to be 0 (zero), so that's why it's not compulsory to include the wacky UGS stuff. As an example, if you write: chmod 67 myfile.txt it's the same as writing chmod 0067 myfile.txt OK now it seems to you that setting the perms requires a lot of math on your behalf, or memorizing. But in fact you only have to remember the numbers 0, 1, 2 and 4. They "define" permissions from right to left. So 0 means no perms, 1 means right (X) perm, 2 means middle (W) perm, 4 is left (R) perm. If you want to mix perms, just add the numbers together. An example: there's myfile.txt, I want to give it rw-r-xrwx perms, and sticky. Meaning: Sticky bit, Owner can read and write, users in the same group as the owner can read and execute, and complete strangers can read, write and execute. I'll think: --S rw- r-x rwx 001 420 401 421, and added: 1657 So I'll type: chmod 1657 myfile.txt Check with: ls -l myfile.txt I know it may look complicated, but it's only until you get used to it, trust me. Be sure to check out the chmod manpage for more info. Those UGS bits can be weird, tricky and even dangerous. And by the way, "UGS" is my own designation, not the official one. Files and Folders ----------------- Let's remember. There are directories and files. Two directories are special: the dot . and the double dot .. . - always points to the current directory you're in. .. - always points to the above directory. So if you want to get outside where you are now, write cd .. The separation mark in Linux addresses is / So if you want to move out two directories at a time, type cd ../.. What's important is that in Linux every file entry is just a link to some data. You can have more entries like this to the same data. They are called hard links. Soft links, (or symbolic links, or symlinks) are just as shortcuts are in Windows(R). You can create any of them with the "ln" command. Use "man ln" for more info. Pipes and Redirects ------------------- These are ways of making the programs work together. <1> When using pipes, the output of the first program will become the input of the second and so on. For instance this file you're viewing was opened by cat /etc/bashref.txt | most "cat" is a program that displays and can concatenate files. it simply takes data into memory then outputs it, doesn't modify the file(s) unless specifically told to. "/etc/bashref.txt" is the file, full address, given as an argument to cat. "most" is a pager program which displays everything nicely. So "cat" took the contents of the text file and threw them to "most", which displayed them. That was a pipe. PROTIP: Of course, I could've just used "most /etc/bashref.txt" instead of the cat pipe mess, but whatever. You can join more than two programs with pipes, in the same line. What follows is a poor example but at least demonstrates this: cat /etc/passwd | grep 'daemon' | sort | most In the above example, "cat" takes the data from the /etc/passwd file, and passes it to "grep" which cancels out the lines which don't contain the sequence "daemon", then passes the result to "sort" (which sorts them alphabetically), then finally "most" displays the resulting junk. PROTIP: gawk and sed are more powerful than grep but are harder to use. Unless you read the manpages. <2> Redirects are sort of like pipes, but they're used between programs and files, instead of programs and programs. echo "This will be written in the Text.txt file." > Text.txt "echo" takes that information and redirects it to a file. Redirects can use the following signs: > output to file, overwrite original file if exists >> output to file, append to end of file if exists < input from file << [mark] input from file until [mark] was read Here's another example: cat file1 file2 > file3 This one puts together file1 with file2 and saves the result as file3. cat > shortmemo.txt << THEEND This will let you write anything you want, including [Enter], until you write "THEEND", and then saves all in shortmemo.txt. PROTIP: Standard input (stdin) = keyboard = file "0", standard output (stdout) = screen = file "1", standard error (stderr) = screen = file "2". And since they're files, kinda, redirection works on them as well. By default, all programs assume them as explained above - that, until you step in and redirect them. Ta-da! And just in case you wondered, doing something crazy like echo "crazy idea" > sort > most will create two files in the current directory. Extra command chaining techniques --------------------------------- So pipes are nice, but there's more. Check this out: logical "AND": && logical "OR": || Sounds boring, huh? Maybe - but they can help you. Abstract "AND" example: command1 && command2 && command3 What will this do? Well if "command1" executes successfully, "command2" is executed as well, and so on. Practical "AND" example: mksquashfs squash/ iso/anubis/anubis.lz && shutdown "OR" example: command1 || command2 || command3 What will happen? "command2" will only execute if "command1" is NOT successfully executed, and so on. "OR" example 2: touch /root/newfile || (echo "hey, you're not root" && su) And yes, you can group them so. Guiding parenthesis advised but not compulsory. Commands Encyclopedia ;) --------------------- Here we talk about some commands. Some will only be runnable as root user. You log in with "su" and there will be a "#" instead of "$". Basic commands -------------- file - identify file type mkdir - create directory rmdir - remove directory touch - create empty file or change/update file's last change time rm - remove file, also works on directories cp - copy file/directory mv - move file/directory cd - change directory pwd - print current (working) directory ls - list contents alias - define your own commands/see list unalias - undefine commands clear - clears screen chmod - change mode (file permissions) chown - change owner man - read manual information cat - data redirection (default is stdin-stdout, which is why it appears to freeze when you start it simply by itself, use Ctrl-D to stop it, that's the signal for "end input") diff - differences between two or more files mc - Midnight Commander, makes it easier to explore the filesystem System commands --------------- dd - lean and mean raw data copier mkfs.X - the mkfs. family will create a filesystem, on either a block device or a data file basename - strip filenames from directory login/su - plain login or login as root user (system admin, superuser) exit/logout - exit a session, or log out adduser - add a new user deluser - delete a user chfn - change user information passwd - change password xargs - useful in pipes, transmits first prog's output as second's arguments, e.g. "ls | xargs echo" tee - splits up input and redirects to files, e.g. "ls | tee listcopy1 listcopy2 > /dev/null" (redirected tee's output to /dev/null - remove that to find out why) nice - a braindamaged way (in my humble opinion at least) to set the "priority" of a certain process df - shows currently mounted devices mount - mount a device to a mount point (a folder) umount - care, it's missing an N - un-mounts a device whereis - finds you stuff find - same as above which - same as previous two but finds executables more/less/most - pagers, for nicer output; this doc you now view with "most" umask - set the permissions "filter" for newly created files top - view most active running programs ps - view all running programs htop - as the previous two combined - and a lot better kill - stop a program pkill - kill by name, sort of telinit/init - change runlevel to something specified shutdown/halt/reboot - should be obvious (runlevel change) ln - link creation mknod - special file creation cron - run tasks automatically at certain times, as set in: /etc/crontab and /etc/cron.X/ directory family fdisk - disk partitioning cfdisk - same as above but easier to use sfdisk - harder to use, but slightly superior to the other two grep - selection of stuff, e.g. "ls | grep 'bas'" will display anything from ls' output containing "bas" pgrep - same as above but looks through running programs' names and displays their PID(s) - you might want to use "pgrep -l" which displays their names too - e.g. "pgrep -l 'kde'" Distro remastering ------------------ mkinitramfs - regenerates the initrd.img file update-initramfs - same as above, but maybe easier to use chroot - "change root" to specified directory, important when you want to remove/add software to your distro (then use "exit") apt-get - manage packages over the Internet dpkg - manage packages locally orphaner - a slightly nicer way of removing packages mksquashfs - create compressed filesystem (i.e. anubis/anubis.lz) mkisofs - create (bootable) CD/DVD filesystem (see build_iso.sh on the CD) gzip - working with the CD's initrd.gz file md5sum - create/verify checksum of file (very important) shaX - the sha family, same purpose as md5sum but less popular head/tail - very useful used together with "cat" and "dd" make-kpkg - Debian tool for automated kernel creation Network/Security/etc commands ----------------------------- ifconfig - display/configure wired network interface iwconfig - display/configure wireless network interface ifup/ifdown - switch a network interface on/off (previous two can do that as well, though) wlanconfig - configure wireless network interface netstat - display current connections netcat/nc - advanced connections creation tool cryptcat - the above, crypto hardened macchanger - change the MAC number (the hardware address of your network card) the MAC is in base 16 so letters from a to f are accepted; try "macchanger -m de:ad:ba:be:ca:fe eth0" one day? ;) nmap - network mapper (you're better off using KNmap instead) dig - nameserver query tool host - same as above but simpler nslookup - same as the previous two but is oldest avahi-X - the avahi- family replaces dig and host traceroute - trace route of a given IP ping - send a "hey are you there?" message to another PC who - who is currently logged in w - same as above but also prints what they do finger - display info about user wget - advanced web downloader dmesg - display last kernel messages last - display contents of system logs lastb - display failed logins iptables - firewall. don't use it unless you know what you're doing. uptime - how long has the been PC running uname - OS information dmidecode - BIOS and hardware information biosdecode - same as above but BIOS-specific hwinfo - hardware information - please use a pager or redirect output sync - flush write cache to disk rkhunter - search for rootkits chkrootkit - same as above unhideX - the unhide family, search for hidden processes/connections packit - "packet analysis and injection tool" hunt - "network security auditing tool" tshark - console version of Wireshark ipcX - the ipc family, all about InterProcess Communication lsof - "list open files", and has a freaking huge manpage fuser - "show which processes use the named files, sockets, or filesystems." iptstate - see currently established connections shred - securely erase ("wipe") data srm - same as above fwlogwatch - shows firewall events from /var/log/messages PROTIP: You'll learn these commands as you use them, don't worry. System Work ----------- Linux is basically a free, improved UNIX-clone, and UNIX was developed in the 1970s. Still its inner workings surpass those of say Windows(R). Bottom line, it is harder to grasp some concepts but well worth the effort. PROTIP: Linux is the kernel, and is surrounded by various open-source tools and apps (most notably the GNU software). Therefore, some people will appreciate that you refer to your OS as GNU/Linux instead of just Linux. But me, I'm just telling you for your general culture, and to distract you. First of all, most things in Linux are regarded as a file. Regular files can also be called hard links. There are also symlinks, character and block devices. The "devices" are Linux's way of dealing with hardware components. Think of them as interfaces to the hardware. Storage media must be mounted in order to be used. For example: mount /dev/sda1 /a_directory This mounts the first partition of the hard drive to the given folder. On your system it may be "hda1" instead of "sda1". The mount points are usually stored in /mnt for convenience. To unmount, use the umount command (yes, it's missing the N). umount /a_directory All this talk about mounting should be familiar even if you're coming from the Windows(R) world. All those illegal game ISO's that you had to mount? Yes, that can be done here as well. :) But you'll never have to download a lame spyware-filled program. Use the "mount" command just like on a device. PROTIP: You can mount Nero(R) NRG images too. An ISO is a CD filesystem. A floppy disk's IMG is a FAT12 filesystem. Under Win, you need extra programs to support either. Linux OTOH can work with many filesystem-files besides these. A wild example: (enter these as given, parts starting with # are comments) su # we have to be root in this example touch ext3-filesys # create an empty file named "ext3-filesys" dd if=/dev/zero of=ext3-filesys bs=10M count=1 # copy 10 MB into our file from special zero device # (infinite supply of zeros, /dev/null also popular) mkfs.ext3 ext3-filesys # format our file as a filesystem! say Yes to the question. # (also, type "mkfs." then press Tab two times) file ext3-filesys # verify what we have mkdir /myext3 # create mount directory mount -o loop ext3-filesys /myext3 # mount our file # (must use -o loop because it's not a block device) ls /myext3 # see what's inside, we can copy/delete anything we want umount /myext3 # unmount exit # exit root account What we've created was a typical filesystem-in-a-file. Just like the LiveCD's initrd.gz file is. [First extract it (gzip command) then mount it.] All changes you make are automatically saved to the host file although the last modification time stamp isn't. You can use mkisofs (growisofs) for ISOs; and some filesystem-makers will simply refuse to work on a file (mkfs.ntfs for example). Finally ------- There are a LOT more things and covered up nicely in the big BASH book. You could also check out: man bash There is more to pipes and redirecting, and the commands can work "remotely", such as: ls /etc/kde3 or can handle more requests from the user: mkdir -p /home/demo/d /home/demo/a tempo ../uppernewdir Don't forget about aliases to save typing, and the manpages. Also, you can edit the /etc/motd2 file if you got fed up of the greeting message. ;) Have fun.