The command line is a way to interact with the computer using specific words called “commands”. When using the command line, we refer to folders as directories. Files and directories on your computer are organized into a filesystem.
A filesystem organizes a computer’s files and directories into a tree structure:
To access the command line, we use the terminal.
To display the name of the directory you are currently in, use the pwd (print working directory) command.
Use the ls command to display the contents of the directory you are currently in on Linux and Mac. When you type ls, the command line looks at the folder you are in and then “lists” the files and folders inside it.
ls command has several more options. Here are three standard options:
Use ls -a to list all file contents, including hidden files and directories. Files started with a dot are hidden and don’t appear when using ls alone.
The -l option lists files and directories as a table. There are four rows and seven columns separated by spaces. Here’s what each column means:
ls -alt lists all contents, including hidden files and directories, in long format, ordered by the date and time they were last modified.
To navigate to another directory, you can use cd command.
cd stands for “change directory”. The cd command takes a directory name as an argument and switches into that directory.
To create a new directory, you can use the mkdir command.
The mkdir command stands for “make directory”. It takes in a directory name as an argument and creates a new one in the current working directory.
To create a new file, use the touch command.
To rename a directory or file, use the mv command.
mv folder/old-file.txt folder/new-file.txt.
The cp command copies files or directories. To copy a file into a directory, use cp with the source file as the first argument and the destination directory as the second argument.
$ cp a.txt b.txt
To move a file into a directory, use mv with the source file as the first argument and the destination directory as the second argument.
rm test.txt rm -r content-folder
The common redirection commands are:
>redirects the standard output of a command to a file, overwriting previous content.
>>redirects the standard output of a command to a file, appending new content to old content.
<redirects standard input to a command.
|redirects the standard output of a command to another command.
cat displays the contents of one or more files to the terminal.
$ cat hello.txt
grep searches for a text pattern and outputs it.
grep 'anna' names.txt
sort sorts lines of text alphabetically.
uniq stands for “unique” and filters out adjacent, duplicate lines in a file.
sed searches for a text pattern, modifies it, and outputs it. It is similar to “find and replace”.
$ sed 's/snow/rain/' forests.txt
In the expression ’s/snow/rain/':
Bash (or shell) scripting can automate repetitive tasks. Any command that can be run in the terminal can be run in a bash script. The beginning of your script file should start with #!/bin/bash. For example, in script.sh file, run this:
Variables are assigned using an equals sign with no space. For example:
To access the value of a variable, we use a dollar sign ($)
You can use conditionals to control the set of commands. Conditionals use if, then, else, fi syntax.
first_greeting= "Nice to meet you!" later_greeting= "How are you?" greeting_occasion=1 if [ $greeting_occasion -lt 1 ] then echo $first_greeting else echo $later_greeting fi
In the example above, if the variable is less than 1, use first_greeting. Otherwise, we use later_greeting.
Bash scripts use a unique set of comparison operators:
When comparing strings, we should put the variable into quotes (") to prevent errors if the variable is null or contains spaces. The common operators for comparing strings are:
if [ "$hello" == "$name"]
You can use three types of loops: for, while, and until.
for name in $hello do echo $name done
#!/bin/bash first_greeting= "Nice to meet you!" later_greeting= "How are you?" greeting_occasion=0 while [ $greeting_occasion -lt 3 ] do if [ $greeting_occasion -lt 1 ] then echo $first_greeting else echo $later_greeting fi greeting_occasion=$((greeting_occasion + 1)) done
until [ $index -eq 5 ] do echo $index index=$((index + 1)) done
echo "What's your name?" read name echo "Your name is $name"
Aliases can be created in the .bashrc or .bash_profile using the alias keyword. It allows calling your scripts without the full filename.