{"id":637,"date":"2019-09-27T15:56:07","date_gmt":"2019-09-27T14:56:07","guid":{"rendered":"https:\/\/blogs.gre.ac.uk\/cmssupport\/?page_id=637"},"modified":"2019-09-27T15:56:07","modified_gmt":"2019-09-27T14:56:07","slug":"using-files-and-directories","status":"publish","type":"page","link":"https:\/\/blogs.gre.ac.uk\/cmssupport\/systems\/unix\/unix-commands\/using-files-and-directories\/","title":{"rendered":"Using files and directories"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Creating files<\/h2>\n\n\n\n<p>touch&nbsp;<em>filename<\/em><br><br>If the filename doesn&#8217;t exist it gets created (0 Bytes).<br>If the filename already exists, touch alters its timestamp to the current time.<br><br>In UNIX, filenames should NOT contain spaces, and so underscores or a capital letter should be used instead to separate words. E.g.&nbsp;% touch my file&nbsp;will not work, therefore you will need to type in either of the following:&nbsp;% touch myFile&nbsp;or&nbsp;%touch my_file. This also applies to whenever directories are created.<br><br>Instructions on creating directories can be found by visiting the following link:<br><a href=\"http:\/\/cms-support.gre.ac.uk\/old\/unix\/unixnavi.html#making_directories\">Navigating the Unix file system &gt; Making Directories<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Copying files<\/h2>\n\n\n\n<p>cp&nbsp;(copy)<br><br>cp&nbsp;<em>file1 file2<\/em>&nbsp;is the command that makes a copy of&nbsp;<em>file1<\/em>&nbsp;in the current working directory and calls it&nbsp;<em>file2<\/em>.<br><br>What needs to be done next is to take a file stored in your home area, and use the&nbsp;<em>cp<\/em>&nbsp;command to copy it to your&nbsp;<em>unixstuff<\/em>&nbsp;directory.<br>This directory was created in &#8216;Making Directories&#8217;. Please view this section by visiting the following link if you haven&#8217;t already done so:<br><a href=\"http:\/\/cms-support.gre.ac.uk\/old\/unix\/unixnavi.html#making_directories\">Navigating the Unix file system &gt; Making Directories<\/a><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Add&nbsp;<em>cd<\/em>&nbsp;to your&nbsp;<em>unixstuff<\/em>&nbsp;directory:<br>% cd ~\/unixstuff<\/li><li>At the UNIX prompt type:<br>% cp ~\/myFile.txt .<br>Don&#8217;t forget to include the full stop &#8216;.&#8217; at the end. Remember, in UNIX, the full stop means the current directory.<br>The above command means copy the file&nbsp;myFile.txt&nbsp;to the current directory, keeping the name the same.<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Moving files<\/h2>\n\n\n\n<p>mv&nbsp;(move)<br><br>mv&nbsp;<em>file1 file2<\/em>&nbsp;moves (or renames)&nbsp;file1&nbsp;to&nbsp;file2<br><br>To move a file from one place to another, use the&nbsp;mv&nbsp;command. This has the effect of moving rather than copying the file, so you end up with only one file rather than two.<br><br>It can also be used to rename a file, by &#8216;moving&#8217; the file to the same directory, but giving it a different name.<br><br>What needs to be done next is to move the file&nbsp;<em>myFile.bak<\/em>&nbsp;to your backup directory (created in &#8216;cd&#8217;, which can be found by visting the link below):<br><a href=\"http:\/\/cms-support.gre.ac.uk\/old\/unix\/unixnavi.html#changing_directories\">Navigating the Unix file system &gt; Changing directories<\/a><br><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Change the directories to your&nbsp;<em>unixstuff<\/em>&nbsp;directory.<\/li><li>Inside the&nbsp;<em>unixstuff<\/em>&nbsp;directory, type the following:<br>% mv myFile.bak backups\/.<\/li><li>Type&nbsp;ls&nbsp;and&nbsp;ls backups&nbsp;to see if it has worked.<br><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Removing files and directories<\/h2>\n\n\n\n<p>rm&nbsp;(remove)<br>rmdir&nbsp;(remove directory)<br><br>To delete (remove) a file, use the&nbsp;rm&nbsp;command. As an example, the following code shows how to create a copy of the myFile.txt file then delete it.<br><br>Inside your&nbsp;<em>unixstuff<\/em>&nbsp;directory, type the following:<br>% cp myFile.txt tempfile.txt<br>% ls&nbsp;(to check if it has created the file)<br>% rm tempfile.txt<br>% ls&nbsp;(to check if it has deleted the file)<br><br>You can use the&nbsp;rmdir&nbsp;command to remove a directory (make sure it is empty first). However, if you try to remove the backups directory, you will notice that this is not possible because Unix will not let you remove a non-empty directory.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Removing a directory that contains files<\/h2>\n\n\n\n<p>To remove a directory which contains files you must use the regular&nbsp;rm&nbsp;command and not the predefined alias&nbsp;rm -i<br><br>In order to do this you will need to start the command with a backslash<br><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\\<\/pre>\n\n\n\n<p>The following code will remove&nbsp;<em>directory<\/em>&nbsp;without prompting to delete each file within it.<br><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\\rm -rf <em>directory<\/em><\/pre>\n\n\n\n<p>WARNING: Never use wildcard matches i.e.&nbsp;* ? .&nbsp;(asterisks, question marks or full stops) and always check twice before hitting the enter or return key.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>cp file1 file2&nbsp;&#8211; copy&nbsp;file1&nbsp;and call it&nbsp;file2<br><br>mv file1 file2&nbsp;&#8211; move or rename&nbsp;file1&nbsp;to&nbsp;file2<br><br>rm file&nbsp;&#8211; remove a file<br><br>rmdir directory&nbsp;&#8211; remove a directory<br><br>\\rm -rf directory&nbsp;&#8211; removes a directory that contains files<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating files touch&nbsp;filename If the filename doesn&#8217;t exist it gets created (0 Bytes).If the filename already exists, touch alters its timestamp to the current time. In UNIX, filenames should NOT contain spaces, and so underscores &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-637","page","type-page","status-publish","hentry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/blogs.gre.ac.uk\/cmssupport\/wp-json\/wp\/v2\/pages\/637","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=637"}],"version-history":[{"count":0,"href":"https:\/\/blogs.gre.ac.uk\/cmssupport\/wp-json\/wp\/v2\/pages\/637\/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=637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}