...
The grep
command is used to search for a specific string within a plain text or CSV file. grep
has many options including case-insensitive search, inverse search, and regular expression search. See /wiki/spaces/utldigitalstewardship/pages/43057645 for a full guide to using grep
.
Displaying file contents (cat
, less
...
, and head
/tail
commands)
There are several commands that can be used to display the contents of plain text or CSV files right in the terminal window. Each displays information in slightly different ways.
To output every line from a file, run the cat
(concatenate) command, followed by the path to a text or CSV file:
If you want to preview the contents of a file without necessarily outputting every line to the terminal, run the less
command, followed by the path to a file. This will fill your terminal window with the contents of the file, stopping once the window is full. You can then scroll down (and back up) through the text file using the down and up arrow keys. Hit the q key to exit the less
output screen.
To quickly output only the first lines in a file, run the head
command, followed by the path to a file. By default, head
will output the first 10 lines of the file, but you can also specify the number of lines to output using the -n
option:
The tail
command works just like head
, but it displays the last lines of the file. Just like head
, you can specify the number of lines to output using -n
:
Terminal multiplexer (tmux
command)
Any time you have to run a process that will take a very long time, it's a good idea to use the tmux
(terminal multiplexer) command to avoid accidentally interrupting the process. Using tmux
you can begin a process, "detach" from the session to close the terminal window without killing the process, and then "reattach" to the session later to review the results of your process. tmux
also allows you to run several commands simultaneously, one per tmux
session.
To launch a new tmux
session, simply enter tmux
. Whenever the active terminal is a tmux
session, a green stripe will be visible along the bottom of the terminal window:
The session number is the value shown on the left side of the stripe. The first session launched will be called 0
, and each subsequent session will be assigned the next largest number.
To detach from the session without interrupting an active process or erasing the terminal history, enter Ctrl+b
, followed by d
. This will return you to the main terminal window. To reattach to a previous tmux
session, run tmux attach -t
, followed by the session number (remember that the first session will be number 0
, not 1
). If there is only one session open, tmux attach
will attach to it. If you aren't sure whether there are any tmux
sessions active, run tmux ls
:
To scroll up and down through the terminal output of a tmux
session, enter Ctrl+b
, followed by [
(left square bracket). This will enable the cursor and allow you to scroll using the arrow keys or page up and page down. Press q
to return to the last line of the terminal.
To close a tmux
session (and kill whatever processes are running within it!), attach to the session and enter Ctrl+d
. This cannot be undone, so before doing this, be sure that your process has finished, and that you have saved whatever terminal output you need.
Piping commands (stdin
, stdout
, and the tee
command)
(Combine with head or tail)