The UNIX/BASH Command Line

  1. The command line is offered by a program called your 'shell'.

  2. Your shell by default is called 'bash', the Bourne Again Shell.

  3. Bash could be replaced by another program if you wanted.

  4. Standard commands are ...

 
 

Command

Example

Works with
STDIN/OUT

Function

ls

ls /tmp

Both

List files (like dir)

ls -l

ls -l /tmp

Both

List files long format

cp

cp src dest

None

Copies files

mv

mv oldname newname

None

Moves or changes the name of a file.
Cannot move a directory across between filesystems.

rm

rm oldfile

None

Deletes a file

rm -rf

rm -rf /

None

Deletes an entire subtree.
Can wipe out your whole hard drive!!

df

df 

Out

Shows free disk space.
Shows all filesystems mounted

du

du /tmp

Out

Shows space used by a subtree
Requires permissions to read the subtree.
Can be slow on big subtrees.

cd

cd

None

Change current dir to home dir.

cd

cd /tmp

None

Change current dir to given dir.

chmod

chmod a+rwx filename

None

Change permissions.
Choices for the first letter are
   a -- all, everyone
   g -- group
   u -- yourself.  The owner of the file
   o --  other.  People not the owner and not in the group
Choices for the second letter are
   r -- read for a file, list for a directory
   w -- write for a file, change for a directory
   x -- execute for a file, access with a known filename for a directory

chmod -R

chmod -R go-rwx .

None

Change permissions for a whole subtree.

chown

chown randy /home/randy

None

Change owner.
You must be root to use this command.  Being the owner of the file is not enough!

chown -R

chown -R randy /home/randy

None

Changes a whole subtree.

less

less /tmp/README

In

Shows file to the screen.

grep 

grep BUGS /tmp/README

Both

Shows lines containing the pattern to the screen.

grep -v

grep -v BUGS /tmp/README

Both

Shows lines NOT containing the pattern to the screen.

cat

cat file1 file2

Both

Concatinates the files.

whoami

whoami

Out

Tells your current username.

su

su randy

None

Changes your current username.
requires a password for everyone except root.

time

time du /tmp

Out

Tells how long the command takes to run.

sort

sort /tmp/file-list

Both

Prints files in alphabetic order. (-r reverses order).

sort -n

sort -n /tmp/list-of-numbers

Both

Prints files in number order. (-r reverses order).

head 

head -20 /tmp/list

Both

Prints only the first twenty lines.

tail

tail -20 /tmp/list

Both

Prints the last twenty lines.

 
Commands can be combined.

To run commands consecutively, try seperating them with a semi-colon.
    ls; df; du

To run commands simultainiusly, seperate them with a '&'.
    rm -rf /tmp & rm -rf /junk

To run commands and not wait for them to finish, end the command line with a '&'.
    rm -rf /junk &

To put the output of one command into the input of another, seperate them with a pipe.
    ls | sort

To place the output of a command into a file, use a greater-than symbol.
    ls > file-list

To have a command read from a file, use a less-than symbol.
    sort < file-list

Questions

How do I make a folder named 'fred'?

How do I copy the file /etc/passwd into that folder?

The file /etc/passwd lists all users. Who is the first user alphabetically?

How would you see all files that start with an “a” in /usr/bin?

How would you count them?