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!
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.
shared
folder using Finder, thenless
) the contents of the file.mkdir NEW_FOLDER
cp FILE DESTINATION
less FILE
mkdir
(use man
for syntax, name it as you like)ls
and Explorer
(Windows) or Finder
(Mac) to make sure you got a copyless
to read the copymv WHAT_TO_MOVE WHERE_TO_MOVE_IT
man cp
; try the -r
flag for better luckrmdir
to remove a directory that is not empty-r
flag with rm
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.
Using wildcards (which is also known as globbing) allows you to select filenames based on patterns of characters—William Shotts
notebooks
-folderfddhs
-environment is selected, then execute the linepython -m fddhs
notebooks/exercises
-folder!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:
level0
level1A
.txt
(there may not be any!).htm
, with any number of characters after htm
ml
, with any number of characters between the .
and the ml
The question mark (?
) matches any single character:
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.
You're suitably impressed, right? No? Still think Explorer/Finder is just dandy? Try this:
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 charactersHow long would that take you using Explorer/Finder...?
exercises
folder and list the contentsspaces suck
; What goes wrong?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.
exercises
, then use quotes to enclose the problematic folder name when changing directoryexercises/level0
, list the contents; you should see the level1
-folders and the files you copied there previously.bat
-files from the directoryrm *.bat
rm * .bat
*
and the .bat