Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Bash/Unix Command Cheat Sheet, Cheat Sheet of Computer Communication Systems

Leaning how to utilize the command line in Unix is crucial for navigating your computer as well as making, deleting, and searching your files.

Typology: Cheat Sheet

2019/2020

Uploaded on 10/09/2020

ekasha
ekasha 🇺🇸

4.8

(22)

270 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Bash/Unix Command Cheat Sheet
Leaning how to utilize the command line in Unix is crucial for navigating your computer
as well as making, deleting, and searching your files. Many of these commands will
come in handy on a very regular basis, so they should become second nature rather
quickly. Until then, feel free to use this sheet!
Before you get started:
% cp ~premapta/setup ./
% tcsh setup
The above command will set up some directories as well as your .cshrc file. This file is
important, as it has in it settings that will point your computer where to important files.
We will do this together in class and it only needs to be done once.
Some General Notes and Definitions
I will be using the term “directory”. Think of these as files, or places on your computer.
For example, your desktop is a directory. A folder on your desktop is a directory within
the desktop directory. etc, etc....
Yourhomedirectory is your starting place. Whenever you first open up the terminal,
you start from here. Often, this is denoted by a “~”. To go back to your home directory:
% cd ~ or even just % cd
An object’s “pathmeans its location on the computer (think of it as an address that tells
you exactly where something is). For example, a file named “file.txt” in the directory
“text_files” in the folderMyStuffon your Desktop would be:
~/Desktop/MyStuff/text_files/file.txt
Think of your path like a stream. Your home directory is the highest point and
everything flows out from there (i.e. everything isdown stream”). Everything within a
directory isdown stream” from that directory.
Any command that takes a file or directory name as input can also take a path as input.
For example, the following lines do the same thing (they remove the file called “file.txt”)
% rm file.txt
% rm ~/Desktop/MyStuff/text_files/file.txt
When you want to perform an action on multiple files in a directory, using*is useful.
% rm * removes all files in a directory (dangerous!)
% rm *.txt removes all files with names that end in ‘.txt’
pf3
pf4
pf5

Partial preview of the text

Download Bash/Unix Command Cheat Sheet and more Cheat Sheet Computer Communication Systems in PDF only on Docsity!

Bash/Unix Command Cheat Sheet

Leaning how to utilize the command line in Unix is crucial for navigating your computer as well as making, deleting, and searching your files. Many of these commands will come in handy on a very regular basis, so they should become second nature rather quickly. Until then, feel free to use this sheet! Before you get started: % cp ~premapta/setup ./ % tcsh setup The above command will set up some directories as well as your .cshrc file. This file is important, as it has in it settings that will point your computer where to important files. We will do this together in class and it only needs to be done once. Some General Notes and Definitions I will be using the term “ directory ”. Think of these as files, or places on your computer. For example, your desktop is a directory. A folder on your desktop is a directory within the desktop directory. etc, etc.... Your “ home ” directory is your starting place. Whenever you first open up the terminal, you start from here. Often, this is denoted by a “~”. To go back to your home directory: % cd ~ or even just % cd An object’s “ path ” means its location on the computer (think of it as an address that tells you exactly where something is). For example, a file named “file.txt” in the directory “text_files” in the folder “MyStuff” on your Desktop would be: ~/Desktop/MyStuff/text_files/file.txt Think of your path like a stream. Your home directory is the highest point and everything flows out from there (i.e. everything is “down stream”). Everything within a directory is “down stream” from that directory. Any command that takes a file or directory name as input can also take a path as input. For example, the following lines do the same thing (they remove the file called “file.txt”) % rm file.txt % rm ~/Desktop/MyStuff/text_files/file.txt When you want to perform an action on multiple files in a directory, using “ * ” is useful. % rm * removes all files in a directory (dangerous!) % rm *.txt removes all files with names that end in ‘.txt’

