Linux Basics
Unix/Linux
They are a powerful class of operating systems, designed to support the needs of programmers. They are multiuser, uses a unified file system, and are heavily based on the command-line interface called the "shell." They are designed to automate tasks. Putting in upfront effort reduces the manual effort needed later, which is called "long-term lazy" (slower to learn, faster to use).
Directory means the same thing as folder. All files and directories are referred as "paths".
~
denotes the home directory of your account.
denotes the current directory..
denotes the parent directory (one level up from current)- When you log into ugrad, you'll be in your home directory.
Connecting to ugrad from your own computer
Windows: download PuTTy, set Host Name as ugradx.cs.jhu.edu
, type account name (JHED), and
password.
Mac: open Terminal, then type the command ssh <your-username>@ugradx.cs.jhu.edu
font-family:
monospace; Linux: open terminal and use ssh <your-username>@ugradx.cs.jhu.edu
.
Command Line
Unix/Linux commands are case-sensitive. Learn Unix/Linu command line from this boot camp.
Basic Commands
pwd
- print working directoryls
- list directory contentscd <folder_name>
- change directory [specify directory name]mkdir <folder_name>
makes a new directory within current directory.less <file_to_view>
view text file screenful at a time.mv <source> <destination>
changes the location of file or folder.cp <source> <destination>
make a copy file or folder.rm <file_to_remove>
remove a file. Not easy to undo deletion.- Use the flag
-r
to delete recursively and remove subdirectories and-i
if you want to verify deletion.
- Use the flag
- Loations can be relative to current directory (E.g.:
cp Hello.c folderForToday/hello.c
) or absolute, based on the full path in file system. exit
logs out of the ugrad system.man
is the manual available at command line. Using with an operation or function gets details (e.g,man cp
orman ispunct
).
Copying Files
cp
command in Unix is how we copy files within one machine. scp
and
pscps
command is secure copy, a way to transfer copies from one machine to another.
In Mac Terminal or Windows Power Shell, use scp <source> <destination>
.
In Windows with PuTTY, use pscp <source> <destination>
in Command Prompt.
- Note that Windows machine use backslash (
\
) instead of forward slash (/
) used in unix for file paths.
Ugrad to Local
scp <username>@ugradx.cs.jhu.edu:<filename>.c .
(in Mac Terminal or Windows Power Shell).
For example,
scp ltran29@ugradx.cs.jhu.edu:HW1/file.c xyz/
Zipping
zip zipped_files.zip file1.c file2.c gitlog.txt
Packs up three files into a zipped_files.zip
.
To confirm that it contains what you want,
unzip -1 zipped_files.zip
To unpack the zip file (i.e., extract its file),
unzip zipped_files.zip
Move and unpack in a new/different folder to avoid overwriting the original files.
I/O Redirection
When executing a command that produces output, redirect the result to a textfile (instead of on screen) by
using '>
outfile':
ls > myfiles.txt
When executing a command that takes input, redirect input from a plain txt file by using
'<
infile':
less < infile.text
Use both at once, or use a pipe command '|
' to send the standard output of one command
directly into standard inputfor another command. For example, to list your files but view the list one
screenful at a time use:
ls | less
echo 4 | ./a.out
sends an input directly into a program.
Homework workflow
- Write code, compile, test, and edit while continuously adding, committing, and pushing changes to personal git repo.
- Generate
gitlog.txt
file to document your commits usinggit log > gitlog.txt
. - Bundle files for submission with
zip hw0.zip file1.c file2.c gitlog.txt
. - Confirm that your zip files contains everything with
unzip -1 hw0.zip
. - Transfer the bundle onto local machine using
scp <username>@ugradx.cs.jhu.edu:<HW0/hw0.zip .
- Upload
hw0.zip
to Gradescope. Wait for feedback and resubmit (only last one is graded).
Tips
*
: wildcard character, helpful with file commands.git add *.c*
adds all files ending in .c to your staging area.ls f*
lists all files that start with f.
!
(bang): repeats the prior command. Can be used alone or with a start of a command name.! !
will execute the prior command.!em
will execute the most recent command that started with "em"
- up and down arrows cycles through your command history
- tab will complete command.
history | grep <keyword>
: searches history with keyword.