I hear Linux is hard, and is for geeks… I am not a geek, so is it still for me?
Of course you're not...
Now that we have cleared that up let's get into how we're going to "talk" to Linux. Linux talks to us people using a shell. It's that green or text-based screen that we i users are familiar seeing. I, of course like the shell sometimes much more then I do some GUI application, but have been known to use a GUI and smile at times. The shell is just a layer that is between you the user or administrator and the kernel.
What is a shell?
A Unix shell is a command language interpreter, the primary purpose of which is to translate command lines typed at a terminal into system actions. The shell itself is a program through which other programs are invoked. Although there are several different Unix shells, among them the C shell (csh), the Bourne shell and the Korn shell, the one most frequently used on Blake within Berkeley UNIX is the C shell.
Getting started in the shell
Since Linux has very few built-in commands, you (the user) will be needing a shell to get information from the system and direct information to the system. When you telnet into any Linux box you will be given a shell once you log in and that shell is determined with your user profile.
Getting started using Linux commands
The sidebar list gives you a decent set of commands to start with. You can build on that with more commands and take commands and join them together. Using a pipe you can have commands take their output and push it to another command. It's one of the most powerful parts of the Linux tools.
who | sort > file
This would take the output of who then pipe the data to the sort command and then place that in a file called "file."
cat file > newfile | grep 'searchstring' newfile
This string of commands would do the following: Cat command would output the contents of a the file to newfile then take the newfile and search it using grep for the search string you want of the file called newfile. This will replay on the screen unless you then pipe the output to a file like this:
cat file > newfile | grep 'searchstring' newfile > grepinfo
Because this is important, I make sure we cover it here, thoroughly. Redirection is simple, and here is the list again in a consolidated version for easy reference.
>: Redirects output
<: Redirect input
>>: Redirects output and appends it
|: Pipe command and pipes data to other commands
>&: Redirects both standard output and standard error
2>: Redirects only standard error
/dev/null: This is a special place to send things to be lost forever. It's usually a place for getting rid of output.
Types of redirection: Linux for the most part uses three standard types of redirection:
stdin: Standard input, keyboard
stdout: Standard output, screen
stderr: Standard error, screen
You can of course redirect any of these to pretty much any place. Examples:
Standard output is redirected through use of '>'
dir mydir > filelist.txt
Standard error is redirected through use of '2>' construct
find / -name core -print 2> /dev/null
Standard input is redirected through use of '<'
isql < build_mydb.sql
It is pretty simple stuff once you get use to the way commands are clumped in order to do very powerful things. I sometimes find myself wishing I could type "WRKACTJOB" and then use the '|' to get the data out that I want – now, that would be powerful. This would be cool, but you can't actually do it:
WRKACTJOB > actjobs.txt | sort | grep QSECOFR actjobs.txt > found.txt
You may be wondering why is understanding this is important. Well, if you are a Linux administrator you're going to need these tools and commands at your finger tips. I know you can pretty much Google anything at this point, but it's always handy to have a nice firm base when you log on to start working on problems and delivering solutions.
Slicing and dicing the data
I am going to once again lay out some more very interesting concepts and commands that will help you with your Linux Administration work.
Refer to the following commands: HEAD, TAIL, CUT, SORT, UNIQ, GREP, and WC. Now we'll dig a little deeper into these commands to make sure we get the most out of them.
Heads and tails: Because these commands are like peanut butter and jelly we are going to cover them together.
The head command can be used to display the start of a file. By default the top 10 lines of the file will be displayed
The tail command is the reverse of the head command will by default print the bottom 10 lines of the file
Both the head and tail commands support the option of indicating how many lines to display
tail -20 /var/log/messages
The tail command can also be used to display updates to files (such as log files) as they are made. This is accomplished with the –f option
tail –f /var/log/messages
Cut: Cut is an interesting command, although it is one that I don't use often. The cut command is one that some people love, and some people can live without (I'm one of the latter). It's one of those commands you have to pipe information to so it sometimes gets lost in the mix; but if used properly, it can be a nice little command to have in your bag.
|ps||Shows my process|
|ps –ef||Shows lots of processes (i.e., all processes on the system)|
|ps –ef | cut –f2||Shows all of the processes and cuts the second field from standard input|
|ps –ef | cut –f2 –d " "||Shows all of the processes and cuts the second field from stands input using|
|ps –ef | tr –s " " | cut –f2 –d " "||Shows all of the processes and cuts the second field (the process id) from standard input having first squeezed all repeating spaces into a single space.|
UNIQ and SORT: The commands UNIQ and SORT and can be used together to find unique entries from standard input:
UNIQ: Analyzes standard input, and removes all duplicate/repeating entries
SORT: Sorts standard input and provides a sorted list to standard output
|ps –ef | tr –s " " | cut –f1 –d " "||Shows the user-name for each process currently running on the system|
|ps –ef | tr –s " " | cut –f1 –d " " | uniq –c||Shows a count of the number of processes being run by a user. NOTE: That many users are listed more than once!|
|ps –ef | tr –s " " | cut –f1 –d " " | sort | uniq||Show the names of users that are running processes on the system. NOTE: There is only one entry for each user.|
|ps –ef | tr –s " " | cut –f1 –d " " | sort | uniq –c||Shows a count of the number of processes being run by each user on the system. NOTE: There is only one entry for each user.|
WC: The wc (word-count) program can be used to count the number of characters, words, and lines from standard input:
|wc /etc/passwd||Output the number of characters, words, and lines from the /etc/passwd file|
|wc –c /etc/passwd||Output just the number of characters from the /etc/passwd file|
|wc -1 /etc/passwd||Output just the number of lines from the /etc/passwd file|
|ps –ef | wc -1||Output the number of lines resulting from the ps –ef command. This will be the number of processes running on the system.|
Alias: Alias is command that is used mostly in your .profile. It's a power-user type of command -- a command that says "I know what the heck I am doing." Typically, alias is used to abbreviate other longer commands. For example I love the System i and wanted to tinker so I type the command TOP, which shows processes and CPU utilization and made an alias for it in my .profile that was quicker for me to type, WRKACTJOB. I know it's more letters but after typing that for 18 years I have to hunt and peck for TOP plus it's just geeky. How I did this:
Multitasking: So you may have heard that Linux is a "real" multitasking operating system -- it truly is. Here are some things you might want to know about starting and stopping jobs that are on your multitasking operating system:
|&||Starts commands or jobs in the background|
|jobs||List all the jobs you have started|
|kill||Kills or stop jobs on the system|
|CTRL+C||Can stop a job running interactively on your terminal window|
|CTRL+Z||Can suspend a job on a system|
|bg||Can place that job in the background|
|fg||Can bring that job to the foreground|
|ps||List the processes on the system|
|ps –ef||List of processes you are running on the system|
Process control: Each job or process on a system has a process identification number (PID). Each of these processes has a parent process called a PPID. The PID is important as this is the way many tasks and other operating-system-level items keep track of jobs running on the system.
Your "main MAN": From time to time you will hear a very rude administrator or support desk person tell you to RTFM, and we all know that is not a very nice thing to say unless you just handed them a manual with all the world answers in it, then of course it's time to RTFM.
The "MAN" command is a new users best friend. MAN is short for manual, hence my digression in the previous paragraph. You can MAN any command in the operating system as long as the MAN is loaded. Here is how that works.
These two commands will give you all kinds of ways to use the commands you have questions about. There is also an INFO command, but I would start with man.
File Management: ls -li smb.conf
iNODE Authority Links Owner Group Size Modified Date File Name
ABOUT THE AUTHOR: David Vasta is the Lotus Notes Administration Team Lead over North America at Atlas Copco. He has 17 years of data center and iSeries experience working in companies such as IBM, REAL and Cingular. He writes a regular blog at System i blogger.
This was first published in October 2008