Many functions in Unix deal with text strings like file names. You’ll need at least two file name wild cards (or shell wild cards) to make life easier.
The first wild card is “ * “, which means “match any character or string of characters.” It’s handy, for instance, for finding a file:
ls -l chap*
will get you a listing of all the files (in your current directory) with names beginning with “chap”.
It will also attempt to return listings from any subdirectory with a name beginning with “chap”.
The second wild card character to know is “?”. It matches only a single letter.
ls chapter?
will return any file names with only a single letter after chapter.
Variables: symbolic names used to represent values stored in memory
There are three types of variables:
Configuration variables
Environment variables
Shell variables
Store information about the setup of the operating system
Cannot be changed
HOME
LOGNAME
TZ (Time Zone)
Determine the characteristics of your session
Can be changed by you or your sys admin
PATH
PS1 (Shell prompt)
PWD (Current Working Directory)
Created at the command line or in a shell script
Useful in shell scripts to store temporary information
dog
memo
my_var
today
~$: NAME=Becky
The string Becky is assigned to the shell variable NAME.
To display the value of NAME, it must be preceded with a dollar sign ($), for example
~$: echo NAME
displays NAME.
~$: echo $NAME
Displays Becky.
Assigning commands to variables
The backquote (`) operator can be used to store the output of a command in a variable:
~$: TODAY=`date`
~$: echo $TODAY
Displays today’s date.
~$: TODAY=date
~$: echo $TODAY
Displays the word “date.”
Recall the pipe special character, and its use in feeding the stdout of one command into the stdin of another command:
ls –la /bin/ | less
Any command that can accept Standard Input and produce Standard Output is called a filter command.
Common filter commands | |
Command | Description |
sort sort -r |
Sorts lines in a file alphabetically
Reverse-alpha sorts lines in a file |
wc wc -l wc -w wc -c |
Counts number of lines, words and characters in a file.
Counts number of lines Counts number of words Counts number of chars |
pr pr -d |
Formats a file for printing, w/ date and page number.
Formats for double-spaced printing. |
tr |
“Translate” or “transform”
Used to replace characters in a text file. |
grep |
Displays those lines in a given file that match a string of your choice.
|
nl | Prefixes a line number to the lines in a file. |
awk | Manipulates text using pattern/action expressions. |
sed | Manipulates text using search/replace expressions. |
The redirection character ( > )
Also recall the redirection character, which sends the output of a command to a text file:
(users) > /var/users.log
If you want to append your command output to a file, rather than “clobbering” it, use the append character:
(users)>>/var/users.log
Sometimes you need more than one command to do the job. Use the semicolon:
(date;users)>>/var/users.log
Running a command in the background ( & )
To execute a command in the background, follow the command with an &. When you run a command that you know is going to take a while to complete, running it in the background lets you continue working in the mean time. On many Linux distros, backgrounding a command gives it a nice value of 4 (see the nice section). Compare:
ls –laR /
with
ls –laR / >/files.txt &
The latter executes ls in the background and immediately gives you a new prompt to continue working.
Use the command:
jobs
to view background job IDs.
Add a ” % ” to kill a background job:
kill %1
Move a backgrounded job to the foreground:
fg %1
Move a foreground job to the background:
Ctrl-z #to assign a background ID to the job
bg %1 #to background that job; Use the ID supplied by the previous command
In Unix, an argument isn’t something you get into with your sibling.
An argument is a parameter, like specifying which directory to list when you give an ls command:
ls –la /home/glenn
In this case, /home/glenn is the argument.
Expanding command output as an argument
Consider this chain of actions.
I want to “touch” every file in my home directory, so I could issue the commands:
touch /home/glenn/* ; touch home/glenn/ *.*; touch /home/glenn/.*
This will refresh the last-modified date of every file in my home. (Now, why would I want to do this?)
But, I decide I don’t want to touch all my files; only the .txt files. So instead I command:
find /home/glenn | grep \.txt
to find my .txt files.
Now I have the list of files I want, I can use that list (which is really the output of my last command) as the input for the touch command:
touch $(find /home/glenn | grep \.txt)
In this case, I’m indicating that I want to run the find command first by bracketing it with:
$( my_command )
But I could also use the format:
$‘my_command’
You can also bracket and expand math expressions, using slightly different notation:
echo “2 plus 2 equals $[2+2].”
Finding Files
To use the locate command, you will need to have an slocate database set up on your system. On many systems it is updated periodically by the cron daemon. Try the locate command to see if it will work on your system:
locate hello
This should list every filename on your system that contains the string “hello.” If this doesn’t work, you can use the commands:
slocate -u
or
updatedb
to build the slocate database. This may take a few minutes. On most systems with slocate installed, slocate is run periodically by the cron facility.
find
The find command lets you search for files by criteria, for instance by user, size or date. The basic syntax is:
find directory_to_start_from [options_if_any] string_to_find
By default, the string_to_find is the file name, or some part of the file name. But you can search by criteria too, such as the owner of the file:
find / –user studenth
In a similar way, the -group option restricts searches to files owned by a group. Or search by the size of the file:
find / -size 100k
Another truly helpful option, if you have remote mounts on the workstation, is -mount:
find / -mount file_name
This restricts find to local disks only.
Note that a bare “find” command lists every file on the workstation.
This command only works for executables, and it will find their man pages and (particularly useful) their configuration files as well. For instance:
whereis bash
will return the full path to bash, bashrc and the man page bash.1.gz. This is handy for finding the true paths to programs that aren’t in your PATH, for instance.
which finds an executable file. Unlike whereis, it does not find man pages as well.
which bash
This command will show the full path to the bash executable.
Get a manual page for a command:
man command
man tracert
What? There’s no output? You need to build the man page database:
makewhatis
Search by keyword:
man –k keyword
man –k columns
apropos columns
(apropos gives the exact same output as man -k.)
Info pages:
info command
info coreutils utility_name
where utility_name is a utility like ls.
And finally, yes, there is a help command, not available for all commands but sometimes useful:
help echo
About those numbers following the command in man page output:
For instance WHOAMI(1).
Manual Page Sections | ||
Section | ||
1 | Commands available to all users | |
2 | System calls | |
3 | Library routines | |
4 | Special device files | |
5 | File formats | |
6 | Games | |
7 | Macro packages | |
8 | Commands available only to root | |
9 | Kernel routines | |
n | New: not yet categorized |
LinuxCommand.org: http://linuxcommand.org/
A good intro to commands and scripting.
SS64: Unix, Oracle, Windows and Mac commands: http://ss64.com/
A great site devoted solely to the ins and outs of the command line in various environments.