Prints the current username
Prints the full date and time
Print the current working directory
list (print to screen) the files and directories within the current working directory
list the files and directories within path
use the 'long' format, showing permissions and ownership details for each file
change the current working directory
create a new directory, optionally in a different path
creates a new empty file, optionally in a different path
cp file location
Copy a file to a new location
cp -r directory location
copy a directory and all subdirectories to a new location
cp -r directory/* location
copy the contents of a directory including all subdirectories to a new location
mv file location
move a file to a new directory
mv file name
rename file to name
delete a file
rm -r directory
delete a directory and everything in it, including subdirectories
rm -i file
-i flag will ask you to confirm before deleting each file or directory. Accepts anything starting in 'y' as confirmation.
chmod [flag] mode file
change permissions on a file
chmod o-r file
remove read permission for 'other' users
chmod +x file
make file executable for all users
chmod can also use octal mode values which we do not discuss in this workshop.
By default permissions for directories are only changed for the directory itself - not the current contents. The
-R flag changes permissions recursively for all files and directories within the given directory.
chmod -R u+w directory
give the current user write permission for directory and all files and subdirectories within it.
"switch user and do" - usually this command is used to act as the root user.
sudo is simply prepended to the command you want to run. All files created or copied using
sudo will have root as their owner.
sudo chmod +x file
chown user directory
change the owner of directory to user
-R flag is required to change the contents of directories:
chown -R user directory
change the owner of directory and all files and subdirectories within it to user
man program - display the manual page for program
This will display in Vim - press
q to quit back to the command line
vim. It will open in 'command mode'
i - enter 'insert mode' to write text.
Esc - exit insert mode and go back to command mode.
:q - quit
:q!- force quit and don't save
:wq filename - "write out and quit" - saves the file as filename and quits
More info about Vim
Ctrl + X - exit: if you have unsaved changes will prompt you to save and enter a filename.
Ctrl + W - "Where is": enter a word or phrase and press enter to go to the first line with the word or phrase.
display contents of file to the screen or STDOUT
display string to the screen or STDOUT
STDIN - Standard input: by default comes from keyboard
STDOUT - Standard output: by default displays on screen
STDERR - Standard error: by default displays on screen
command > filename
direct OUTPUT to file: overwrite if file exists, create new file if it doesn't.
command >> filename
direct OUTPUT to file: append to file if file exists, create new file if it doesn't.
command1 | command2
direct OUTPUT of command1 to INPUT of command2.
run file.sh as a bash script
run file.sh as a bash script after it has been made executable, if file is in the current working directory (directory does not need to be in PATH)
ln -s source_file target_file
create a symbolic link from target_file to source_file
view the last 10 lines of file
tail -r file
view the first 10 lines of file
tail -f file
continuously view the end of file to show new lines as they are appended
direct contents of url to STDOUT
curl url > file
download contents of url as file
grep string file
output lines in file containing string to STDOUT
grep -n string file
same as above but include line numbers in output
grep -c string file
only output the total number of lines
grep -i string file
make search case-insensitive
Note that string can be a regular expression.
sed arguments file
edit file according to arguments and print to STDOUT
sed 's/old_word/new_word/' file
replace old_word with new_word the first time it appears.
sed 's/old_word/new_word/g' file
replace all instances of old_word with new_word.
There are many other ways to use
sed. For example:
sed -n '1,5p' file
output the first five lines of file
sed '15c\' file
delete line 15
man sed for more information, or check out Bruce Barnett's useful tutorial. Don't worry if it's a bit confusing - sed is complex
tar -cf archive.tar file1 file2 file1
create a tar file called
archive.tar containing files 1 to 3.
tar -cf archive.tar directory
create a tar file called
archive.tar containing directory
tar -xf archive.tar
extract the contents of the file
compress archive.tar into a file called
unzip the file
tar -xf archive.tar
extract the file
tar -czf archive.tar.gz directory
create a compressed tar archive containing directory
tar -xzf archive.tar.gz
decompress and extract the contents of
rsync -az source destination
use archive mode and compression to sync files between the source directory and the destination location.
rsync -az --del source destination
sync as above and delete any files at destination that don't exist at source
SSH or 'secure shell' allows you to log in to a remote machine and enter commands in your terminal that will execute on the remote machine - basically you are on the command line remotely.
remotely access machine as user
create a 'key pair' that will be saved to
~/.ssh/id_rsa by default.
copy public key to remote machine
More info at Digital Ocean
At some point you will want to download programs/packages that do not come bundled with your operating system. Note that Cygwin is really an emulator so you can't use *nix packages directly using Cygwin.
You can download packages in Ubuntu using
apt-get install packagename
apt-get install jq
You may need to use
The best way to download command line packages for MacOS is using Homebrew
Paste this into your command line prompt, press
[Enter] and follow the prompts.
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Now you can install packages:
brew install jq
You should not use
sudo with Homebrew
Check out the slides
Move on to Projects
Check the Guide to *nix directories