This lecture focuses on defining new bash commands and scripting practices. Many useful bash commands are defined that we commonly used throughout the rest of the course.
curl - capture url
pwd - display the name of the present working directory.
cd - change directory
ls - view contents
mkdir - make directory
man - manual command, display information regarding other commands
ssh - secure shell, connecting to remote servers
less - view text information regarding a file
cp - copy a file or directory
mv - move a file or directory
rm - remove a file or directory
grep - pattern-matching, prints the lines that match a specified pattern within each file
| - piping, used to “pipe” the output of one command or program as the input for another command or program
wc - word count of a specified file (FLAGS -> -l = line count)
basename - prints the base name (name with no leading or trailing path) of a specified file or directory
dirname - print the directory path to specified file or directory
xargs - build and execute command lines from standard input
cut - remove sections from each line of a file (FLAGS -> -d = delimiter, -f = field)
sort - sort lines of text files
uniq - only return unique elements (no repeats)
for i in {start..end..step}
do
echo $i
done
Variables: start - starting index of for loop, end - ending index of for loop, step - amount
by which to iterate the current index at each step.
To access the values of variables in our commands, we use the dollar sign character, $,
followed by the name of our variable.
Ex) for f in find random
do
echo $f
done
The above command echos the name of each file in the random directory to standard
output.
You will be assigned a local machine at the beginning of the year, and must enter your
information according to the below format and enter your password when prompted.
Format: YOUR_USERNAME@stlawu.edu@cslinuxlab-YOUR_NUMBER.stlawu.local
Commands such as the for loop illustrated above can be executed in one line when
utilizing the semicolon character. For example, the following for loops are equivalent:
for f in random do echo $f done
for f in random; do echo $f; done
You can view the contents of your pwd with the ls command, and you can copy, move or remove any file or directory in the pwd with the commands cp, mv, and rm respectfully. You can also back out a level from your current directory with the command cd .., as well as move to other subdirectories via cd DIR_NAME.
for i in random/* do echo $i done
The above for loop includes an asterisk following the random/ directory. This technique is referred to as file globbing, in which the use of a wildcard character such as an asterisk gets expanded upon execution of the command to relevant files and directories.
The man command is useful for finding information regarding any of the linux
commands. This command provides you with the name of the command, formatting, uses, and much more helpful information.
The pattern matching command grep can be utilized in combination with escape characters to match file names or contents based on the rules specified by the escape characters. For example, including a carat (^) at the beginning of the string following grep will ensure that we pattern match files whose names begin with the specified string. Similarly, we can use the dollar-sign ($) at the end of the string following grep to ensure that we only pattern match with files whose names end with the specified string. Using these in combination enforces both of the rules, which is to say that the result of the pattern-matching will only contain files that both begin and end with the specified string. Finally, we can utilize two escape characters (\) to specify when we’re using other special characters such as wildcards and dots as information within the string rather than separate commands.
Ex) find random | grep ^\.ac$; output -> files that begin and end with the string “.ac”.
Using the same method by which we accessed our values of previously defined
variables, we can also run a command and provide it’s output as text in the command line arguments for another command.
Ex) for i in {0..#(find random | grep ace | wc -l)} do echo $i done
xargs The xargs command builds and executes command lines from standard input.
Ex) xargs basename | grep ^9; Output -> using standard input as params to xargs, print the basename of all files of the xargs specified name and pattern-match the result for names beginning with the character 9.
Conditionals work by ending the statement with if in reverse (fi).
Ex) if [ -f $f ]; then do echo $f; fi;
You can view the exit status of a program’s execution using echo $?
You can redirect output from a program into a file of your choosing using the greater than symbol.
Ex) find random > randomfiles.txt