Introduction to the command line
Opening Bash
- Terminal (Mac)
- Cygwin (Windows <= 9)
- Ubuntu (Windows 10)
- JSLinux (other)
Navigating and keyboard shortcuts
- arrow left and arrow right to move along a line
- Ctrl + C to 'cancel'
- Ctrl + A to go to start of line
- Ctrl + E to go to end of line
- arrow up (or Ctrl + N) to go to last (next) command, arrow down (or Ctrl + P) to go to previous command
- Tab to 'autocomplete'
Working with the file system
whoami
Prints the current username
date
Prints the full date and time
pwd
Print the current working directory
ls
ls
list (print to screen) the files and directories within the current working directory
ls path
list the files and directories within path
ls -l
use the 'long' format, showing permissions and ownership details for each file
cd
cd directoryname
change the current working directory
mkdir
mkdir [path/]name
create a new directory, optionally in a different path
touch
touch [path/]filename
creates a new empty file, optionally in a different path
cp
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
mv file location
move a file to a new directory
mv file name
rename file to name
rm
rm file
delete a file
rm -r directory
delete a directory and everything in it, including subdirectories
rm -i file
The -i
flag will ask you to confirm before deleting each file or directory. Accepts anything starting in 'y' as confirmation.
Permissions
chmod
chmod [flag] mode file
change permissions on a file
e.g.
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.
e.g.
chmod -R u+w directory
give the current user write permission for directory and all files and subdirectories within it.
sudo
"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.
e.g.
sudo chmod +x file
chown
chown user directory
change the owner of directory to user
The -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
"The Manual"
man
program - display the manual page for program
This will display in Vim - press q
to quit back to the command line
e.g.
man pwd
Editing text
Vi and Vim
Open with vi
or 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
nano
Open with nano
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.
cat
cat file
display contents of file to the screen or STDOUT
echo
echo string
display string to the screen or STDOUT
Environment settings and PATH
Streams
STDIN - Standard input: by default comes from keyboard
STDOUT - Standard output: by default displays on screen
STDERR - Standard error: by default displays on screen
Redirecting Streams
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.
Bash (command)
bash file.sh
run file.sh as a bash script
./file.sh
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)
Links
ln -s source_file target_file
create a symbolic link from target_file to source_file
tail
tail 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
curl
curl url
direct contents of url to STDOUT
curl url > file
download contents of url as file
grep
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
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
Check out 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 and gzip
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 archive.tar
.
gzip archive.tar
compress archive.tar into a file called archive.tar.gz
gunzip archive.tar.gz
unzip the file
tar -xf archive.tar
extract the file archive.tar
Combining tar and gzip in one command
tar -czf archive.tar.gz directory
create a compressed tar archive containing directory
tar -xzf archive.tar.gz
decompress and extract the contents of archive.tar.gz
rsync
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
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.
ssh user@machine
remotely access machine as user
ssh-keygen
create a 'key pair' that will be saved to ~/.ssh/id_rsa
by default.
ssh-copy-id username@remote_host
copy public key to remote machine
More info at Digital Ocean
Downloading programs and packages
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.
Ubuntu Linux (with Windows 10)
You can download packages in Ubuntu using apt-get
:
apt-get install packagename
e.g.
apt-get install jq
installs jq
You may need to use sudo
MacOS
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
installs jq
You should not use sudo
with Homebrew