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.
will return any file names with only a single letter after chapter.
There are three types of variables:
Store information about the setup of the operating system
Cannot be changed
TZ (Time Zone)
Determine the characteristics of your session
Can be changed by you or your sys admin
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
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
~$: echo $NAME
The backquote (`) operator can be used to store the output of a command in a variable:
~$: echo $TODAY
Displays today’s 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|
Sorts lines in a file alphabetically
Reverse-alpha sorts lines in a file
Counts number of lines, words and characters in a file.
Counts number of lines
Counts number of words
Counts number of chars
Formats a file for printing, w/ date and page number.
Formats for double-spaced printing.
“Translate” or “transform”
Used to replace characters in a text file.
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.|
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:
Sometimes you need more than one command to do the job. Use the semicolon:
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 /
ls –laR / >/files.txt &
The latter executes ls in the background and immediately gives you a new prompt to continue working.
Use the command:
to view background job IDs.
Add a ” % ” to kill a background job:
Move a backgrounded job to the foreground:
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.
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:
You can also bracket and expand math expressions, using slightly different notation:
echo “2 plus 2 equals $[2+2].”
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:
This should list every filename on your system that contains the string “hello.” If this doesn’t work, you can use the commands:
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.
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:
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.
This command will show the full path to the bash executable.
Get a manual page for a command:
What? There’s no output? You need to build the man page database:
Search by keyword:
man –k keyword
man –k columns
(apropos gives the exact same output as man -k.)
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:
For instance WHOAMI(1).
|Manual Page Sections|
|1||Commands available to all users|
|4||Special device files|
|8||Commands available only to root|
|n||New: not yet categorized|
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.