; Linux Command Line Introduction
Learning Center
Plans & pricing Sign in
Sign Out

Linux Command Line Introduction

VIEWS: 738 PAGES: 23

A brief Introduction to the Command Line Linux

More Info
  • pg 1
									Introduction to Command Line Linux

Author: Prasanth Philip
This document can be freely redistributed according to the terms of the GNU General Public License. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License". Abstract This is an introductory guide for people learning to use a Linux system. It focuses on command-line usage. This is not because everyone will be doing everything at the command line. On the contrary, there are many useful GUI tools and programs in Linux, and some people will be content to stay with these. However, in contrast to GUI programs, which can be learned largely by "click and try", the command-line requires some deliberate learning, and reading documentation. The benefit of this investment is that you will learn the tools and tricks of the command line which are in many cases faster, more powerful, and more flexible than any GUI program. There are many other introductions to Linux on the web; this one is customized for users in UBC Physics and Astronomy. Since I don't want to cover all the possible cases and variations, I'll also assume that you're sitting at an X-terminal of some sort - in other words, a fancy GUI with lots of windows and things. I'm going to assume that you've figured out how to open a terminal or console window (look for something called Konsole or Gnome Terminal). I'm going to assume that your shell is the bash shell. If you don't know what that is, don't worry - it's the default Linux shell. You may also want to read Matt Choptuik's introduction to Unix.

Table of Contents 1. The Basics 1.1. Eleven essential commands 1.2. Getting help for a specific command 1.3. File names with spaces 1.4. Copying and pasting 1.5. Recalling previous commands 1.6. Be lazy: use TAB to minimize typing 1.7. File permissions 2. Useful Tools 2.1. The network is the computer: ssh and scp 2.2. Finding and searching files 2.3. Creating and extracting file archives (zip, gzip and tar) 2.4. Emacs - a powerful editor 3. Running long calculations 3.1. Getting the prompt back after starting a job 3.2. Keeping a job running after you log out 3.3. Suspending a long-running job and restarting it later 3.4. Running a series of jobs 3.5. More advanced scripts 4. When you're not in the department 4.1. Accessing data from any Windows computer 4.2. Linux without installation - at home or travelling 5. More Quick Tips 5.1. Scroll with the keyboard 5.2. Copy with xclip 5.3. Lots of keyboard shortcuts 5.4. ssh and scp without typing a password each time 5.5. Send a quick e-mail message without starting any mail program 6. Where to go to learn more

1 The Basics
1.1. Eleven essential commands

These are the commands which you have to know in order to be basically functional with a shell (command line). List directory contents. ls ls -l long listing with dates and permissions ls -a list all, including hidden files (hidden files start with a period) Change directory. cd - change to previous directory cd cd ~ change to home directory cd .. change to parent directory pwd cp cp -r somedir somedest Copy recursively (directory and all contents) mv Move (or rename) a file (or directory) Delete (remove) a file. rm -r somedir Delete recursively (directory and all contents) rm rm -ri somedir Delete recursively, but prompt before each removal rm -rf somedir Delete recursively, never prompt mkdir Create directory. rmdir Remove (empty) directory. Print file contents (to console). cat Scroll through file contents, one page at a time. less exit SPACEBAR to advance a page, B to go back a page, Q to quit, /pattern to search for pattern Close shell. If this is your login shell, you will log out. Print current (working) directory. Copy file(s).

1.2. Getting help for a specific command
To get detailed help on a particular command or program, you can try the following. Man pages If you want help on a specific command (ls for example), type man ls. You move around in man as you do with less: SPACEBAR is forward a page, B is back a page, Q is quit. Info pages Info pages are similar to man pages, but contain links and other complex structures. There are many key-strokes for navigating info; if you call info without any arguments, then hit H, you'll get some help on using info. KDE or Gnome Help This Applies only if it's KDE or Gnome program. In either case, you can launch the help browser from the Start menu on the Panel, or choose Help from the program menu, or press F1. /usr/share/doc Every package must install at least some minimal help into a subdirectory in Sometimes it's in html format (read with a browser), sometimes it's plain text (read with less), sometimes it's compressed text (has an ending like .gz, read with zless).

The home page of the project Every command or program belongs to some project (or company). Usually they have a home page which provides copious, up-to-date and indexed information

