================================ THE ESSENTIAL LINUX COMMAND-LINE ================================ An introduction to the Linux console, oriented towards the novice-user with no prior experience using a command-line environment. Deepak N. 09 March 2003. http://puggy.symonds.net/~deep/ n.deepak@gmail.com This document is in ASCII text, in order to make it readily accessible from any text editor. It is intended to be read after viewing the presentation introducing you to the Linux operating system. A very basic (essential) coverage of the vi and emacs text editors can be found in the file 4viemacs.txt. This document is distributed under the GNU Free Documentation Licence. -------------------- FILES IN THIS PRIMER ==================== 01. bash! --------- Introduction to the Linux command-line 02. Lonely Shell -- Commandeering your Linux system 03. Yes, Master --- (Helpful) advanced topics 04. C-x C-c :q! --- Appendix: Essential vi and emacs --------------------------------------------- LONELY SHELL: COMMANDEERING YOUR LINUX SYSTEM ============================================= Now that you have been introduced to the shell, this file lists the commands you need to know as a user of Linux. I have arranged the commands in a task-oriented manner so that the learning process might be easier. Go ahead and type them out! --------------- REFERENCES USED =============== I have organised the commands below in the same way as in the book 'Linux Command Instant Reference' published by Sybex Inc., USA. (ISBN: 0-7821-2748-7) I have also used the book 'UNIX Concepts & Applications', 2/Ed, by Sumitabha Das, published by Tata-McGraw Hill, Delhi, India. (ISBN: 0-07-463090-3) 'The Linux System Administrators' Guide' by Lars Wirzenius and Joanna Oja, v0.6.2, part of the Linux Documentation Project, was also very helpful. Many thanks to Karl O. Pinc for his valuable suggestions in improving this primer. Thanks also to Swetha for her comments and contributions. -------- CONTENTS ======== o Logging in and managing sessions o Getting help and information in Linux o Finding and killing processes o Navigating the file-system and finding files o Managing files and directories o Archiving, compressing and extracting files o Working with text files o Working with multimedia o Managing your disks o Configuring and managing your system Particulary important commands are marked with an asterisk. -------------------------------- Logging In and Managing Sessions ================================ *How do I change my password? passwd *How do I log out of the system? exit *How do I shut down my computer? halt *How do I reboot my computer? reboot *How do I get into the graphical interface? startx How do I gain superuser privileges without logging out? su -l (You will be prompted for the root password.) The GUI doesn't seem to work. How to configure? xf86config *********************************************************************** ------------------------------------- Getting Help and Information in Linux ===================================== How do I display or print a calendar? cal o Displaying the calendar for year 1492 AD: cal 1492 o Displaying the calendar for July, 1776 AD: cal 7 1776 How do I see the current system date and time? date How do I get information on other users? w *How do I find out what a basic shell command does, say cd? help cd How do I find out my user and group names and IDs? whoami *How do I get a one-line help on a command, say grep? whatis grep *How do I get information about GNU utilities, say emacs? info emacs *How do I learn what a command can do, say for cdrecord? man cdrecord *How do I find out my current directory? pwd How do I find out information about my Linux? uname -a (The -a is an option: it tells uname to output All the information.) How do I find out how long my system is up? uptime @@@@@ I suggest you look up documentation for: w: man w info: info info man: man man uname: man uname @@@@@ *********************************************************************** ----------------------------- Finding and Killing Processes ============================= *How do I view a list of running processes? ps -A (-A means all processes run by All users.) *How do I terminate a process, say with PID 5076? kill -9 5076 (Use the ps command to find out the PID of a process.) (9 is the 'sure kill' signal.) How do I know how much RAM is unused? free -m (-m outputs data in terms of megabytes) @@@@@ I suggest you look up man pages for: ps: man ps @@@@@ *********************************************************************** ------------------------------------------- Navigating the Filesystem and Finding Files =========================================== Note: all commands accept path-names instead of files. For example, if you are in /home/tomh directory and want to change to mydir subdirectory, both these commands are equivalent: $ cd mydir $ cd /home/tomh/mydir *How do I change directory, say to mydir under current directory? cd mydir *How do I list the files in the current directory? ls o I want to know more than its name ls -l The -l (Long) option shows the following data: + Permissions assigned to the file/directory + Number of links to the file/directory + Owner of the file/directory + Group to which the file/directory belongs + Size of the file in bytes + Date/time at which the file was last modified + Name of the file o I want to list files with names starting with 'chap' ls chap* o I want to list files in the subdirectories also ls -R chap* (-R means recursive) o I want to sort the files as per their Size ls -Slh (Here we have combined options -S, -l, and -h.) o I want to sort files by their modification Time ls -t o I want to see hidden files as well ls -a (-a means All files.) o I want to see file-sizes as B/kB/MB/GB ls -lh (-h means Human-readable.) (Say: ls | less if the output scrolls too fast. More info on piping in 3adv.txt.) *How do I find out the type of a file, say test.gz? file test.gz *How do I find all files changed in the last two hours? find -ctime -2 (find is discussed in some detail later as an advanced topic.) *How do I locate the binary for a command, say lilo? whereis lilo *How do I create a 'shortcut' to 'My Documents' folder in Windows? ln -s "/mnt/winc/My Documents" win-mydocs (You can now type 'cd win-mydocs' to go to My Documents.) *How do I quickly locate a file in the filesystem, say log files? locate "*.log" (If locate says 'Unable to find database file', use the updatedb command given below.) How do I create a database of filenames for quick search? updatedb (updatedb needs superuser access.) (find searches the hard-drive; locate searches a database.) @@@@@ I suggest you look up documentation for: ls: man ls @@@@@ *********************************************************************** ------------------------------ Managing Files and Directories ============================== Note: all commands accept path-names instead of files. In order to edit a file, use an easy editor like pico or Joe. Just say: pico file_name How do I create an empty file test? > test (NOTE: This overwrites 'test' if it exists.) How do I input text to a file 'test' from command-line? cat > test (Press CTRL-d when you are done.) *How do I make sure my files cannot be read by others? chmod go-r * (chmod is covered in some detail later in this document.) *How do I copy files? (NOTE: cp overwrites existing files.) o Copy a file test to another file test2 cp test test2 o Copy a file test to directory mydir cp test mydir o Copy test and test2 to directory newdir cp test test2 mydir o Copy a directory tree dir1 to dir2 cp -a dir1 dir2 o Ask before overwriting cp -i test test2 (-i means interactive.) o Force overwriting cp -f test test2 How do I view statistics about a file test? stat test *How do I create a new directory, say newdir under the current directory? mkdir newdir (You can create multiple directories with a single mkdir.) How do I remove an empty directory, say newdir in the current directory? rmdir newdir (You can delete multiple directories with a single rmdir.) *How do I rename a file, say from test to test2? mv test test2 (NOTE: mv overwrites existing files.) o Ask before overwriting mv -i test test2 o Force overwriting mv -f test test2 *How do I move files? (NOTE: mv overwrites existing files.) o Move a file test to directory newdir mv test newdir o Move test and test2 to directory newdir mv test test2 newdir o Move a directory tree olddir to newdir mv olddir --target-directory=newdir o Ask before overwriting test2 mv -i test test2 o Force overwriting of test2 mv -f test test2 *How do I delete a file, say test? rm test (NOTE: Files, once deleted, cannot be recovered.) (You can delete multiple files with a single rm.) *How do I delete a directory tree somedir? rm -rf somedir (The -r option specifies recursive delete, -f forces deletion.) (NOTE: Use this command with caution.) How do I find out how many words are in a file foo? wc foo (wc gives three data: number of lines, number of words, number of characters.) (wc accepts multiple files.) @@@@@ I suggest you look up documentation for: wc: man wc @@@@@ *********************************************************************** ------------------------------------------- Archiving, Compressing and Extracting Files =========================================== *How do I compress a file huge_file to the maximum extent? gzip --best huge_file [OR] bzip2 -9 huge_file [OR] zip -9 test.zip huge_file *How do I decompress such a file? gunzip huge_file.gz [OR] bunzip2 huge_file.bz2 [OR] unzip test.zip *How do I List the files in a compressed file? gzip -l huge_file.gz [OR] unzip -l test.zip How do I view a file in a compressed archive without extracting it? zcat huge_file.gz [OR] bzcat huge_file.bz2 *How do I Create a new tar archive, named backup.tar, containing files from the directory imp_dir? tar -cf backup.tar imp_dir (-f indicates create a File.) *How do I Update backup.tar with changed files? tar -uf backup.tar imp_dir *How do I lisT the files present in backup.tar? tar -tf backup.tar *How do I eXtract files from backup.tar? tar -xf backup.tar How do I create a compressed tarball of imp_dir in one shot? tar -czf backup.tar.gz imp_dir [OR] tar -cjf backup.tar.bz2 imp_dir @@@@@ I suggest you look up documentation for: gzip: man gzip bzip2: man bzip2 zip: man zip tar: info tar @@@@@ *********************************************************************** ----------------------- Working With Text Files ======================= *How do I view the contents of a file, say test? cat test *How do I view the contents one page at a time? less test ('more test' also does the same job. 'less' is better than 'more') *How do I compare two files test and test2? cmp test test2 *How do I find files containing the word "winner" in the current directory? grep "winner" * How do I find files containing the words "foo" or "bar" in the current directory as well as its subdirectories? egrep -r "foo | bar" * (The grep family is discussed in some detail later as an advanced topic) How do I view the first 5 lines of a file test? head -5 test How do I view the last 5 lines of a file test? tail -5 test How do I view all the lines from the 5th line? tail +5 test *How do I check the spelling of a file test? ispell test How do I sort the lines of a file test in dictionary-order? sort test o How do I sort it in Reverse order? sort -r test o How do I sort a set of Numbers? sort -n test How do I print a file test? lpr test How do I split a file test into 1KB chunks? split -b 1024 test How do I replace all occurrences of "foo" by "bar" in a file test, and save the file as test2? cat test | tr "foo" "bar" > test2 (Pipes and redirections are discussed a bit more as advanced topics later) @@@@@ I suggest you look up documentation for: less: man less sort: man sort lpr: man lpr split: man split @@@@@ *********************************************************************** ----------------------- Working With Multimedia ======================= How do I view an image, say file.jpg? display file.jpg (Needs X and ImageMagick package.) *How do I play an audio CD? cdp *How do I rip an audio CD into separate .wav files? cdparanoia -B "1-" *How do I play an mp3, say song.mp3? mpg123 song.mp3 & (The & indicates that the song should keep playing in background while I do other tasks.) How do I record the microphone input into a .wav file? rec -t wav recording.wav How do I view a HTML file in the shell? links file.html [OR] lynx file.html @@@@@ I suggest you look up documentation for: display: man display cdparanoia: man cdparanoia rec: man rec @@@@@ *********************************************************************** ------------------- Managing Your Disks =================== How do I create partitions on my primary master hard disk? fdisk /dev/hda (fdisk needs superuser access. Replace a with b for primary slave, c for secondary master and d for secondary slave.) *How do I find how much space is left on my hard drive? df -h (-h means Human-readable output.) *How do I find out how much space the current directory consumes? du -sh (-s means Summary, and -h means Human-readable output.) How do I repair a filesystem which has been mounted read-only? fsck -a (If your filesystem is corrupted, Linux mounts it read-only and gives you a basic prompt. Run fsck from here.) (-a means Automatic repair.) *How do I mount a floppy disk? mount /mnt/floppy (The actual mount-point may vary. For example, Debian mounts floppies under /floppy.) *How do I unmount it? umount /mnt/floppy *How do I create an emergency Linux boot-up disk? /sbin/mkbootdisk --device /dev/fd0 2.4.2-2 (Replace the 2.4.2-2 with your kernel number, also give the proper device file for your floppy drive. Use 'uname' to find out the kernel number.) @@@@@ I suggest you look up man pages for: fdisk: man fdisk du: man du @@@@@ *********************************************************************** ------------------------------------ Configuring and Managing Your System ==================================== NOTE1: All the commands below require super-user access. NOTE2: The package-management commands given below are for Red Hat and RH-based systems such as Mandrake. If you use, say Debian, you would need to use a simple text-mode graphical tool called 'dselect' to manage software. *How do I add a user tom? useradd tom *How do I delete a user tom? userdel tom *How do I change the password for a user tom? passwd tom *How do I install a package foo-1.0.rpm? rpm -i foo-1.0.rpm *How do I uninstall a package foo-1.0.rpm? rpm -e foo *How do I upgrade a package foo-1.0.rpm to foo-1.1.rpm? rpm -U foo-1.1.rpm How do I list the files of a package foo-1.0.rpm? rpm -ql foo How do I get information on a package foo-1.0.rpm? rpm -qi foo How do I find out the package to which a file, say /bin/ls, belongs? rpm -qf /bin/ls --- I suggest you look up man pages for: rpm: man rpm --- Well, that's it! Those are the only commands you really need to know as a desktop user. There are many more, but you don't need to learn them for most tasks you do daily. Whoever said that in order to learn Linux commands you have to buy that 1000-page book? Take your own time in learning and understanding the commands. Gradually your mind will begin to enter the right command for the right job. Once you are confident, you can take up the advanced material in the next file.