Tips and Tricks for Bash usage - labordynamicsinstitute/replicability-training GitHub Wiki
For All Users
- if you installed Visual Studio Code, you can open it from the (Git) bash command prompt by simply typing
code
, orcode REPLICATION.md
. This should open Visual Studio Code with a document preloaded. - To quickly find a list of all files of a certain type (e.g.,
dta
Stata data files, ordo
Stata program files), you can use a quick command:
find name/of/directory -name \*.dta
find name/of/directory -name \*.do
(name/of/directory
can also be .
(dot), if you want the search to start in the directory you are currently in). You can then copy the list found (if not too long) into your report.
- To count the number of files of a certain type, append
| wc -l
(starts with the pipe character|
), e.g.,
find name/of/directory -name \*.do | wc -l
will count the number of .do
files found in name/of/directory
.
For Windows Users
- In the Bash shell, always use
/
forward slashes, not\
backward slashes. - A better "tool" to use the Bash shell is the Windows Terminal (install from the Windows App Store.
- You can add "Git Bash" to the default options in the Windows Terminal by adding the following code to the Settings (open Settings, then
Open JSON file
, find a section called ""profiles"
", and append to it, by adding first a comma after the last}
, then the following code):
- You can add "Git Bash" to the default options in the Windows Terminal by adding the following code to the Settings (open Settings, then
{
"closeOnExit": "graceful",
"colorScheme": "Campbell",
"commandline": "%PROGRAMFILES%/Git/bin/bash.exe -l -i",
"cursorColor": "#FFFFFF",
"cursorShape": "bar",
"fontFace": "Consolas",
"fontSize": 12,
"guid": "{14ad203f-52cc-4110-90d6-d96e0f41b64d}",
"historySize": 9001,
"icon": "%PROGRAMFILES%/Git/mingw64/share/git/git-for-windows.ico",
"name": "Git Bash",
"snapOnInput": true,
"tabTitle": "Git Bash"
}
(note: on CISER, you may need to adjust %PROGRAMFILES%
, depending on where Git bash is installed.)
Advanced usage
Tmux
On Linux machines, use tmux
to create text-only sessions that can be reconnected to.
Saving tmux buffers
From here
- Use
prefix + :
, then type incapture-pane -S -3000 + Return
. (Replace-3000
with however many lines you'd like to save, or with-
for all lines.) This copies those lines into a buffer. - Then, to save the buffer to a file, just use
prefix + :
again, and type insave-buffer filename.txt + return
.
(By default Prefix
is Ctrl+B
.)
Collecting screen output from command line stuff
julia program.jl | tee name_of_log_file.log
or more sophisticated
julia program.jl | tee logfile_$(date +%F-%H-%m).log