================================ 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. 05 August 2002. 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 -------------------------------------- YES, MASTER: (HELPFUL) ADVANCED TOPICS ====================================== I now proceed to give you some more advanced topics, but I have tried my best to make them easy to understand for even a beginner. There is also a brief explanation on how to use three commands which I thought were too complex for any beginner. They only serve to improve your productivity with the command-line. Read this file after you are comfortable with the Linux shell and its behaviour with respect to commands listed in the previous file. -------- CONTENTS ======== o Background, foreground, running, stopping o Three important but somewhat difficult commands + find + chmod + grep o A crash-course on redirection and piping + Input-redirection + Output-redirection + Error-redirection + Piping ----------------------------------------- BACKGROUND, FOREGROUND, RUNNING, STOPPING ========================================= Unlike DOS, you can run multiple commands in Linux at the same time. Let's say you have a huge file to be sorted. Terminate the sort command with the ampersand (&) symbol. The shell will execute it in the background: $ sort huge_file & [1] 962 $ _ 962 is the PID (process ID) of your job. It is executed in the background, and the prompt is returned immediately. If a process is executing for too long, and you didn't set it to run as a background process, here is how to get back the prompt and set this process to run in background. 1. Press CTRL-z to stop the executing process. 2. You will get the prompt. Now type bg and press ENTER. 3. The process will be executed in the background, and you will get back the prompt for other work. 4. If at any time you want this to continue to execute in the foreground, press fg at the prompt and press ENTER. ------------------------------------------------- THREE IMPORTANT (BUT SOMEWHAT DIFFICULT) COMMANDS ================================================= I now give additional coverage to some commands. You can read this section at leisure, since it is somewhat terse compared to other sections. ---- find ---- find is a very versatile command, with no equivalent in DOS. You can use 'find' to search for files using a variety of search-conditions, and hen perform many actions for the results. find has this syntax: find Some examples will show you how you can use 'find' to make your life easy: How do I find a file with the name fstab? find / -name fstab Note: / indicates that find should search all the directories. -name indicates that it should search for a file with name fstab. The default action is to output on-screen, so we can leave this. How do I find files with extension mp3? find / -name "*.mp3" Note: Don't forget the quotes. Otherwise the shell will expand *.mp3 to all the files with mp3 extension in your current directory and you will end up getting useless results. How do I find which files in my home directory were changed in the past two days? find ~ -mtime -2 Note: Here the search-condition is modification time: -mtime. You can also give +2 if you want to look at files _not_ modified in the last two days. How do I find files accessed in the last 2 hours? find ~ -amin -120 Note: -amin denotes access in minute-interval. You can also use mmin similarly. As before, +120 denotes files which were not accessed in the past 2 hours. How do I find files with size greater than 1 MB? find / -size +1024 Note: -size indicates file-size. Again, -1024 outputs files with size less than 1 MB. You can also find files greater than 1 MB but less than 2 MB: find / -size +1024 -size -2048 How do I find files in the current directory newer than a file 'test'? find . -newer test Note: -newer indicates that find should look for files newer than the file 'test'. How do I find files with tmp extension and delete them with confirmation? find / -name "*.tmp" -ok rm {} \; Note: Firstly, don't get carried away by the terseness of the command above. -ok is an action, and this is the first instance in which we are specifically asking find to take this action. -ok indicates that the command following it must be executed with confirmation every time. In our case, find will ask you for confirmation every time it uses rm to delete a file. The "{} \;" are simply required as a rule. They have no meaning otherwise. How do I delete all files in the /tmp directory older than a month? find /tmp -mtime +30 -exec rm -f {} \; Note: -exec is similar to -ok, but this time no confirmation is asked. The files are silently deleted. ----- chmod ----- chmod is another very important command used to change permissions for your files and directories. It is another typical example of the cryptic UNIX commands, a brother of 'find', so to say. I will try to simplify it as much as possible. Firstly, permissions are of three types: r: read permission w: write permission x: execute permission These permissions can be assigned to three 'domains': u: owner g: group o: others Create a file and check its default permissions: $ > test $ ls -l test -rw-rw-r-- 1 deepak deepak 0 Feb 19 13:11 test $ _ Now I will explain what the string at the beginning means. Let's make things easier by dividing it into groups: - rw- rw- r-- The first '-' indicates it is an ordinary file. There are three triplets after this. The first triplet shows the permissions for the owner. The second triplet shows the permissions for the group. The third triplet shows the permissions for others. So we find that by default, the owner and the group can read and write (rw-) to the file, but cannot execute it. Others can only read (r--) the file, but cannot write to it or execute it. Now I shall give you several examples of using chmod to change permissions: How do I make 'test' executable by everybody? chmod ugo+x test NOTE: Here we are adding ('+') execute-permission (x) to owner, group and others (ugo). How do I remove write-permission to group? chmod g-w test NOTE: Here we are removing ('-') write-permission (w) to the group (g). How do I remove all permissions to everybody except myself? chmod go-rwx test NOTE: Here we are removing ('-') read/write/execute permissions (rwx) to group (g) as well as others (o). How do I make 'test' read-only to everybody? chmod ugo+r-wx test NOTE: Here we are adding ('+') read-permission (r) to everybody (ugo), and removing ('-') write/execute permissions (wx) to everybody (ugo). Here is how to use chmod to secure your directories: r permission gives someone access to read the contents of a directory. w permission enables someone to add files to a directory or remove files from it. x permission enables someone to change to that directory. See the default permissions: $ mkdir newdir $ ls -ld newdir drwxrwxr-x 2 deepak deepak 1024 Feb 19 13:25 newdir $ _ Separating into groups: d rwx rwx r-x what do we deduce? d indicates this is a directory, the owner as well as his group can read/write/execute the directory, others can read the contents of the directory as well as go to that directory, but cannot add files to it or delete files from it. Check out these examples: How do I disable group-members from adding/removing files from newdir? chmod g-w newdir How do I disable group as well as others from changing to newdir? chmod go-x newdir Never give write-permission to a directory to 'others', because that will enable them to delete files from the directory, even if they are read-only! Play around with chmod until you think you are comfortable with it! ----------- grep Family ----------- grep is the last command I am going to discuss here. grep stands for 'get regular expression'. You can use grep if you want to look for files which contain a specific pattern. grep has been further extended by commands like egrep and fgrep. grep has this syntax: grep Grep displays the line(s) which contains the pattern in each of the files. As before, I will use examples to tell you about grep and its extensions. How do I find all occurrences of 'linux' in all files of the current directory? grep linux * How do I find all occurrences of Linus Torvalds in .txt files of the current directory as well as its subdirectories? grep -r "Linus Torvalds" *.txt NOTE: Use double-quotes (or single-quotes) to search for multi-word patterns like the one above. You can also say: find . -type f -exec grep doc {} \; How do I ask grep to ignore case (upper/lower) during pattern-matching? grep -i linux * How do I also print the matching line-numbers? grep -in linux * NOTE: Here we ignore case, as well as print numbers (n). I just want to know the files which contain the pattern. grep -il linux * NOTE: This only displays the names of the files which match. How can I search for both Linux and Torvalds? egrep 'Linux|Torvalds' * NOTE: '|' indicates an 'or' relationship. How do I list regular files with the pattern 'doc' in their names? find . -type f | grep doc [OR] Can I use wild-cards? Sure you can, but they don't mean the same as they do in the command prompt. Look up the man page for grep for further information, under the section 'Regular Expressions'. It is usually unnecessary to master them all, but sometimes they can make a task a whole lot simpler! ---------------------------------------- A CRASH-COURSE ON REDIRECTION AND PIPING ======================================== Redirection and piping are two powerful concepts in UNIX. They are so useful that even DOS has borrowed them. I am going to give you a fast-track introduction to redirection and piping, so that you can make use of them to simplify your tasks. It is not necessary for you to know this, but if you do, then it is not going to be a waste. Since this topic is also somewhat advanced, you can read this at leisure. Commands take an input, process it, and give an output. The input can come from a file or from an input 'stream', usually the keyboard. They output their results onto an output 'stream', usually the monitor. They may also generate error messages, which are sent to an error 'stream', again the monitor in most cases. Thus we have three 'streams': * The input stream, stdin * The output stream, stdout * The error stream, stderr ----------------- INPUT REDIRECTION ----------------- Redirection comes into picture when we manipulate the input and/or output streams from their default devices. Let me make this clear. Keyboard is the default input device, right? And what does the keyboard do? Give some characters to the command. How about taking these characters from a file? This simple concept is called input direction. See this example: If you don't specify any files, wc command takes its input from stdin, the standard input device. Hence, if you type wc and press ENTER, wc waits for you type something and then press CTRL-d to terminate your input. As soon as you have signalled termination, it will output statistics on your text. See this sample session: $ wc This is some sample text over multiple lines. ^d 3 8 46 $ _ What happened here is that the command took its input from stdin, which is the keyboard by default. Now, let us override this default by specifying a file as the input device. We will type the same text onto test and then run the command as below. $ wc < test 3 8 46 $ _ Great! This is the power of UNIX. The command wc doesn't know where its input came from. The shell saw the left-chevron (<) and understood that there was an input-direction. Hence it opened the file test and directed its characters to wc. In plain words, the file became the stdin, instead of the keyboard. That is, we _redirected_ the input stream. To see for yourself the truth in my statement 'The command doesn't know where its input came from', observe the sequence below: $ wc test 3 8 46 test $ wc < test 3 8 46 $ _ ------------------ OUTPUT REDIRECTION ------------------ Output redirection is also very similar to input redirection. Instead of outputting to the terminal which is the default output device, we ask the shell to redirect it to a file. The command itself doesn't know where its output is headed. An example can make things clear. Consider this session: $ sort data_file ... sorted output is seen ... $ _ Now let us redirect this to a file: $ sort data_file > sorted_file $ _ Amazing! Again, the shell saw the right-chevron and saw that there was an output-redirection. Hence instead of outputting to the default output device (the terminal), it sent the output to the file sorted_file. There is no output at the terminal at all! ----------------- ERROR REDIRECTION ----------------- Let's say you are trying to open a non-existent file. $ cat nofile cat: nofile: No such file or directory $ _ Now let's try redirecting this to a file err_file $ cat nofile > err_file cat: nofile: No such file or directory $ cat err_file $ _ What happened? err_file was created by the shell, but nofile had nothing to send to it. And why not? Because nofile didn't exist. But cat did output something on the screen! Yes, that was the error-message directed to the default stderr device, which is also the terminal. Now try this: $ cat nofile 2> err_file $ cat err_file cat: nofile: No such file or directory $ _ It worked! 2 is called the 'file descriptor' for stderr. The file descriptors for stdin and stdout are 0 and 1 respectively. ------ PIPING ------ Piping is the concept of redirecting the output of a command as the input to another command. This is another very useful feature. A pipe is formed by the '|' symbol as follows: cmd1 | cmd2 The output of cmd1 will serve as the input to cmd2. The pipeline can consist of many stages: cmd1 | cmd2 | ... | cmdn Technically, in a pipeline, the standard-output of one command becomes the standard-input of the next command. Here are some examples to make this concept clear: How do I prevent the output of a command from scrolling? grep "some common pattern" * | less NOTE: less is the paginator which waits for keyboard-input once a screen is full. The shell sends the output of grep to the less command, which outputs data one-screen at a time. How do I list all the packages installed in my system, sorted? rpm -qa | sort NOTE: Here the output of the rpm command is fed to the sort command. If we want this output to be put in a file, say, sorted_pkglist, then we can say: rpm -qa | sort > sorted_pkglist See the power of redirection and piping? How do I display a count of the number of files and directories in the current directory? ls | wc -l NOTE: The shell sends the output of ls to wc. wc counts the number of lines (due to the -l option), and tells you the number of files/directories in the current directory. How do I find the 5 largest files in the current directory? ls -S | head -5 NOTE: ls -S sorts files by size. head takes the topmost five lines from this output, and shows you the 5 top hoggers. An application has frozen. How do I find its PID to kill it? ps -A | grep "crashed_app" How do I list only the directories in the current directory? ls -l | grep "^d" NOTE: ^d means d at the beginning of the line. Alternatively: ls -F | grep "/$" (/$ means list entries with a / at the end.) If output scrolls too fast, pipe it to more: ls -F | grep "/$" | more We have come to the end of this document introducing you to the most basic commands at the Linux console, and how to make the most out of it. I hope I have done a fair job of it, and you have an idea of what you can achieve with Linux. After practising these commands, it would be a good idea to buy some book on Linux and further polish your Linux skills. As you learn more and more, you will discover that you are really enjoying working with Linux. You will realise that the joy derived from this is something that DOS or Windows can never provide.