{"id":634,"date":"2019-09-27T15:47:35","date_gmt":"2019-09-27T14:47:35","guid":{"rendered":"https:\/\/blogs.gre.ac.uk\/cmssupport\/?page_id=634"},"modified":"2019-09-27T15:47:35","modified_gmt":"2019-09-27T14:47:35","slug":"unixnavi","status":"publish","type":"page","link":"https:\/\/blogs.gre.ac.uk\/cmssupport\/systems\/unix\/unix-commands\/unixnavi\/","title":{"rendered":"Navigating the Unix file system"},"content":{"rendered":"\n<p>Listing files and directories<\/p>\n\n\n\n<p>ls&nbsp;(short for list)<br><br>When you first login, your current working directory is your home directory. Your home directory has the same name as your username e.g. wug01, and it is where your personal files and subdirectories are saved. To find out what is in your home directory, type:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">% ls<\/pre>\n\n\n\n<p>The&nbsp;ls&nbsp;command lists the contents of your current working directory.<br><br>There may be no files visible in your home directory, in which case the Unix prompt will be returned. Alternatively, there may already be some files inserted by the System Administrator when your account was created.<br><br>ls&nbsp;does not, in fact, cause all the files in your home directory to be listed, but only those ones whose name does not begin with a dot (.) Files beginning with a dot (.) are known as hidden files and usually contain important program configuration information. They are hidden because you should not change them unless you are very familiar with Unix.<br><br>To list all files in your home directory including those whose names begin with a dot, type:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">% ls -a<\/pre>\n\n\n\n<p>ls&nbsp;is an example of a command which can take options:&nbsp;-a&nbsp;is an example of an option. The options change the behaviour of the command. There are online manual pages that tell you which options a particular command can take, and how each option modifies the behaviour of the command. (See later in this tutorial)<\/p>\n\n\n\n<p>Making directories<\/p>\n\n\n\n<p>mkdir&nbsp;(make directory)<br><br>We will now make a subdirectory in your home directory to hold the files you will be creating and using in the course of this tutorial. To make a subdirectory called&nbsp;unixstuff&nbsp;in your current working directory, type:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">% mkdir unixstuff<\/pre>\n\n\n\n<p>To see the directory you have just created, type:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">% ls<\/pre>\n\n\n\n<p>You can also create additional sub-directoires with a single command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">% mkdir -p unixstuff\/backups<\/pre>\n\n\n\n<p>Changing directories<\/p>\n\n\n\n<p>cd&nbsp;(change directory)<br><br>The command&nbsp;cd &lt;directory&gt;&nbsp;means &#8216;change the current working directory to&nbsp;directory&#8217;. The current working directory may be thought of as the directory you are in, i.e. your current position in the file-system tree.<br><br>To change to the directory you have just made, type:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">% cd unixstuff<\/pre>\n\n\n\n<p>Type&nbsp;ls&nbsp;to see the contents (which should be empty)<\/p>\n\n\n\n<p>The directories\u00a0.\u00a0and\u00a0..<\/p>\n\n\n\n<p>In the&nbsp;unixstuff&nbsp;directory, type:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">% ls -a<\/pre>\n\n\n\n<p>As you can see, in the unixstuff directory (and in all other directories), there are two special directories called &#8220;.&#8221; and &#8220;..&#8221;<br><br>In Unix, &#8220;.&#8221; means the current directory, so typing the following means stay where you are (the&nbsp;unixstuff&nbsp;directory):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">% cd .<\/pre>\n\n\n\n<p>(Please note that there is a space between&nbsp;cd&nbsp;and the full stop)<br><br>This may not seem very useful at first, but using &#8220;.&#8221; as the name of the current directory will save a lot of typing.<br><br>&#8220;..&#8221; means the parent of the current directory, so typing the following will take you one directory up the hierarchy (back to your home directory). Try it now:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">% cd ..<\/pre>\n\n\n\n<p>Please note that typing&nbsp;cd&nbsp;with no argument always returns you to your home directory. This is very useful if you are lost in the file system.<\/p>\n\n\n\n<p>Path names<\/p>\n\n\n\n<p>pwd&nbsp;(print working directory)<br><br>Path names enable you to work out where you are in relation to the whole file system.<br><br>E.g. to find out the absolute path name of your home directory, type&nbsp;cd&nbsp;to get back to your home directory and then type:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">% pwd<\/pre>\n\n\n\n<p>The full pathname will look something like this:<br>\/home\/wug01<\/p>\n\n\n\n<p>Your home directory (~)<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Type the &#8216;cd&#8217; command to your home directory:<br>cd<\/li><li>Afterwards, type the following to list the contents of your&nbsp;unixstuff&nbsp;directory:<br>% ls unixstuff<\/li><li>Next, type the following:<br>% ls backups<\/li><li>You will then get a message like this:<br><br>backups: No such file or directory<br><br>This will happen because&nbsp;backups&nbsp;is not in your current working directory.<\/li><\/ol>\n\n\n\n<p>In order to use a command on a file (or directory) that is not in the current working directory (i.e. the directory you are currently in), you will need to do either of the following:<br>Option A: Use the&nbsp;cd&nbsp;command for the correct directory<br>Option B: Specify its full path name<br><br>To list the contents of your backups directory, you will need to type the following:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">% ls unixstuff\/backups\n\n~ (your home directory)<\/pre>\n\n\n\n<p>Home directories can also be referred to by the tilde ~ character. It can be used to specify paths starting at your home directory. So typing the following will list the contents of your unixstuff directory, regardless of where you currently are within the file system:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">% ls ~\/unixstuff<\/pre>\n\n\n\n<p>Summary<\/p>\n\n\n\n<p>ls&nbsp;&#8211; list files and directories<br><br>ls -a&nbsp;&#8211; list all files and directories<br><br>mkdir&nbsp;&#8211; make a directory<br><br>cd&nbsp;&#8211; directory change to named directory<br><br>cd&nbsp;&#8211; change to home directory<br><br>cd ~&nbsp;&#8211; change to home directory<br><br>cd ..&nbsp;&#8211; change to parent directory<br><br>pwd&nbsp;&#8211; display the path of the current directory<br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Listing files and directories ls&nbsp;(short for list) When you first login, your current working directory is your home directory. Your home directory has the same name as your username e.g. wug01, and it is where &hellip;<\/p>\n","protected":false},"author":53,"featured_media":0,"parent":584,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-634","page","type-page","status-publish","hentry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/blogs.gre.ac.uk\/cmssupport\/wp-json\/wp\/v2\/pages\/634","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.gre.ac.uk\/cmssupport\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.gre.ac.uk\/cmssupport\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.gre.ac.uk\/cmssupport\/wp-json\/wp\/v2\/users\/53"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.gre.ac.uk\/cmssupport\/wp-json\/wp\/v2\/comments?post=634"}],"version-history":[{"count":0,"href":"https:\/\/blogs.gre.ac.uk\/cmssupport\/wp-json\/wp\/v2\/pages\/634\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.gre.ac.uk\/cmssupport\/wp-json\/wp\/v2\/pages\/584"}],"wp:attachment":[{"href":"https:\/\/blogs.gre.ac.uk\/cmssupport\/wp-json\/wp\/v2\/media?parent=634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}