Solutions

Creating Files a Different Way

  1. The touch command generates a new file called 'my_file.txt' in your home directory. If you are in your home directory, you can observe this newly generated file by typing 'ls' at the command line prompt. 'my_file.txt' can also be viewed in your GUI file explorer.
  2. When you inspect the file with 'ls -l', note that the size of 'my_file.txt' is 0kb. In other words, it contains no data. If you open 'my_file.txt' using your text editor it is blank.
  3. Some programs do not generate output files themselves, but instead require that empty files have already been generated. When the program is run, it searches for an existing file to populate with its output. The touch command allows you to efficiently generate a blank text file to be used by such programs.

Using rm Safely

rm: remove regular file 'thesis/quotations.txt'?

The -i option will prompt before every removal. The Unix shell doesn't have a trash bin, so all the files removed will disappear forever. By using the -i flag, we have the chance to check that we are deleting only the files that we want to remove.

Moving to the Current Folder

mv ../analyzed/sucrose.dat ../analyzed/maltose.dat .

Recall that .. refers to the parent directory (i.e. one above the current directory) and that . refers to the current directory.

Renaming Files

  1. No. While this would create a file with the correct name, the incorrectly named file still exists in the directory and would need to be deleted.
  2. Yes, this would work to rename the file.
  3. No, the period(.) indicates where to move the file, but does not provide a new file name; identical file names cannot be created.
  4. No, the period(.) indicates where to copy the file, but does not provide a new file name; identical file names cannot be created.

Moving and Copying

We start in the /Users/jamie/data directory, and create a new folder called recombine. The second line moves (mv) the file proteins.dat to the new folder (recombine). The third line makes a copy of the file we just moved. The tricky part here is where the file was copied to. Recall that .. means "go up a level", so the copied file is now in /Users/jamie. Notice that .. is interpreted with respect to the current working directory, not with respect to the location of the file being copied. So, the only thing that will show using ls (in /Users/jamie/data) is the recombine folder.

  1. No, see explanation above. proteins-saved.dat is located at /Users/jamie
  2. Yes
  3. No, see explanation above. proteins.dat is located at /Users/jamie/data/recombine
  4. No, see explanation above. proteins-saved.dat is located at /Users/jamie

Copy with Multiple Filenames

If given more than one file name followed by a directory name (i.e. the destination directory must be the last argument), cp copies the files to the named directory.

If given three file names, cp throws an error because it is expecting a directory name as the last argument.

cp: target 'morse.txt' is not a directory

.

Using Wildcards

The solution is 3.

  1. shows all files whose names contain zero or more characters (*) followed by the letter t, then zero or more characters (*) followed by ane.pdb. This gives ethane.pdb methane.pdb octane.pdb pentane.pdb.
  2. shows all files whose names start with zero or more characters (*) followed by the letter t, then a single character (?), then ne. followed by zero or more characters (*). This will give us octane.pdb and pentane.pdb but doesn't match anything which ends in thane.pdb.
  3. fixes the problems of option 2 by matching two characters (??) between t and ne. This is the solution.
  4. only shows files starting with ethane..

More on Wildcards

cp *calibration.txt /backup/calibration
cp 2015-11-* ~/send_to_bob/all_november_files/
cp *-23-dataset* ~send_to_bob/all_datasets_created_on_a_23rd/

.

Organizing Directories and Files

mv *.dat analyzed

Jamie needs to move her files fructose.dat and sucrose.dat to the analyzed directory. The shell will expand *.dat to match all .dat files in the current directory. The mv command then moves the list of .dat files to the "analyzed" directory.

Copy a folder structure but not the files

The first set of commands achieves this objective. First we have a recursive copy of a data folder. Then two rm commands which remove all files in the specified directories. The shell expands the '*' wild card to match all files and subdirectories.

The second set of commands have the wrong order: attempting to delete files which haven't yet been copied, followed by the recursive copy command which would copy them.

The third set of commands would achieve the objective, but in a time-consuming way: the first command copies the directory recursively, but the second command deletes interactively, prompting for confirmation for each file and directory.