ag@linux:~$

history 5 of 36

My First Linux Commands: Files, Directories and Users

Early notes on moving, renaming, viewing and deleting files with mv, mkdir, less and rm, plus adding and removing users, with fixes and examples.

Written

From my Linux study notes, 2012. Some commands and versions here are old, so please reuse them carefully. Anything marked "added later", and the notes at the end, explain what has changed since.

These were some of the first Linux commands I wrote down while learning. My original notes are kept below, with corrections where a command was wrong.

Before you start

  • You need a Linux terminal. Practise in a test folder (for example mkdir ~/practice && cd ~/practice) so you cannot damage important files.
  • The user commands (useradd, userdel) must be run as root or with sudo.

My original notes

  1. For move file:

    Terminal window
    mv oldfile newfile

    The original said noldfile, which was a typo for oldfile. Note that when the destination is a new name in the same directory, this actually renames the file (Linux has no separate rename command for single files).

  2. To make new directory:

    Terminal window
    mkdir dirname
  3. To put file into a new directory:

    Terminal window
    mv textfile dirname/

    The trailing / makes it clear that dirname is a directory. If the directory does not exist, mv gives an error instead of silently renaming the file.

  4. To change file name:

    Terminal window
    rm filename

    Correction: this is wrong. rm deletes a file, it does not rename it. To change a file name, use mv:

    Terminal window
    mv oldname newname
  5. To view the text file:

    Terminal window
    less filename
  6. To add new user:

    Terminal window
    useradd

    useradd needs a user name, for example useradd username. Run on its own it only prints usage help.

  7. To del user:

    Terminal window
    userdel username
  8. less command to see the text file: press q to quit.

A worked example

Try these in your practice folder:

Terminal window
echo "hello" > notes.txt
mkdir archive
mv notes.txt old-notes.txt
mv old-notes.txt archive/
ls archive
less archive/old-notes.txt

What each line does:

  1. Creates a small text file called notes.txt.
  2. Creates a directory called archive.
  3. Renames notes.txt to old-notes.txt.
  4. Moves it into archive.
  5. Lists the contents of archive, so you can check the move worked.
  6. Opens the file in less (press q to quit).

Notes for today

  • mv overwrites the destination file without asking if it already exists. Use mv -i to be asked first. The same -i option works with cp and rm.
  • rm does not use a Recycle Bin. Deleted files are gone. rm -i filename asks for confirmation.
  • cp oldfile newfile copies a file and keeps the original; mv moves or renames it.
  • After useradd username, set a password with passwd username so the account can log in. On Debian and Ubuntu, adduser is a friendlier interactive alternative.
  • userdel username keeps the user’s home directory. userdel -r username removes the home directory and mail spool as well, so only use it when you are sure.
  • Useful keys in less: /word searches forward, n jumps to the next match, Space goes down a page, g goes to the start and G to the end.