Super Simple Unix Tutorial

A super simple tutorial about Unix.

No fluff, just for beginners.

Listing files: the ls command

The Unix file system is divided into files and folders, just like more traditional desktops. You start the terminal in your home directory, represented by the symbol “~”.

To list the files and directories in your current directory, use:

ls

Special symbols to know: “*” means any number of characters, and “?” means one character. So to list all text files in the current directory:

ls *.txt

And to list files that look like myfile_a.dat, myfile_b.dat, myfile_c.dat:

ls myfile_?.dat

More about directories (also called folders)

You can also look in other directories (or folders, as some people call them). Directories are separated using the “/” symbol. Say you’re in your home directory (~). That directory contains files and other (sub)directories. Often one of the subdirectories contained in the home directory is “Downloads”. If you’re currently in your home directory and want to see the text files in your “Downloads” directory:

ls ~/Downloads/*.txt

Other useful symbols: “.” means the current directory, and “..” means the directory just above the current directory. So if you’re in the home directory, these two commands are equivalent:

ls ~/Downloads/*.txt
ls ./Downloads/*.txt

If you’re in the “Downloads” directory, a subdirectory of the home directory, these two commands are equivalent:

ls ~/*.txt
ls ../*.txt

You can also use “..” multiple times. Think about what this means:

ls ../../my_files/hello.txt

Note for speed: At the command line, you don’t have to type everything. You just have to type enough so the operating system can figure out what you mean, and then press tab. Tab basically means “autocomplete.” So if you have a file named “this_file_has_a_name_that_is_way_too_long_to_type.txt”, and no other file in the current directory starts with “this_file_has…”, you can just type “this_file_has” and press tab, and it will autocomplete.

Changing directories: the cd command

The cd command means “change directory.” It has a format similar to the ls command. So if you’re in the home directory and want to make the “Downloads” subdirectory your new current directory:

cd Downloads

If you’re in the home directory and want a list of text files in the “Downloads” directory, these two code blocks will give you similar information:

ls ./Downloads/*.txt
cd Downloads
ls *.txt

Copying and moving files: the cp and mv commands

The cp command is for copying files. It accepts two parameters, one for the source file and one for the destination. If the destination is a directory, the cp command will place a file with the same name in that directory. If the destination is a file, cp will create that file and fill it with the same content as the source file. So if you want to copy a file called “myfile.txt” in the home directory to the “Downloads” directory:

cp ~/myfile.txt ~/Downloads/

If you want to do the same thing but would rather the version of the file in “Downloads” be named “myfile2.txt”:

cp ~/myfile.txt ~/Downloads/myfile2.txt

By default, the cp command only copies source files (not subdirectories). The -r flag (option) tells cp to include subdirectories. For example, to copy all the files in the home directory, as well as any subdirectories and the files contained therein, use the -r flag:

cp -r ~/* ~/Downloads/

The mv command works exactly the same way, except it moves the file instead of copying it.

mv ~/myfile.txt ~/Downloads/
mv -r ~/* ~/Downloads/

Deleting files: the rm command

The rm command can be used to delete files, but be careful! There is no trash can/recycle bin in Unix.

rm ~/myfile.txt

To include subdirectories in the list of things to be deleted, use -r, but be very careful! You can end up deleting a lot of files. This command, for example, would delete all the files in your home directory as well as all subdirectories (pretty much all your files!). Never type this!!!

rm -r ~/*

Viewing files: the cat command

Pick a good text editor for making major changes to files, but if you just want to quickly see the contents of a text file, you can use the cat command.

cat myfile.txt

Pipping and saving

The cat command is especially useful when using “piping.” Piping, represented by the “|” symbol, means take the output of the command to the left, and feed it into the command to the right. Consider this command:

cat myfile.txt | grep "moose"

The grep command means “only show me file lines that contain a given string of letters.” So the above command first outputs all the lines of myfile.txt, and then feeds those into the grep command. Only those lines in myfile.txt that contain the word “moose” will appear on your screen. It’s a great way to filter and search through files.

Piping works with all sorts of commands. For example, these two commands will typically give similar results:

ls *myfiles*
ls | grep "myfiles"

You can also string multiple pipes together. This command will give you all the file lines in myfile.txt that contain both “moose” and “attack”:

cat myfile.txt | grep "moose" | grep "attack"

Saving command output is also useful. This can be accomplished with the “>” symbol. For example, this command will save the names of all the files and directories in the home directory to a file called “filelist.txt”:

ls ~/* > filelist.txt

You can use piping and saving together:

cat myfile.txt | grep "moose" | grep "attack" > lines_about_moose_attacks.txt

Working with remote computers: ssh, scp, and rsync

To log into a remote computer also running Unix, use the ssh command. You have to specify your username and the remote address of the other computer:

ssh username@remote_super_computer.pitt.edu

To copy files from your computer to a remote computer, use the scp command:

scp ~/myfile.txt username@remote_super_computer.pitt.edu:~/Downloads/
scp -r ~/* username@remote_super_computer.pitt.edu:~/

The scp command copies all the files, even if the identical files already exist on the remote computer. If you’ve already copied the files once and now you just want to copy over changes to the files, use the rsync command. It’s much faster:

rsync ~/myfile.txt username@remote_super_computer.pitt.edu:~/Downloads/
rsync -r ~/* username@remote_super_computer.pitt.edu:~/

Conclusion

This tutorial is a very preliminary introduction. There are many more Unix commands, and the commands I’ve described all have parameters (options) that I haven’t covered. But I hope this document has at least given you a general idea of how Unix works. There are many more advanced tutorials on the internet. Best of luck!