Building Shell Commands

Building Shell Commands

Some Common Linux Commands
clear Clear your terminal’s screen clear
reset Reset your terminal to default settings reset
finger Display a user’s information finger; finger user
who List all currently logged-in users who
w List current users and their tasks w
whoami Show your own current logged-in name whoami
id Lists your UID and GIDs id
date Display today’s date date
cal Display this month’s calendar cal
exit Leave a sub-shell exit
logout End your user session logout
head View the first ten lines of a file; or specify how many lines head /var/log/messages
head -3 /var/log/messages
tail

View the last ten lines of a file; or specify how many lines; optionally specify the line to start on.

tail also can be used to continually track a file with the -f (follow) option

tail /var/log/messages
tail -3 /var/log/messages
tail +10 /var/log/messages
tail -f /var/log/messages
touch Refresh the last-modified date of a file to now;
create the file if it doesn’t exist
touch ./myfile
chown

Change the Owner of a file
Change the Owner of a directory, Recursively

Note: You can change both owner and group with one command.

chown user filename
chown -R user directory
chown user:group filename
chown user.group filename

chgrp

Change the Group ownership of a file

Also Note: Regular users can only change a file’s group to a group of which they are already a member.

chgrp group filename

 

First, we need wild cards

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”.

 

A one-letter wild card

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.

 

BASH Shell Metacharacters
$ Marks a shell variable echo $myvar
~ The current user’s Home directory cd ~/bin
& Execute a command in the background gedit &
; In-line return, used for multiple commands on one line who; w
> Output redirection ls > listing.txt
>> Output append who; date >> users.txt
< Input redirection command < file.txt
<< Input append
| “Pipes” command output to the next command’s input ls /bin | grep gzip
\ “Escapes” the special meaning of a special character grep \$ my_accounts.txt
` Command substitution myvar=`ls`
( ) { } Command grouping echo (date; who)

 

Variables: symbolic names used to represent values stored in memory

There are three types of variables:

Configuration variables

Environment variables

Shell variables

 

BASH Quoting
” “ “Weak quotes” – These exist ONLY TO PRESERVE SPACES echo “Hello, $USER”
gets you:
Hello, root
‘ ‘

‘Single-quotes’ are ‘strong quotes’ or ‘literal quotes.’

  • Code inside them won’t run.
  • Variables inside them won’t be expanded (interpreted).
echo ‘Hello, $USER’
gets you:
Hello, $USER

 

Configuration variables

Store information about the setup of the operating system

Cannot be changed

HOME

LOGNAME

TZ (Time Zone)

 

Environment variables

Determine the characteristics of your session

Can be changed by you or your sys admin

PATH

PS1 (Shell prompt)

PWD (Current Working Directory)

 

Shell Variables

Created at the command line or in a shell script

Useful in shell scripts to store temporary information

dog

memo

my_var

today

 

Assigning values to variables

~$: 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.”

 

The pipe ( | )

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
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

 

The append character ( >> )

If you want to append your command output to a file, rather than “clobbering” it, use the append character:

(users)>>/var/users.log

 

Sequential commands

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

 

A word about arguments

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’

 

Expanding arithmetic

You can also bracket and expand math expressions, using slightly different notation:

echo “2 plus 2 equals $[2+2].”

 

Finding Files

locate

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.

 

whereis

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

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.

 

Getting On-Line Help

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
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

 

Resources

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.