ag@linux:~$

history 28 of 36

Basic Linux Commands Revisited: mv, mkdir, less and useradd

My 2017 cheat sheet of everyday commands (mv, mkdir, less, rm, useradd, userdel), corrected, with how to read files in less and manage user accounts.

Written

From my Linux study notes, 2017. 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.

A short cheat sheet of commands I used every day. The same list also appears in my 2012 post, “My First Linux Commands: Files, Directories and Users”; this is the 2017 copy. My original notes are below, with corrections, and the added notes focus on reading files with less and managing users.

Before you start

  • You need a Linux terminal. Practise file commands in a test folder.
  • useradd and userdel change system accounts, so they must be run as root or with sudo. Use a test machine or virtual machine if you can.

My original notes

  1. For move file:

    Terminal window
    mv oldfile newfile

    The original said noldfile, a typo for oldfile. With a new name in the same directory, this renames the file.

  2. To make new directory:

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

    Terminal window
    mv textfile dirname/
  4. To change file name:

    Terminal window
    rm filename

    Correction: rm deletes a file; it does not rename it. Use mv to change a file name:

    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 the new user’s name, for example useradd username.

  7. To del user:

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

Reading files with less

less shows a file one screen at a time without changing it, so it is safe for looking at configuration and log files.

Key What it does
Space / b Next page / previous page
Arrow keys Move one line up or down
/word Search forward for “word”
n / N Next / previous search match
g / G Go to the start / end of the file
q Quit

To keep watching a file that is still growing (such as a log), less +F filename follows new lines; press Ctrl+C to stop following and q to quit. tail -f filename does a similar job.

Adding and removing a user: worked example

Run as root or with sudo:

Terminal window
sudo useradd -m testuser
sudo passwd testuser
id testuser
sudo userdel -r testuser

What each line does:

  1. Creates the account testuser. -m makes sure a home directory (/home/testuser) is created; some distributions do this by default and some do not.
  2. Sets the password, so the account can log in. Without a password the account is locked.
  3. Shows the user’s ID number and groups, which confirms the account exists.
  4. Deletes the account. -r also removes the home directory and mail spool; without -r, the home directory is left behind.

Notes for today

  • On Debian and Ubuntu, adduser username is an interactive alternative to useradd that creates the home directory and asks for a password.
  • mv overwrites an existing destination file without warning. Use mv -i to be asked first.
  • rm has no Recycle Bin. rm -i asks before each deletion.
  • User accounts are listed in /etc/passwd. You can check one with getent passwd username.