Introduction to Command Line
1. Learning Commands
Most commands follow the same format: command, then flags/options, then arguments.
$ command [flags/options] [arguments]
Without Arguments or Options: Simply type the command and hit return
. For example:
$ ls
Because there is no argument, this command would print the names of the files and directories within our the current working directory.
With Arguments: Commands can accept arguments that modify their behavior. For example, the cd
command changes the directory, requiring a path as an argument:
$ ls ~/path/to/directory
By including the file path in the argument, we are specifying what directory we would like to see. This code will print the contents of "directory", even though it is not our current working directory.
With Flags : Flags modify the way a command runs. They start with -
followed by a letter, or --
followed by a word. For example:
$ ls -l
The -l
flag makes ls
display detailed information about the contents of the current working directory.
With Arguments and Flags : You can combine arguments and flags. Flags generally precede any arguments passed to a UNIX command. For example, to list all files, including hidden ones (-a flag
), in the Desktop directory:
$ ls -a Desktop
Combing Flags: We can combine multiple flags with a single - (dash), with no spaces between flags. For example:
$ ls -Fa
In this example, ls -F
prints each type of file, where directories are indicated with a /
before the name. In addition, ls -a
prints all files, including the hidden ones.
2.Basic Examples
Example #1.Move into the 'Desktop' directory.
$ cd Desktop
orcd /Users/name/Desktop
Example #2. Make a new directory. Note: avoid using spaces in file names; underscores are also not recommended.
$ mkdir -p ~/Desktop/practice/example
In this example, we are attempting to create a directory named "example". By adding the -p
flag, we are able to create a path of directories. Instead of only making one directory, "example", we are able to create a parent directory "practice" and the subdirectory "example" in one line of code. Otherwise, our could would look like this (which is also an acceptable way to make directories):
$ cd ~/Desktop
$ mkdir practice
$ cd practice
$ mkdir example
$
Example #3.Copy a file. In the first argument, put the original name of the file. In the second argument, put the name of the new duplicate file (and new file path if desired).
$ cp [original] [new]
Example #4 Copy a directory and all its contents.
$ cp -r [source] [destination]
Example #5. Rename or copy a file or directory to a new location. In the first argument, put the original file location and name. In the second argument, put the new location (and new name, if you want).
mv [old].txt ../[new].txt
Example #6. Remove a file permanently
$ rm [path]
Example #7. Remove a file or directory permanently. The -r
flag signals "recursive", resulting in permanently deleting the directory and its contents.
$ rm -rf [path]
Important Note: Deleted files are gone forever! There is no trash or recycle bin. The -i
flag initiates interactive mode, which forces confirmation before permanent deletion.
$ rm -i [path]
$ touch [filename]
: create an empty file
Useful for generating placeholder files required by some programs.
$ nano [filename]
: Create or edit a file. If the file does not exist, it will be created. Type your content, then save and exit.
Shortcut Keys in Nano:
Command | Description |
---|---|
Ctrl + O |
Save the file (into a current or new name). |
Ctrl + X |
Exit the editor. If unsaved, nano will prompt to save. |
Ctrl + K |
Cut (“kill”) a line of text. Repeated use cuts multiple lines. |
Ctrl + U |
Paste the cut text. |
- Wildcard Characters
-
*
: Matches zero or more characters. Example:*.txt
matches all.txt
files. ?
: Matches any single character. Example:?.txt
matchesa.txt
but notany.txt
.
$ cat
: print the contents of files.- Example:
cat file1.txt file2.txt
- Example:
$ more
: similar to cat, but displays one page at a time, use spacebar to continue$ less -N [file]
: displays content page by page with line numbers. Use q to exit.$ sort [file]
: sorts file contents alphanumerically.- Use -n for numerical sorting.
$ head [file]
: displays the first 10 lines.- Use -n [#] to specify the number of lines.
$ tail [file]
: displays the last 10 lines.- Use -n [#] to specify the number of lines.
3 Variables
A variable in the shell is a reference to actual data. You can create, assign, and delete variables without specifying a data type. Variable names can contain letters (a-z, A-Z), numbers (0-9), and underscores (_). However, a variable name must start with a letter or an underscore, not a number.
Declaring Variables: to create a variable, use the following syntax:
$ [variable_name]=value
Note: No spaces are allowed betwen the variable name, the equals sign, and the value. Use consistent and simple filenames to facilitate looping.
Accessing Values: to retrieve the value of a variable, prefix its name with a dollar sign $
, as seen below
$ echo $variable_name
4. Pipes and Filters
- Pipe Operator:
$ [command] | [command]
: Redirect the output of one command into another.- Example:
ls | wc
- Counts lines, words, and characters in the output of ls.
- Example:
$ command > [file]
: Redirect output into a file. Note: this will overwrite the file.$ command >> [file]
: Append output to a file$ wc [file]
: Count lines, words, and characters.Example: $ wc -l *.txt
Output the line count for every file.grep [pattern] [file]
: Search for a pattern in files.Use -v to exclude lines with the pattern, -i for case-insensitivity.
Examples:
ls | grep "D" | wc
: counts the number of words containing "D" in the ls output.$ ls -l > filename.list
: Redirect output to a file:$ cat filename.list | grep keyword > filefound.list
: Pipe output to another command$ cut -d , -f 2 [file].csv | sort | uniq
: Remove duplicates
5. For Loops
For Loops allow us to execute a series of commands repeatedly for each item in a list. For loops are often used in scripts. Below is a for loop syntax template. In this example, 'variable'
represents each item in the 'list'
during each iteration
for variable in list
do
[commands]
done
Variable Expansion: Use $variable
or ${variable}
to access the value of a variable. For example:
for filename in file1 file2 file3
do
echo $filename
head -n 2 $filename | tail -n 1 >> $filename.txt
done
- For each file ('file 1', 'file 2', 'file 3'), this loop prints the file name. Then, it extracts the second line of the file (by getting the first 2 lines and then taking the last line) and appends it to a new file,
$filename.txt
. - Avoid using spaces, quotes, or wildcard characters,
*, ?
, in filenames to simplify variable expansion.