Difference between revisions of "SW:Unix"
Line 1: | Line 1: | ||
− | + | = Basic UNIX Commands = | |
− | The basic form of any UNIX command is: command_name options argument(s) | + | __TOC__ |
− | and programs. Thus, while ls is a valid Unix command, LS is not. | + | == Introduction == |
− | except for the space bar and all of the following : * ? ! \ \ ‘ “ > ;, | + | The basic form of any UNIX command is: command_name options argument(s) |
− | ^ ( ) $ ~. These characters cannot be used in filenames because they have special | + | * '''Unix is case-sensitive. ''' |
− | meaning to the shell. For instance, the first two characters are used as | + | * Unix distinguishes between upper and lower case letters in the names of files and programs. Thus, while ls is a valid Unix command, LS is not. |
− | “wildcards”. | + | * Login names and passwords are also case-sensitive. |
− | program rather than at the Unix shell prompt. The Shell prompt reappears | + | * A filename in Unix can consist of any combination of characters on the keyboard except for the space bar and all of the following : * ? ! \ \ ‘ “ > ; , ^ ( ) $ ~. These characters cannot be used in filenames because they have special meaning to the shell. For instance, the first two characters are used as “wildcards”. |
− | whenever you exit those programs. | + | * Some programs, such as Pico, have their own commands that you type within the program rather than at the Unix shell prompt. The Shell prompt reappears whenever you exit those programs. |
− | {| class="wikitable" | + | |
− | + | == Navigating the Filesystem == | |
+ | {| class="wikitable" style="text-align: center;" | ||
! Task | ! Task | ||
! Command | ! Command | ||
− | |- | + | ! Notes |
+ | |- | ||
| Lists your files | | Lists your files | ||
− | | ls | + | | '''ls''' |
− | |- | + | | Available options: -l (long format) -a (view all files, including hidden ones) |
− | + | |- | |
− | | | + | | Change to a directory. |
− | | | + | | '''cd ''DirName''''' |
− | | | + | | Use "'''cd ..'''" to change to the parent directory. |
− | | | + | |- |
− | | | + | | See the full path for the current directory. |
+ | | pwd | ||
+ | | | ||
+ | |} | ||
+ | |||
+ | == File and Directory Handling Commands == | ||
+ | {| class="wikitable" style="text-align: center;" | ||
+ | ! Task | ||
+ | ! Command | ||
+ | ! Notes | ||
+ | |- | ||
| Move a file into a different directory, or rename the file. | | Move a file into a different directory, or rename the file. | ||
− | | mv file1 file2 | + | | '''mv ''file1 file2''''' |
− | |- | + | | If moving a file to a new directory, the full path to that directory is needed. <br> Example: '''mv ''file1 $SCRATCH/NewDir''''' <br>'''Note:''' In the above case, if you do not wish to rename the file, you do not need to add a filename, the directory name is sufficient. |
+ | |- | ||
| Copy a file | | Copy a file | ||
− | | cp file1 file2 | + | | '''cp ''file1 file2''''' |
− | |- | + | | To copy a directory, use the '''-r''' option |
− | | Removes a file. | + | |- |
+ | | Removes a file. | ||
| rm filename | | rm filename | ||
− | |- | + | | Preferably use rm -i which will ask you for confirmation before deleting the file |
+ | |- <--! | ||
| Tells you how many lines, words, and characters there are in a file | | Tells you how many lines, words, and characters there are in a file | ||
− | | wc filename | + | | wc filename |
− | |- | + | |- |
| Lets you change the read, write, and execute permissions on your files. chmod o+r filename will make the file readable for everyone, and chmod o-r filename will make it unreadable for others. | | Lets you change the read, write, and execute permissions on your files. chmod o+r filename will make the file readable for everyone, and chmod o-r filename will make it unreadable for others. | ||
− | | chmod option file | + | | chmod option file --> |
− | |- | + | |- |
| Make a new directory | | Make a new directory | ||
− | | mkdir | + | | '''mkdir ''DirName''''' |
− | + | | | |
− | + | |} | |
− | | | + | |
− | | | + | == Viewing and Searching Files == |
− | |||
− | |||
− | |||
{| class="wikitable" | {| class="wikitable" | ||
|- class="header" | |- class="header" | ||
Line 69: | Line 81: | ||
| Search for all lines containing a keyword in a given file. Use the -i option to ignore upper/lower case distinctions. To search for a phrase, you must enclose the phrase with single quotes. | | Search for all lines containing a keyword in a given file. Use the -i option to ignore upper/lower case distinctions. To search for a phrase, you must enclose the phrase with single quotes. | ||
| class="nowrap" | grep keyword filename | | class="nowrap" | grep keyword filename | ||
− | |} | + | |} |
+ | |||
+ | == About Yourself and Other People == | ||
{| class="wikitable" | {| class="wikitable" | ||
|- class="header" | |- class="header" | ||
Line 98: | Line 112: | ||
| Tells you who's logged on, and which computer they are logged on to. | | Tells you who's logged on, and which computer they are logged on to. | ||
| class="nowrap" | who | | class="nowrap" | who | ||
− | |} | + | |} |
+ | |||
+ | == Miscellaneous Commands == | ||
{| class="wikitable" | {| class="wikitable" | ||
|- class="header" | |- class="header" |
Revision as of 10:48, 28 April 2017
Basic UNIX Commands
Contents
Introduction
The basic form of any UNIX command is: command_name options argument(s)
- Unix is case-sensitive.
- Unix distinguishes between upper and lower case letters in the names of files and programs. Thus, while ls is a valid Unix command, LS is not.
- Login names and passwords are also case-sensitive.
- A filename in Unix can consist of any combination of characters on the keyboard except for the space bar and all of the following : * ? ! \ \ ‘ “ > ; , ^ ( ) $ ~. These characters cannot be used in filenames because they have special meaning to the shell. For instance, the first two characters are used as “wildcards”.
- Some programs, such as Pico, have their own commands that you type within the program rather than at the Unix shell prompt. The Shell prompt reappears whenever you exit those programs.
Task | Command | Notes |
---|---|---|
Lists your files | ls | Available options: -l (long format) -a (view all files, including hidden ones) |
Change to a directory. | cd DirName | Use "cd .." to change to the parent directory. |
See the full path for the current directory. | pwd |
File and Directory Handling Commands
Task | Command | Notes |
---|---|---|
Move a file into a different directory, or rename the file. | mv file1 file2 | If moving a file to a new directory, the full path to that directory is needed. Example: mv file1 $SCRATCH/NewDir Note: In the above case, if you do not wish to rename the file, you do not need to add a filename, the directory name is sufficient. |
Copy a file | cp file1 file2 | To copy a directory, use the -r option |
Removes a file. | rm filename | Preferably use rm -i which will ask you for confirmation before deleting the file |
Tells you how many lines, words, and characters there are in a file | wc filename | |
Lets you change the read, write, and execute permissions on your files. chmod o+r filename will make the file readable for everyone, and chmod o-r filename will make it unreadable for others. | chmod option file --> | |
Make a new directory | mkdir DirName |
Viewing and Searching Files
Task | Command |
---|---|
Display a file. | cat filename |
View a file one page at a time. Press [space-bar]if you want to see another page, type [q]if you want to quit reading, or [b]to go back a page. |
less filename |
View the first ten lines of a file. Use the -N option where N is a positive nonzero integer to specify the number of lines to display. | head filename |
View the last ten lines of a file. Use the -N option where N is a positive nonzero integer to specify the number of lines to display. | tail filename |
Searching for a keyword in a file using less | less filename
/keyword |
Search for all lines containing a keyword in a given file. Use the -i option to ignore upper/lower case distinctions. To search for a phrase, you must enclose the phrase with single quotes. | grep keyword filename |
About Yourself and Other People
Task | Command |
---|---|
List your processes. Contains information such as the process ID, which you might need to kill a particular process which would be creating difficulties on your computer | ps -u yourusername |
Ends the process, whose process ID you typed in. | kill processID |
Show what your disk quota is, how much you're using, and incase you have exceeded your quota. | quota -v |
Shows the disk usage of the files and the directories in filename, du -sk will give a total of all files and directories on the disk. | du filename |
Lists your last logins. | last yourusername |
Gives you lots of information about that user, e.g. when they last read their mail and whether they're logged in. | finger username |
Tells you who's logged in, and what they're doing. | w |
Tells you who's logged on, and which computer they are logged on to. | who |
Miscellaneous Commands
Task | Command |
---|---|
Shows the current date and time | date |
Shows a calendar of the current month. If you type in cal 10 1995 you will get the calendar for the month of October in the year 1995. | cal |
Shows you the manual page for the command | man commandname |
Refreshes or clears your screen. | clear OR Ctrl + l |
Stops running program or the command | Ctrl + c |
Suspends the currently running program or command | Ctrl + z |
Ends your login session | exit OR logout |