Working on the command line

The few commands you've learned so far obviously won't get you far, we need more.

graphical user interfaces make easy tasks easy, while command line interfaces make difficult tasks possible—William Shotts

As we start working on files and folders, follow along in Explorer/Finder: they will reflect the changes you make at the command line!

Copying and moving/renaming

These two operations (cp and mv) are probably amongst the top 5 you will ever use!

NB There is no separate concept/utility of renaming a file or folder: this is achieved using a move.

  • For VirtualBox-users

    • In your Host OS (Windows or Mac), locate a textual file and copy-paste it into the folder you previously shared with the virtual machine. Then, in the VM

    • (in Terminal) Change directory to /media/sf_shared, list its contents and view (less) the contents of the file.

  • For Mac-users
    • If you didn't already, make a new shared folder using Finder, then
    • (in Terminal) Change directory to it, list its contents and view (less) the contents of the file.

Make a folder and copying into it

mkdir NEW_FOLDER
cp FILE DESTINATION
less FILE
  • make a new folder using the command mkdir (use man for syntax, name it as you like)
  • copy the file to the new directory
  • use ls and Explorer (Windows) or Finder (Mac) to make sure you got a copy
  • use less to read the copy

Remove files and folders

rmdir DIRECTORY
rm FILE
  • The command rmdir will remove a folder; Try to remove the folder you just copied into. (You cannot, why?)
  • The command rm will remove files; Remove the copy of the file you created above.
  • Now try to remove the folder again

Move files and folders

mv WHAT_TO_MOVE WHERE_TO_MOVE_IT
  • create a new directory and move the text file to it
  • move the directory to a new directory (i.e., rename the directory to something else)
  • create a new directory
  • move the renamed directory into the new directory (making it a subfolder)
  • copy the text file from the subfolder back into the current working directory; recall the relevant special notation.
    • 'here' is a very common place you'll want to copy/move to, remember the dot!

Recursive operations

  • copy can also be a rename; make a copy of the text file with a new name
  • try to make a copy (with a new name) of the directory (with the subfolder in it);
    • what goes wrong the first time?
    • man cp; try the -r flag for better luck
    • 'r' is for 'recursive', a very common flag; use it with caution!
  • recall (or try) that you cannot use rmdir to remove a directory that is not empty
  • to remove the copy you just created, use the -r flag with rm

Here are dragons!!!!!!

The combination of a careless -r flag with a poorly-considered (or typed) wildcard expression (see below) will likely end in tears!

There is no notion of a Trash bin; when you remove something, it's gone.

Another common Gotcha! is overwriting existing files with a cp or a mv:

cp file1 file2  # and the same for mv

will overwrite file2 with the contents of file1!

To those faint of heart, you can use the -i flag (for 'interactive'); rm will ask you to confirm ('y') or deconfirm ('n') each file or directory it is about to enter or delete. Similarly, cp and mv will ask before overwriting any files.

Try the interactive-flag

Wildcards: where things get interesting

Using wildcards (which is also known as globbing) allows you to select filenames based on patterns of characters—William Shotts

Prelude: initialise a nested directory structure

  • change directory to the notebooks-folder
  • make sure the fddhs-environment is selected, then execute the line
python -m fddhs
  • take a look at what has appeared in the notebooks/exercises-folder!
    • use a graphical file browser (either the one on your host or in the guest)

'Anything'

See pp. 26-27 in The Linux Command Line for a list of the most common wildcards. By far the one most used is the asterisk: * matches any characters. Let's practice:

  • change directory to level0
  • list the contents of the directory level1A
  • repeat the above, but only show the files that have a dot in them
    • hint: use the asterisk wildcard to implement "anything-dot-anything"
  • now only list the files ending in .txt (there may not be any!)
  • now list files ending in .htm, with any number of characters after htm
  • now copy to level0 files with a dot in the name, ending in ml, with any number of characters between the . and the ml

More wildcards

The question mark (?) matches any single character:

  • list files (in level1A) whose extension is exactly 3 letters long and ends at

Hard brackets [] can be used to denote ranges, e.g., [0-9] for any digit, [a-z] for any lowercase letter, [A-Z] similarly for uppercase letters, [b,d,7] for those characters specifically, etc.

  • list files that start with a capital (uppercase) letter
  • list files, ending with any extension, that have a digit anywhere in their names

The kicker

You're suitably impressed, right? No? Still think Explorer/Finder is just dandy? Try this:

  • from all level1-folders except A, copy to level0 all files starting with a lowercase letter, including a 2 or a 6 anywhere in the body (except the first character), and ending with a 3-letter extension with at as the final two characters

How long would that take you using Explorer/Finder...?

Interlude: On ('white') space

Why spaces in file/directory names are a Bad Idea

  • go back to the exercises folder and list the contents
  • change directory to spaces suck; What goes wrong?
  • try again, now using tab-completion (type: cd spa[tab])

The '\' you see is called 'escaping whitespace'. White space is a meaningful symbol in bash: it separates commands, flags and arguments from each other.

In some cases, you can use quotes (") to indicate that a sequence of words should be intepreted as a single argument instead of a sequence of arguments.

  • go back to exercises, then use quotes to enclose the problematic folder name when changing directory

How extra spaces lead to tears

  • go to exercises/level0, list the contents; you should see the level1-folders and the files you copied there previously
  • let's assume you want to remove all the .bat-files from the directory
    • the command you should use is: rm *.bat
    • accidentally, you type this instead: rm * .bat
    • note the space between the * and the .bat
  • intentionally make the accident now; what happened and why?

Next up: Getting serious