Creating, moving, and copying

Creating things

Covered topics: creating directories with mkdir, using nano text editor, deleting with rm and rmdir.

$ mkdir thesis
$ ls -F
$ ls -F thesis
$ cd thesis
$ nano draft.txt   # let's spend few minutes learning how to use nano; can also use other editors
$ ls
$ more draft.txt   # displays a file one page at a time
$ cd ..
$ rm thesis   # getting an error - why?
$ rmdir thesis   # again getting an error - why?
$ rm thesis/draft.txt
$ rmdir thesis

Also could do ‘rm -r thesis’ in lieu of the last two commands.

You can watch a video for this topic after the workshop.

Moving and copying things

Covered topics: mv and cp.

$ mkdir thesis
$ nano thesis/draft.txt
$ ls thesis
$ mv thesis/draft.txt thesis/quotes.txt
$ ls thesis
$ mv thesis/quotes.txt .   # . stands for current directory
$ ls thesis
$ ls
$ ls quotes.txt
$ cp quotes.txt thesis/quotations.txt
$ ls quotes.txt thesis/quotations.txt
$ rm quotes.txt
$ ls quotes.txt thesis/quotations.txt

More than two arguments to mv and cp:

$ touch  intro.txt  methods.txt  index.txt   # create three empty files
$ ls
$ mv  intro.txt  methods.txt  index.txt  thesis   # the last argument is the destination directory
$ ls
$ ls thesis
Question `misspelled file`

Suppose that you created a .txt file in your current directory to contain a list of the statistical tests you will need to do to analyze your data, and named it statstics.txt. After creating and saving this file, you realize you misspelled the filename! You want to correct the mistake, which of the following commands could you use to do so?

  1. cp statstics.txt statistics.txt
  2. mv statstics.txt statistics.txt
  3. mv statstics.txt .
  4. cp statstics.txt .

You can watch a video for this topic after the workshop.


Aliases are one-line shortcuts/abbreviations to avoid typing a longer command, e.g.

$ alias ls='ls -aFh'
$ alias pwd='pwd -P'
$ alias hi='history'
$ alias top='top -o cpu -s 10 -stats "pid,command,cpu,mem,threads,state,user"'
$ alias cedar='ssh -Y'
$ alias weather='curl'
$ alias cal='gcal --starting-day=1'  # starts on Monday

Now, instead of typing ssh -Y, you can simply type cedar. To see all your defined aliases, type alias. To remove, e.g. the alias cedar, type unalias cedar.

You may want to put all your alias definitions into the file ~/.bashrc which is run every time you start a new local or remote shell.

Question `safer mv and cp` Write simple aliases for safer mv, cp so that these do not automatically overwrite the target. Hint: use their manual pages. Where would you store these aliases?
Question `safer rm` Write simple alias for safer rm.
Question 9

What is the output of the last ls command in the sequence shown below?

$ pwd
$ ls
$ mkdir recombine
$ mv proteins.dat recombine
$ cp recombine/proteins.dat ../proteins-saved.dat
$ ls
  1. proteins-saved.dat recombine
  2. recombine
  3. proteins.dat recombine
  4. proteins-saved.dat