User Tools

Site Tools


unix_102

This is an old revision of the document!


UNIX 102

Required Reading

Modifying Program Behavior

Many commands will run without any additional information from the user, but most options will also allow you to modify it's behavior by passing it new information. Obviously one method of passing information from the user to a program is for the program to print a question to the screen and wait for the user to respond. However, that's not a very efficient method of interaction if you're trying to automate your workflow. A better method would be for the user to give the program access to everything it needs to know up front so that the computer doesn't have to stop and ask questions. This also allows long programs or scripts to run overnight while the user goes home, which is very important for users who, for instance, want to process large amounts of data or run large numbers of simulations.

Options and Arguments

One method of passing extra information to a program is to add this information to the command line when you type your command into the shell. The command line is the entire string of characters that you type into the shell, starting with the first character and ending when you press return.

Command line options for a program can usually be found by either typing the command, then a space, and then typing

-h

or

--help

. Most programs will respond by giving you additional information about options and arguments that can be passed to the command on the command line. Another way is to type

man <command>

to see if the program comes with it's own documentation.

Most command line options in Unix are prefixed by a single or double dash (- or ). (Note: The difference between single and double dashes can be difficult to see on this web page. I'll let you know if something is single or double.)

Examples of command line arguments:

peek@catus:~/Documents/LinuxTutorial$ ls
Aware_-_Kontinuum.flac	log-messages.txt				README
Glaciers-SD.mp4		MendelMax_3_Full_Kit_Packing_Slip_-_Sheet1.csv

In the above example, I type the command

ls

without any arguments. The output is ls' default behavior. But I can modify what ls does:

peek@catus:~/Documents/LinuxTutorial$ ls -1
Aware_-_Kontinuum.flac
Glaciers-SD.mp4
log-messages.txt
MendelMax_3_Full_Kit_Packing_Slip_-_Sheet1.csv
README

This time I pass ls -1, telling it that I want it to print everything in only one column. This is especially handy for piping the output of ls to another command or script that will do something useful.

peek@catus:~/Documents/LinuxTutorial$ ls -al
total 35620
drwxrwx---  2 peek peek     4096 May 13 13:33 .
drwxr-x--- 47 peek peek     4096 May 13 12:03 ..
-rw-rw----  1 peek peek 30725004 May 12 15:32 Aware_-_Kontinuum.flac
-rw-rw----  1 peek peek  2358156 May 12 15:29 Glaciers-SD.mp4
-rw-r-----  1 peek peek  3359600 May 12 16:02 log-messages.txt
-rw-rw----  1 peek peek     3188 May 12 16:12 MendelMax_3_Full_Kit_Packing_Slip_-_Sheet1.csv
-rw-rw----  1 peek peek      579 May 12 15:41 README
peek@catus:~/Documents/LinuxTutorial$ 

The above shows an argument that will tell ls to show me a lot of extra information about the files in this directory. Specifically, column one shows me permissions, columns 3 and 4 show me user and group ownership, column 5 shows me file size in bytes, and columns 6-8 shows me the date and time that the file or directory was last modified.

Configuration Files

Another method of passing information along to a program is by creating or editing a configuration file. Configuration files are specific to the program that reads them, so specifics won't be given here. Just being aware of their existence can make your life easier.

Environment Variables

A third common method of passing information to a program is through environment variables. These are special variables that are given to the shell and the shell will remember them and pass their values along to any scripts or programs that the shell executes.

Some Useful Commands

Command Description
pwd
Returns the shell's current working directory.
$ pwd
/home/peek
cd
Used by itself, the cd command will return the user's current working directory to the top of their home area (usually /home/<username>). When used with a command line argument, the cd command will attempt to change the current working directory to the directory name passed to it as an argument.
$ pwd
/home/peek/usr/bin
$ cd
$ pwd
/home/peek
$ cd /tmp
$ pwd
/tmp
ls
Used by itself, the ls command will list the directory contents of the current working directory. The output format can be modified with command line arguments – and, optionally, the user may give a list of files or directories to list.
$ ls
Aware_-_Kontinuum.flac	log-messages.txt				README
Glaciers-SD.mp4		MendelMax_3_Full_Kit_Packing_Slip_-_Sheet1.csv
$ ls -al
total 35620
drwxrwx---  2 peek peek     4096 May 13 13:33 .
drwxr-x--- 47 peek peek     4096 May 13 12:03 ..
-rw-rw----  1 peek peek 30725004 May 12 15:32 Aware_-_Kontinuum.flac
-rw-rw----  1 peek peek  2358156 May 12 15:29 Glaciers-SD.mp4
-rw-r-----  1 peek peek  3359600 May 12 16:02 log-messages.txt
-rw-rw----  1 peek peek     3188 May 12 16:12 MendelMax_3_Full_Kit_Packing_Slip_-_Sheet1.csv
-rw-rw----  1 peek peek      579 May 12 15:41 README
$ ls /tmp
aws_root.log
dvdcss-DnlBhJ
dvdcss-q9Iio2
hsperfdata_root
ssh-8eL6NRDqQg
ssh-gUTLY6GtJG
systemd-private-6289425d307e43a98f4f52405e0d97ac-colord.service-IaXZv6
systemd-private-6289425d307e43a98f4f52405e0d97ac-tor@default.service-CIBSIM
$ ls /tmp /var/tmp
/tmp:
aws_root.log
dvdcss-DnlBhJ
dvdcss-q9Iio2
hsperfdata_root
ssh-8eL6NRDqQg
ssh-gUTLY6GtJG
systemd-private-6289425d307e43a98f4f52405e0d97ac-colord.service-IaXZv6
systemd-private-6289425d307e43a98f4f52405e0d97ac-tor@default.service-CIBSIM

/var/tmp:
systemd-private-6008fcbada2e41ed9bddfe6b012eb599-colord.service-c0jtLz
systemd-private-6008fcbada2e41ed9bddfe6b012eb599-tor@default.service-B7IV7c
systemd-private-6289425d307e43a98f4f52405e0d97ac-colord.service-iHELip
systemd-private-6289425d307e43a98f4f52405e0d97ac-tor@default.service-J0PeqU
file
Tells you what type of data is in a file.
$ ls -1
Aware_-_Kontinuum.flac
Glaciers-SD.mp4
log-messages.txt
MendelMax_3_Full_Kit_Packing_Slip_-_Sheet1.csv
README
$ file README
README: ASCII text
$ file --mime-type *
Aware_-_Kontinuum.flac:                         audio/x-flac
Glaciers-SD.mp4:                                video/mp4
log-messages.txt:                               text/plain
MendelMax_3_Full_Kit_Packing_Slip_-_Sheet1.csv: text/plain
README:                                         text/plain
less
Lets you view a file's contents. If the file's contents are longer than a single page on your terminal, then less will pause and wait for you to press the space bar before it continues with the next page. Less will also allow you to perform searches
$ less README
This is a standard README file.  It's a plain text file that tells you
something about the contents of the other files and directories located here.
Readme files aren't always available but it's nice when they are.

Attribution:

* Glaciers-SD.mp4
        Science World: How do you protect glaciers?
        Released by: Polyester Studio
        Date: April 28, 2016 (approx.)
  URL: https://vimeo.com/164133990

* Aware_-_Kontinuum.flac
  Kontinuum - Aware [NCS Release]
        Released by: NoCopyrightSounds
        Date: May 27, 2015
        URL: https://soundcloud.com/nocopyrightsounds/kontinuum-aware-ncs-release

~
~
~
~
README (END)
mkdir
rmdir
Create or remove a directory.
$ ls -ald ERASEME
ls: cannot access 'ERASEME': No such file or directory
$ mkdir ERASEME
$ ls -ald ERASEME
drwxrwx--- 2 peek peek 4096 May 16 14:04 ERASEME
$ rmdir ERASEME
$ ls -ald ERASEME
ls: cannot access 'ERASEME': No such file or directory
cp
Copy a file to a new filename, or to a new directory.
$ mkdir ERASME
$ ls -ald ERASEME/*
ls: cannot access 'ERASEME/*': No such file or directory
$ cp README ERASEME/
$ ls -ald ERASEME/*
-rw-rw---- 1 peek peek 579 May 16 14:15 ERASEME/README
$ cp README ERASEME/mullets-are-a-way-of-life
$ ls -ald ERASEME/*
-rw-rw---- 1 peek peek 579 May 16 14:16 ERASEME/mullets-are-a-way-of-life
-rw-rw---- 1 peek peek 579 May 16 14:15 ERASEME/README
rm
Remove a file (NOTE: THERE IS NO GOING BACK!)
$ ls -ald ERASEME/*
-rw-rw---- 1 peek peek 579 May 16 14:16 ERASEME/mullets-are-a-way-of-life
-rw-rw---- 1 peek peek 579 May 16 14:15 ERASEME/README
$ rm ERASEME/mullets-are-a-way-of-life
$ ls -ald ERASEME/*
-rw-rw---- 1 peek peek 579 May 16 14:15 ERASEME/README
mv
Move or rename a file.
$ ls -ald ERASEME/*
-rw-rw---- 1 peek peek 579 May 16 14:15 ERASEME/README
$ mv ERASEME/README ERASEME/duck-duck-goose
$ ls -ald ERASEME/*
-rw-rw---- 1 peek peek 579 May 16 14:15 ERASEME/duck-duck-goose
$ mkdir ERASEME/subdirectory
$ ls -ald ERASEME/*
-rw-rw---- 1 peek peek  579 May 16 14:15 ERASEME/duck-duck-goose
drwxrwx--- 2 peek peek 4096 May 16 14:24 ERASEME/subdirectory
$ mv ERASEME/duck-duck-goose ERASEME/subdirectory
$ ls -ald ERASEME/*
drwxrwx--- 2 peek peek 4096 May 16 14:25 ERASEME/subdirectory
$ ls -ald ERASEME/subdirectory/*
-rw-rw---- 1 peek peek 579 May 16 14:15 ERASEME/subdirectory/duck-duck-goose
find
Locates a file or directory somewhere inside either the current directory or a subdirectory. Find is a very powerful tool, and I won't cover it's use in depth here, but I introduce it so that you know what I'm talking about when I use it later. When run without any command line arguments, find will print out a recursive listing of all files and directories located in the current directory and below. That's good enough for now.
cat
Copies input to output.
wc
Counts characters, words, and lines.
head -<n> 
Prints the first <n> lines of input, then ignores the rest.
tail -<n> 
Prints the last <n> lines of input, then ignores the rest.

Playing Around

Now that you have a few commands to play with, let's play with them:

Type:
$ mkdir /tmp/playground
Type:
$ find /tmp/playground
/tmp/playground</code |
^ Type: | <code>$ cd /tmp/playground
Type:
pwd
/tmp/playground
Type:
$ mkdir dir1 dir2
Type:
$ cp /etc/passwd .
Type:
$ find
.
./passwd
./dir1
./dir2
Type:
mv passwd fun
Type:
$ find
.
./dir1
./dir2
./fun
Type:
mv fun dir1
Type:
$ find
.
./dir1
./dir1/fun
./dir2
Type:
mv dir1 dir2
Type:
$ find
.
./dir2
./dir2/dir1
./dir2/dir1/fun
Type:
cd
Type:
rmdir /tmp/playground
rmdir: failed to remove '/tmp/playground': Directory not empty
Type:
rm /tmp/playground/dir2/dir1/fun
Type:
find /tmp/playground
/tmp/playground
/tmp/playground/dir2
/tmp/playground/dir2/dir1
Type:
rmdir /tmp/playground/dir2/dir1
Type:
rmdir /tmp/playground/dir2
Type:
rmdir /tmp/playground
unix_102.1464103706.txt.gz · Last modified: 2016/05/24 15:28 by peek