% rm test.* removes all files with names that start with “test.” % rm test*.txt removes files with names that start with “test” and end with “.txt” Many of the commands discussed below have options connected with them. Options are usually denoted by upper or lowercase letters (and sometimes numbers) after the command, usually preceded by a ‘-’. For example, - [option1][option2] If you forget the exact syntax to use with a command, or need to find what options are available, use the man command. For example: % man ls This will give you the manual page for the ls command, including a list of the available options and their actions. Tab completion is very useful. When you are typing in a command or the name of a file, hitting tab will have the computer attempt to auto complete what you were typing based on the available options. If there is more than one option, it will give you a list of the possible commands/files. For example, type “ip[TAB]”. The computer gives you a list of all files, directories, and commands that start with “ip”. Type in “ipy[TAB]” and the computer auto completes the command to “ipython”.


Listing files in a directory

% ls Gives a list of (almost) everything in your current directory % ls - a Lists everything in the directory (including files that start with a ‘.’) % ls - l Lists Things in an up-down list with some information, like when the file was last edited and the size of the file % ls - la Does both options!


Navigating directories

% cd [directory path] Moves you to a new directory

% echo hello > [file] opens a file and prints “hello” to it. This will overwrite the file and delete whatever was in it at first % echo hello >> [file] Same as above, but it will append the file rather than overwrite it % cat [file] Print out the contents of a file % cat [file 1 ] > [file2] Print the contents of file 1 to file (overwriting file2) % cat [file 1 ] >> [file2] Append file2 with the contents of file 1 % grep [something] [file] print out the lines in file that include the string “something” in the line % more [file] View the contents of a file. Better than cat if the file is long. Push spacebar to scroll down % tail - [number] [file] View the last [number] of lines of a file % head - [number] [file] View the first [number] of lines of a file % wc [file] Prints number of lines, words, and size of file


Sorting Streams

% sort – k number [file] | more Alphabetical sort, where number refers to the column. % sort – n – k number [file] | more String numerical sort. % sort – g – k number [file] | more General numerical sort.

______________________________________________________________________

Making “tar balls” (compressed files)

% tar cfz filename.tar.gz [list of files] Compress files into filename.tar.gz. (note the lack of a “-” before the cfz option here....) % tar xfz filename.tar.gz Decompress filename.tar.gz and put all the files in it into your current directory


Some examples using “ piping ” ( “|”)

% cat [file] | grep word Print the lines in file that include “word” in the line % grep word [file] | wc Gives the number of lines, words, and size of the portion of file that grep returns as having “word” in the line

Checking the memory usage on your machine

Sometimes you will run commands that will push your machine to its limit. Or, you might be using a shared device and want to avoid hogging all the memory for yourself. To check what processes are being done by your machine type in the command %top To only look at processes you are running, type %top - u [username] This will cause some information to pop up on your terminal. For example: top - 09:05:39 up 23 days, 21:18, 7 users, load average: 0.10, 0.04, 0. Tasks: 223 total, 1 running, 222 sleeping, 0 stopped, 0 zombie Cpu(s): 0.4%us, 0.3%sy, 0.0%ni, 99.3%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 16247332k total, 2899880k used, 13347452k free, 162968k buffers Swap: 83 88600k total, 4k used, 8388596k free, 1420460k cached The first couple lines are pretty useless. The line starting with “Cpu(s)” tells you how your computer is using its CPUs. Really, the only thing relevant here is that if you are running something, if it isn’t using 100% of the CPUs, then it is technically running inefficiently (though I would say this usually doesn’t warrant any worry).

A specific example: % scp file premapta@astrolab23@astro.washington.edu:~/python/pro The above line copies the file “file” to the directory python/pro within the account of “premapta” on the astrolab23 machine. Recall from above that in order to copy something FROM an astrolab machine TO your laptop you would do something like the following: % scp premapta@astrolab23@astro.washington.edu:~/python/pro/ file. Where the period at the end just means to copy that file to whatever directory you are currently in (you could put in a more specific path).