You use the ____ character in script files to mark comments.
In this episode we will learn about creating and moving files and directories, using the 0 directory as an example. Show
Step one: see where we are and what we already haveWe should still be in the 1 directory on the Desktop, which we can check using:
Next we’ll move to the 0 directory and see what it contains:
Create a directoryLet’s create a new directory called 3 using the command 4 (which has no output):
As you might guess from its name, 5 means ‘make directory’. Since 3 is a relative path (i.e., does not have a leading slash, like 7), the new directory is created in the current working directory:
Since we’ve just created the 3 directory, there’s nothing in it yet:
Note that 5 is not limited to creating single directories one at a time. The 0 option allows 5 to create a directory with nested subdirectories in a single operation:
The 2 option to the 3 command will list all nested subdirectories within a directory. Let’s use 4 to recursively list the new directory hierarchy we just created in the 5 directory: 0 1
Create a text fileLet’s change our working directory to 3 using 00, then run a text editor called Nano to create a file called 01: 2
Let’s type in a few lines of text. Once we’re happy with our text, we can press Ctrl+O (press the Ctrl or Control key and, while holding it down, press the O key) to write our data to disk (we’ll be asked what file we want to save this to: press Return to accept the suggested default of 01).Once our file is saved, we can use Ctrl+X to quit the editor and return to the shell.
02 doesn’t leave any output on the screen after it exits, but 3 now shows that we have created a file called 01: 3 4
Moving files and directoriesReturning to the 36 directory, 7In our 3 directory we have a file 01 which isn’t a particularly informative name, so let’s change the file’s name using 39, which is short for ‘move’: 8The first argument tells 39 what we’re ‘moving’, while the second is where it’s to go. In this case, we’re moving 41 to 42, which has the same effect as renaming the file. Sure enough, 3 shows us that 3 now contains one file called 45: 9 0One must be careful when specifying the target file name, since 39 will silently overwrite any existing file with the same name, which could lead to data loss. An additional option, 47 (or 48), can be used to make 39 ask you for confirmation before overwriting.Note that 39 also works on directories.Let’s move 45 into the current working directory. We use 39 once again, but this time we’ll use just the name of a directory as the second argument to tell 39 that we want to keep the filename but put the file somewhere new. (This is why the command is called ‘move’.) In this case, the directory name we use is the special directory name 5 that we mentioned earlier. 1The effect is to move the file from the directory it was in to the current working directory. 3 now shows us that 3 is empty: 9 3Alternatively, we can confirm the file 45 is no longer present in the 3 directory by explicitly trying to list it: 4 5 3 with a filename or directory as an argument only lists the requested file or directory. If the file given as the argument doesn’t exist, the shell returns an error as we saw above. We can use this to see that 45 is now present in our current directory: 6 0
Copying files and directoriesThe 67 command works very much like 39, except it copies a file instead of moving it. We can check that it did the right thing using 3 with two paths as arguments — like most Unix commands, 3 can be given multiple paths at once: 1 2We can also copy a directory and all its contents by using the recursive option 71, e.g. to back up a directory: 3We can check the result by listing the contents of both the 3 and 73 directory: 4 5
Removing files and directoriesReturning to the 36 directory, let’s tidy up this directory by removing the 45 file we created. The Unix command we’ll use for this is 01 (short for ‘remove’): 1We can confirm the file has gone using 3: 6 3
If we try to remove the 3 directory using 09, we get an error message: 5 6This happens because 01 by default only works on files, not directories. 01 can remove a directory and all its contents if we use the recursive option 71, and it will do so without any confirmation prompts: 7Given that there is no way to retrieve files deleted using the shell, 13 should be used with great caution (you might consider adding the interactive option 14).Operations with multiple files and directoriesOftentimes one needs to copy or move several files at once. This can be done by providing a list of individual filenames, or specifying a naming pattern using wildcards.
Using wildcards for accessing multiple files at once
|