-
Notifications
You must be signed in to change notification settings - Fork 0
Bash Reference_12550194
Created by Ahmed Raza Hasan, last modified on Jun 01, 2017
(*** under construction - turn back now, and have your mortal life spared ***)
The bash shell is a highly useful means of interacting with your computer (or, in the case of our server, a computer). We all use the bash shell in order to get our Jupyter Notebooks set up and perhaps run some command line programs, but turns out it can do a lot more past that!
Understanding even basic Unix commands can go a really long way, especially in a genomics environment where the inputs are big and the outputs can be bigger. Working with large text files in particular is a breeze with said tools where something like Python or R would struggle to even load them in. Finally, bash is incredibly powerful when it comes to automating tasks with ease.
A quick glossary:
- stdin = input. this is what you are entering into the command line
- stdout = output. this is what gets printed to the console after something is run
pwd - prints current directory to stdout
- the current directory you are in determines what bash 'immediately
sees' when you, say, call upon a file
- if this makes no sense, I promise it will soon enough
whoami - in case of identity crisis
ls - lists files in current directory
- super useful!
- like most Unix commands, can be altered with flags
- flags are instantiated using a single hyphen (-), and almost always consist of just one letter
- caveat - sometimes flags are entire words, and start with two dashes (ie --flag) but you likely won't see these as often
- for instance,
ls -lwill return files in the directory in a list format, whilels -tlists them in descending order since they were last modified - multiple flags can be used at once, either as
ls -l -tor justls -lt
man - pull up the help page for a command
- did you forget the
lsflags already? - well, no sweat - running
man lswill pull up a handy reference - scroll up and down with the arrow keys, and press q to exit the man (manual) page
cd - change directory
- switches working directory to specified folder
- for instance,
cd Desktop/will take me to a folder called Desktop/, *assuming *Desktop/is in my current directory - obviously this isn't always the case - but to go back a folder, use
cd ..or cd ../- this can be chained together using forward slashes - for instance,
cd ../../will go back two folders up in the hierarchy (i.e. from/scratch/research/projectsto/scratch/)
- this can be chained together using forward slashes - for instance,
- if the path given to cd starts at the root directory, then bash won't
complain about being given a folder that's not in your current working
directory
- for instance, I can do
cd /scratch/research/referenceseven if I'm currently inscratch/research/projects/chlamydomonas, as long as I write out the full path
- for instance, I can do
echo - prints input back to stdout
- bash's equivalent of
print(), really -
echo hello therewill returnhello there - inputs don't *have* to be in quotes, but bash won't mind if you do wrap them with either single or double quotes
cat - print contents of file
-
cat <file>will simply return the contents of the file to stdout - however,
catcan take multiple files as arguments, and will *concatenate *them before printing to stdout - for instance,
cat <file1> <file2>will return the entire contents of both file1 and file2 one after the other - this is useful for quickly combining large flat files containing data, and synergizes super well with pipe operations (more on that in the tips and tricks article)
mkdir - creates a new folder
-
mkdir myfolderwill create an empty folder calledmyfolderin your current working directory -
mkdir folder1 folder2will make two folders namedfolder1andfolder2respectively
mv - move files around/rename files
-
mv file.txt data/fileswill movefile.txtto/data/files - adding the
-vflag (i.e.mv -v) is especially useful, as bash will explicitly print out what it's moving and where -
mvworks well with wildcards in moving several files around - more on that later on as well - finally,
mvalso allows us to rename files by 'moving' a file on top of itself- for instance,
mv file.txt my_file.txtwill renamefile.txttomy_file.txt
- for instance,
rm - delete things
- this is a frighteningly efficient command, and has no undo button
- in Unix, there is no Recycle Bin for deleted files to go to - they are promptly sent to the void and you will never, ever see them again. you hear that? never.
-
rm -iwill prompt for confirmation before you delete something, so it might be good to make a habit of that -
rmwill complain if you make it delete a folder, but that can be bypassed withrm -rf - possibly goes without saying, but be extremely careful when using
rmon multiple files at once!
rmdir - delete a folder
- an alternative to the scarier
rm -rfif you feel like it
chmod
head
tail
less
sort
wc
gzip
pipe
>
>>
!$
!!
Document generated by Confluence on May 22, 2024 11:44
- Chlamy Tips
- Coding Tips.
- HpcnodeLife.
- Other Awesome Pages.