You can read man and info pages with Konqueror (the KDE web browser) or Galeon, which is nice because then they are nicely formatted and have links and such like. For example, to read the man page on the tar command, enter man:/tar in the address bar; to read the info page on tar enter info:/tar. To get the list of info topics, just enter info:/.

1.3. File names with spaces
Spaces on the command line separate arguments. Therefore file names containing spaces require special treatment to avoid being interpreted as two separate files. There are a couple of methods. You can use quotes:

cp "file name with spaces" file_name_without_spaces You can also use the backslash, which indicates that any special significance is to be removed from the following character. Thus a space, instead of being the delimiter for arguments on a command line, is to be interpreted as a regular character. cp file\ name\ with\ spaces file_name_without_spaces

1.4. Copying and pasting
Copying and pasting in Linux is really easy (yes, easier than Windows). Just mark the text you want to copy with the mouse, holding down the left mouse button in the usual way. Then click the middle mouse button (on a wheel mouse the middle button is the wheel) where you want to insert the marked text. Voila! You didn't even need to touch the keyboard! (CTRL-C and CTRL-V also work like you expect them to most of the time.) For example, suppose you just typed a command, and you want to type a another similar command. If you can still see your original command in the console, just mark it with the mouse, then click the middle mouse button anywhere in the terminal window to insert the marked command at the prompt. You can now edit this command with the left and right arrow keys before hitting enter to execute it. There's another way to recover and edit previous commands, which I'll cover in the next section.

You can double-click to mark a whole work, or triple-click to mark a whole phrase. Remember - you haven't only marked it, you've put it in the clipboard.

1.5. Recalling previous commands
You can scroll though your previous commands (command history) by using the UP and DOWN arrow keys. A more advanced trick is to use CTRL-R, which is particularly useful when you want to recall a command you used some time ago. Hit CTRL-R and then start typing some fragment of the command you're searching for. The prompt will automatically update to show you the most recent match to the fragment you've typed so far. If you want the matching command previous to the one shown, hit CTRL-R again. Once you've got the previous command you're looking for, hit ENTER to run it again, or start editing it.

1.6. Be lazy: use TAB to minimize typing
Some commands and especially some file names can be pretty long. Don't want to type it all? Type the first few letters, then hit TAB. The shell will fill in the rest, as far as there

is a unique completion (either command or filename). If there is not a unique completion to the end, you might have to type another character or two and then hit TAB again.

1.7. File permissions
Since Linux is a multi-user system, file permissions are important. In Linux, there are separate read, write and execute permissions for the owner of the file ("user"), the "group", and everyone else ("other"). You can find out what permissions a file has with ls -l . Here's an example:
-rw-rw-r-1 nodwell mbelab 45570 Jul 9 13:10 kate.png

The series of 9 characters at the beginning of the line spcifies the permissions in order of owner, group, other. In this case, there are read and write permissions for owner and group, and only read permissions for other. You can also see that the owner is "nodwell", and the group is "mbelab". As an example of changing permissions, let's assume I have a file called foo which I want others in the "mbelab" group to be able to read and write. To do this I change the group to mbelab and set the group read and write permissions: chgrp mbelab foo chmod g+rw foo I should also make sure that the directory that this is in, and all the higher (lower?) directories are actually readable by the group. For example: chmod g+rx /home/nodwell/shared_stuff chmod g+rx /home/nodwell Notice that I also set the executable bit for the directories, which allows someone in the group to actually switch into that directory. (Not very logical I admit): To change the group and permissions on a directory and everything in it, including subdirectories and their contents, use the -R (recursive) flag: chgrp -R mbelab /home/nodwell/shared_stuff chmod -R g+rw /home/nodwell/shared_stuff Now suppose that we want all files which are created in /home/nodwell/shared_stuff to automatically have the group mbelab. Assuming you've already changed the group of this directory to mbelab, you can do this by setting the group sticky bit: chmod g+s /home/nodwell/shared_stuff

2. Useful Tools
2.1. The network is the computer: ssh and scp
If you're using Linux, you're rarely working on just one machine. To log in on another machine, use ssh, like this ssh yourid@physics.ubc.ca So now you have a terminal window on the remote machine. You can only run command-line stuff, right? Wrong! Try typing gedit or kedit or netscape. (I'm assuming one of these is actually available on the remote machine.) You might notice that this blocks your terminal window until you close the program again. We'll find a solution to that a little later. You also need to know how to move files around between computers. That's where scp comes in. It's just like cp, but not limited to the local machine. scp somefile yourid@physics.ubc.ca:somedirectory Like cp, scp understands the -r flag, for recursive copy, which is really useful for copying whole directories. scp -r somedir nodwell@physics.ubc.ca:. (What's that dot at the end? It's shorthand for "the current directory", which by default will be your home directory on the remote machine.)

2.2. Finding and searching files
The fastest way to find files is with the locate command. locate is uses a database which is updated every night. This makes it very fast, but it will not find files which have been created the same day. The easiest way to use locate is just to specify a fragment of the file name, for example: locate foo For trimming down the results of your search, grep is invaluable. For example, to find all files which contain both report and eric in the complete file name (including path) we can use: locate report | grep eric

If you know anything about "regular expressions", then you can do a more specific search. A good intro (in fact probably as much information as you'll ever need) to regular expressions can be found at A Tao of Regular Expressions. If you want to specify more than the file name, for example the date modified or the file size, then the find command is the one to use. find actually searches the disk, so it can be a little bit slower. Here is an example of searching for files which are located in the directory /home/eric/misc and all subdirectories, which start with report, which have been modified in the last 2 days, and which are at least 10kb in size: find /home/eric/misc -name 'report*' -mtime -2 -size +10k If you want to find files by their contents, this can be done by combining a number of Linux tools together. (Combining several simple tools to do one complicated job is the Unix approach, and it is very powerful once learned.) We will use find to construct a list of files, and grep to look for lines containing a certain word. We will combine them with a pipe (|) and the command xargs which takes the output from one command and uses it as an argument for calling a second command. This sounds complicated, but the example is not too bad: find -type f | xargs grep -i report Here we are looking for files in the current directory (and subdirectories) which contain the word "report". The -type f flag ensures that we only get a list of regular files (and not directories and links for example) to pass to grep. The -i flag means ignore case, so we are looking for "Report" as well as "report". If you might have filenames with spaces or other special characters, this won't work quite as expected. In this case we must use: find -type f -print0 | xargs -0 grep -i report There is a really good tutorial on finding files in the info pages. If you are using the Konqueror web browser or the Galeon web browser, you can read it by entering info:/find in the address bar. Otherwise type info find on the command line.

2.3. Creating and extracting file archives (zip, gzip and tar)
For a single file, you get the best compression usually with bzip2: bzip2 -zv filein > fileout.bz2 This file can be recovered with bzip2 -xv filout.bz2 > filein For multiple files, put them together in a directory and use tar

tar -cvjf file.tar.bz2 directory To extract again use tar -xvjf file.tar.bz2 Although bzip2 gives the best compression, gzip is faster, so you may prefer it. With gzip compression, the above two commands become tar -cvzf file.tar.gz directory tar -xvzf file.tar.gz The most common compression format on Windows systems is pkzip: the corresponding commands available on linux are zip and unzip. You can always use the command unp to uncompress a file in any compression format. (Well I've never found anything it couldn't uncompress.) If you want to use a graphical tool to compress and decompress files, there is ark or fileroller.

2.4. Emacs - a powerful editor
Emacs is a powerful editor. It runs either a text console or graphically, as an X program. (To force it to start in the text console mode, use emacs -nw.) For programming, or HTML, or LaTeX, or other similar structured text files, it is very useful. Note that the "Meta" key, which is indicated by "M-" on the reference card, is the "Alt" on PC keyboards.

3. Running long calculations
3.1. Getting the prompt back after starting a job
Use & at the end of the command. This puts the job in the background. For example: ./myjob 10 & To see all your background jobs (the ones started in the current shell), enter: jobs To bring one of these to the foreground, use: fg %n where n is the job number (yes it needs a % in front of it). To move it to the background again, hit CTRL-Z. This however doesn't just move the job to the background, it suspends it. To get it running again, use bg %n

3.2. Keeping a job running after you log out
Use nohup. For example, suppose you want to run ./myjob 20. In this case, you could use: nohup nice ./myjob 20 > out_file & There are several things to note here:
• •


The & at the end of the command line is there so you get the command prompt back immediately (see the previous section). We have redirected the output to a file out_file with > out_file. If you don't do this, any output to STDIN (normally the console) will automatically go into a file called nohup.out. We've niced the job to reduce it's execution priority. This is courteous .

If you want to see what's in nohup.out (or whatever you called it) at any time, you can do the usual cat nohup.out Here's another potentially more useful command:

tail -f nohup.out tail shows you the last few lines of the files. In addition, the -f flag ("follow") means that as lines get added to the file, they are displayed on the screen. This looks just like when you run the program without nohup, except when you hit CTRL-C, the program keeps on running in the background. One nice refinement is to have the output mailed to you on completion. Try this: nohup nice ./longjob job_options ; cat nohup.out | mail $USER &

3.3. Suspending a long-running job and restarting it later
First find the PID (process ID) with top or with ps. For example, I might list all jobs belonging to me with: ps -u eric To stop the job, send it a STOP signal: kill -s STOP PID (replace PID with the actual PID number). It is now no longer running, but it still occupies memory (although the memory it uses will probably be swapped out to disk). When you want to resume execution, give it a CONT signal: kill -s CONT PID

3.4. Running a series of jobs
Suppose you want to run ten jobs overnight. You could just start them all at once (using nohup as described above). However, it would be more efficient to run them sequentially. Besides being faster in total (less multitasking overhead), it is also much more considerate to other users to run your jobs in series instead of in parallel. An easy way to run things in sequence is to use a script. For example:
#!/bin/bash # This script runs mycalc with arguments 10 through 100. ./mycalc 10 ./mycalc 20 ./mycalc 30 ./mycalc 40 ./mycalc 50 ./mycalc 60 ./mycalc 70 ./mycalc 80 ./mycalc 90

./mycalc 100

If you save this script as docalc, you need to make it executable before you can run it. chmod +x docalc You could run it like this nohup nice ~/docalc &

3.5. More advanced scripts
As an example of the things you can do with bash scripts, the example script in the previous section could also have been written like this:
#!/bin/bash # This script runs mycalc with arguments 10 through 100. ARGS="10 20 30 40 50 60 70 80 90 100" for ARG in $ARGS do ./mycalc $ARG done

Obviously, if you have a lot of jobs to run, this is easier than writing a script with one line per job. This just gives you a taste of the possibilities. If you find this kind of thing useful, then you should check out Heiner's SHELLdorado.

4. When you're not in the department
4.1. Accessing data from any Windows computer
For Windows computers external to the physics local network, you need to get some ssh/scp program. Fortunately, this is easy when you know where to look. There is a free ssh (and scp) program from the physics web site (internal access only). A publically accessible download location is ftp://ftp.ssh.com/pub/ssh/. This program has a very nice GUI interface. However you need to have installation rights on the Windows computer to install and use it. For other cases, Putty ( http://www.chiark.greenend.org.uk/%7Esgtatham/putty/download.html )comes in very handy. There is a program for an ssh terminal (PuTTY), and an scp program (pscp). The pscp program is particularly elegant, since it doesn't even need to be "installed" on the Windows computer. Just put pscp.exe in the directory to/from which you want to transfer files, then open a terminal window (usually under Accessories), cd to the directory (something like CD C:\Users\Bob), and use pscp like you would use scp at the command line on physics. Yet another option is Knoppix, which is described in the next section...

4.2. Linux without installation - at home or travelling
There's a fantastic Linux distro called Knoppix. It's a complete Linux system on a CD. You stick the CD in any PC with a CD drive, boot it up, and you have a functioning Linux system with much useful software, like Mozilla, OpenOffice and SSH. It does a fantastic job of hardware detection, and you can mount the hard disks if you need to read/write to them. It's also easy to establish a network connection. In fact if there's a Windows installation on the computer, it will read the network configuration from the system files on the Windows partition. When finished with Knoppix, pop out the CD and reboot and the computer reverts to its former personality. I really recommend that you take a Knoppix CD with you when you travel. It makes it very easy to connect back to physics.

5. More Quick Tips
5.1. Scroll with the keyboard
You can scroll back to see lines which have scrolled off the top of your shell window with CTRL-PAGEUP and CTRL-PAGEDOWN.

5.2. Copy with xclip
Here's another useful trick with X-Windows copying and pasting: you can pipe output directly into the clipboard with xclip. For example, if you want to e-mail someone a directory listing, do ls | xclip then middle-click to paste into your email.

5.3. Lots of keyboard shortcuts
Here are some common keystrokes. Most of these will work at the command line, and in many Linux programs. KDE and Gnome programs may be an exception - their default control key mapping is sometimes closer to Windows. However you can change to Unix Default in the KDE Control Center, under Look & Feel, Key Bindings and in the Gnome Control Center, under Keyboard Shortcuts. Once you learn these key bindings, you will find that you can work very fast across many different programs. In addition to the key bindings described below, there are the standard arrow keys, page up and page down keys and so on. However the ones described below all lie under your fingers, and so are faster once you learn them. Table 1. Common editing key bindings CTRL-A Move to the end of the line. CTRL-E Move to the beginning of the line. CTRL-F Move forward one character. CTRL-B Move backward one character. ALT-F Move forward one word. ALT-B Move backward one word. CTRL-V Move one page down. ALT-V Move one page up. CTRL-D Delete one character forward.

CTRL-H Delete one character backward. ALT-D Delete one word forward. CTRL-K Delete (Kill) everything from the cursor forwards (to the end of the line). CTRL-U Delete everything from the cursor backwards (to the beginning of the line). CTRL-T Transpose two characters. ALT-T Transpose two words. ESC . Insert the last argument (word) from the previous command. Table 2. Common control key bindings CTRLC CTRL\ CTRLD CTRLG CTRLL Terminate the current program, or sometimes cancel some operation. Seriously kill the current program!! Use only after CTRL-C has failed! End-of-file. This is often used to signal the end of input, or to exit a program.

Cancel the current operation. A VERY useful command to get yourself out of things. Redraw the console. You will find this one useful on physics, where the new mail notification tends to overwrite whatever you are working on. The only time you need this is if you accidently hit CTRL-S. In that case the CTRLconsole is locked until you hit CTRL-Q to unlock it. Apparently once a long long Q time ago this was actually a useful feature. Recall command containing characters. After hitting CTRL-R, type the fragment CTRLyou want to search for. If it returns a command which isn't the one one want, hit R CTRL-R again to cycle through matches. Suspend the current program and put it in the background. This is very useful CTRLonce you figure out how it works. Refer to the section on putting jobs in the Z background.

5.4. ssh and scp without typing a password each time
You can create and exchange ssh keys to avoid repeatedly typing your password. I will use the example of connection from a machine called berserk to a machine called physics. On berserk, create an ssh key pair with ssh-keygen -t dsa It will prompt you whether you want a pass-phrase or not. If you choose one, you have to type this once per log-in session, but it's rather more secure. It's up to you.

Now copy the .ssh/id_dsa.pub file to physics. From berserk run scp .ssh/id_dsa.pub userid@physics:tempid Here we've renamed it tempid. Then on physics run cat tempid >> .ssh/authorized_keys This will add the contents of tempid to .ssh/authorized_keys. That's it! Your can now remove the temporary file: rm tempid. Now when you use ssh to login to physics from berserk, you won't be asked for your password! Of course, you can do all the same steps, interchanging berserk and physics to go the other direction.

If this doesn't work for you, it might be because the permissions on your home directory are too loose. Running the following command on both berserk and physics might help: chmod -R go-rwx ~ This removes reading, writing and executing permissions from all your files for group and other, leaving only permissions for user (you).

5.5. Send a quick e-mail message without starting any mail program
Just type for example: mail bob@physics.ubc.ca Then enter a subject when prompted, followed by the message body. When finished, type CTRL-D. Presto! I often use this to send quick reminders to myself.

• apropos word which command time command • time cat • nice info • renice 19 -p $$ • look reference grep --color • /usr/share/dict/words gpg -c file gpg file.gpg • alias hd='od -Ax -tx1z -v' • alias realpath='readlink -f' • set | grep $USER • ls /usr/bin | pr -T9 -W$COLUMNS touch -c -t 0304050607 file • cd • cd (cd dir && command) • pushd . reference

Show commands pertinent to word. See also threadsafe Show full path name of command See how long a command takes Start stopwatch. Ctrl-d to stop. See also sw Run a low priority command (info in this case) Make shell (script) low priority. Use for non interactive tasks Quickly search (sorted) dictionary for prefix Highlight occurances of regular expression in dictionary Encrypt file Decrypt file Handy hexdump. (usage e.g.: • hd /proc/self/cmdline | less) Canonicalize path. (usage e.g.: • realpath ~/../$USER) Search current environment Print in 9 columns to width of terminal Set file timestamp (YYMMDDhhmm)

Dir Navigation
Go to previous directory Go to home directory Go to dir, execute command and return to current dir Put current dir on stack so you can popd back to it

gzip < /dev/cdrom > cdrom.iso.gz Save copy of data cdrom mkisofs -V NAME -r dir | gzip > Create cdrom image from contents of dir cdrom.iso.gz mount -o loop cdrom.iso /mnt/dir Mount the cdrom image at /mnt/dir (read only) cdrecord -v dev=/dev/cdrom blank=fast Clear a CDRW gzip -dc cdrom.iso.gz | cdrecord -v Burn cdrom image (use dev=ATAPI -scanbus to confirm dev=/dev/cdrom dev) cdparanoia -B Rip audio tracks from CD to wav files in current dir cdrecord -v dev=/dev/cdrom -audio Make audio CD from all wavs in current dir (see also *.wav cdrdao) oggenc --tracknum='track' Make ogg file from wav file track.cdda.wav -o 'track.ogg'

tar c dir/ | bzip2 > dir.tar.bz2 Make archive of dir/ bzip2 -dc dir.tar.bz2 | tar x Extract archive (use gzip instead of bzip2 for tar.gz files) tar c dir/ | gzip | gpg -c | ssh Make encrypted archive of dir/ on remote machine user@remote 'dd of=dir.tar.gz.gpg' find dir/ -name '*.txt' | tar c --filesMake archive of subset of dir/ and below from=- | bzip2 > dir_txt.tar.bz2 find dir/ -name '*.txt' | xargs cp -a -Make copy of subset of dir/ and below target-directory=dir_txt/ --parents ( tar c /dir/to/copy ) | ( cd /where/to/ Copy (with permissions) copy/ dir to /where/to/ dir && tar x -p ) ( cd /dir/to/copy && tar c . ) | ( cd Copy (with permissions) contents of copy/ dir to /where/to/ && tar x -p ) /where/to/ ( tar c /dir/to/copy ) | ssh -C Copy (with permissions) copy/ dir to remote:/where/to/ user@remote 'cd /where/to/ && tar x dir p' dd bs=1M if=/dev/hda | gzip | ssh Backup harddisk to remote machine user@remote 'dd of=hda.gz'

Rsync (Use The --Dry-Run Option For Testing)
rsync -P Only get diffs. Do multiple times for troublesome rsync://rsync.server.com/path/to/file downloads file rsync --bwlimit=1000 fromfile tofile Locally copy with rate limit. It's like nice for I/O rsync -az -e ssh --delete ~/public_html/ Mirror web site (using compression and encryption) remote.com:'~/public_html' rsync -auz -e ssh remote:/dir/ . && Synchronize current directory with remote one rsync -auz -e ssh . remote:/dir/

File Searching
• alias l='ls -l --color=auto' quick dir listing • ls -lrt List files by date. See also newest find -name '*.[ch]' | xargs grep -E 'expr' Search 'expr' in this dir and below. See also findrepo find -type f -print0 | xargs -r0 grep -F Search all regular files for 'example' in this dir and below 'example' find -maxdepth 1 -type f | xargs grep -F Search all regular files for 'example' in this dir 'example' find -maxdepth 1 -type d | while read Process each item with multiple commands (in while dir; do echo $dir; echo cmd2; done loop) • find -type f ! -perm -444 Find files not readable by all (useful for web site) • find -type d ! -perm -111 Find dirs not accessible by all (useful for web site) • locate -r 'file[^/]*\.txt' Search cached index for names. This re is like glob


Networking (Note Ifconfig, Route, Mii-Tool, Nslookup Commands Are Obsolete)
• ip link show List interfaces ethtool interface List interface status ip link set dev eth0 name wan Rename eth0 to wan ip addr add brd + dev eth0 Add ip and mask( ip link set dev interface up Bring interface up (or down) ip route add default via Set default gateway to tc qdisc add dev lo root handle 1:0 Add 20ms latency to loopback device (for testing) • netem delay 20msec • tc qdisc del dev lo root Remove latency added above • host pixelbeat.org Lookup ip address for name or vice versa • hostname -i Lookup local ip address (equivalent to host `hostname`) • netstat -tupl List internet services on a system • netstat -tup List active connections to/from system

Wget (Multi Purpose Download Tool)
(cd cmdline && wget -nd -pHEKk Store local browsable version of a page to the current dir http://www.pixelbeat.org/cmdline.html) wget -c Continue downloading a partially downloaded file http://www.example.com/large.file wget -r -nd -np -l1 -A '*.jpg' Download a set of files to the current directory http://www.example.com/dir/ wget ftp://remote/file[1-9].iso/ FTP supports globbing directly wget -q -O• http://www.pixelbeat.org/timeline.html Process output directly | grep 'a href' | head echo 'wget url' | at 01:00 Download url at 1AM to current dir Do a low priority download (limit to 20KB/s in this wget --limit-rate=20k url case) • wget -nv --spider bookmarks.html --force-html -i Check links in a file Efficiently update a local copy of a site (handy from cron)

wget http://www.example.com/


Windows (Note Samba Is The Package That Provides All This Windows Specific Networking Support)
• smbtree nmblookup -A Find windows machines. See also findsmb Find the windows (netbios) name associated with ip

smbclient -L windows_box mount -t smbfs -o fmask=666,guest Mount a windows share //windows_box/share /mnt/share echo 'message' | smbclient -M Send popup to windows machine (off by default in XP windows_box sp2)

address List shares on windows machine or samba server

• echo '(1 + sqrt(5))/2' | bc -l • echo 'obase=16; ibase=10; 64206' | bc • • • • • Quick math (Calculate φ). See also bc Base conversion (decimal to hexadecimal) Base conversion (hex to dec) ((shell arithmetic echo $((0x2dec)) expansion)) echo 'pad=20; min=64; More complex (int) e.g. This shows max FastE packet (100*10^6)/((pad+min)*8)' | bc rate echo 'pad=20; min=64; print Python handles scientific notation (100E6)/((pad+min)*8)' | python echo 'pad=20; plot [64:1518] (100*10**6)/((pad+x)*8)' | gnuplot - Plot FastE packet rate vs packet size persist seq 100 | (tr '\n' +; echo 0) | bc Add a column of numbers. See also add and funcpy

Text Manipulation (Note Sed Uses Stdin And Stdout, So If You Want To Edit Files, Append <Oldfile >Newfile)
sed 's/string1/string2/g' Replace string1 with string2 sed 's/\(.*\)1/\12/g' Modify anystring1 to anystring2 sed '/ *#/d; /^ *$/d' Remove comments and blank lines sed ':a; /\\$/N; s/\\\n//; ta' Concatenate lines with trailing \ sed 's/[ \t]*$//' Remove trailing spaces from lines sed 's/\([\\`\\"$\\\\]\)/\\\1/g' Escape shell metacharacters active within double quotes sed -n '1000p;1000q' Print 1000th line sed -n '10,20p;20q' Print lines 10 to 20 sed -n Extract title from HTML web page 's/.*<title>\(.*\)<\/title>.*/\1/ip;T;q' sort -t. -k1,1n -k2,2n -k3,3n -k4,4n Sort IPV4 ip addresses • echo 'Test' | tr '[:lower:]' '[:upper:]' Case conversion • tr -dc '[:print:]' < /dev/urandom Filter non printable characters • grep 'processor' /proc/cpuinfo | wc -l Count lines

Set Operations (Note LANG=C Is For Speed) (Note Also These Assume No Duplicate Lines Within A File)
LANG=C sort file1 file2 | uniq LANG=C sort file1 file2 | uniq -d Union of unsorted files Intersection of unsorted files

LANG=C sort file1 file1 file2 | uniq -u Difference of unsorted files LANG=C sort file1 file2 | uniq -u Symmetric Difference of unsorted files LANG=C comm file1 file2 | sed Union of sorted files 's/^\t*//' LANG=C comm -12 file1 file2 Intersection of sorted files LANG=C comm -13 file1 file2 Difference of sorted files LANG=C comm -3 file1 file2 | sed Symmetric Difference of sorted files 's/^\t*//'

cal -3 cal 9 1752 date -d fri date --date='25 Dec' +%A date --date '1970-01-01 • 1234567890 seconds' • TZ=':America/Los_Angeles' date • • • • Display a calendar Display a calendar for a particular month year What date is it this friday. See also day What day does xmas fall on, this year UTC Convert number of seconds since the epoch to a date What time is it on West coast of US (use tzselect to find TZ)

echo "mail -s 'get the train' P@draigBrady.com < /dev/null" | at Email reminder 17:45 echo "DISPLAY=$DISPLAY • xmessage cooker" | at "NOW + 30 Popup reminder minutes"

• printf "%'d\n" 1234 Print number with thousands grouping appropriate to locale get ls to do thousands grouping appropriate to locale Extract info from locale database

• BLOCK_SIZE=\'1 ls -l • echo "I live in `locale territory`" locale | cut -d= -f1 | xargs locale -kc | • List fields in locale database less • LANG=en_IE.utf8 locale int_prefix Lookup locale info for specific country. See also ccodes

Disk Space (See Also Fslint)
• • • • • ls -lSr du -s * | sort -k1,1rn | head df -h df -i fdisk -l rpm -q -a --qf • '%10{SIZE}\t%{NAME}\n' | sort k1,1n Show files, biggest last Show top disk users in current dir. See also dutop Show free disk space Show free inodes Show disks partitions sizes and types (run as root) List all packages by installed size (Bytes) on rpm distros


dpkg-query -W -f='${InstalledList all packages by installed size (KBytes) on deb Size;10}\t${Package}\n' | sort -k1,1n distros dd bs=1 seek=2TB if=/dev/null Create a large test file (taking no space). See also • of=ext3.test truncate

• • • • • strace -c ls >/dev/null strace -f -e open ls >/dev/null ltrace -f -e getenv ls >/dev/null lsof -p $$ lsof ~ Summarise/profile system calls made by command List system calls made by command List library calls made by command List paths that process id has open List processes that have specified path open Show network traffic except ssh. See also tcpdump_not_me List processes in a hierarchy

• tcpdump not port 22

• ps -e -o pid,args --forest ps -e -o • pcpu,cpu,nice,state,cputime,args --sort List processes by % cpu usage pcpu | sed '/^ 0.0 /d' ps -e -orss=,args= | sort -b -k1,1n | pr • List processes by mem usage. See also ps_mem.py TW$COLUMNS ps -C firefox-bin -L -o • List all threads for a particular process pid,tid,pcpu,state • ps -p 1,2 List info for particular process IDs • last reboot Show system reboot history • free -m Show amount of (remaining) RAM (-m displays in MB) • watch -n1 'cat /proc/interrupts' Watch changeable data continuously

System Information (See Also Sysinfo)
hdparm -i /dev/hda hdparm -tT /dev/hda badblocks -s /dev/hda • mount | column -t • cat /proc/partitions • • • • grep MemTotal /proc/meminfo grep "model name" /proc/cpuinfo lspci -tv lsusb -tv Show info about disk hda Do a read speed test on disk hda Test for unreadable blocks on disk hda Show mounted filesystems on the system (and align output) Show all partitions registered on the system Show RAM total seen by the system Show CPU(s) info Show PCI info Show USB info

Recode (Obsoletes Iconv, Dos2unix, Unix2dos)
• recode -l | less recode Show available conversions (aliases on each line) windows-1252.. Windows "ansi" to local charset (auto does CRLF

• • • •

file_to_change.txt conversion) recode utf-8/CRLF.. file_to_change.txt Windows utf8 to local charset recode iso-8859-15..utf8 Latin9 (western europe) to utf8 file_to_change.txt recode ../b64 < file.txt > file.b64 Base64 encode recode /qp.. < file.txt > file.qp Quoted printable decode recode ..HTML < file.txt > file.html Text to HTML recode -lf windows-1252 | grep euro Lookup http://www.pixelbeat.org/docs/utf8.html echo -n 0x80 | recode latin-9/x1..dump Show what a code represents in latin-9 charmap echo -n 0x20AC | recode ucsShow latin-9 encoding 2/x2..latin-9/x echo -n 0x20AC | recode ucs-2/x2..utfShow utf-8 encoding 8/x

• mc • • • • screen links gnuplot octave Powerful file manager that can browse rpm, tar, ftp, ssh, ... Virtual terminals with detach capability, ... Web browser Interactive/scriptable graphing Matlab like environment

6. Where to go to learn more
Another introduction to to Unix at UBC has been written by Matt Choptuik. You can find it http://laplace.physics.ubc.ca/410/Notes_unix.html More handy tips for using the http://www.luv.asn.au/overheads/bash.html bash shell can be found at

A fantastic resource for unix shells in general and shell programming can be found at http://www.shelldorado.com/

To top