Skip to content

Instantly share code, notes, and snippets.

@jedavidson
Last active March 1, 2021 00:48
Show Gist options
  • Save jedavidson/542a917864d27c6bf802c738cb6d0f6c to your computer and use it in GitHub Desktop.
Save jedavidson/542a917864d27c6bf802c738cb6d0f6c to your computer and use it in GitHub Desktop.
A list of examples showing off the commands in CSESoc's 2021 Into the Unixverse workshop.
########################
# File system commands #
########################
# Show the current working directory
pwd
# View the contents of the working directory
ls
# View the contents of a specific directory
# folder must exist for this to work
# This can be either a relative or absolute path
ls folder
# View the contents in vertical list format using the -l flag
# This also gives some extra information; COMP1521 will teach you more about this
ls -l
# View the contents of the working directory including hidden files using the -a flag
# Unix treats files starting with . as hidden
ls -a
# You can combine these two flags together into one flag
# They are commonly used operations that this is actually what you'll want to use
# most of the time when you want to use ls
ls -la
# Display more readable file size information using the -h flag
# However, using -h on its own has no effect: must use with -l flag from before
ls -lh
# View the entire structure of every subfolder using the -R flag
# For large/deeply-nested projects, this does become a mess to read though
# An alternative is tree(1): https://linux.die.net/man/1/tree
ls -R
# Change the working directory
# This can be either a relative or absolute path
cd folder
# Change the current directory to be home
cd
# Change the current directory to be the parent directory
# This does not work if you are currently in root (/),
# but will work everywhere else
cd ..
# Create a new directory
# This can be either a relative or absolute path
# Importantly, it must not already exist
mkdir folder
# You can use "complex" paths, but be careful
# Right here, folder has to exist and contain a directory called subfolder
# in order for this to work
mkdir folder/subfolder/subsubfolder
# You can create multiple directories at the same time
mkdir folder1 folder2 folder3
# Create a new file within the working directory
touch file
# You can use relative paths too
touch ~/folder/file
# You can create multiple files at the same time
touch file1 file2 file3
# Remove a file
# This can be either a relative or absolute path
# You should be *very* careful with this, as there is no way to undo this,
# and you can seriously damage your file system if you delete something critical
rm file
# Remove a folder and all of its contents using the -r flag
# This can be either a relative or absolute path
# This will work for deleting just files too but is unnecessary
rm -r folder
# You can delete multiple things at the same time
rm -r file1 file2 file3 folder1 folder2 folder3
# Copy a file
# Both arguments can be either relative or absolute paths
cp folder1/file folder2/file
# Copy a folder using the -r flag
cp -r folder1/subfolder folder2/subfolder
# Move a file
# Both arguments can be either relative or absolute paths
# If you're moving a folder, you don't need a special flag
# You should be careful with this, as if the destination path
# already exists, you will overwrite it with the source path's contents
mv folder subfolder/folder
# You can use this command to rename files
mv file new_file_name
###################
# Output commands #
###################
# Write a message to the screen
# It's good practice to quote the things you want to write
echo "Hello, UNSW!"
# Not quoting your messages with echo works too, but it's more crude
echo Hello, UNSW!
# The type of quotations you use can impact the behaviour of this command
# This is not an important distinction if you're starting off,
# but for the sake of completeness, here is an example of the difference
echo "My home folder is $HOME" # My home folder is /home/username
# Interpolated the $HOME shell variable
echo 'My home folder is $HOME' # My home folder is $HOME
# Did not interpolate $HOME, treated it at as raw text
# Display the contents of a file to the screen
# This can be either a relative or absolute path
cat file
# If you want the output to include line numbers, use the -n flag
cat -n file
# You can show the content of multiple files at the same time
# The outputs will be put one after another
cat file1 file2 file3
# Progressively scroll through the contents of a file
# Vim keys are the best way to work with this command
# Press j to move the cursor down
# k to move the cursor up
# g to jump to the top of the file
# G to jump to the bottom of the file
# q to quit/stop viewing the contents
less file
# View the first lines of a file
# By default, it shows the first 10
# This can be either a relative or absolute path
head file
# View the first n lines of a file using the -n flag
head -n file
# If you use tail, you view the last lines of a file instead of first
tail file
tail -n file
########################
# Tools and navigation #
########################
# View the documentation for a command
# This is displayed to you through less, so use the bindings described
# when we covered less to navigate the documentation
man command
# You can view the documentation for man itself if you want
man man
# View the entire history of commands you've ever entered in the shell
history
# Here is a nice cheatsheet for all of the bash navigation shortcuts:
# https://blog.ssdnodes.com/blog/cheatsheet-bash-shortcuts/
###############
# Redirection #
###############
# An example of using < to redirect a command's input
# Here, we will assume you have compiled a C program that simply repeats what the user types in
# To make it repeat the contents of a text file instead of taking from stdin,
# you can use the < operator and specify the file you want it to read from
# What you've done is basically remake cat!
./repeat < text_file.txt
# An example of using > to redirect a command's output
# Let's assume that, for some reason, you want to save the output of ls to a file
# You can do that simply using the > operator, and specifying a file to save to
# This overwrites the content of the file you're saving to, so be careful
ls > ls_output.txt
# If instead you want to append/add to the file rather than overwrite it,
# replace > with >>
ls >> ls_output.txt
##################################
# Composing commands using pipes #
##################################
# An example of using | to feed the output of one command as input to another
# We will use a pipe to print out the last 5 lines of the first 10 lines of a file
# (in effect, showing the 6th, 7th, 8th, 9th and 10th lines of the file)
# The first 10 lines from head are stopped from being output immediately,
# and are instead sent to become input to tail
head file | tail -5
# A more complicated example of a pipe, showing how powerful a feature it is
# This pipeline displays an alphabetised list of file extensions in the
# working directory, and the number of files of each extension
# You probably don't know most of the commands used here, but don't worry
# as this is just an example for demonstration purposes
ls | rev | fgrep '.' | cut -d. -f1 | rev | sort | uniq -c | sed -e 's/^ *//g'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment