User Tools

Site Tools


using_unix

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
using_unix [2017/05/12 12:07] cgeb2001using_unix [2021/10/27 14:04] (current) 24.222.40.124
Line 1: Line 1:
-====== **Basic UNIX commands** ======+====== Basic UNIX commands ======
  
  
-Some general comments+Some general comments (Oct 2021)
 These commands are issued on the command line and should work on a MAC or in a UNIX environment like perun. These commands are issued on the command line and should work on a MAC or in a UNIX environment like perun.
 Do not have spaces in the names of files or directories. If you want to separate portions of the name to make it more obvious use _ or . or – like so lord_of_the_rings.txt Do not have spaces in the names of files or directories. If you want to separate portions of the name to make it more obvious use _ or . or – like so lord_of_the_rings.txt
Line 13: Line 13:
  
  
-•ls --- lists your files 
ls -l --- lists your files in 'long format', which contains lots of useful information, e.g. the exact size of the file, who owns the file and who has the right to look at it, and when it was last modified. 
ls -a --- lists all files, including the ones whose filenames begin in a dot, which you do not always want to see. 
There are many more options, for example to list files by size, by date, recursively etc.+**ls** --- lists your files 
ls -l --- lists your files in 'long format', which contains lots of useful information, e.g. the exact size of the file, who owns the file and who has the right to look at it, and when it was last modified. 
ls -a --- lists all files, including the ones whose filenames begin in a dot, which you do not always want to see. 
There are many more options, for example to list files by size, by date, recursively etc.
  
-•cd dirname --- change directory. You basically 'go' to another directory, and you will see the files in that directory when you do 'ls'. You always start out in your 'home directory', and you can get back there by typing 'cd' without arguments. 'cd ..' will get you one level up from your current position.  You can navigate to any directory by providing the absolute path for that directory. So, cd /home/myname/blastfiles/jan would take you to the jan directory in the directory blastfiles in the directory myname which is in the basal directory home. You can also use relative pathnames to navigate to directories. For example if you were already in /home/myname/blastfiles/ you could do cd jan to go into the jan directory. A period .  denotes current directory+**cd dirname** --- change directory. You basically 'go' to another directory, and you will see the files in that directory when you do 'ls'. You always start out in your 'home directory', and you can get back there by typing 'cd' without arguments. 'cd ..' will get you one level up from your current position.  You can navigate to any directory by providing the absolute path for that directory. So, cd /home/myname/blastfiles/jan would take you to the jan directory in the directory blastfiles in the directory myname which is in the basal directory home. You can also use relative pathnames to navigate to directories. For example if you were already in /home/myname/blastfiles/ you could do cd jan to go into the jan directory. A period .  denotes current directory
  
-•more filename --- shows the first part of a file - as much as will fit on one screen. Just hit the space bar to see more or q to quit+**more filename** --- shows the first part of a file - as much as will fit on one screen. Just hit the space bar to see more or q to quit
  
-•less filename -- similar to more but has more sophisticated search and navigation abilities. Once  you are within the less window you can navigate or search with certain keys, Eg. b for previous page, g for first line, G for last line, /searchtext to find a string, etc (see man page man less).+**less filename** -- similar to more but has more sophisticated search and navigation abilities. Once  you are within the less window you can navigate or search with certain keys, Eg. b for previous page, g for first line, G for last line, /searchtext to find a string, etc (see man page man less).
  
-•cat filename ---displays the entire content of a file to the screen+**cat filename** ---displays the entire content of a file to the screen
  
-•mv filename1 filename2 --- moves a file (i.e. gives it a different name, or moves it into a different directory (see below)+**mv filename1 filename2** --- moves a file (i.e. gives it a different name, or moves it into a different directory (see below)
  
-•cp filename1 filename2 --- copies a file+**cp filename1 filename2** --- copies a file
  
-•rm filename --- removes a file. It is wise to use the option rm -i, which will ask you for confirmation before actually deleting anything. You can make this your default by making an alias in your .cshrc file.+**rm filename** --- removes a file. It is wise to use the option rm -i, which will ask you for confirmation before actually deleting anything. You can make this your default by making an alias in your .cshrc file.
  
-•diff filename1 filename2 --- compares files, and shows where they differ+**diff filename1 filename2** --- compares files, and shows where they differ
  
-•wc filename --- tells you how many lines, words, and characters there are in a file+**wc filename** --- tells you how many lines, words, and characters there are in a file
  
-•chmod options filename --- lets you change the read, write, and execute permissions on your files. The default is that only you can look at them and change them, but you may sometimes want to change these permissions.  For example, if you write a program or install someone else’s program you need to make it executable to run it. There are three levels of permissions: the owner of the file (you); the group you belong to; and everyone else. Each level has it own permissions. To see the permissions for the three levels you can do ls –l and this will list the permissions for each file. It will probably look something like this -rw-r--r--. The first position indicates whether it is a file or a folder(directory). A d in this position indicates that it is a directory while a - indicates that it is a file. The first position is then followed by 9 positions, 3 for the owner of the file, 3 for the group you belong to and 3 for everyone else. Within the group of 3 the first position is for read permission (r), the second position indicates write permission (w) and the third position indicates executable permission (x). So, -rw-r--r-- means that it is a file and the owner has read and write permission while those in the group that the owner belongs to only have read permission (r) just like everyone else. –rw-rw-rw- means the owner, the group and everyone else have both read and write permission. Note that directories must be executable for you to access them. So, drwxr-xr-x is a directory that is accessible to the owner, the group and everyone else but only the owner has write permission for that directory. There are several options to modify file and directory permissions. The easiest is to assign values to the permissions. R=4, w=2, x=1. So -rw-r--r—is 644 (read/write = 4+2 for the owner, read =4 for the group, read=4 for everyone else. So, to give the owner, the group and everyone else read/write permissions you would issue the command chmod 666 filename.+**chmod** options **filename** --- lets you change the read, write, and execute permissions on your files. The default is that only you can look at them and change them, but you may sometimes want to change these permissions.  For example, if you write a program or install someone else’s program you need to make it executable to run it. There are three levels of permissions: the owner of the file (you); the group you belong to; and everyone else. Each level has it own permissions. To see the permissions for the three levels you can do ls –l and this will list the permissions for each file. It will probably look something like this -rw-r--r--. The first position indicates whether it is a file or a folder(directory). A d in this position indicates that it is a directory while a - indicates that it is a file. The first position is then followed by 9 positions, 3 for the owner of the file, 3 for the group you belong to and 3 for everyone else. Within the group of 3 the first position is for read permission (r), the second position indicates write permission (w) and the third position indicates executable permission (x). So, -rw-r--r-- means that it is a file and the owner has read and write permission while those in the group that the owner belongs to only have read permission (r) just like everyone else. –rw-rw-rw- means the owner, the group and everyone else have both read and write permission. Note that directories must be executable for you to access them. So, drwxr-xr-x is a directory that is accessible to the owner, the group and everyone else but only the owner has write permission for that directory. There are several options to modify file and directory permissions. The easiest is to assign values to the permissions. R=4, w=2, x=1. So -rw-r--r—is 644 (read/write = 4+2 for the owner, read =4 for the group, read=4 for everyone else. So, to give the owner, the group and everyone else read/write permissions you would issue the command chmod 666 filename.
  
-•gzip filename --- compresses files, so that they take up much less space. Usually text files compress to about half their original size, but it depends very much on the size of the file and the nature of the contents. There are other tools for this purpose but gzip usually gives the highest compression rate. Gzip produces files with the ending '.gz' appended to the original filename.+**gzip filename** --- compresses files, so that they take up much less space. Usually text files compress to about half their original size, but it depends very much on the size of the file and the nature of the contents. There are other tools for this purpose but gzip usually gives the highest compression rate. Gzip produces files with the ending '.gz' appended to the original filename.
  
-•gunzip filename --- uncompresses files compressed by gzip. +**gunzip filename** --- uncompresses files compressed by gzip. 
-•mkdir dirname --- make a new directory +**mkdir dirname** --- make a new directory 
-•pwd --- tells you where you currently are. +**pwd** --- tells you where you currently are. 
-•man command --- will show the manual entry for the unix command. For example, man ls will show all the options for the common unix command ls. +**man command** --- will show the manual entry for the unix command. For example, man ls will show all the options for the common unix command ls. 
-•* is the UNIX symbol for global and can be with many of the other commands. Eg to list all the files in a directory that start with the  character b ls b*. To list all the files that start with g and end in fasta  ls g*fasta. To copy all the files that end in blastx to the directory above the current one cp *blastx .. . To change the permissions on all fasta files to rw, rw, r chmod 664 *fasta. Remove all the files that start with t and end in pid rm t*pid+***** is the UNIX symbol for global and can be with many of the other commands. Eg to list all the files in a directory that start with the  character b ls b*. To list all the files that start with g and end in fasta  ls g*fasta. To copy all the files that end in blastx to the directory above the current one cp *blastx .. . To change the permissions on all fasta files to rw, rw, r chmod 664 *fasta. Remove all the files that start with t and end in pid rm t*pid
  
  
Line 47: Line 47:
 More advanced UNIX commands More advanced UNIX commands
  
-grep searchtext filename --- will search a file line by line for the presence of a searchtext. For example, grep frodo lord_of_the_rings.txt will search for the text frodo in the file lord_of_the_rings. If your search text includes spaces or special characters you need to double quote it – grep “the one ring" lord_of_the_rings. One of the options for grep is -v which means find all the lines that DO NOT contain the string and output them to the screen (or file if you redirect the output (see below)). Another useful grep option is -i which means ignore the case of the searchtext, so grep -i foo file will find lines that contain FOO, foo, FoO, fOO, FOo, fOo in the file file.+**grep searchtext filename** --- will search a file line by line for the presence of a searchtext. For example, grep frodo lord_of_the_rings.txt will search for the text frodo in the file lord_of_the_rings. If your search text includes spaces or special characters you need to double quote it – grep “the one ring" lord_of_the_rings. One of the options for grep is -v which means find all the lines that DO NOT contain the string and output them to the screen (or file if you redirect the output (see below)). Another useful grep option is -i which means ignore the case of the searchtext, so grep -i foo file will find lines that contain FOO, foo, FoO, fOO, FOo, fOo in the file file.
  
-ssh -l username url_of_system. Eg. ssh -l curtisba perun.biochem.dal.ca.  You will then be asked for your password for the remote system. Allows you to connect (login) to external computer systems via the ssh protocol. The external computer system must have a ssh server running (most UNIX systems do).+**ssh -l username url_of_system.** Eg. ssh -l curtisba perun.biochem.dal.ca.  You will then be asked for your password for the remote system. Allows you to connect (login) to external computer systems via the ssh protocol. The external computer system must have a ssh server running (most UNIX systems do).
  
-scp - secure copy protocol. Allows you to securely copy files from one computer system to another. To copy a file from you local computer to a remote computer+**scp** - secure copy protocol. Allows you to securely copy files from one computer system to another. To copy a file from you local computer to a remote computer
 scp name_of_file username@remote_computer_system:/absolute_pathway_for_destination_folder/ scp name_of_file username@remote_computer_system:/absolute_pathway_for_destination_folder/
 eg. scp myfile curtisba@perun.biochem.dal.ca:/home/curtisba/raxmlruns/ eg. scp myfile curtisba@perun.biochem.dal.ca:/home/curtisba/raxmlruns/
Line 61: Line 61:
 You will be asked for the password for the remote computer system. You will be asked for the password for the remote computer system.
  
-sort -will sort the contents of a file and print it to the screen (or file if redirected).  Without the option -n (for numeric) the contents will be sorted alphabetically.+**sort** -will sort the contents of a file and print it to the screen (or file if redirected).  Without the option -n (for numeric) the contents will be sorted alphabetically.
 Lines are sorted based on the first character in the first column. You can sort by other columns (or fields) with columns (fields) separated by blank space. Eg to sort a file that contains by the numbers in the second column Lines are sorted based on the first character in the first column. You can sort by other columns (or fields) with columns (fields) separated by blank space. Eg to sort a file that contains by the numbers in the second column
 bob 310 bob 310
Line 69: Line 69:
 To sort in reverse numerically sort -nrk2 file_name To sort in reverse numerically sort -nrk2 file_name
  
-> -unix for redirect output. Normally the results of a unix command like ls will output to the screen. However, you can redirect the results to a file. Eg. ls -l > what_files_exist+**>** -unix for redirect output. Normally the results of a unix command like ls will output to the screen. However, you can redirect the results to a file. Eg. ls -l > what_files_exist
  
-| -unix for piping results  of one command into another command. This allows you to string together unix commands. Eg. ls - l | grep curtisba | wc will  tell me how many files in the current directory contain the searchtext curtisba+**|** -unix for piping results  of one command into another command. This allows you to string together unix commands. Eg. ls - l | grep curtisba | wc will  tell me how many files in the current directory contain the searchtext curtisba
  
-cut -cut up lines of a file by  columns or a single character delimiter. Eg to cut the 3 lines in the file money by the comma and print out what type of metal cut -d"," -f2+**cut** -cut up lines of a file by  columns or a single character delimiter. Eg to cut the 3 lines in the file money by the comma and print out what type of metal cut -d"," -f2
 30,gold 30,gold
 50,silver 50,silver
using_unix.1494601647.txt.gz · Last modified: by cgeb2001