Basic Shell Commands

July 31, 2013 ยท View on GitHub


1. Shell Basics:

CommandDefinition
.a single period refers to the current directory
..a double period refers to the directory immediately above the current directory
~refers to your home directory. Note: this command does NOT work on Windows machines (Mac and Linux are okay)
cd ./dirnamechanges the current directory to the directory dirname
ls -Ftells you what files and directories are in the current directory
pwdtells you what directory you are in (pwd stands for print working directory)
historylists previous commands you have entered. `history
man cmddisplays the manual page for a command.

2. Creating Things:

a) How to create new files and directories..

CommandDefinition
mkdir ./dirnamemakes a new directory called dirname below the current directory. Note: Windows users will need to use \ instead of / for the path separator
nano filenameif filename does not exist, nano creates it and opens the nano text editor. If the file exists, nano opens it. Note: (i) You can use a different text editor if you like. In gnome Linux, gedit works really well too. (ii) nano (or gedit) create text files. It doesn't matter what the file extension is (or if there is one)

b) How to delete files and directories...

Remember that deleting is forever. There is NO going back

CommandDefinition
rm ./filenamedeletes a file called filename from the current directory
rmdir ./dirnamedeletes the directory dirname from the current directory. Note: dirname must be empty for rmdir to run.

c) How to copy and rename files and directories...

CommandDefinition
mv tmp/filename .moves the file filename from the directory tmp to the current directory. Note: (i) the original filename in tmp is deleted. (ii) mv can also be used to rename files (e.g., mv filename newname
cp tmp/filename .copies the file filename from the directory tmp to the current directory. Note: (i) the original file is still there

3. Pipes and Filters

a) How to use wildcards to match filenames...

Wildcards are a shell feature that makes the command line much more powerful than any GUI file managers. Wildcards are particularly useful when you are looking for directories, files, or file content that can vary along a given dimension. These wildcards can be used with any command that accepts file names or text strings as arguments.

Table of commonly used wildcards

WildcardMatches
*zero or more characters
?exactly one character
[abcde]exactly one of the characters listed
[a-e]exactly one character in the given range
[!abcde]any character not listed
[!a-e]any character that is not in the given range
{software,carpentry}exactly one entire word from the options given

See the cheatsheet on regular expressions for more "wildcard" shortcuts.

b) How to redirect to a file and get input from a file ...

Redirection operators can be used to redirect the output from a program from the display screen to a file where it is saved (or many other places too, like your printer or to another program where it can be used as input).

CommandDescription
>write stdout to a new file; overwrites any file with that name (e.g., ls *.md > mardkownfiles.txt)
>>append stdout to a previously existing file; if the file does not exist, it is created (e.g., ls *.md >> markdownfiles.txt)
<assigns the information in a file to a variable, loop, etc (e.g., n < markdownfiles.md)

b.1) How to use the output of one command as the input to another with a pipe...

A special kind of redirection is called a pipe and is denoted by |.

CommandDescription
|Output from one command line program can be used as input to another one (e.g. ls *.md | head gives you the first 5 *.md files in your directory)
Example:
ls *.md | head | sed -i `s/markdown/software/g`

changes all the instances of the word markdown to software in the first 5 *.md files in your current directory.

4. How to repeat operations using a loop...

Loops assign a value in a list or counter to a variable that takes on a different value each time through the loop. There are 2 primary kinds of loops: for loops and while loops.

a) For loop

For loops loop through variables in a list

for varname in list
do
    command1 $varname
    command2 $varname
done

where,

  • for, in, do, and done are keywords
  • list contains a list of values separated by spaces. e.g. list can be replaced by 1 2 3 4 5 6 or by Bob Mary Sue Greg. list can also be a variable:
  • varname is assigned a value without using a $ and the value is retrieved using $varname

--

list[0]=Sam
list[1]=Lynne
list[2]=Dhavide
list[3]=Trevor
.
.
.
list[n]=Mark

which is referenced in the loop by:

for varname in ${list[@]}
do
    command1 $varname
    command2 $varname
done

Note: Bash is zero indexed, so counting always starts at 0, not 1.

b) While Loop

While loops loop through the commands until a condition is met. For example

COUNTER=0
while [ ${COUNTER} -lt 10 ]; do
    command 1
    command 2
    COUNTER=`expr ${COUNTER} + 1` 
done

continues the loop as long as the value in the variable COUNTER is less than 10 (incremented by 1 on each iteration of the loop).

  • while, do, and done are keywords

b.1) Commonly used conditional operators

OperatorDefinition
-eqis equal to
-neis not equal to
-gtgreater than
-gegreater than or equal to
-ltless than
-leless than or equal to

Use man bash or man test to learn about other operators you can use.

6. Finding Things

a) How to select lines matching patterns in text files...

To find information within files, you use a command called grep.

Example commandDescription
grep [options] day haiku.txtfinds every instance of the string day in the file haiku.txt and pipes it to standard output

a.1) Commonly used grep options

grep options
-Etells grep you will be using a regular expression. Enclose the regular expression in quotes. Note: the power of grep comes from using regular expressions. Please see the regular expressions sheet for examples
-imakes matching case-insensitive
-nlimits the number of lines that match to the first n matches
-vshows lines that do not match the pattern (inverts the match)
-woutputs instances where the pattern is a whole word

b) How to find files with certain properties...

To find file and directory names, you use a command called find

Example commandDescription
find . -type dfind recursively descends the directory tree for each path listed to match the expression given in the command line with file or directory names in the search path

b.1) Commonly used find options

find options
-type [df]d lists directories; f lists files
-maxdepth nfind automatically searches subdirectories. If you don't want that, specify the number of levels below the working directory you would like to search
-mindepth nstarts find's search n levels below the working directory